Skip to content

Commit

Permalink
[Fix] Fix recursive let for well formed check (apache#5780)
Browse files Browse the repository at this point in the history
  • Loading branch information
icemelon authored and zhiics committed Jul 2, 2020
1 parent 14a2bfc commit 42eba1c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
10 changes: 7 additions & 3 deletions src/relay/analysis/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,13 @@ class VarVisitor : protected ExprVisitor, protected PatternVisitor {
}

void VisitExpr_(const LetNode* op) final {
MarkBounded(op->var);
VisitExpr(op->value);
VisitExpr(op->body);
Expr let = GetRef<Let>(op);
while (auto let_node = let.as<LetNode>()) {
MarkBounded(let_node->var);
VisitExpr(let_node->value);
let = let_node->body;
}
VisitExpr(let);
}

void VisitPattern(const Pattern& p) final { PatternVisitor::VisitPattern(p); }
Expand Down
21 changes: 15 additions & 6 deletions src/relay/analysis/well_formed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,21 @@ class WellFormedChecker : private ExprVisitor, PatternVisitor {
}

void VisitExpr_(const LetNode* l) final {
Scope s(this);
// we do letrec only for FunctionNode,
// but shadowing let in let binding is likely programming error, and we should forbidden it.
Bound(l->var);
CheckWellFormed(l->value);
CheckWellFormed(l->body);
std::vector<Scope*> scopes;
Expr let = GetRef<Let>(l);
while (auto let_node = let.as<LetNode>()) {
scopes.push_back(new Scope(this));
// we do letrec only for FunctionNode,
// but shadowing let in let binding is likely programming error, and we should forbidden it.
Bound(let_node->var);
CheckWellFormed(let_node->value);
let = let_node->body;
}
CheckWellFormed(let);
while (!scopes.empty()) {
delete scopes.back();
scopes.pop_back();
}
}

void VisitExpr_(const FunctionNode* f) final {
Expand Down

0 comments on commit 42eba1c

Please sign in to comment.