Skip to content

Commit

Permalink
Fix ref on function param
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright authored and adamdruppe committed Jun 30, 2024
1 parent a50339d commit 64d260f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 10 deletions.
8 changes: 5 additions & 3 deletions changelog/dmd.reflocal.dd
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

For example, one can now write:
```
struct S { int a; }

void main()
{
int i;
ref int r = i;
S s;
ref int r = s.a;
r = 3;
assert(i == 3);
assert(s.a == 3);
}
```
4 changes: 2 additions & 2 deletions compiler/src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1105,8 +1105,8 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
}
}

if ((dsym.storage_class & (STC.ref_ | STC.field)) == STC.ref_ && !dsym._init)
.error(dsym.loc, "%s `%s` - initializer is required for `ref` variable", dsym.kind, dsym.toPrettyChars, dsym.type.toChars());
if ((dsym.storage_class & (STC.ref_ | STC.field | STC.parameter | STC.temp | STC.foreach_)) == STC.ref_ && !dsym._init && dsym.ident != Id.This)
.error(dsym.loc, "%s `%s` - initializer is required for `ref` variable", dsym.kind, dsym.toPrettyChars);

FuncDeclaration fd = parent.isFuncDeclaration();
if (dsym.type.isscope() && !(dsym.storage_class & STC.nodtor))
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dmd/escape.d
Original file line number Diff line number Diff line change
Expand Up @@ -1956,9 +1956,9 @@ void escapeByRef(Expression e, EscapeByResults* er, bool live = false, bool retR
auto v = e.var.isVarDeclaration();
if (v)
{
if (v.storage_class & STC.ref_ && v.storage_class & (STC.foreach_ | STC.temp) && v._init)
if (v.storage_class & STC.ref_ && v._init)
{
/* If compiler generated ref temporary
/* If ref
* (ref v = ex; ex)
* look at the initializer instead
*/
Expand Down
5 changes: 2 additions & 3 deletions compiler/test/fail_compilation/diag9679.d
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ TEST_OUTPUT:
---
fail_compilation/diag9679.d(15): Error: variable `diag9679.main.n` - storage class `auto` has no effect if type is not inferred, did you mean `scope`?
fail_compilation/diag9679.d(16): Error: variable `diag9679.main.S.a` - field declarations cannot be `ref`
fail_compilation/diag9679.d(23): Error: returning `r` escapes a reference to local variable `r`
fail_compilation/diag9679.d(30): Error: returning `r` escapes a reference to local variable `r`
fail_compilation/diag9679.d(23): Error: returning `r` escapes a reference to local variable `i`
---
*/



void main()
{
if (ref n = 1) {}
Expand Down Expand Up @@ -54,4 +54,3 @@ void test5()
{
ref int r5;
}

0 comments on commit 64d260f

Please sign in to comment.