Skip to content

Commit

Permalink
I was wrong about return ref... lifetime analysis actually handles it.
Browse files Browse the repository at this point in the history
  • Loading branch information
TurkeyMan committed Nov 17, 2024
1 parent 95385e3 commit a5fcdb6
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 64 deletions.
2 changes: 0 additions & 2 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -3288,7 +3288,6 @@ private bool functionParameters(const ref Loc loc, Scope* sc,
else if (p.storageClass & STC.ref_)
{
if (global.params.rvalueRefParam != FeatureState.disabled &&
// !(p.storageClass & STC.return_) &&
!arg.isLvalue() &&
targ.isCopyable())
{ /* allow rvalues to be passed to ref parameters by copying
Expand All @@ -3299,7 +3298,6 @@ private bool functionParameters(const ref Loc loc, Scope* sc,
ev = new CommaExp(arg.loc, ev, new VarExp(arg.loc, v));
arg = ev.expressionSemantic(sc);
}
// arg = arg.toLvalue(sc, (p.storageClass & STC.return_) ? "create `return ref` parameter from" : "create `ref` parameter from");
arg = arg.toLvalue(sc, "create `ref` parameter from");

// Look for mutable misaligned pointer, etc., in @safe mode
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/typesem.d
Original file line number Diff line number Diff line change
Expand Up @@ -2287,8 +2287,8 @@ Type typeSemantic(Type type, const ref Loc loc, Scope* sc)
// default arg must be an lvalue
if (isRefOrOut && !isAuto &&
!(fparam.storageClass & STC.constscoperef) &&
(global.params.rvalueRefParam == FeatureState.disabled || (fparam.storageClass & (STC.out_ | STC.return_))))
e = e.toLvalue(sc, (global.params.rvalueRefParam != FeatureState.disabled) ? "create default argument for `return ref` / `out` parameter from" : "create default argument for `ref` / `out` parameter from");
(global.params.rvalueRefParam == FeatureState.disabled || (fparam.storageClass & STC.out_)))
e = e.toLvalue(sc, (global.params.rvalueRefParam != FeatureState.disabled) ? "create default argument for `out` parameter from" : "create default argument for `ref` / `out` parameter from");

fparam.defaultArg = e;
return (e.op != EXP.error);
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/compilable/reverthelp.d
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ Revertable language changes listed by -revert=name:
=dip1000 revert DIP1000 changes (Scoped Pointers) (https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1000.md)
=intpromote revert integral promotions for unary + - ~ operators
=dtorfields don't destruct fields of partially constructed objects
=rvaluerefparam revert rvalue arguments to ref parameters
=rvaluerefparam revert rvalue arguments to ref parameters (https://gist.github.com/andralex/e5405a5d773f07f73196c05f8339435a)
----
*/
6 changes: 0 additions & 6 deletions compiler/test/fail_compilation/b20011.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@ fail_compilation/b20011.d(28): Error: cannot modify expression `S1(cast(ubyte)0u
fail_compilation/b20011.d(31): Error: cannot modify expression `S2(null).member` because it is not an lvalue
fail_compilation/b20011.d(32): Error: cannot modify expression `S2(null).member` because it is not an lvalue
fail_compilation/b20011.d(35): Error: cannot modify expression `U1(cast(ubyte)0u, ).m2` because it is not an lvalue
fail_compilation/b20011.d(40): Error: function `assignableByRef` is not callable using argument types `(ubyte)`
fail_compilation/b20011.d(40): cannot pass rvalue argument `S1(cast(ubyte)0u).member` of type `ubyte` to parameter `ref ubyte p`
fail_compilation/b20011.d(37): `b20011.main.assignableByRef(ref ubyte p)` declared here
fail_compilation/b20011.d(41): Error: function `assignableByOut` is not callable using argument types `(ubyte)`
fail_compilation/b20011.d(41): cannot pass rvalue argument `S1(cast(ubyte)0u).member` of type `ubyte` to parameter `out ubyte p`
fail_compilation/b20011.d(38): `b20011.main.assignableByOut(out ubyte p)` declared here
fail_compilation/b20011.d(42): Error: function `assignableByConstRef` is not callable using argument types `(ubyte)`
fail_compilation/b20011.d(42): cannot pass rvalue argument `S1(cast(ubyte)0u).member` of type `ubyte` to parameter `ref const(ubyte) p`
fail_compilation/b20011.d(39): `b20011.main.assignableByConstRef(ref const(ubyte) p)` declared here
---
*/
module b20011;
Expand Down
4 changes: 1 addition & 3 deletions compiler/test/fail_compilation/fail20183.d
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/* REQUIRED_ARGS: -preview=dip1000
TEST_OUTPUT:
---
fail_compilation/fail20183.d(1016): Error: function `addr` is not callable using argument types `(int)`
fail_compilation/fail20183.d(1016): cannot pass rvalue argument `S(0).i` of type `int` to parameter `return ref int b`
fail_compilation/fail20183.d(1004): `fail20183.addr(return ref int b)` declared here
fail_compilation/fail20183.d(1016): Error: address of variable `__rvalue2` assigned to `p` with longer lifetime
fail_compilation/fail20183.d(1017): Error: address of expression temporary returned by `s()` assigned to `q` with longer lifetime
fail_compilation/fail20183.d(1018): Error: address of struct literal `S(0)` assigned to `r` with longer lifetime
---
Expand Down
2 changes: 1 addition & 1 deletion compiler/test/fail_compilation/fail7603b.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
TEST_OUTPUT:
---
fail_compilation/fail7603b.d(7): Error: cannot create default argument for `return ref` / `out` parameter from constant `true`
fail_compilation/fail7603b.d(7): Error: cannot create default argument for `out` parameter from constant `true`
---
*/
void test(out bool val = true) { }
30 changes: 0 additions & 30 deletions compiler/test/fail_compilation/test21807.d
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,3 @@ class Foo
ca = getSArray();
}
}

/*
TEST_OUTPUT:
---
fail_compilation/test21807.d(117): Error: function `addr` is not callable using argument types `(int)`
fail_compilation/test21807.d(117): cannot pass rvalue argument `S(0).i` of type `int` to parameter `return ref int b`
fail_compilation/test21807.d(106): `test21807.addr(return ref int b)` declared here
---
*/
#line 100

struct S
{
int i;
}

int* addr(return ref int b)
{
return &b;
}

class Foo2
{
int* ptr;

this()
{
ptr = addr(S().i); // struct temporary
}
}
28 changes: 9 additions & 19 deletions compiler/test/fail_compilation/tolvalue.d
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
/**
TEST_OUTPUT:
---
fail_compilation/tolvalue.d(28): Error: cannot take address of template `templateFunc(T)()`, perhaps instantiate it first
fail_compilation/tolvalue.d(29): Error: cannot take address of type `int`
fail_compilation/tolvalue.d(30): Error: cannot take address of constant `3`
fail_compilation/tolvalue.d(31): Error: cannot take address of operator `$`
fail_compilation/tolvalue.d(32): Error: cannot take address of compiler-generated variable `__ctfe`
fail_compilation/tolvalue.d(33): Error: cannot take address of manifest constant `f`
fail_compilation/tolvalue.d(38): Error: cannot create default argument for `ref` / `out` parameter from constant `3`
fail_compilation/tolvalue.d(39): Error: cannot create default argument for `ref` / `out` parameter from compiler-generated variable `__ctfe`
fail_compilation/tolvalue.d(40): Error: cannot create default argument for `ref` / `out` parameter from manifest constant `f`
fail_compilation/tolvalue.d(45): Error: cannot modify constant `3`
fail_compilation/tolvalue.d(46): Error: cannot modify compiler-generated variable `__ctfe`
fail_compilation/tolvalue.d(47): Error: cannot modify manifest constant `f`
fail_compilation/tolvalue.d(25): Error: cannot take address of template `templateFunc(T)()`, perhaps instantiate it first
fail_compilation/tolvalue.d(26): Error: cannot take address of type `int`
fail_compilation/tolvalue.d(27): Error: cannot take address of constant `3`
fail_compilation/tolvalue.d(28): Error: cannot take address of operator `$`
fail_compilation/tolvalue.d(29): Error: cannot take address of compiler-generated variable `__ctfe`
fail_compilation/tolvalue.d(30): Error: cannot take address of manifest constant `f`
fail_compilation/tolvalue.d(35): Error: cannot modify constant `3`
fail_compilation/tolvalue.d(36): Error: cannot modify compiler-generated variable `__ctfe`
fail_compilation/tolvalue.d(37): Error: cannot modify manifest constant `f`
---
*/

Expand All @@ -33,13 +30,6 @@ void addr()
auto x6 = &E.f;
}

void refArg()
{
void f0(ref int = 3) {}
void f1(ref bool = __ctfe) {}
void f3(ref E = E.f) {}
}

void inc(int lz)
{
3++;
Expand Down

0 comments on commit a5fcdb6

Please sign in to comment.