From c0e8bebf4e9aa26d21c5192973db2419c3130155 Mon Sep 17 00:00:00 2001 From: Walter Bright Date: Mon, 29 Apr 2024 23:00:27 -0700 Subject: [PATCH] allow ref on locals, globals, and statics --- compiler/src/dmd/dsymbolsem.d | 4 ++-- compiler/test/compilable/testlocalref.d | 14 ++++++++++++++ compiler/test/fail_compilation/diag9679.d | 6 ++++-- 3 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 compiler/test/compilable/testlocalref.d diff --git a/compiler/src/dmd/dsymbolsem.d b/compiler/src/dmd/dsymbolsem.d index 65a1b0436502..12cc5ff256ff 100644 --- a/compiler/src/dmd/dsymbolsem.d +++ b/compiler/src/dmd/dsymbolsem.d @@ -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()) diff --git a/compiler/test/compilable/testlocalref.d b/compiler/test/compilable/testlocalref.d new file mode 100644 index 000000000000..88f35d988f70 --- /dev/null +++ b/compiler/test/compilable/testlocalref.d @@ -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; +} diff --git a/compiler/test/fail_compilation/diag9679.d b/compiler/test/fail_compilation/diag9679.d index 85923b7189e1..8a934ae50187 100644 --- a/compiler/test/fail_compilation/diag9679.d +++ b/compiler/test/fail_compilation/diag9679.d @@ -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; } }