diff options
Diffstat (limited to 'source/slang/slang-check-stmt.cpp')
| -rw-r--r-- | source/slang/slang-check-stmt.cpp | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/source/slang/slang-check-stmt.cpp b/source/slang/slang-check-stmt.cpp index 550993957..151c9324a 100644 --- a/source/slang/slang-check-stmt.cpp +++ b/source/slang/slang-check-stmt.cpp @@ -54,6 +54,8 @@ void SemanticsStmtVisitor::visitDeclStmt(DeclStmt* stmt) // that need to be recursively checked. // ensureDeclBase(stmt->decl, DeclCheckState::DefinitionChecked, this); + if (auto decl = as<Decl>(stmt->decl)) + decl->hiddenFromLookup = false; } void SemanticsStmtVisitor::visitBlockStmt(BlockStmt* stmt) @@ -66,14 +68,41 @@ void SemanticsStmtVisitor::visitBlockStmt(BlockStmt* stmt) if (as<AggTypeDeclBase>(decl)) ensureAllDeclsRec(decl, DeclCheckState::DefinitionChecked); } + + // Consider this code: + // ``` + // { + // int a = 5 + b; // should error. + // int b = 3; + // } + // + // ``` + // In order to detect the error trying to use `b` before it's declared within + // a block, our lookup logic contains a condition that ignores a decl if its + // `hiddenFromLookup` field is set to `true`. + // See _lookUpDirectAndTransparentMembers(). + // This field will be set to false when we reach the decl through the DeclStmt. + // + if (auto seqStmt = as<SeqStmt>(stmt->body)) + { + for (auto subStmt : seqStmt->stmts) + { + if (auto declStmt = as<DeclStmt>(subStmt)) + { + if (auto decl = as<Decl>(declStmt->decl)) + decl->hiddenFromLookup = true; + } + } + } } checkStmt(stmt->body); } void SemanticsStmtVisitor::visitSeqStmt(SeqStmt* stmt) { - for (auto ss : stmt->stmts) + for (auto& ss : stmt->stmts) { + ss = maybeParseStmt(ss, *this); checkStmt(ss); } } |
