Skip to content

Commit

Permalink
fix boxed value assign
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Jan 21, 2024
1 parent 940a1d4 commit 033fff1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
1 change: 0 additions & 1 deletion lib/rivet/src/resolver/Register.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 11 additions & 10 deletions rivetc/src/codegen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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_):
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
44 changes: 22 additions & 22 deletions rivetc/src/codegen/ir.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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):
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/valid/src/traits.ri
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 033fff1

Please sign in to comment.