diff options
| author | Yong He <yonghe@outlook.com> | 2025-02-05 12:32:56 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-02-05 12:32:56 -0800 |
| commit | 7911c9437333692db275d2dff41264f4c8023be8 (patch) | |
| tree | dd83ca191f47aed0bd512dfb9412038a7b7d3f0e /source/slang/slang-check-stmt.cpp | |
| parent | 613f43a080f84e2680fb78dc4ed60a553da3b418 (diff) | |
Use two-stage parsing to disambiguate generic app and comparison. (#6281)
* Use two-stage parsing to disambiguate generic app and comparison.
* Typo fix.
* Update doc.
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); } } |
