Skip to content

Commit a5fcdb6

Browse files
committed
I was wrong about return ref... lifetime analysis actually handles it.
1 parent 95385e3 commit a5fcdb6

File tree

8 files changed

+14
-64
lines changed

8 files changed

+14
-64
lines changed

compiler/src/dmd/expressionsem.d

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3288,7 +3288,6 @@ private bool functionParameters(const ref Loc loc, Scope* sc,
32883288
else if (p.storageClass & STC.ref_)
32893289
{
32903290
if (global.params.rvalueRefParam != FeatureState.disabled &&
3291-
// !(p.storageClass & STC.return_) &&
32923291
!arg.isLvalue() &&
32933292
targ.isCopyable())
32943293
{ /* allow rvalues to be passed to ref parameters by copying
@@ -3299,7 +3298,6 @@ private bool functionParameters(const ref Loc loc, Scope* sc,
32993298
ev = new CommaExp(arg.loc, ev, new VarExp(arg.loc, v));
33003299
arg = ev.expressionSemantic(sc);
33013300
}
3302-
// arg = arg.toLvalue(sc, (p.storageClass & STC.return_) ? "create `return ref` parameter from" : "create `ref` parameter from");
33033301
arg = arg.toLvalue(sc, "create `ref` parameter from");
33043302

33053303
// Look for mutable misaligned pointer, etc., in @safe mode

compiler/src/dmd/typesem.d

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,8 +2287,8 @@ Type typeSemantic(Type type, const ref Loc loc, Scope* sc)
22872287
// default arg must be an lvalue
22882288
if (isRefOrOut && !isAuto &&
22892289
!(fparam.storageClass & STC.constscoperef) &&
2290-
(global.params.rvalueRefParam == FeatureState.disabled || (fparam.storageClass & (STC.out_ | STC.return_))))
2291-
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");
2290+
(global.params.rvalueRefParam == FeatureState.disabled || (fparam.storageClass & STC.out_)))
2291+
e = e.toLvalue(sc, (global.params.rvalueRefParam != FeatureState.disabled) ? "create default argument for `out` parameter from" : "create default argument for `ref` / `out` parameter from");
22922292

22932293
fparam.defaultArg = e;
22942294
return (e.op != EXP.error);

compiler/test/compilable/reverthelp.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ Revertable language changes listed by -revert=name:
99
=dip1000 revert DIP1000 changes (Scoped Pointers) (https://github.com/dlang/DIPs/blob/master/DIPs/other/DIP1000.md)
1010
=intpromote revert integral promotions for unary + - ~ operators
1111
=dtorfields don't destruct fields of partially constructed objects
12-
=rvaluerefparam revert rvalue arguments to ref parameters
12+
=rvaluerefparam revert rvalue arguments to ref parameters (https://gist.github.com/andralex/e5405a5d773f07f73196c05f8339435a)
1313
----
1414
*/

compiler/test/fail_compilation/b20011.d

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,9 @@ fail_compilation/b20011.d(28): Error: cannot modify expression `S1(cast(ubyte)0u
55
fail_compilation/b20011.d(31): Error: cannot modify expression `S2(null).member` because it is not an lvalue
66
fail_compilation/b20011.d(32): Error: cannot modify expression `S2(null).member` because it is not an lvalue
77
fail_compilation/b20011.d(35): Error: cannot modify expression `U1(cast(ubyte)0u, ).m2` because it is not an lvalue
8-
fail_compilation/b20011.d(40): Error: function `assignableByRef` is not callable using argument types `(ubyte)`
9-
fail_compilation/b20011.d(40): cannot pass rvalue argument `S1(cast(ubyte)0u).member` of type `ubyte` to parameter `ref ubyte p`
10-
fail_compilation/b20011.d(37): `b20011.main.assignableByRef(ref ubyte p)` declared here
118
fail_compilation/b20011.d(41): Error: function `assignableByOut` is not callable using argument types `(ubyte)`
129
fail_compilation/b20011.d(41): cannot pass rvalue argument `S1(cast(ubyte)0u).member` of type `ubyte` to parameter `out ubyte p`
1310
fail_compilation/b20011.d(38): `b20011.main.assignableByOut(out ubyte p)` declared here
14-
fail_compilation/b20011.d(42): Error: function `assignableByConstRef` is not callable using argument types `(ubyte)`
15-
fail_compilation/b20011.d(42): cannot pass rvalue argument `S1(cast(ubyte)0u).member` of type `ubyte` to parameter `ref const(ubyte) p`
16-
fail_compilation/b20011.d(39): `b20011.main.assignableByConstRef(ref const(ubyte) p)` declared here
1711
---
1812
*/
1913
module b20011;

compiler/test/fail_compilation/fail20183.d

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
/* REQUIRED_ARGS: -preview=dip1000
22
TEST_OUTPUT:
33
---
4-
fail_compilation/fail20183.d(1016): Error: function `addr` is not callable using argument types `(int)`
5-
fail_compilation/fail20183.d(1016): cannot pass rvalue argument `S(0).i` of type `int` to parameter `return ref int b`
6-
fail_compilation/fail20183.d(1004): `fail20183.addr(return ref int b)` declared here
4+
fail_compilation/fail20183.d(1016): Error: address of variable `__rvalue2` assigned to `p` with longer lifetime
75
fail_compilation/fail20183.d(1017): Error: address of expression temporary returned by `s()` assigned to `q` with longer lifetime
86
fail_compilation/fail20183.d(1018): Error: address of struct literal `S(0)` assigned to `r` with longer lifetime
97
---
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
TEST_OUTPUT:
33
---
4-
fail_compilation/fail7603b.d(7): Error: cannot create default argument for `return ref` / `out` parameter from constant `true`
4+
fail_compilation/fail7603b.d(7): Error: cannot create default argument for `out` parameter from constant `true`
55
---
66
*/
77
void test(out bool val = true) { }

compiler/test/fail_compilation/test21807.d

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,3 @@ class Foo
2323
ca = getSArray();
2424
}
2525
}
26-
27-
/*
28-
TEST_OUTPUT:
29-
---
30-
fail_compilation/test21807.d(117): Error: function `addr` is not callable using argument types `(int)`
31-
fail_compilation/test21807.d(117): cannot pass rvalue argument `S(0).i` of type `int` to parameter `return ref int b`
32-
fail_compilation/test21807.d(106): `test21807.addr(return ref int b)` declared here
33-
---
34-
*/
35-
#line 100
36-
37-
struct S
38-
{
39-
int i;
40-
}
41-
42-
int* addr(return ref int b)
43-
{
44-
return &b;
45-
}
46-
47-
class Foo2
48-
{
49-
int* ptr;
50-
51-
this()
52-
{
53-
ptr = addr(S().i); // struct temporary
54-
}
55-
}

compiler/test/fail_compilation/tolvalue.d

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
/**
22
TEST_OUTPUT:
33
---
4-
fail_compilation/tolvalue.d(28): Error: cannot take address of template `templateFunc(T)()`, perhaps instantiate it first
5-
fail_compilation/tolvalue.d(29): Error: cannot take address of type `int`
6-
fail_compilation/tolvalue.d(30): Error: cannot take address of constant `3`
7-
fail_compilation/tolvalue.d(31): Error: cannot take address of operator `$`
8-
fail_compilation/tolvalue.d(32): Error: cannot take address of compiler-generated variable `__ctfe`
9-
fail_compilation/tolvalue.d(33): Error: cannot take address of manifest constant `f`
10-
fail_compilation/tolvalue.d(38): Error: cannot create default argument for `ref` / `out` parameter from constant `3`
11-
fail_compilation/tolvalue.d(39): Error: cannot create default argument for `ref` / `out` parameter from compiler-generated variable `__ctfe`
12-
fail_compilation/tolvalue.d(40): Error: cannot create default argument for `ref` / `out` parameter from manifest constant `f`
13-
fail_compilation/tolvalue.d(45): Error: cannot modify constant `3`
14-
fail_compilation/tolvalue.d(46): Error: cannot modify compiler-generated variable `__ctfe`
15-
fail_compilation/tolvalue.d(47): Error: cannot modify manifest constant `f`
4+
fail_compilation/tolvalue.d(25): Error: cannot take address of template `templateFunc(T)()`, perhaps instantiate it first
5+
fail_compilation/tolvalue.d(26): Error: cannot take address of type `int`
6+
fail_compilation/tolvalue.d(27): Error: cannot take address of constant `3`
7+
fail_compilation/tolvalue.d(28): Error: cannot take address of operator `$`
8+
fail_compilation/tolvalue.d(29): Error: cannot take address of compiler-generated variable `__ctfe`
9+
fail_compilation/tolvalue.d(30): Error: cannot take address of manifest constant `f`
10+
fail_compilation/tolvalue.d(35): Error: cannot modify constant `3`
11+
fail_compilation/tolvalue.d(36): Error: cannot modify compiler-generated variable `__ctfe`
12+
fail_compilation/tolvalue.d(37): Error: cannot modify manifest constant `f`
1613
---
1714
*/
1815

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

36-
void refArg()
37-
{
38-
void f0(ref int = 3) {}
39-
void f1(ref bool = __ctfe) {}
40-
void f3(ref E = E.f) {}
41-
}
42-
4333
void inc(int lz)
4434
{
4535
3++;

0 commit comments

Comments
 (0)