Skip to content

Commit

Permalink
allow ref on locals, globals, and statics
Browse files Browse the repository at this point in the history
  • Loading branch information
WalterBright committed Apr 30, 2024
1 parent e60bfd1 commit c0e8beb
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1001,9 +1001,9 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
}
}

if ((dsym.storage_class & (STC.ref_ | STC.parameter | STC.foreach_ | STC.temp | STC.result)) == STC.ref_ && dsym.ident != Id.This)
if ((dsym.storage_class & (STC.ref_ | STC.field)) == (STC.ref_ | STC.field) && dsym.ident != Id.This)
{
.error(dsym.loc, "%s `%s` - only parameters, functions and `foreach` declarations can be `ref`", dsym.kind, dsym.toPrettyChars);
.error(dsym.loc, "%s `%s` - field declarations cannot be `ref`", dsym.kind, dsym.toPrettyChars);
}

if (dsym.type.hasWild())
Expand Down
14 changes: 14 additions & 0 deletions compiler/test/compilable/testlocalref.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
struct S { int a; }

void test()
{
S s;
ref int r1 = s.a;
r1 = 3;
__gshared S t2;
ref int r2 = t2.a;
static S t3;
ref int r3 = t3.a;
extern S t4;
ref int r4 = t4.a;
}
6 changes: 4 additions & 2 deletions compiler/test/fail_compilation/diag9679.d
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/*
TEST_OUTPUT:
---
fail_compilation/diag9679.d(11): Error: variable `diag9679.main.n` - only parameters, functions and `foreach` declarations can be `ref`
fail_compilation/diag9679.d(12): Error: variable `diag9679.main.n` - storage class `auto` has no effect if type is not inferred, did you mean `scope`?
fail_compilation/diag9679.d(13): Error: variable `diag9679.main.n` - storage class `auto` has no effect if type is not inferred, did you mean `scope`?
fail_compilation/diag9679.d(14): Error: variable `diag9679.main.S.a` - field declarations cannot be `ref`
---
*/


void main()
{
if (ref n = 1) {}
if (auto int n = 1) {}
struct S { ref int a; }
}

0 comments on commit c0e8beb

Please sign in to comment.