Skip to content

Commit

Permalink
Fix union type instantiation and pendingRealizations placement
Browse files Browse the repository at this point in the history
  • Loading branch information
inumanag committed Nov 10, 2023
1 parent f3901b8 commit f255698
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
4 changes: 2 additions & 2 deletions codon/parser/visitors/typecheck/ctx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ types::TypePtr TypeContext::instantiate(const SrcInfo &srcInfo,
if (auto l = i.second->getLink()) {
i.second->setSrcInfo(srcInfo);
if (l->defaultType) {
pendingDefaults.insert(i.second);
getRealizationBase()->pendingDefaults.insert(i.second);
}
}
}
if (t->getUnion() && !t->getUnion()->isSealed()) {
t->setSrcInfo(srcInfo);
pendingDefaults.insert(t);
getRealizationBase()->pendingDefaults.insert(t);
}
if (auto r = t->getRecord())
if (r->repeats && r->repeats->canRealize())
Expand Down
2 changes: 1 addition & 1 deletion codon/parser/visitors/typecheck/ctx.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ struct TypeContext : public Context<TypecheckItem> {
types::TypePtr returnType = nullptr;
/// Typechecking iteration
int iteration = 0;
std::set<types::TypePtr> pendingDefaults;
};
std::vector<RealizationBase> realizationBases;

/// The current type-checking level (for type instantiation and generalization).
int typecheckLevel;
std::set<types::TypePtr> pendingDefaults;
int changedNodes;

/// The age of the currently parsed statement.
Expand Down
11 changes: 7 additions & 4 deletions codon/parser/visitors/typecheck/infer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ StmtPtr TypecheckVisitor::inferTypes(StmtPtr result, bool isToplevel) {
bool anotherRound = false;
// Special case: return type might have default as well (e.g., Union)
if (ctx->getRealizationBase()->returnType)
ctx->pendingDefaults.insert(ctx->getRealizationBase()->returnType);
for (auto &unbound : ctx->pendingDefaults) {
ctx->getRealizationBase()->pendingDefaults.insert(
ctx->getRealizationBase()->returnType);
for (auto &unbound : ctx->getRealizationBase()->pendingDefaults) {
if (auto tu = unbound->getUnion()) {
// Seal all dynamic unions after the iteration is over
if (!tu->isSealed()) {
Expand All @@ -113,7 +114,7 @@ StmtPtr TypecheckVisitor::inferTypes(StmtPtr result, bool isToplevel) {
anotherRound = true;
}
}
ctx->pendingDefaults.clear();
ctx->getRealizationBase()->pendingDefaults.clear();
if (anotherRound)
continue;

Expand Down Expand Up @@ -936,7 +937,9 @@ TypecheckVisitor::generateSpecialAst(types::FuncType *type) {
N<ThrowStmt>(N<CallExpr>(N<IdExpr>("std.internal.types.error.TypeError"),
N<StringExpr>("invalid union call"))));
// suite->stmts.push_back(N<ReturnStmt>(N<NoneExpr>()));
unify(type->getRetType(), ctx->instantiate(ctx->getType("Union")));

auto ret = ctx->instantiate(ctx->getType("Union"));
unify(type->getRetType(), ret);
ast->suite = suite;
} else if (startswith(ast->name, "__internal__.get_union_first:0")) {
// def __internal__.get_union_first(union: Union[T0]):
Expand Down

0 comments on commit f255698

Please sign in to comment.