diff --git a/lib/rivet/src/resolver/Register.ri b/lib/rivet/src/resolver/Register.ri index 035da32f9..0ebe5fd47 100644 --- a/lib/rivet/src/resolver/Register.ri +++ b/lib/rivet/src/resolver/Register.ri @@ -35,7 +35,6 @@ pub struct Register { old_sym := self.sym; self.walk_decl(decl); self.sym.scope = ^mut ast.Scope(owner: self.sym); - //self.sym.scope.owner = self.sym; self.abi = old_abi; self.sym = old_sym; } diff --git a/rivetc/src/codegen/__init__.py b/rivetc/src/codegen/__init__.py index 3f67c2b6d..cddc4486c 100644 --- a/rivetc/src/codegen/__init__.py +++ b/rivetc/src/codegen/__init__.py @@ -571,10 +571,7 @@ def gen_expr_with_cast(self, expected_typ_, expr, custom_tmp = None): res_expr = self.gen_expr(expr, custom_tmp) assert res_expr != None - expr_typ = expr.expected_typ if hasattr( - expr, "expected_typ" - ) else expr.typ - if expected_typ == self.ir_type(expr_typ): + if expected_typ == res_expr.typ: return res_expr if isinstance(res_expr, ir.IntLit) and self.comp.is_int(expected_typ_): @@ -621,6 +618,10 @@ def gen_expr_with_cast(self, expected_typ_, expr, custom_tmp = None): expr_sym = expr.typ.symbol() expected_sym = expected_typ_.symbol() + expr_typ = expr.expected_typ if hasattr( + expr, "expected_typ" + ) else expr.typ + if expected_sym.kind == TypeKind.Trait and expr_typ != expected_typ_ and expr_sym != expected_sym and expr.typ != self.comp.none_t: res_expr = self.trait_value(res_expr, expr_typ, expected_typ_) elif expr_sym.kind == TypeKind.Enum and expr_sym.info.is_tagged and expr_sym.info.has_variant( @@ -761,14 +762,14 @@ def gen_expr(self, expr, custom_tmp = None): elif isinstance(expr, ast.BuiltinCallExpr): if expr.name == "as": arg1 = expr.args[1] - arg1_is_voidptr = isinstance( + arg1_is_rawptr = isinstance( arg1.typ, type.Ptr ) and arg1.typ.typ == self.comp.void_t if ( isinstance(expr.typ, type.Ptr) and expr.typ.value_is_boxed() - and (arg1.typ == expr.typ.typ or arg1_is_voidptr) + and (arg1.typ == expr.typ.typ or arg1_is_rawptr) ): - if not arg1_is_voidptr: + if not arg1_is_rawptr: return self.gen_expr(arg1) ir_typ = self.ir_type(expr.typ.typ) return ir.Inst( @@ -1708,7 +1709,7 @@ def gen_expr(self, expr, custom_tmp = None): elif isinstance(expr, ast.UnaryExpr): if expr.op == Kind.Xor: right = self.gen_expr(expr.right) - if isinstance(right.typ, ir.Ptr): + if isinstance(right.typ, ir.Ptr) and right.typ.is_managed: return right size, _ = self.comp.type_size(expr.right_typ, True) res = self.boxed_instance(self.ir_type(expr.right_typ), size) @@ -2343,13 +2344,13 @@ def gen_left_assign(self, expr, right, assign_op): self.inside_lhs_assign = old_inside_lhs_assign return None, require_store_ptr else: - left = self.gen_expr_with_cast(expr.typ, expr) + left = self.gen_expr(expr) elif isinstance(expr, ast.SelectorExpr): if expr.is_indirect or expr.is_boxed_indirect: left_ir = self.gen_expr(expr.left) left = ir.Inst(ir.InstKind.LoadPtr, [left_ir], left_ir.typ.typ) else: - left = self.gen_expr_with_cast(expr.typ, expr) + left = self.gen_expr(expr) elif isinstance(expr, ast.IndexExpr): left_ir_typ = self.ir_type(expr.left_typ) left_sym = expr.left_typ.symbol() diff --git a/rivetc/src/codegen/ir.py b/rivetc/src/codegen/ir.py index 149e9d843..57d5d6752 100644 --- a/rivetc/src/codegen/ir.py +++ b/rivetc/src/codegen/ir.py @@ -29,7 +29,7 @@ def get_ir_op(op): elif op == Kind.Rshift: op_kind = InstKind.Rshift else: - return None + op_kind = None return op_kind class Type: @@ -46,7 +46,7 @@ def __str__(self): return self.name def __eq__(self, other): - return str(self) == str(other) + return isinstance(other, Type) and self.name == other.name class Ptr: def __init__(self, typ, is_managed = False): @@ -73,25 +73,7 @@ def __str__(self): return f"*{self.typ}" def __eq__(self, other): - return str(self) == str(other) - -VOID_T = Type("void") -RAWPTR_T = VOID_T.ptr() -BOOL_T = Type("bool") -RUNE_T = Type("rune") -C_INT_T = Type("int") -CHAR_T = Type("char") -UINT8_T = Type("uint8") -UINT64_T = Type("uint64") -Float64_T = Type("float64") -INT_T = Type("ri_int") -UINT_T = Type("ri_uint") -SLICE_T = Type("_R4core5Slice") -DYN_ARRAY_T = Type("_R4core8DynArray") -STRING_T = Type("_R4core6string") -TEST_T = Type("_R4core4Test") -TEST_RUNNER_T = Type("_R4core10TestRunner") -THROWABLE_T = Type("_R4core9Throwable").ptr(True) + return isinstance(other, Ptr) and self.typ == other.typ class Array: def __init__(self, typ, size): @@ -108,7 +90,7 @@ def __str__(self): return f"{[self.size]}{self.typ}" def __eq__(self, other): - return str(self) == str(other) + return isinstance(other, Array) and self.typ == other.typ and self.size == other.size class Function: def __init__(self, args, ret_typ): @@ -127,6 +109,24 @@ def __str__(self): def __eq__(self, other): return str(self) == str(other) +VOID_T = Type("void") +RAWPTR_T = VOID_T.ptr() +BOOL_T = Type("bool") +RUNE_T = Type("rune") +C_INT_T = Type("int") +CHAR_T = Type("char") +UINT8_T = Type("uint8") +UINT64_T = Type("uint64") +Float64_T = Type("float64") +INT_T = Type("ri_int") +UINT_T = Type("ri_uint") +SLICE_T = Type("_R4core5Slice") +DYN_ARRAY_T = Type("_R4core8DynArray") +STRING_T = Type("_R4core6string") +TEST_T = Type("_R4core4Test") +TEST_RUNNER_T = Type("_R4core10TestRunner") +THROWABLE_T = Type("_R4core9Throwable").ptr(True) + class RIRFile: def __init__(self, mod_name): self.mod_name = mod_name diff --git a/tests/valid/src/traits.ri b/tests/valid/src/traits.ri index ac1c63035..a43fb720a 100644 --- a/tests/valid/src/traits.ri +++ b/tests/valid/src/traits.ri @@ -70,3 +70,17 @@ func ship(a: ^Eq, b: ^Eq) -> bool { test "traits: default method" { @assert(ship(^Poketrait("Shell"), ^Poketrait("Shell"))); } + +trait Jexs { + name: string; +} + +struct Impltor < Jexs { } + +func getter(name: string) -> string { + return name; +} + +test "traits: fields" { + @assert(getter(Jexs(Impltor(name: "jexs99")).name) == "jexs99"); +}