Skip to content

Commit

Permalink
Fix 24051 - Safety attrib inference of enum/immut/const decls (#15434)
Browse files Browse the repository at this point in the history
* Fix 24051 - Safety attrib inference of enum/immut/const decls

* Make `INVALID_HANDLE_VALUE` @trusted

* Push and pop scopes
  • Loading branch information
dkorpel authored Jul 27, 2023
1 parent 081af53 commit fd14379
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
3 changes: 3 additions & 0 deletions compiler/src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1219,8 +1219,11 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
bool needctfe = dsym.isDataseg() || (dsym.storage_class & STC.manifest);
if (needctfe)
sc = sc.startCTFE();
sc = sc.push();
sc.varDecl = dsym; // https://issues.dlang.org/show_bug.cgi?id=24051
exp = exp.expressionSemantic(sc);
exp = resolveProperties(sc, exp);
sc = sc.pop();
if (needctfe)
sc = sc.endCTFE();
ei.exp = exp;
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dmd/func.d
Original file line number Diff line number Diff line change
Expand Up @@ -4558,7 +4558,7 @@ bool setUnsafe(Scope* sc,
.error(loc, fmt, arg0 ? arg0.toChars() : "", arg1 ? arg1.toChars() : "", arg2 ? arg2.toChars() : "");
return true;
}
else if (!(sc.varDecl.storage_class & STC.system))
else if (!(sc.varDecl.storage_class & STC.trusted))
{
sc.varDecl.storage_class |= STC.system;
}
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dmd/semantic2.d
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,9 @@ private extern(C++) final class Semantic2Visitor : Visitor
return;

//printf("VarDeclaration::semantic2('%s')\n", toChars());
sc = sc.push();
sc.varDecl = vd;
scope(exit) sc.varDecl = null;
scope(exit) sc = sc.pop();

if (vd.aliasTuple) // if it's a tuple
{
Expand Down
26 changes: 26 additions & 0 deletions compiler/test/fail_compilation/systemvariables_var_init.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
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
---
*/

// https://issues.dlang.org/show_bug.cgi?id=24051

enum uint* ptrEnum = cast(uint*) 0xC00000;
const uint* ptrConst = cast(uint*) 0xC00000;
uint* ptrVarSafe = null;
uint* ptrVar = cast(uint*) 0xC00000;
@trusted uint* ptrTrusted = cast(uint*) 0xC00000;

void varInitializers() @safe
{
*ptrEnum = 0;
*ptrConst = 0;
*ptrVarSafe = 0;
*ptrVar = 0;
*ptrTrusted = 0;
}
2 changes: 1 addition & 1 deletion druntime/src/core/sys/windows/winbase.d
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ enum : DWORD {
STD_ERROR_HANDLE = 0xFFFFFFF4
}

enum HANDLE INVALID_HANDLE_VALUE = cast(HANDLE) (-1);
@trusted enum HANDLE INVALID_HANDLE_VALUE = cast(HANDLE) (-1);

enum : DWORD {
GET_TAPE_MEDIA_INFORMATION = 0,
Expand Down

0 comments on commit fd14379

Please sign in to comment.