typecheck-v2
Ibrahim Numanagić 2025-02-25 20:36:34 -08:00
parent 6dd6f47461
commit 574c74188b
5 changed files with 26 additions and 0 deletions

View File

@ -106,6 +106,7 @@ enum Error {
SLICE_STEP_ZERO,
OP_NO_MAGIC,
INST_CALLABLE_STATIC,
CATCH_EXCEPTION_TYPE,
TYPE_CANNOT_REALIZE_ATTR,
TYPE_UNIFY,
TYPE_FAILED,
@ -426,6 +427,8 @@ template <class... TA> std::string Emsg(Error e, const TA &...args) {
return fmt::format("unsupported operand type(s) for {}: '{}' and '{}'", args...);
case Error::INST_CALLABLE_STATIC:
return fmt::format("Callable cannot take static types");
case Error::CATCH_EXCEPTION_TYPE:
return fmt::format("'{}' does not inherit from BaseException", args...);
case Error::TYPE_CANNOT_REALIZE_ATTR:
return fmt::format("type of attribute '{}' of object '{}' cannot be inferred",

View File

@ -11,6 +11,7 @@ namespace codon::ast {
using namespace types;
using namespace matcher;
using namespace error;
/// Transform asserts.
/// @example
@ -119,6 +120,16 @@ void TypecheckVisitor::visit(TryStmt *stmt) {
} else {
// Handle all other exceptions
c->exc = transformType(c->getException());
auto t = extractClassType(c->exc);
bool exceptionOK = false;
for (auto &p : getSuperTypes(t))
if (p->is("std.internal.types.error.BaseException.0")) {
exceptionOK = true;
break;
}
if (!exceptionOK)
E(Error::CATCH_EXCEPTION_TYPE, c->exc, t->toString());
if (val)
unify(val->getType(), extractType(c->getException()));
ctx->blockLevel++;

View File

@ -3,3 +3,5 @@
__all__ = ["jit", "convert", "JITError"]
from .decorator import jit, convert, execute, JITError
__codon__ = False

View File

@ -417,3 +417,5 @@ class ProxyFunc:
data: Ptr[byte]
T: type
TR: type
__codon__: Static[bool] = True

View File

@ -569,3 +569,11 @@ def foo(x, y):
return a
return a
print foo(5, (1, 2, 3)) #: 40
#%% nontype_name,barebones
class Foo:
def goo(self):
print(self.__name__)
Foo().goo()
#! error: 'Foo' object has no attribute '__name__'
#! during the realization