Add supplemental error for @system variable access (#15464)

This commit is contained in:
Dennis 2023-08-01 12:08:49 +02:00 committed by GitHub
parent 04f46ff87d
commit 825e750b93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 15 deletions

View file

@ -1167,10 +1167,11 @@ extern (C++) class VarDeclaration : Declaration
bool inClosure; /// is inserted into a GC allocated closure
bool inAlignSection; /// is inserted into an aligned section on stack
}
bool systemInferred; /// @system was inferred from initializer
}
import dmd.common.bitfields : generateBitFields;
mixin(generateBitFields!(BitFields, ushort));
mixin(generateBitFields!(BitFields, uint));
byte canassign; // it can be assigned to
ubyte isdataseg; // private data for isDataseg 0 unset, 1 true, 2 false

View file

@ -244,7 +244,7 @@ public:
// The index of this variable on the CTFE stack, ~0u if not allocated
unsigned ctfeAdrOnStack;
private:
uint16_t bitFields;
uint32_t bitFields;
public:
int8_t canassign; // // it can be assigned to
uint8_t isdataseg; // private data for isDataseg
@ -278,6 +278,8 @@ public:
bool inAlignSection() const; // is inserted into aligned section on stack
bool inAlignSection(bool v);
#endif
bool systemInferred() const;
bool systemInferred(bool v);
static VarDeclaration *create(const Loc &loc, Type *t, Identifier *id, Initializer *init, StorageClass storage_class = STCundefined);
VarDeclaration *syntaxCopy(Dsymbol *) override;
void setFieldOffset(AggregateDeclaration *ad, FieldState& fieldState, bool isunion) override final;

View file

@ -1485,6 +1485,7 @@ extern (C++) abstract class Expression : ASTNode
else
{
sc.varDecl.storage_class |= STC.system;
sc.varDecl.systemInferred = true;
}
}
return false;

View file

@ -875,6 +875,13 @@ Lagain:
if (sc.setUnsafePreview(global.params.systemVariables, false, loc,
"cannot access `@system` variable `%s` in @safe code", sd))
{
if (auto v = sd.isVarDeclaration())
{
if (v.systemInferred)
errorSupplemental(v.loc, "`%s` is inferred to be `@system` from its initializer here", v.toChars());
else
errorSupplemental(v.loc, "`%s` is declared here", v.toChars());
}
return ErrorExp.get();
}
}

View file

@ -6090,8 +6090,10 @@ public:
bool inClosure(bool v);
bool inAlignSection() const;
bool inAlignSection(bool v);
bool systemInferred() const;
bool systemInferred(bool v);
private:
uint16_t bitFields;
uint32_t bitFields;
public:
int8_t canassign;
uint8_t isdataseg;

View file

@ -4561,6 +4561,7 @@ bool setUnsafe(Scope* sc,
else if (!(sc.varDecl.storage_class & STC.trusted))
{
sc.varDecl.storage_class |= STC.system;
sc.varDecl.systemInferred = true;
}
}
return false;

View file

@ -2,15 +2,24 @@
REQUIRED_ARGS: -preview=systemVariables
TEST_OUTPUT:
---
fail_compilation/systemvariables.d(30): Error: cannot access `@system` variable `gInt` in @safe code
fail_compilation/systemvariables.d(31): Error: cannot access `@system` variable `gInt` in @safe code
fail_compilation/systemvariables.d(32): Error: cannot access `@system` variable `gArr` in @safe code
fail_compilation/systemvariables.d(33): Error: cannot access `@system` variable `gArr` in @safe code
fail_compilation/systemvariables.d(34): Error: cannot access `@system` variable `gInt` in @safe code
fail_compilation/systemvariables.d(37): Error: cannot access `@system` variable `lSys` in @safe code
fail_compilation/systemvariables.d(38): Error: cannot access `@system` variable `lSys` in @safe code
fail_compilation/systemvariables.d(39): Error: cannot access `@system` variable `lSys` in @safe code
fail_compilation/systemvariables.d(41): Error: cannot access `@system` variable `eInt` in @safe code
fail_compilation/systemvariables.d(39): Error: cannot access `@system` variable `gInt` in @safe code
fail_compilation/systemvariables.d(29): `gInt` is declared here
fail_compilation/systemvariables.d(40): Error: cannot access `@system` variable `gInt` in @safe code
fail_compilation/systemvariables.d(29): `gInt` is declared here
fail_compilation/systemvariables.d(41): Error: cannot access `@system` variable `gArr` in @safe code
fail_compilation/systemvariables.d(31): `gArr` is declared here
fail_compilation/systemvariables.d(42): Error: cannot access `@system` variable `gArr` in @safe code
fail_compilation/systemvariables.d(31): `gArr` is declared here
fail_compilation/systemvariables.d(43): Error: cannot access `@system` variable `gInt` in @safe code
fail_compilation/systemvariables.d(29): `gInt` is declared here
fail_compilation/systemvariables.d(46): Error: cannot access `@system` variable `lSys` in @safe code
fail_compilation/systemvariables.d(45): `lSys` is declared here
fail_compilation/systemvariables.d(47): Error: cannot access `@system` variable `lSys` in @safe code
fail_compilation/systemvariables.d(45): `lSys` is declared here
fail_compilation/systemvariables.d(48): Error: cannot access `@system` variable `lSys` in @safe code
fail_compilation/systemvariables.d(45): `lSys` is declared here
fail_compilation/systemvariables.d(50): Error: cannot access `@system` variable `eInt` in @safe code
fail_compilation/systemvariables.d(30): `eInt` is declared here
---
*/

View file

@ -2,9 +2,12 @@
REQUIRED_ARGS: -preview=systemVariables
TEST_OUTPUT:
---
fail_compilation/systemvariables_var_init.d(21): Error: cannot access `@system` variable `ptrEnum` in @safe code
fail_compilation/systemvariables_var_init.d(22): Error: cannot access `@system` variable `ptrConst` in @safe code
fail_compilation/systemvariables_var_init.d(24): Error: cannot access `@system` variable `ptrVar` in @safe code
fail_compilation/systemvariables_var_init.d(24): Error: cannot access `@system` variable `ptrEnum` in @safe code
fail_compilation/systemvariables_var_init.d(16): `ptrEnum` is inferred to be `@system` from its initializer here
fail_compilation/systemvariables_var_init.d(25): Error: cannot access `@system` variable `ptrConst` in @safe code
fail_compilation/systemvariables_var_init.d(17): `ptrConst` is inferred to be `@system` from its initializer here
fail_compilation/systemvariables_var_init.d(27): Error: cannot access `@system` variable `ptrVar` in @safe code
fail_compilation/systemvariables_var_init.d(19): `ptrVar` is inferred to be `@system` from its initializer here
---
*/