Skip to content

Commit

Permalink
Fix #360; Fix #424
Browse files Browse the repository at this point in the history
  • Loading branch information
inumanag committed Feb 26, 2025
1 parent 6dd6f47 commit 574c741
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions codon/compiler/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions codon/parser/visitors/typecheck/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace codon::ast {

using namespace types;
using namespace matcher;
using namespace error;

/// Transform asserts.
/// @example
Expand Down Expand Up @@ -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++;
Expand Down
2 changes: 2 additions & 0 deletions jit/codon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
__all__ = ["jit", "convert", "JITError"]

from .decorator import jit, convert, execute, JITError

__codon__ = False
2 changes: 2 additions & 0 deletions stdlib/internal/core.codon
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,5 @@ class ProxyFunc:
data: Ptr[byte]
T: type
TR: type

__codon__: Static[bool] = True
8 changes: 8 additions & 0 deletions test/parser/typecheck/test_access.codon
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 574c741

Please sign in to comment.