Fix Bugzilla 15315 - can break immutable with std.algorithm.move (#9032)

This commit is contained in:
Nick Treleaven 2024-07-28 23:49:54 +01:00 committed by GitHub
parent 7a14c896dd
commit 9ffe71fea3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 10 deletions

View file

@ -1071,10 +1071,20 @@ Params:
copy is performed.
*/
void move(T)(ref T source, ref T target)
if (__traits(compiles, target = T.init))
{
moveImpl(target, source);
}
/// ditto
template move(T)
if (!__traits(compiles, imported!"std.traits".lvalueOf!T = T.init))
{
///
deprecated("Can't move into `target` as `" ~ T.stringof ~ "` can't be assigned")
void move(ref T source, ref T target) => moveImpl(target, source);
}
/// For non-struct types, `move` just performs `target = source`:
@safe unittest
{
@ -1184,6 +1194,19 @@ pure nothrow @safe @nogc unittest
assert(s53 is s51);
}
@system unittest
{
static struct S
{
immutable int i;
~this() @safe {}
}
alias ol = __traits(getOverloads, std.algorithm.mutation, "move", true)[1];
static assert(__traits(isDeprecated, ol!S));
// uncomment after deprecation
//static assert(!__traits(compiles, { S a, b; move(a, b); }));
}
/// Ditto
T move(T)(return scope ref T source)
{