Fix #54: BindPattern is not bound during inference inside a match-expression

This commit is contained in:
Sam Vervaeck 2024-02-19 14:17:12 +01:00
parent 521e1f1e4f
commit 60f1e4519e
Signed by: samvv
SSH key fingerprint: SHA256:dIg0ywU1OP+ZYifrYxy8c5esO72cIKB+4/9wkZj1VaY
3 changed files with 19 additions and 2 deletions

View file

@ -1637,6 +1637,9 @@ namespace bolt {
}; };
class MatchCase : public Node { class MatchCase : public Node {
Scope* TheScope = nullptr;
public: public:
InferContext* Ctx; InferContext* Ctx;
@ -1657,6 +1660,14 @@ namespace bolt {
Token* getFirstToken() const override; Token* getFirstToken() const override;
Token* getLastToken() const override; Token* getLastToken() const override;
inline Scope* getScope() override {
if (TheScope == nullptr) {
TheScope = new Scope(this);
}
return TheScope;
}
}; };
class MatchExpression : public Expression { class MatchExpression : public Expression {

View file

@ -78,6 +78,12 @@ namespace bolt {
} }
break; break;
} }
case NodeKind::MatchCase:
{
auto Case = static_cast<MatchCase*>(X);
visitPattern(Case->Pattern, Case);
break;
}
case NodeKind::LetDeclaration: case NodeKind::LetDeclaration:
{ {
auto Decl = static_cast<LetDeclaration*>(X); auto Decl = static_cast<LetDeclaration*>(X);

View file

@ -508,6 +508,7 @@ namespace bolt {
auto Instance = static_cast<InstanceDeclaration*>(Let->Parent); auto Instance = static_cast<InstanceDeclaration*>(Let->Parent);
auto Class = cast<ClassDeclaration>(Instance->getScope()->lookup({ {}, getCanonicalText(Instance->Name) }, SymbolKind::Class)); auto Class = cast<ClassDeclaration>(Instance->getScope()->lookup({ {}, getCanonicalText(Instance->Name) }, SymbolKind::Class));
// TODO check if `Class` is nullptr
auto SigLet = cast<LetDeclaration>(Class->getScope()->lookupDirect({ {}, Let->getNameAsString() }, SymbolKind::Var)); auto SigLet = cast<LetDeclaration>(Class->getScope()->lookupDirect({ {}, Let->getNameAsString() }, SymbolKind::Var));
auto Params = addClassVars(Class, false); auto Params = addClassVars(Class, false);
@ -1297,7 +1298,7 @@ namespace bolt {
auto Y = static_cast<ReferenceExpression*>(N); auto Y = static_cast<ReferenceExpression*>(N);
auto Def = Y->getScope()->lookup(Y->getSymbolPath()); auto Def = Y->getScope()->lookup(Y->getSymbolPath());
// Name lookup failures will be reported directly in inferExpression(). // Name lookup failures will be reported directly in inferExpression().
if (Def == nullptr || Def->getKind() == NodeKind::SourceFile) { if (Def == nullptr || Def->getKind() != NodeKind::LetDeclaration) {
return; return;
} }
// This case ensures that a deeply nested structure that references a // This case ensures that a deeply nested structure that references a
@ -1307,7 +1308,6 @@ namespace bolt {
RefGraph.addEdge(Stack.top(), Def->Parent); RefGraph.addEdge(Stack.top(), Def->Parent);
return; return;
} }
ZEN_ASSERT(Def->getKind() == NodeKind::LetDeclaration);
if (!Stack.empty()) { if (!Stack.empty()) {
RefGraph.addEdge(Def, Stack.top()); RefGraph.addEdge(Def, Stack.top());
} }