diff --git a/compiler/src/dmd/dsymbolsem.d b/compiler/src/dmd/dsymbolsem.d index 2e8225108b6c..0b2117c4ca91 100644 --- a/compiler/src/dmd/dsymbolsem.d +++ b/compiler/src/dmd/dsymbolsem.d @@ -647,6 +647,11 @@ Dsymbol search_correct(Scope* _this, Identifier ident) // https://github.com/dlang/dmd/issues/18763 if (decl.isAliasDeclaration() && decl.inuse) return null; + + // Same reasoning, but for a variable whose own initializer + // is still being resolved. https://github.com/dlang/dmd/issues/20411 + if (decl.isVarDeclaration() && sc.varDecl == decl) + return null; } // Or `deprecated` ones if we're not in a deprecated scope if (s.isDeprecated() && !sc.isDeprecated()) @@ -2410,7 +2415,10 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor return; } //printf("inferring type for %s with init %s\n", dsym.toChars(), dsym._init.toChars()); + sc = sc.push(); + sc.varDecl = dsym; // https://github.com/dlang/dmd/issues/20411 dsym._init = dsym._init.inferInitializerType(sc, dsym.type, global.errorSink); + sc = sc.pop(); dsym.type = dsym._init.initializerToExpression(null, sc.inCfile).type; if (autoDollarDims.length) diff --git a/compiler/test/fail_compilation/fix20411.d b/compiler/test/fail_compilation/fix20411.d new file mode 100644 index 000000000000..3770a7a5fea9 --- /dev/null +++ b/compiler/test/fail_compilation/fix20411.d @@ -0,0 +1,9 @@ +/* +TEST_OUTPUT: +--- +fail_compilation/fix20411.d(9): Error: undefined identifier `AlmostSimilar` +--- +*/ + +// https://github.com/dlang/dmd/issues/20411 +const almostSimilar = AlmostSimilar;