Fix TryStmt constructors

typecheck-v2
Ibrahim Numanagić 2025-03-05 11:56:54 -08:00
parent 0d4e35576a
commit 3291f5c4d1
3 changed files with 18 additions and 15 deletions

View File

@ -298,7 +298,7 @@ try_stmt <-
}
/ ('try' _ ':' _ suite) (SAMEDENT 'finally' _ ':' _ suite)? {
return asts(Try, LOC,
ac_stmt(V0), vector<ExceptStmt*>{},
ac_stmt(V0), vector<ExceptStmt*>{}, nullptr,
VS.size() > 1 ? ac_stmt(V1) : nullptr
);
}

View File

@ -121,7 +121,8 @@ void TypecheckVisitor::visit(TryStmt *stmt) {
// Handle all other exceptions
c->exc = transformType(c->getException());
auto t = extractClassType(c->exc);
if (c->getException()) {
auto t = extractClassType(c->getException());
bool exceptionOK = false;
for (auto &p : getSuperTypes(t))
if (p->is("std.internal.types.error.BaseException.0")) {
@ -129,9 +130,10 @@ void TypecheckVisitor::visit(TryStmt *stmt) {
break;
}
if (!exceptionOK)
E(Error::CATCH_EXCEPTION_TYPE, c->exc, t->toString());
E(Error::CATCH_EXCEPTION_TYPE, c->getException(), t->toString());
if (val)
unify(val->getType(), extractType(c->getException()));
}
ctx->blockLevel++;
c->suite = SuiteStmt::wrap(transform(c->getSuite()));
ctx->blockLevel--;
@ -221,6 +223,7 @@ void TypecheckVisitor::visit(WithStmt *stmt) {
as, N<ExprStmt>(N<CallExpr>(N<DotExpr>(N<IdExpr>(var), "__enter__"))),
N<TryStmt>(!content.empty() ? N<SuiteStmt>(content) : clone(stmt->getSuite()),
std::vector<ExceptStmt *>{},
nullptr,
N<SuiteStmt>(N<ExprStmt>(
N<CallExpr>(N<DotExpr>(N<IdExpr>(var), "__exit__")))))};
}

View File

@ -576,5 +576,5 @@ class Foo:
def goo(self):
print(self.__name__)
Foo().goo()
#! error: 'Foo' object has no attribute '__name__'
#! during the realization
#! 'Foo' object has no attribute '__name__'
#! during the realization of goo