diff --git a/codon/compiler/error.h b/codon/compiler/error.h index b0671a9d..6b7be678 100644 --- a/codon/compiler/error.h +++ b/codon/compiler/error.h @@ -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 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", diff --git a/codon/parser/visitors/typecheck/error.cpp b/codon/parser/visitors/typecheck/error.cpp index a26f3e0c..a946bef3 100644 --- a/codon/parser/visitors/typecheck/error.cpp +++ b/codon/parser/visitors/typecheck/error.cpp @@ -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++; diff --git a/jit/codon/__init__.py b/jit/codon/__init__.py index 568e4bf9..e6ef34f9 100644 --- a/jit/codon/__init__.py +++ b/jit/codon/__init__.py @@ -3,3 +3,5 @@ __all__ = ["jit", "convert", "JITError"] from .decorator import jit, convert, execute, JITError + +__codon__ = False diff --git a/stdlib/internal/core.codon b/stdlib/internal/core.codon index 443bbc2e..77bd5d6d 100644 --- a/stdlib/internal/core.codon +++ b/stdlib/internal/core.codon @@ -417,3 +417,5 @@ class ProxyFunc: data: Ptr[byte] T: type TR: type + +__codon__: Static[bool] = True diff --git a/test/parser/typecheck/test_access.codon b/test/parser/typecheck/test_access.codon index f6b0f2f9..5650f86f 100644 --- a/test/parser/typecheck/test_access.codon +++ b/test/parser/typecheck/test_access.codon @@ -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