1
0
mirror of https://github.com/exaloop/codon.git synced 2025-06-03 15:03:52 +08:00

Fix TryStmt constructors

This commit is contained in:
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,8 +298,8 @@ try_stmt <-
} }
/ ('try' _ ':' _ suite) (SAMEDENT 'finally' _ ':' _ suite)? { / ('try' _ ':' _ suite) (SAMEDENT 'finally' _ ':' _ suite)? {
return asts(Try, LOC, return asts(Try, LOC,
ac_stmt(V0), vector<ExceptStmt*>{}, ac_stmt(V0), vector<ExceptStmt*>{}, nullptr,
VS.size() > 1 ? ac_stmt(V1): nullptr VS.size() > 1 ? ac_stmt(V1) : nullptr
); );
} }
else_finally <- else_finally <-

View File

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

View File

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