Skip to content

Commit

Permalink
std.traits: Adapt unittest to pass with -preview=in (#7609)
Browse files Browse the repository at this point in the history
* std.traits: Use delegates within some __traits(compiles) check to avoid constfold

With the new -preview=in check, the const-folding seems to be a bit
too aggressive when an rvalue is passed, meaning that the check might
fail (probably due to the code that initialize the temporary).
To avoid this issue, we simply wrap the assignment in a lambda that
will not be called, as there is no downside to that change.

* std.traits: Adapt unittest to pass with `-preview=in`
  • Loading branch information
Geod24 authored Aug 29, 2020
1 parent 6194b17 commit aca1e12
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions std/traits.d
Original file line number Diff line number Diff line change
Expand Up @@ -5180,10 +5180,10 @@ enum isAssignable(Lhs, Rhs = Lhs) = isRvalueAssignable!(Lhs, Rhs) && isLvalueAss
}

// ditto
private enum isRvalueAssignable(Lhs, Rhs = Lhs) = __traits(compiles, lvalueOf!Lhs = rvalueOf!Rhs);
private enum isRvalueAssignable(Lhs, Rhs = Lhs) = __traits(compiles, { lvalueOf!Lhs = rvalueOf!Rhs; });

// ditto
private enum isLvalueAssignable(Lhs, Rhs = Lhs) = __traits(compiles, lvalueOf!Lhs = lvalueOf!Rhs);
private enum isLvalueAssignable(Lhs, Rhs = Lhs) = __traits(compiles, { lvalueOf!Lhs = lvalueOf!Rhs; });

@safe unittest
{
Expand Down Expand Up @@ -5219,11 +5219,26 @@ private enum isLvalueAssignable(Lhs, Rhs = Lhs) = __traits(compiles, lvalueOf!Lh
static assert( isAssignable!(S4, immutable int));

struct S5 { @disable this(); @disable this(this); }
struct S6 { void opAssign(in ref S5); }
static assert(!isAssignable!(S6, S5));
static assert(!isRvalueAssignable!(S6, S5));
static assert( isLvalueAssignable!(S6, S5));
static assert( isLvalueAssignable!(S6, immutable S5));

// `-preview=in` is enabled
static if (!is(typeof(mixin(q{(in ref int a) => a}))))
{
struct S6 { void opAssign(in S5); }

static assert(isRvalueAssignable!(S6, S5));
static assert(isLvalueAssignable!(S6, S5));
static assert(isAssignable!(S6, S5));
static assert(isAssignable!(S6, immutable S5));
}
else
{
mixin(q{ struct S6 { void opAssign(in ref S5); } });

static assert(!isRvalueAssignable!(S6, S5));
static assert( isLvalueAssignable!(S6, S5));
static assert(!isAssignable!(S6, S5));
static assert( isLvalueAssignable!(S6, immutable S5));
}
}


Expand Down

0 comments on commit aca1e12

Please sign in to comment.