From 60f1e4519eb9ac4b96271267d7f7bcfc554b6b9f Mon Sep 17 00:00:00 2001 From: Sam Vervaeck Date: Mon, 19 Feb 2024 14:17:12 +0100 Subject: [PATCH] Fix #54: BindPattern is not bound during inference inside a match-expression --- bootstrap/cxx/include/bolt/CST.hpp | 11 +++++++++++ bootstrap/cxx/src/CST.cc | 6 ++++++ bootstrap/cxx/src/Checker.cc | 4 ++-- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/bootstrap/cxx/include/bolt/CST.hpp b/bootstrap/cxx/include/bolt/CST.hpp index ad0c0e02d..06feeae6e 100644 --- a/bootstrap/cxx/include/bolt/CST.hpp +++ b/bootstrap/cxx/include/bolt/CST.hpp @@ -1637,6 +1637,9 @@ namespace bolt { }; class MatchCase : public Node { + + Scope* TheScope = nullptr; + public: InferContext* Ctx; @@ -1657,6 +1660,14 @@ namespace bolt { Token* getFirstToken() const override; Token* getLastToken() const override; + inline Scope* getScope() override { + if (TheScope == nullptr) { + TheScope = new Scope(this); + } + return TheScope; + } + + }; class MatchExpression : public Expression { diff --git a/bootstrap/cxx/src/CST.cc b/bootstrap/cxx/src/CST.cc index ffaa0f5cb..82643d7b9 100644 --- a/bootstrap/cxx/src/CST.cc +++ b/bootstrap/cxx/src/CST.cc @@ -78,6 +78,12 @@ namespace bolt { } break; } + case NodeKind::MatchCase: + { + auto Case = static_cast(X); + visitPattern(Case->Pattern, Case); + break; + } case NodeKind::LetDeclaration: { auto Decl = static_cast(X); diff --git a/bootstrap/cxx/src/Checker.cc b/bootstrap/cxx/src/Checker.cc index eee54574c..a0ef8e2c2 100644 --- a/bootstrap/cxx/src/Checker.cc +++ b/bootstrap/cxx/src/Checker.cc @@ -508,6 +508,7 @@ namespace bolt { auto Instance = static_cast(Let->Parent); auto Class = cast(Instance->getScope()->lookup({ {}, getCanonicalText(Instance->Name) }, SymbolKind::Class)); + // TODO check if `Class` is nullptr auto SigLet = cast(Class->getScope()->lookupDirect({ {}, Let->getNameAsString() }, SymbolKind::Var)); auto Params = addClassVars(Class, false); @@ -1297,7 +1298,7 @@ namespace bolt { auto Y = static_cast(N); auto Def = Y->getScope()->lookup(Y->getSymbolPath()); // Name lookup failures will be reported directly in inferExpression(). - if (Def == nullptr || Def->getKind() == NodeKind::SourceFile) { + if (Def == nullptr || Def->getKind() != NodeKind::LetDeclaration) { return; } // This case ensures that a deeply nested structure that references a @@ -1307,7 +1308,6 @@ namespace bolt { RefGraph.addEdge(Stack.top(), Def->Parent); return; } - ZEN_ASSERT(Def->getKind() == NodeKind::LetDeclaration); if (!Stack.empty()) { RefGraph.addEdge(Def, Stack.top()); }