Skip to content

Commit

Permalink
Merge branch 'main' into rivet_fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS authored Dec 10, 2023
2 parents c3d57dd + 45dafc5 commit f57efc7
Showing 1 changed file with 43 additions and 63 deletions.
106 changes: 43 additions & 63 deletions lib/rivet/src/checker/exprs.ri
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extend Checker {
.Never(branch.pos)
},
.Paren(paren) -> self.check_expr(paren.expr),
.NoneLiteral -> .None,
.NoneLiteral -> .None(),
.BoolLiteral -> self.table.bool_t,
.CharLiteral(char_lit) -> {
char_lit.type = if char_lit.is_byte {
Expand Down Expand Up @@ -49,30 +49,25 @@ extend Checker {
self_lit.type = self_lit.obj.type;
self_lit.type
},
.SelfTy -> .Void,
.SelfTy -> .Void(),
.EnumLiteral(enum_lit) -> {
enum_lit.type = .Void;
enum_lit.type = .Void();
if sym := self.expected_type.symbol(); sym.info is .Enum(enum_info) {
if variant := enum_info.get_variant(enum_lit.value) {
enum_lit.sym = sym;
enum_lit.variant = variant;
enum_lit.type = .Basic(sym, pos: enum_lit.pos);
if enum_info.is_tagged and !enum_lit.from_is_cmp
if enum_info.is_boxed and !enum_lit.from_is_cmp
and !enum_lit.is_instance {
if variant.has_type {
mut err := report.error_builder(
"variant `{}` cannot be initialized without arguments".fmt(
enum_lit.value
),
enum_lit.pos
);
err.add_help(
"if you intend not to pass any value, add `()`: `{}()`",
mut err := report.error_builder(
"cannot use variant `.{}` as a simple value".fmt(
enum_lit.value
);
err.emit();
}
enum_lit.is_instance = true;
), enum_lit.pos
);
err.add_note(
"make an instance instead: `.{}()`", enum_lit.value
);
err.emit();
}
} else {
report.error(
Expand All @@ -89,7 +84,7 @@ extend Checker {
.ArrayLiteral(mut array_lit) -> self.check_array_literal(array_lit),
.Ident(ident) -> {
ident.type = if ident.name == "_" {
.Void
.Void()
} else if ident.is_comptime {
ident.builtin.type()
} else if ident.is_sym or ident.is_obj {
Expand All @@ -106,10 +101,10 @@ extend Checker {
}
func_sym.type(self.table.universe)
},
else -> .Void
else -> .Void()
}
} else {
.Void
.Void()
};
ident.type
},
Expand All @@ -133,7 +128,7 @@ extend Checker {
err.add_help("use index access to element 0 instead");
}
err.emit();
.Void
.Void()
};
indirect.type
},
Expand All @@ -143,7 +138,7 @@ extend Checker {
opt.inner
} else {
report.error("cannot check a non-option value", option_check.left.position());
.Void
.Void()
};
option_check.type
},
Expand Down Expand Up @@ -176,13 +171,13 @@ extend Checker {
}
}
old_expected_type := self.expected_type;
self.expected_type = .Void;
self.expected_type = .Void();
self.check_stmts(block.stmts);
self.expected_type = old_expected_type;
block.type = if block.is_expr {
self.check_expr(block.expr)
} else {
.Void
.Void()
};
if block.type is .Void and block.is_expr {
// if the expression has no value then it is another statement
Expand Down Expand Up @@ -216,7 +211,7 @@ extend Checker {
);
}
self.inside_guard_expr = old_inside_guard_expr;
.Void
.Void()
},
.Return(mut return_expr) -> self.check_return(return_expr),
.Throw(mut throw_expr) -> self.check_throw(throw_expr),
Expand Down Expand Up @@ -259,7 +254,7 @@ extend Checker {
mut size: uint := 0;
mut is_mut := false;
mut has_expected_type := false;
mut value_type := ast.Type.Void;
mut value_type := ast.Type.Void();
if value_sym := self.expected_type.symbol(); value_sym.info is .Array
or value_sym.info is .DynArray {
has_expected_type = true;
Expand All @@ -268,7 +263,7 @@ extend Checker {
if value_sym.info is .Array(array_info) {
size = array_info.size;
}
is_mut = value_sym.info.is_mut_arr_or_dyn_array();
is_mut = value_sym.info.is_mut_arr_or_vec();
} else if !self.expected_type.is_void() {
has_expected_type = true;
value_type = self.expected_type;
Expand All @@ -284,19 +279,17 @@ extend Checker {
} else {
self.check_types(type, value_type) catch |err| {
mut err_b := report.error_builder(err.to_string(), value.position());
err_b.add_note(if array_lit.is_dyn {
"in element {} of dynamic array literal"
} else {
err_b.add_note(if array_lit.is_arr {
"in element {} of array literal"
} else {
"in element {} of dynamic array literal"
}.fmt(i + 1));
err_b.emit();
};
}
}
self.expected_type = old_expected_type;
array_lit.type = if array_lit.is_dyn {
.Basic(self.table.universe.add_or_get_dyn_array(value_type, is_mut), pos: array_lit.pos)
} else {
array_lit.type = if array_lit.is_arr {
arr_len := if array_lit.values.len > 0 {
array_lit.values.len
} else {
Expand All @@ -309,28 +302,15 @@ extend Checker {
self.table.universe.add_or_get_array(value_type, arr_len, is_mut),
pos: array_lit.pos
)
} else {
.Basic(self.table.universe.add_or_get_dyn_array(value_type, is_mut), pos: array_lit.pos)
};
return array_lit.type;
}

func check_selector(mut self, mut selector: ast.Expr.Selector) -> ast.Type {
selector.type = if selector.is_path {
if selector.left_sym is ast.TypeSym(type_sym) {
if type_sym.info is .Enum(enum_info) {
if variant := enum_info.get_variant(selector.field_name); variant.has_type {
mut err := report.error_builder(
"variant `{}` cannot be initialized without arguments".fmt(
selector.field_name
),
selector.pos
);
err.add_help(
"if you intend not to pass any value, add `()`: `{}()`",
selector.field_name
);
err.emit();
}
}
.Basic(type_sym, pos: selector.pos)
} else {
match selector.sym is {
Expand All @@ -353,7 +333,7 @@ extend Checker {
);
err.add_note("please report this bug, thanks =D");
err.emit();
.Void
.Void()
}
}
}
Expand All @@ -373,7 +353,7 @@ extend Checker {
);
err.add_help("use parentheses to call the method");
err.emit();
.Void
.Void()
} else {
mut err := report.error_builder(
"cannot take value of associated function `{}`".fmt(
Expand All @@ -392,7 +372,7 @@ extend Checker {
sym.type_of(), left_sym.name, selector.field_name
), selector.pos
);
.Void
.Void()
}
} else {
mut err := report.error_builder(
Expand All @@ -408,15 +388,15 @@ extend Checker {
);
}
err.emit();
.Void
.Void()
}
} else {
report.error(
"type `{}` has no field `{}`".fmt(
selector.left_type, selector.field_name
), selector.field_pos
);
.Void
.Void()
}
};
return selector.type;
Expand All @@ -436,7 +416,7 @@ extend Checker {
.Array, .DynArray -> {
elem_type := left_sym.info.elem_type()?;
if index.index is .Range {
is_mut := left_sym.info.is_mut_arr_or_dyn_array();
is_mut := left_sym.info.is_mut_arr_or_vec();
if left_sym.info is .DynArray {
index.left_type
} else {
Expand Down Expand Up @@ -577,7 +557,7 @@ extend Checker {
if left_sym := left_type.symbol() {
match binary.op {
.Plus, .Minus, .Mul, .Div, .Mod, .Xor, .Amp, .Pipe -> {
mut promoted_type := ast.Type.Void;
mut promoted_type := ast.Type.Void();
if left_sym.info.is_compound() {
if op_method := left_sym.find_method(binary.op.to_string()) {
promoted_type = op_method.ret_type;
Expand Down Expand Up @@ -636,7 +616,7 @@ extend Checker {
op_method := if binary.op == .KwIn { "==" } else { "!=" };
if !(left_sym.info.is_primitive()
or !(left_sym.info is .Enum(enum_info)
and enum_info.is_tagged))
and enum_info.is_boxed))
and !left_sym.has_method(op_method) {
mut err := report.error_builder(
"cannot use operator `{}` with type `{}`".fmt(
Expand Down Expand Up @@ -675,7 +655,7 @@ extend Checker {
self.check_name_case(
.Snake, "variable", binary.var_obj.name, binary.var_obj.pos
);
if left_sym.info is .Enum(enum_info) and enum_info.is_tagged {
if left_sym.info is .Enum(enum_info) and enum_info.is_boxed {
variant := @as(ast.Expr.EnumLiteral, binary.right).variant;
if variant.has_type {
binary.var_obj.type = variant.type;
Expand All @@ -696,11 +676,11 @@ extend Checker {
}
}
if left_sym.info is .Enum(enum_info) {
if enum_info.is_tagged and binary.op !in [.KwIs, .KwNotIs] {
if enum_info.is_boxed and binary.op !in [.KwIs, .KwNotIs] {
report.error(
"tagged enum types only support `is` and `!is`", binary.pos
"boxed enum types only support `is` and `!is`", binary.pos
);
} else if !enum_info.is_tagged and binary.op !in [.Eq, .Ne] {
} else if !enum_info.is_boxed and binary.op !in [.Eq, .Ne] {
report.error(
"enum types only support `==` and `!=`", binary.pos
);
Expand Down Expand Up @@ -783,7 +763,7 @@ extend Checker {
);
}
}
mut branch_t := ast.Type.Void;
mut branch_t := ast.Type.Void();
if i == 0 {
branch_t = self.check_expr(branch.expr);
if if_expr.expected_type is .Void {
Expand Down Expand Up @@ -903,11 +883,11 @@ extend Checker {
right_type := self.check_expr(assign.right);
self.expected_type = old_expected_type;
if assign.left is .Ident(ident) and ident.name == "_" {
return .Void;
return .Void();
}
self.check_types(right_type, left_type) catch |err| {
report.error(err.to_string(), assign.right.position());
};
return .Void;
return .Void();
}
}

0 comments on commit f57efc7

Please sign in to comment.