diff --git a/compiler/src/dmd/statementsem.d b/compiler/src/dmd/statementsem.d index ae68d6a67e..d0e1b9a23d 100644 --- a/compiler/src/dmd/statementsem.d +++ b/compiler/src/dmd/statementsem.d @@ -3542,6 +3542,7 @@ Statement statementSemanticVisit(Statement s, Scope* sc) ls2.statement = ls; sc = sc.push(); + sc.lastVar = sc.enclosing.lastVar; sc.scopesym = sc.enclosing.scopesym; sc.ctorflow.orCSX(CSX.label); @@ -3549,6 +3550,10 @@ Statement statementSemanticVisit(Statement s, Scope* sc) sc.slabel = ls; if (ls.statement) ls.statement = ls.statement.statementSemantic(sc); + + //issue 24534: lastVar may have been updated in the nested scope + sc.enclosing.lastVar = sc.lastVar; + sc.pop(); result = ls; diff --git a/compiler/test/fail_compilation/issue24534.d b/compiler/test/fail_compilation/issue24534.d new file mode 100644 index 0000000000..1689b6c5d1 --- /dev/null +++ b/compiler/test/fail_compilation/issue24534.d @@ -0,0 +1,25 @@ +/* +TEST_OUTPUT: +--- +fail_compilation/issue24534.d(12): Error: `goto` skips declaration of variable `issue24534.f1.y1` +fail_compilation/issue24534.d(13): declared here +fail_compilation/issue24534.d(20): Error: `goto` skips declaration of variable `issue24534.f2.y2` +fail_compilation/issue24534.d(22): declared here +--- +*/ +void f1(){ //always failed with error about skipping a declaration + int x1; + goto Label1; + int y1; + Label1: + int z1; +} + +void f2(){ //compiled fine before this bug was fixed + int x2; + goto Label2; + Dummy2: + int y2; + Label2: + int z2; +}