disambiguate enum blocks and complete distinct type operations
This commit is contained in:
+388
-128
@@ -942,13 +942,21 @@ type_builtin_value :: proc(checker: ^Checker, kind: Type_Builtin, value: types.T
|
||||
case .Align_Of:
|
||||
return i128(types.alignment_of(value, &checker.module.types, checker.target))
|
||||
case .Min_Value:
|
||||
if types.is_unsigned(value, checker.target) {
|
||||
representation := value
|
||||
if backing, ok := types.distinct_scalar_backing(value, &checker.module.types); ok {
|
||||
representation = backing
|
||||
}
|
||||
if types.is_unsigned(representation, checker.target) {
|
||||
return 0
|
||||
}
|
||||
return -(i128(1) << u32(types.bits(value, checker.target)-1))
|
||||
return -(i128(1) << u32(types.bits(representation, checker.target)-1))
|
||||
case .Max_Value:
|
||||
bit_count := types.bits(value, checker.target)
|
||||
sign_bit_count := 1 if types.is_signed(value, checker.target) else 0
|
||||
representation := value
|
||||
if backing, ok := types.distinct_scalar_backing(value, &checker.module.types); ok {
|
||||
representation = backing
|
||||
}
|
||||
bit_count := types.bits(representation, checker.target)
|
||||
sign_bit_count := 1 if types.is_signed(representation, checker.target) else 0
|
||||
return (i128(1) << u32(bit_count-sign_bit_count))-1
|
||||
case:
|
||||
return 0
|
||||
@@ -976,16 +984,22 @@ build_type_builtin :: proc(
|
||||
id := source.addf(checker.diagnostics, checker.ast_module.exprs[expr.args[0]].span, "layout target must be a sized runtime value type, got %s", type_label(checker, target))
|
||||
return invalid_hir_expr(checker, expr.span, id, types.USIZE)
|
||||
}
|
||||
if (kind == .Min_Value || kind == .Max_Value) && !types.is_concrete_integer(target) {
|
||||
bound_representation := target
|
||||
if backing, ok := types.distinct_scalar_backing(target, &checker.module.types); ok {
|
||||
bound_representation = backing
|
||||
}
|
||||
if (kind == .Min_Value || kind == .Max_Value) && !types.is_concrete_integer(bound_representation) {
|
||||
id := source.addf(checker.diagnostics, checker.ast_module.exprs[expr.args[0]].span, "integer bound target must be a concrete integer type, got %s", type_label(checker, target))
|
||||
return invalid_hir_expr(checker, expr.span, id, types.USIZE)
|
||||
}
|
||||
result_type := types.USIZE if kind == .Size_Of || kind == .Align_Of else target
|
||||
_, distinct_ok := types.distinct_scalar_backing(result_type, &checker.module.types)
|
||||
return build_constant_expr(
|
||||
checker,
|
||||
expr,
|
||||
Constant{kind=.Value, value=type_builtin_value(checker, kind, target)},
|
||||
result_type,
|
||||
distinct_ok,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -4363,7 +4377,9 @@ validate_meta_schema :: proc(checker: ^Checker) {
|
||||
(tag == "array" && !types.equal(field.type, array_info)) ||
|
||||
(tag == "record" && !types.equal(field.type, record_info)) ||
|
||||
(tag == "enum" && !types.equal(field.type, enum_info)) ||
|
||||
(tag != "array" && tag != "record" && tag != "enum" && !types.is_void(field.type)) {
|
||||
(tag == "distinct" && !is_type_metatype_syntax(checker, field.type)) ||
|
||||
(tag != "array" && tag != "record" && tag != "enum" && tag != "distinct" &&
|
||||
!types.is_void(field.type)) {
|
||||
valid = false
|
||||
break
|
||||
}
|
||||
@@ -4704,6 +4720,43 @@ mark_spec_demanded :: proc(checker: ^Checker, id: Spec_Id, stack: ^[dynamic]Spec
|
||||
append(stack, id)
|
||||
}
|
||||
|
||||
Numeric_Operation_Type :: struct {
|
||||
result: types.Type,
|
||||
representation: types.Type,
|
||||
}
|
||||
|
||||
numeric_operation_type :: proc(checker: ^Checker, left, right: types.Type) -> (Numeric_Operation_Type, bool) {
|
||||
if types.equal(left, right) {
|
||||
if representation, ok := types.distinct_scalar_backing(left, &checker.module.types); ok {
|
||||
return Numeric_Operation_Type{result=left, representation=representation}, true
|
||||
}
|
||||
}
|
||||
result := types.widest(left, right)
|
||||
if !types.is_concrete_scalar(result) {
|
||||
return {}, false
|
||||
}
|
||||
return Numeric_Operation_Type{result=result, representation=result}, true
|
||||
}
|
||||
|
||||
numeric_literal_accepts_type :: proc(
|
||||
checker: ^Checker,
|
||||
expr: ast.Expr_Id,
|
||||
demand: types.Type,
|
||||
pkg: ast.Package_Id,
|
||||
file: ast.File_Id,
|
||||
) -> bool {
|
||||
representation := demand
|
||||
if backing, ok := types.distinct_scalar_backing(demand, &checker.module.types); ok {
|
||||
representation = backing
|
||||
}
|
||||
if constant := eval_integer_constant_in_context(checker, expr, pkg, file); constant.kind == .Value {
|
||||
return types.is_float(representation, checker.target) ||
|
||||
types.is_concrete_integer(representation) &&
|
||||
fits_integer_type(constant.value, representation, checker.target)
|
||||
}
|
||||
return is_float_constant_expr(checker, expr) && types.is_float(representation, checker.target)
|
||||
}
|
||||
|
||||
Infer_Frame :: struct {
|
||||
expr: ast.Expr_Id,
|
||||
expected: types.Type,
|
||||
@@ -4715,6 +4768,8 @@ Infer_Frame :: struct {
|
||||
mapping: []int,
|
||||
args: []types.Type,
|
||||
template: ast.Function_Id,
|
||||
numeric_operation: bool,
|
||||
reverse_operands: bool,
|
||||
}
|
||||
|
||||
merge_inferred_test_error :: proc(checker: ^Checker, incoming: types.Type) {
|
||||
@@ -4772,8 +4827,16 @@ infer_division_builtin :: proc(
|
||||
right_hint := hint if types.is_valid(hint) else left
|
||||
right = infer_nested_expr(checker, expr.args[1], locals, pkg, file, demanded, local_types, right_hint)
|
||||
}
|
||||
result := types.widest(left, right)
|
||||
return result if types.is_concrete_scalar(result) && !types.is_bool(result) else types.INVALID
|
||||
if left_const && types.is_distinct(right, &checker.module.types) &&
|
||||
numeric_literal_accepts_type(checker, expr.args[0], right, pkg, file) {
|
||||
left = right
|
||||
}
|
||||
if right_const && types.is_distinct(left, &checker.module.types) &&
|
||||
numeric_literal_accepts_type(checker, expr.args[1], left, pkg, file) {
|
||||
right = left
|
||||
}
|
||||
operation, ok := numeric_operation_type(checker, left, right)
|
||||
return operation.result if ok && !types.is_bool(operation.representation) else types.INVALID
|
||||
}
|
||||
|
||||
infer_compound_expr :: proc(
|
||||
@@ -4796,28 +4859,38 @@ infer_compound_expr :: proc(
|
||||
_ = infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types)
|
||||
return types.BOOL
|
||||
case .Bit_Not:
|
||||
hint := expected if types.is_concrete_integer(expected) else types.INVALID
|
||||
operand := infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types, hint)
|
||||
return operand if types.is_concrete_integer(operand) else types.INVALID
|
||||
operand := infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types)
|
||||
representation := types.runtime_representation(operand, store)
|
||||
return operand if types.is_concrete_integer(representation) else types.INVALID
|
||||
case .Bit_And, .Bit_Or, .Bit_Xor:
|
||||
hint := expected if types.is_concrete_integer(expected) else types.INVALID
|
||||
left_const := is_numeric_constant_expr(checker, expr.left)
|
||||
right_const := is_numeric_constant_expr(checker, expr.right)
|
||||
left, right := types.INVALID, types.INVALID
|
||||
if left_const && !right_const && !types.is_valid(hint) {
|
||||
if types.is_concrete_integer(expected) {
|
||||
left = infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types, expected)
|
||||
right = infer_nested_expr(checker, expr.right, locals, pkg, file, demanded, local_types, expected)
|
||||
} else if left_const && !right_const {
|
||||
right = infer_nested_expr(checker, expr.right, locals, pkg, file, demanded, local_types)
|
||||
left = infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types, right)
|
||||
left = infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types)
|
||||
} else {
|
||||
left = infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types, hint)
|
||||
right = infer_nested_expr(checker, expr.right, locals, pkg, file, demanded, local_types, hint if types.is_valid(hint) else left)
|
||||
left = infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types)
|
||||
right = infer_nested_expr(checker, expr.right, locals, pkg, file, demanded, local_types)
|
||||
}
|
||||
result := types.widest(left, right)
|
||||
return result if types.is_concrete_integer(result) else types.INVALID
|
||||
if left_const && types.is_distinct(right, store) &&
|
||||
numeric_literal_accepts_type(checker, expr.left, right, pkg, file) {
|
||||
left = right
|
||||
}
|
||||
if right_const && types.is_distinct(left, store) &&
|
||||
numeric_literal_accepts_type(checker, expr.right, left, pkg, file) {
|
||||
right = left
|
||||
}
|
||||
operation, ok := numeric_operation_type(checker, left, right)
|
||||
return operation.result if ok && types.is_concrete_integer(operation.representation) else types.INVALID
|
||||
case .Shift_Left, .Shift_Right, .Shift_Left_Saturating:
|
||||
hint := expected if types.is_concrete_integer(expected) else types.INVALID
|
||||
left := infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types, hint)
|
||||
left := infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types)
|
||||
right := infer_nested_expr(checker, expr.right, locals, pkg, file, demanded, local_types, types.U64)
|
||||
return left if types.is_concrete_integer(left) && types.is_unsigned(right, checker.target) else types.INVALID
|
||||
representation := types.runtime_representation(left, store)
|
||||
return left if types.is_concrete_integer(representation) && types.is_unsigned(right, checker.target) else types.INVALID
|
||||
case .Eq, .Ne, .Lt, .Le, .Gt, .Ge, .And, .Or:
|
||||
left_expr := checker.ast_module.exprs[expr.left]
|
||||
right_expr := checker.ast_module.exprs[expr.right]
|
||||
@@ -5135,6 +5208,16 @@ infer_expr :: proc(
|
||||
continue
|
||||
}
|
||||
if constant.kind == .Value {
|
||||
if frame.numeric_operation {
|
||||
if representation, ok := types.distinct_scalar_backing(frame.expected, &checker.module.types);
|
||||
ok && (types.is_float(representation, checker.target) ||
|
||||
types.is_concrete_integer(representation) &&
|
||||
fits_integer_type(constant.value, representation, checker.target)) {
|
||||
last = frame.expected
|
||||
_ = pop(&stack)
|
||||
continue
|
||||
}
|
||||
}
|
||||
last = constraint_integer_literal_type(frame.expected, constant.value)
|
||||
if !types.is_valid(last) {
|
||||
last = types.I64
|
||||
@@ -5162,7 +5245,15 @@ infer_expr :: proc(
|
||||
}
|
||||
_ = pop(&stack)
|
||||
case .Float:
|
||||
last = types.F64
|
||||
if frame.numeric_operation {
|
||||
if representation, ok := types.distinct_scalar_backing(frame.expected, &checker.module.types);
|
||||
ok && types.is_float(representation, checker.target) {
|
||||
last = frame.expected
|
||||
_ = pop(&stack)
|
||||
continue
|
||||
}
|
||||
}
|
||||
last = frame.expected if types.is_float(frame.expected, checker.target) else types.F64
|
||||
_ = pop(&stack)
|
||||
case .String, .Array, .Null, .Unreachable, .Undefined, .Address, .Deref, .Index, .Slice,
|
||||
.Field, .Unwrap, .Orelse, .Try, .Catch, .Struct_Literal, .Keyed, .Enum_Literal, .Cast,
|
||||
@@ -5294,7 +5385,17 @@ infer_expr :: proc(
|
||||
append(&stack, Infer_Frame{expr=expr.left, template=ast.INVALID_FUNCTION})
|
||||
case .Add, .Sub, .Mul, .Div:
|
||||
stack[frame_index].stage = 1
|
||||
append(&stack, Infer_Frame{expr=expr.left, template=ast.INVALID_FUNCTION})
|
||||
left_const := is_numeric_constant_expr(checker, expr.left)
|
||||
right_const := is_numeric_constant_expr(checker, expr.right)
|
||||
stack[frame_index].reverse_operands = left_const && !right_const
|
||||
first := expr.right if stack[frame_index].reverse_operands else expr.left
|
||||
first_expected := frame.expected if
|
||||
types.is_concrete_scalar(frame.expected) && !types.is_bool(frame.expected) else types.INVALID
|
||||
append(&stack, Infer_Frame{
|
||||
expr=first,
|
||||
expected=first_expected,
|
||||
template=ast.INVALID_FUNCTION,
|
||||
})
|
||||
case .Call:
|
||||
if expr.left != ast.INVALID_EXPR {
|
||||
callee_type := infer_nested_expr(checker, expr.left, locals, pkg, file, demanded, local_types)
|
||||
@@ -5567,38 +5668,54 @@ infer_expr :: proc(
|
||||
if frame.stage == 1 {
|
||||
stack[frame_index].left = last
|
||||
stack[frame_index].stage = 2
|
||||
append(&stack, Infer_Frame{expr=expr.right, template=ast.INVALID_FUNCTION})
|
||||
second := expr.left if frame.reverse_operands else expr.right
|
||||
second_expected := types.INVALID
|
||||
numeric_operation := false
|
||||
if is_numeric_constant_expr(checker, second) {
|
||||
second_expected = last
|
||||
if representation, ok := types.distinct_scalar_backing(last, &checker.module.types);
|
||||
ok && !types.is_bool(representation) {
|
||||
numeric_operation = true
|
||||
}
|
||||
}
|
||||
append(&stack, Infer_Frame{
|
||||
expr=second,
|
||||
expected=second_expected,
|
||||
template=ast.INVALID_FUNCTION,
|
||||
numeric_operation=numeric_operation,
|
||||
})
|
||||
continue
|
||||
}
|
||||
if frame.stage == 2 {
|
||||
right := last
|
||||
if expr.kind == .Add && types.is_many_pointer(frame.left, &checker.module.types) && types.is_concrete_integer(right) {
|
||||
last = frame.left
|
||||
first, second := frame.left, last
|
||||
left, right := first, second
|
||||
if frame.reverse_operands {
|
||||
left, right = second, first
|
||||
}
|
||||
if expr.kind == .Add && types.is_many_pointer(left, &checker.module.types) && types.is_concrete_integer(right) {
|
||||
last = left
|
||||
} else if operation, ok := numeric_operation_type(checker, left, right); ok {
|
||||
last = operation.result
|
||||
} else if is_numeric_constant_expr(checker, expr.right) &&
|
||||
is_numeric_demand(frame.left, checker.target) &&
|
||||
expr_accepts_numeric_demand(checker, expr.right, frame.left, locals, pkg, file) {
|
||||
last = frame.left
|
||||
is_numeric_demand(left, checker.target) &&
|
||||
expr_accepts_numeric_demand(checker, expr.right, left, locals, pkg, file) {
|
||||
last = left
|
||||
} else if is_numeric_constant_expr(checker, expr.left) &&
|
||||
is_numeric_demand(right, checker.target) &&
|
||||
expr_accepts_numeric_demand(checker, expr.left, right, locals, pkg, file) {
|
||||
last = right
|
||||
} else if is_numeric_demand(frame.left, checker.target) &&
|
||||
} else if is_numeric_demand(left, checker.target) &&
|
||||
!numeric_operand_is_open(checker, expr.left, locals, pkg, file) &&
|
||||
expr_accepts_numeric_demand(checker, expr.right, frame.left, locals, pkg, file) {
|
||||
// Propagate only from an authoritative (fixed-type) left operand. A left
|
||||
// operand that is still a provisional open constant carries only its
|
||||
// smallest-signed default, which must not poison the sibling's family;
|
||||
// two provisional operands are resolved together by the backward demand
|
||||
// from the declaration/use.
|
||||
_ = record_demand(checker, expr.right, frame.left, locals, local_types, pkg, file)
|
||||
last = frame.left
|
||||
expr_accepts_numeric_demand(checker, expr.right, left, locals, pkg, file) {
|
||||
_ = record_demand(checker, expr.right, left, locals, local_types, pkg, file)
|
||||
last = left
|
||||
} else if is_numeric_demand(right, checker.target) &&
|
||||
!numeric_operand_is_open(checker, expr.right, locals, pkg, file) &&
|
||||
expr_accepts_numeric_demand(checker, expr.left, right, locals, pkg, file) {
|
||||
_ = record_demand(checker, expr.left, right, locals, local_types, pkg, file)
|
||||
last = right
|
||||
} else {
|
||||
last = types.widest(frame.left, right)
|
||||
last = types.INVALID
|
||||
}
|
||||
_ = pop(&stack)
|
||||
continue
|
||||
@@ -7214,23 +7331,33 @@ build_constant_expr :: proc(
|
||||
expr: ast.Expr,
|
||||
constant: Constant,
|
||||
expected: types.Type,
|
||||
numeric_operation := false,
|
||||
) -> hir.Expr_Id {
|
||||
materialized := expected
|
||||
nominal := types.INVALID
|
||||
if numeric_operation {
|
||||
if representation, ok := types.distinct_scalar_backing(expected, &checker.module.types);
|
||||
ok && !types.is_bool(representation) {
|
||||
materialized = representation
|
||||
nominal = expected
|
||||
}
|
||||
}
|
||||
recovery_type := types.I64
|
||||
if types.is_concrete_integer(expected) {
|
||||
if types.is_concrete_integer(materialized) {
|
||||
recovery_type = expected
|
||||
}
|
||||
// An integer constant in a float context (e.g. `pi float = 3`) folds to a
|
||||
// float literal, mirroring build_float_expr's bit packing.
|
||||
if constant.kind == .Value && types.is_float(expected, checker.target) {
|
||||
if constant.kind == .Value && types.is_float(materialized, checker.target) {
|
||||
fval := f64(constant.value) // ponytail: silent precision loss past 2^53, like C int->double
|
||||
bits := transmute(i64)fval
|
||||
if types.bits(expected, checker.target) == 32 {
|
||||
if types.bits(materialized, checker.target) == 32 {
|
||||
bits = i64(transmute(u32)f32(fval))
|
||||
}
|
||||
return add_hir_expr(checker, hir.Expr{
|
||||
kind = .Float,
|
||||
span = expr.span,
|
||||
type = expected,
|
||||
type = nominal if types.is_valid(nominal) else materialized,
|
||||
integer = bits,
|
||||
target = hir.INVALID_REF,
|
||||
left = hir.INVALID_EXPR,
|
||||
@@ -7251,7 +7378,7 @@ build_constant_expr :: proc(
|
||||
return invalid_hir_expr(checker, expr.span, id, recovery_type)
|
||||
}
|
||||
if constant.kind == .Overflow ||
|
||||
(!types.is_concrete_integer(expected) && expected != types.UINT && !fits_i64(constant.value)) {
|
||||
(!types.is_concrete_integer(materialized) && materialized != types.UINT && !fits_i64(constant.value)) {
|
||||
id := source.add(
|
||||
checker.diagnostics,
|
||||
expr.span,
|
||||
@@ -7264,8 +7391,8 @@ build_constant_expr :: proc(
|
||||
if constant.value >= 0 && constant.value <= i128(0xffff_ffff_ffff_ffff) {
|
||||
value = transmute(i64)u64(constant.value)
|
||||
}
|
||||
result_type := constraint_integer_literal_type(expected, constant.value)
|
||||
if expected == types.UINT && !types.is_valid(result_type) {
|
||||
result_type := constraint_integer_literal_type(materialized, constant.value)
|
||||
if materialized == types.UINT && !types.is_valid(result_type) {
|
||||
id := source.addf(
|
||||
checker.diagnostics,
|
||||
expr.span,
|
||||
@@ -7274,8 +7401,8 @@ build_constant_expr :: proc(
|
||||
)
|
||||
return invalid_hir_expr(checker, expr.span, id, types.U64)
|
||||
}
|
||||
if types.is_concrete_integer(expected) {
|
||||
if !fits_integer_type(constant.value, expected, checker.target) {
|
||||
if types.is_concrete_integer(materialized) {
|
||||
if !fits_integer_type(constant.value, materialized, checker.target) {
|
||||
id := source.addf(
|
||||
checker.diagnostics,
|
||||
expr.span,
|
||||
@@ -7285,7 +7412,7 @@ build_constant_expr :: proc(
|
||||
)
|
||||
return invalid_hir_expr(checker, expr.span, id, expected)
|
||||
}
|
||||
result_type = expected
|
||||
result_type = nominal if types.is_valid(nominal) else materialized
|
||||
}
|
||||
return add_hir_expr(
|
||||
checker,
|
||||
@@ -7302,11 +7429,26 @@ build_constant_expr :: proc(
|
||||
)
|
||||
}
|
||||
|
||||
build_float_expr :: proc(checker: ^Checker, expr: ast.Expr, expected: types.Type) -> hir.Expr_Id {
|
||||
build_float_expr :: proc(
|
||||
checker: ^Checker,
|
||||
expr: ast.Expr,
|
||||
expected: types.Type,
|
||||
numeric_operation := false,
|
||||
) -> hir.Expr_Id {
|
||||
materialized := expected
|
||||
result_type := types.F64
|
||||
if types.is_float(expected, checker.target) {
|
||||
result_type = expected
|
||||
} else if types.is_valid(expected) {
|
||||
if numeric_operation {
|
||||
if representation, ok := types.distinct_scalar_backing(expected, &checker.module.types);
|
||||
ok && types.is_float(representation, checker.target) {
|
||||
materialized = representation
|
||||
result_type = expected
|
||||
}
|
||||
}
|
||||
if types.is_float(materialized, checker.target) {
|
||||
if !types.is_valid(result_type) || !types.is_distinct(result_type, &checker.module.types) {
|
||||
result_type = materialized
|
||||
}
|
||||
} else if types.is_valid(materialized) {
|
||||
id := source.addf(
|
||||
checker.diagnostics,
|
||||
expr.span,
|
||||
@@ -7317,7 +7459,7 @@ build_float_expr :: proc(checker: ^Checker, expr: ast.Expr, expected: types.Type
|
||||
}
|
||||
value := transmute(f64)expr.integer
|
||||
bits := transmute(i64)value
|
||||
if types.bits(result_type, checker.target) == 32 {
|
||||
if types.bits(materialized, checker.target) == 32 {
|
||||
bits = i64(transmute(u32)f32(value))
|
||||
}
|
||||
return add_hir_expr(checker, hir.Expr{
|
||||
@@ -7346,6 +7488,8 @@ Build_Expr_Frame :: struct {
|
||||
arg_types: []types.Type,
|
||||
template: ast.Function_Id,
|
||||
resolution: int,
|
||||
numeric_operation: bool,
|
||||
reverse_operands: bool,
|
||||
}
|
||||
|
||||
hir_location_writable :: proc(checker: ^Checker, expr_id: hir.Expr_Id, locals: []Build_Local) -> bool {
|
||||
@@ -7731,11 +7875,23 @@ build_scalar_cast :: proc(
|
||||
) -> hir.Expr_Id {
|
||||
store := &checker.module.types
|
||||
actual := checker.module.exprs[value].type
|
||||
if types.can_retype_distinct(actual, target, store) {
|
||||
return add_hir_expr(checker, hir.Expr{
|
||||
kind=.Retype,
|
||||
span=span,
|
||||
type=target,
|
||||
left=value,
|
||||
target=hir.INVALID_REF,
|
||||
right=hir.INVALID_EXPR,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
}
|
||||
valid_target := types.is_concrete_scalar(target) && !types.is_bool(target)
|
||||
actual_repr := types.runtime_representation(actual, store)
|
||||
actual_item, actual_item_ok := types.node(store, actual)
|
||||
explicit_enum := actual_item_ok && actual_item.kind == .Enum && actual_item.explicit_backing
|
||||
valid_actual := (types.is_concrete_scalar(actual) || explicit_enum) &&
|
||||
_, distinct_scalar := types.distinct_scalar_backing(actual, store)
|
||||
valid_actual := (types.is_concrete_scalar(actual) || explicit_enum || distinct_scalar) &&
|
||||
types.is_concrete_scalar(actual_repr) && !types.is_bool(actual_repr)
|
||||
if !valid_target || !valid_actual {
|
||||
id := source.addf(
|
||||
@@ -7757,6 +7913,38 @@ build_scalar_cast :: proc(
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
}
|
||||
normalize_index_expr :: proc(checker: ^Checker, value: hir.Expr_Id, span: source.Span) -> hir.Expr_Id {
|
||||
store := &checker.module.types
|
||||
current := value
|
||||
current_type := checker.module.exprs[current].type
|
||||
representation, distinct_ok := types.distinct_scalar_backing(current_type, store)
|
||||
if distinct_ok && types.is_concrete_integer(representation) {
|
||||
for {
|
||||
backing, ok := types.distinct_backing(current_type, store)
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
current = add_hir_expr(checker, hir.Expr{
|
||||
kind=.Retype,
|
||||
span=span,
|
||||
type=backing,
|
||||
left=current,
|
||||
target=hir.INVALID_REF,
|
||||
right=hir.INVALID_EXPR,
|
||||
diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
current_type = backing
|
||||
}
|
||||
}
|
||||
current_expr := &checker.module.exprs[current]
|
||||
if current_expr.kind == .Integer &&
|
||||
fits_integer_type(i128(current_expr.integer), types.USIZE, checker.target) {
|
||||
current_expr.type = types.USIZE
|
||||
return current
|
||||
}
|
||||
return coerce_expr(checker, current, types.USIZE, span)
|
||||
}
|
||||
|
||||
|
||||
build_function_value :: proc(
|
||||
checker: ^Checker,
|
||||
@@ -7836,11 +8024,12 @@ build_nested_expr :: proc(
|
||||
expected: types.Type,
|
||||
pkg: ast.Package_Id,
|
||||
file: ast.File_Id,
|
||||
numeric_operation := false,
|
||||
) -> hir.Expr_Id {
|
||||
outer := checker.build_stack
|
||||
checker.build_stack = nil
|
||||
checker.build_stack.allocator = checker.allocator
|
||||
result := build_expr(checker, expr_id, locals, global_reads, calls, expected, pkg, file)
|
||||
result := build_expr(checker, expr_id, locals, global_reads, calls, expected, pkg, file, numeric_operation)
|
||||
delete(checker.build_stack)
|
||||
checker.build_stack = outer
|
||||
return result
|
||||
@@ -7901,19 +8090,29 @@ build_division_builtin :: proc(
|
||||
left, right := hir.INVALID_EXPR, hir.INVALID_EXPR
|
||||
if left_const && !right_const && !types.is_valid(hint) {
|
||||
right = build_nested_expr(checker, expr.args[1], locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
left = build_nested_expr(checker, expr.args[0], locals, global_reads, calls, checker.module.exprs[right].type, pkg, file)
|
||||
right_type := checker.module.exprs[right].type
|
||||
_, distinct_ok := types.distinct_scalar_backing(right_type, &checker.module.types)
|
||||
left = build_nested_expr(checker, expr.args[0], locals, global_reads, calls, right_type, pkg, file, distinct_ok)
|
||||
} else {
|
||||
left = build_nested_expr(checker, expr.args[0], locals, global_reads, calls, hint, pkg, file)
|
||||
right_hint := hint if types.is_valid(hint) else checker.module.exprs[left].type
|
||||
right = build_nested_expr(checker, expr.args[1], locals, global_reads, calls, right_hint, pkg, file)
|
||||
_, distinct_ok := types.distinct_scalar_backing(right_hint, &checker.module.types)
|
||||
right = build_nested_expr(
|
||||
checker, expr.args[1], locals, global_reads, calls, right_hint, pkg, file,
|
||||
distinct_ok && right_const,
|
||||
)
|
||||
}
|
||||
result := types.widest(checker.module.exprs[left].type, checker.module.exprs[right].type)
|
||||
if !types.is_concrete_scalar(result) || types.is_bool(result) {
|
||||
operation, ok := numeric_operation_type(
|
||||
checker,
|
||||
checker.module.exprs[left].type,
|
||||
checker.module.exprs[right].type,
|
||||
)
|
||||
if !ok || types.is_bool(operation.representation) {
|
||||
id := source.add(checker.diagnostics, expr.span, "division builtins require compatible numeric operands")
|
||||
return invalid_hir_expr(checker, expr.span, id)
|
||||
}
|
||||
left = coerce_expr(checker, left, result, checker.module.exprs[left].span)
|
||||
right = coerce_expr(checker, right, result, checker.module.exprs[right].span)
|
||||
left = coerce_expr(checker, left, operation.result, checker.module.exprs[left].span)
|
||||
right = coerce_expr(checker, right, operation.result, checker.module.exprs[right].span)
|
||||
result_kind := hir.Expr_Kind.Div_Trunc
|
||||
#partial switch kind {
|
||||
case .Floor: result_kind = .Div_Floor
|
||||
@@ -7924,7 +8123,7 @@ build_division_builtin :: proc(
|
||||
case:
|
||||
}
|
||||
return add_hir_expr(checker, hir.Expr{
|
||||
kind=result_kind, span=expr.span, type=result, left=left, right=right,
|
||||
kind=result_kind, span=expr.span, type=operation.result, left=left, right=right,
|
||||
target=hir.INVALID_REF, diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
}
|
||||
@@ -8303,8 +8502,8 @@ build_compound_expr :: proc(
|
||||
})
|
||||
case .Index:
|
||||
container := build_nested_expr(checker, expr.left, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
index := build_nested_expr(checker, expr.right, locals, global_reads, calls, types.USIZE, pkg, file)
|
||||
index = coerce_expr(checker, index, types.USIZE, expr.span)
|
||||
index := build_nested_expr(checker, expr.right, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
index = normalize_index_expr(checker, index, expr.span)
|
||||
if invalid, propagated := propagate_invalid_expr(checker, expr.span, container, index); propagated {
|
||||
return invalid
|
||||
}
|
||||
@@ -8347,8 +8546,8 @@ build_compound_expr :: proc(
|
||||
bounds[1] = hir.INVALID_EXPR
|
||||
for bound, index in expr.args {
|
||||
if bound != ast.INVALID_EXPR {
|
||||
bounds[index] = build_nested_expr(checker, bound, locals, global_reads, calls, types.USIZE, pkg, file)
|
||||
bounds[index] = coerce_expr(checker, bounds[index], types.USIZE, checker.ast_module.exprs[bound].span)
|
||||
bounds[index] = build_nested_expr(checker, bound, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
bounds[index] = normalize_index_expr(checker, bounds[index], checker.ast_module.exprs[bound].span)
|
||||
if invalid, propagated := propagate_invalid_expr(checker, expr.span, bounds[index]); propagated {
|
||||
delete(bounds, checker.allocator)
|
||||
return invalid
|
||||
@@ -8578,13 +8777,13 @@ build_compound_expr :: proc(
|
||||
target=hir.INVALID_REF, right=hir.INVALID_EXPR, diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
case .Bit_Not:
|
||||
hint := expected if types.is_concrete_integer(expected) else types.INVALID
|
||||
operand := build_nested_expr(checker, expr.left, locals, global_reads, calls, hint, pkg, file)
|
||||
operand := build_nested_expr(checker, expr.left, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
if invalid, propagated := propagate_invalid_expr(checker, expr.span, operand); propagated {
|
||||
return invalid
|
||||
}
|
||||
operand_type := checker.module.exprs[operand].type
|
||||
if !types.is_concrete_integer(operand_type) {
|
||||
representation := types.runtime_representation(operand_type, store)
|
||||
if !types.is_concrete_integer(representation) {
|
||||
id := source.add(checker.diagnostics, expr.span, "'~' requires a concrete integer operand")
|
||||
return invalid_hir_expr(checker, expr.span, id, operand_type)
|
||||
}
|
||||
@@ -8593,47 +8792,59 @@ build_compound_expr :: proc(
|
||||
target=hir.INVALID_REF, right=hir.INVALID_EXPR, diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
case .Bit_And, .Bit_Or, .Bit_Xor:
|
||||
hint := expected if types.is_concrete_integer(expected) else types.INVALID
|
||||
left_const := is_numeric_constant_expr(checker, expr.left)
|
||||
right_const := is_numeric_constant_expr(checker, expr.right)
|
||||
left, right: hir.Expr_Id
|
||||
if left_const && !right_const && !types.is_valid(hint) {
|
||||
if types.is_concrete_integer(expected) {
|
||||
left = build_nested_expr(checker, expr.left, locals, global_reads, calls, expected, pkg, file)
|
||||
right = build_nested_expr(checker, expr.right, locals, global_reads, calls, expected, pkg, file)
|
||||
} else if left_const && !right_const {
|
||||
right = build_nested_expr(checker, expr.right, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
left = build_nested_expr(checker, expr.left, locals, global_reads, calls, checker.module.exprs[right].type, pkg, file)
|
||||
right_type := checker.module.exprs[right].type
|
||||
_, distinct_ok := types.distinct_scalar_backing(right_type, store)
|
||||
left = build_nested_expr(checker, expr.left, locals, global_reads, calls, right_type, pkg, file, distinct_ok)
|
||||
} else {
|
||||
left = build_nested_expr(checker, expr.left, locals, global_reads, calls, hint, pkg, file)
|
||||
right_hint := hint if types.is_valid(hint) else checker.module.exprs[left].type
|
||||
right = build_nested_expr(checker, expr.right, locals, global_reads, calls, right_hint, pkg, file)
|
||||
left = build_nested_expr(checker, expr.left, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
right_hint := checker.module.exprs[left].type
|
||||
_, distinct_ok := types.distinct_scalar_backing(right_hint, store)
|
||||
right = build_nested_expr(
|
||||
checker, expr.right, locals, global_reads, calls, right_hint, pkg, file,
|
||||
distinct_ok && right_const,
|
||||
)
|
||||
}
|
||||
if invalid, propagated := propagate_invalid_expr(checker, expr.span, left, right); propagated {
|
||||
return invalid
|
||||
}
|
||||
result_type := types.widest(checker.module.exprs[left].type, checker.module.exprs[right].type)
|
||||
if !types.is_concrete_integer(result_type) {
|
||||
operation, ok := numeric_operation_type(
|
||||
checker,
|
||||
checker.module.exprs[left].type,
|
||||
checker.module.exprs[right].type,
|
||||
)
|
||||
if !ok || !types.is_concrete_integer(operation.representation) {
|
||||
id := source.add(checker.diagnostics, expr.span, "bitwise operation requires compatible concrete integer operands")
|
||||
return invalid_hir_expr(checker, expr.span, id)
|
||||
}
|
||||
left = coerce_expr(checker, left, result_type, checker.module.exprs[left].span)
|
||||
right = coerce_expr(checker, right, result_type, checker.module.exprs[right].span)
|
||||
left = coerce_expr(checker, left, operation.result, checker.module.exprs[left].span)
|
||||
right = coerce_expr(checker, right, operation.result, checker.module.exprs[right].span)
|
||||
kind := hir.Expr_Kind.Bit_And
|
||||
#partial switch expr.kind {
|
||||
case .Bit_Or: kind = .Bit_Or
|
||||
case .Bit_Xor: kind = .Bit_Xor
|
||||
}
|
||||
return add_hir_expr(checker, hir.Expr{
|
||||
kind=kind, span=expr.span, type=result_type, left=left, right=right,
|
||||
kind=kind, span=expr.span, type=operation.result, left=left, right=right,
|
||||
target=hir.INVALID_REF, diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
case .Shift_Left, .Shift_Right, .Shift_Left_Saturating:
|
||||
hint := expected if types.is_concrete_integer(expected) else types.INVALID
|
||||
left := build_nested_expr(checker, expr.left, locals, global_reads, calls, hint, pkg, file)
|
||||
left := build_nested_expr(checker, expr.left, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
right := build_nested_expr(checker, expr.right, locals, global_reads, calls, types.U64, pkg, file)
|
||||
if invalid, propagated := propagate_invalid_expr(checker, expr.span, left, right); propagated {
|
||||
return invalid
|
||||
}
|
||||
left_type := checker.module.exprs[left].type
|
||||
right_type := checker.module.exprs[right].type
|
||||
if !types.is_concrete_integer(left_type) {
|
||||
left_representation := types.runtime_representation(left_type, store)
|
||||
if !types.is_concrete_integer(left_representation) {
|
||||
id := source.add(checker.diagnostics, checker.module.exprs[left].span, "shifted value must be a concrete integer")
|
||||
return invalid_hir_expr(checker, expr.span, id, left_type)
|
||||
}
|
||||
@@ -8643,7 +8854,7 @@ build_compound_expr :: proc(
|
||||
}
|
||||
if constant := eval_integer_constant_in_context(checker, expr.right, pkg, file);
|
||||
constant.kind == .Value && expr.kind != .Shift_Left_Saturating &&
|
||||
constant.value >= i128(types.bits(left_type, checker.target)) {
|
||||
constant.value >= i128(types.bits(left_representation, checker.target)) {
|
||||
id := source.addf(
|
||||
checker.diagnostics, checker.module.exprs[right].span,
|
||||
"shift count %d exceeds %s width", constant.value, types.name(left_type),
|
||||
@@ -8703,11 +8914,13 @@ build_compound_expr :: proc(
|
||||
} else if right_numeric_const && !left_numeric_const {
|
||||
left = build_nested_expr(checker, expr.left, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
hint := checker.module.exprs[left].type
|
||||
right = build_nested_expr(checker, expr.right, locals, global_reads, calls, hint, pkg, file)
|
||||
_, distinct_ok := types.distinct_scalar_backing(hint, store)
|
||||
right = build_nested_expr(checker, expr.right, locals, global_reads, calls, hint, pkg, file, distinct_ok)
|
||||
} else if left_numeric_const && !right_numeric_const {
|
||||
right = build_nested_expr(checker, expr.right, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
hint := checker.module.exprs[right].type
|
||||
left = build_nested_expr(checker, expr.left, locals, global_reads, calls, hint, pkg, file)
|
||||
_, distinct_ok := types.distinct_scalar_backing(hint, store)
|
||||
left = build_nested_expr(checker, expr.left, locals, global_reads, calls, hint, pkg, file, distinct_ok)
|
||||
} else {
|
||||
left = build_nested_expr(checker, expr.left, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
right = build_nested_expr(checker, expr.right, locals, global_reads, calls, types.INVALID, pkg, file)
|
||||
@@ -8738,11 +8951,13 @@ build_compound_expr :: proc(
|
||||
}
|
||||
operand_type = types.BOOL
|
||||
} else {
|
||||
operand_type = types.widest(left_type, right_type)
|
||||
if !types.is_concrete_scalar(operand_type) || types.is_bool(operand_type) {
|
||||
operation, ok := numeric_operation_type(checker, left_type, right_type)
|
||||
if !ok ||
|
||||
types.is_bool(operation.representation) && expr.kind != .Eq && expr.kind != .Ne {
|
||||
id := source.add(checker.diagnostics, expr.span, "comparison requires compatible numeric operands")
|
||||
return invalid_hir_expr(checker, expr.span, id, types.BOOL)
|
||||
}
|
||||
operand_type = operation.result
|
||||
}
|
||||
left = coerce_expr(checker, left, operand_type, checker.module.exprs[left].span)
|
||||
right = coerce_expr(checker, right, operand_type, checker.module.exprs[right].span)
|
||||
@@ -8978,17 +9193,21 @@ build_binary_arith :: proc(
|
||||
left=left, right=right, target=hir.INVALID_REF, diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
}
|
||||
result := types.widest(checker.module.exprs[left].type, checker.module.exprs[right].type)
|
||||
if !types.is_concrete_scalar(result) {
|
||||
operation, ok := numeric_operation_type(
|
||||
checker,
|
||||
checker.module.exprs[left].type,
|
||||
checker.module.exprs[right].type,
|
||||
)
|
||||
if !ok || types.is_bool(operation.representation) {
|
||||
id := source.add(checker.diagnostics, span, "arithmetic requires compatible numeric operands")
|
||||
return invalid_hir_expr(checker, span, id)
|
||||
}
|
||||
if op == .Div && !types.is_float(result, checker.target) {
|
||||
if op == .Div && !types.is_float(operation.representation, checker.target) {
|
||||
id := source.add(
|
||||
checker.diagnostics, span,
|
||||
"integer '/' is not allowed; use divtrunc!, divfloor!, divexact!, or divceil!",
|
||||
)
|
||||
return invalid_hir_expr(checker, span, id, result)
|
||||
return invalid_hir_expr(checker, span, id, operation.result)
|
||||
}
|
||||
result_kind := hir.Expr_Kind.Add
|
||||
#partial switch op {
|
||||
@@ -8996,10 +9215,10 @@ build_binary_arith :: proc(
|
||||
case .Mul: result_kind = .Mul
|
||||
case .Div: result_kind = .Div
|
||||
}
|
||||
coerced_left := coerce_expr(checker, left, result, checker.module.exprs[left].span)
|
||||
coerced_right := coerce_expr(checker, right, result, checker.module.exprs[right].span)
|
||||
coerced_left := coerce_expr(checker, left, operation.result, checker.module.exprs[left].span)
|
||||
coerced_right := coerce_expr(checker, right, operation.result, checker.module.exprs[right].span)
|
||||
return add_hir_expr(checker, hir.Expr{
|
||||
kind=result_kind, span=span, type=result, left=coerced_left, right=coerced_right,
|
||||
kind=result_kind, span=span, type=operation.result, left=coerced_left, right=coerced_right,
|
||||
target=hir.INVALID_REF, diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
}
|
||||
@@ -9013,6 +9232,7 @@ build_expr :: proc(
|
||||
expected := types.INVALID,
|
||||
pkg := ast.Package_Id(0),
|
||||
file := ast.File_Id(0),
|
||||
numeric_operation := false,
|
||||
) -> hir.Expr_Id {
|
||||
stack := checker.build_stack
|
||||
checker.build_stack = nil
|
||||
@@ -9030,7 +9250,12 @@ build_expr :: proc(
|
||||
delete(stack)
|
||||
}
|
||||
}
|
||||
append(&stack, Build_Expr_Frame{expr=expr_id, expected=expected, template=ast.INVALID_FUNCTION})
|
||||
append(&stack, Build_Expr_Frame{
|
||||
expr=expr_id,
|
||||
expected=expected,
|
||||
template=ast.INVALID_FUNCTION,
|
||||
numeric_operation=numeric_operation,
|
||||
})
|
||||
last := hir.INVALID_EXPR
|
||||
|
||||
for len(stack) > 0 {
|
||||
@@ -9067,7 +9292,7 @@ build_expr :: proc(
|
||||
constant = eval_constant(checker, frame.expr)
|
||||
}
|
||||
if constant.kind == .Value || constant.kind == .Overflow || constant.kind == .Div_By_Zero || constant.kind == .Non_Exact {
|
||||
last = build_constant_expr(checker, expr, constant, frame.expected)
|
||||
last = build_constant_expr(checker, expr, constant, frame.expected, frame.numeric_operation)
|
||||
_ = pop(&stack)
|
||||
continue
|
||||
}
|
||||
@@ -9093,7 +9318,7 @@ build_expr :: proc(
|
||||
last = invalid_hir_expr(checker, expr.span, expr.diagnostic)
|
||||
_ = pop(&stack)
|
||||
case .Float:
|
||||
last = build_float_expr(checker, expr, frame.expected)
|
||||
last = build_float_expr(checker, expr, frame.expected, frame.numeric_operation)
|
||||
_ = pop(&stack)
|
||||
case .Name:
|
||||
last = hir.INVALID_EXPR
|
||||
@@ -9269,13 +9494,20 @@ build_expr :: proc(
|
||||
append(&stack, Build_Expr_Frame{expr=expr.left, expected=types.INVALID, template=ast.INVALID_FUNCTION})
|
||||
case .Add, .Sub, .Mul, .Div:
|
||||
stack[frame_index].stage = 1
|
||||
// Preserve assignment/return context for literal operands, e.g.
|
||||
// assigning `i + 1` back into a `u32` local.
|
||||
left_expected := types.INVALID
|
||||
if types.is_concrete_scalar(frame.expected) && !types.is_bool(frame.expected) {
|
||||
left_expected = frame.expected
|
||||
left_const := is_numeric_constant_expr(checker, expr.left)
|
||||
right_const := is_numeric_constant_expr(checker, expr.right)
|
||||
stack[frame_index].reverse_operands = left_const && !right_const
|
||||
first := expr.right if stack[frame_index].reverse_operands else expr.left
|
||||
first_expected := types.INVALID
|
||||
if !stack[frame_index].reverse_operands &&
|
||||
types.is_concrete_scalar(frame.expected) && !types.is_bool(frame.expected) {
|
||||
first_expected = frame.expected
|
||||
}
|
||||
append(&stack, Build_Expr_Frame{expr=expr.left, expected=left_expected, template=ast.INVALID_FUNCTION})
|
||||
append(&stack, Build_Expr_Frame{
|
||||
expr=first,
|
||||
expected=first_expected,
|
||||
template=ast.INVALID_FUNCTION,
|
||||
})
|
||||
case .Call:
|
||||
if expr.left != ast.INVALID_EXPR {
|
||||
stack[frame_index].stage = 6
|
||||
@@ -9481,7 +9713,7 @@ build_expr :: proc(
|
||||
constructor_type := types.resolve_alias(named_type, &checker.module.types)
|
||||
constructor_item, constructor_ok := types.node(&checker.module.types, constructor_type)
|
||||
scalar_alias := named_ok && named_item.kind == .Alias &&
|
||||
types.is_concrete_scalar(constructor_type) && !types.is_bool(constructor_type)
|
||||
types.is_concrete_scalar(constructor_type)
|
||||
if scalar_alias {
|
||||
if len(expr.args) != 1 {
|
||||
id := source.addf(
|
||||
@@ -9681,7 +9913,8 @@ build_expr :: proc(
|
||||
continue
|
||||
}
|
||||
operand_type := checker.module.exprs[operand].type
|
||||
if !types.is_signed(operand_type, checker.target) && !types.is_float(operand_type, checker.target) {
|
||||
representation := types.runtime_representation(operand_type, &checker.module.types)
|
||||
if !types.is_signed(representation, checker.target) && !types.is_float(representation, checker.target) {
|
||||
id := source.add(checker.diagnostics, expr.span, "negation requires a signed integer or float")
|
||||
last = invalid_hir_expr(checker, expr.span, id)
|
||||
} else {
|
||||
@@ -9689,7 +9922,10 @@ build_expr :: proc(
|
||||
kind=.Negate, span=expr.span, type=operand_type, left=operand,
|
||||
target=hir.INVALID_REF, right=hir.INVALID_EXPR, diagnostic=source.INVALID_DIAGNOSTIC,
|
||||
})
|
||||
if types.is_signed(frame.expected, checker.target) || types.is_float(frame.expected, checker.target) {
|
||||
expected_representation := types.runtime_representation(frame.expected, &checker.module.types)
|
||||
if types.equal(operand_type, frame.expected) &&
|
||||
(types.is_signed(expected_representation, checker.target) ||
|
||||
types.is_float(expected_representation, checker.target)) {
|
||||
last = coerce_expr(checker, last, frame.expected, expr.span)
|
||||
}
|
||||
}
|
||||
@@ -9699,21 +9935,36 @@ build_expr :: proc(
|
||||
if frame.stage == 1 {
|
||||
stack[frame_index].left = last
|
||||
stack[frame_index].stage = 2
|
||||
right_expected := types.INVALID
|
||||
if types.is_many_pointer(checker.module.exprs[last].type, &checker.module.types) {
|
||||
right_expected = types.USIZE
|
||||
} else if eval_constant(checker, expr.right).kind == .Value {
|
||||
// A constant RHS adopts the concrete LHS type before numeric
|
||||
// compatibility is checked.
|
||||
right_expected = checker.module.exprs[last].type
|
||||
first_type := checker.module.exprs[last].type
|
||||
second := expr.left if frame.reverse_operands else expr.right
|
||||
second_expected := types.INVALID
|
||||
numeric_operation := false
|
||||
if types.is_many_pointer(first_type, &checker.module.types) {
|
||||
second_expected = types.USIZE
|
||||
} else if is_numeric_constant_expr(checker, second) {
|
||||
second_expected = first_type
|
||||
if representation, ok := types.distinct_scalar_backing(first_type, &checker.module.types);
|
||||
ok && !types.is_bool(representation) {
|
||||
numeric_operation = true
|
||||
}
|
||||
} else if types.is_concrete_scalar(frame.expected) && !types.is_bool(frame.expected) {
|
||||
right_expected = frame.expected
|
||||
second_expected = frame.expected
|
||||
}
|
||||
append(&stack, Build_Expr_Frame{expr=expr.right, expected=right_expected, template=ast.INVALID_FUNCTION})
|
||||
append(&stack, Build_Expr_Frame{
|
||||
expr=second,
|
||||
expected=second_expected,
|
||||
template=ast.INVALID_FUNCTION,
|
||||
numeric_operation=numeric_operation,
|
||||
})
|
||||
continue
|
||||
}
|
||||
if frame.stage == 2 {
|
||||
last = build_binary_arith(checker, expr.kind, frame.left, last, expr.span)
|
||||
first, second := frame.left, last
|
||||
left, right := first, second
|
||||
if frame.reverse_operands {
|
||||
left, right = second, first
|
||||
}
|
||||
last = build_binary_arith(checker, expr.kind, left, right, expr.span)
|
||||
_ = pop(&stack)
|
||||
continue
|
||||
}
|
||||
@@ -10062,7 +10313,8 @@ build_expr :: proc(
|
||||
if frame.stage == 8 {
|
||||
distinct_item, ok := types.node(&checker.module.types, frame.target_type)
|
||||
actual := checker.module.exprs[last].type
|
||||
if !ok || distinct_item.kind != .Distinct || !types.equal(actual, distinct_item.child) {
|
||||
if !ok || distinct_item.kind != .Distinct ||
|
||||
!types.can_retype_distinct(actual, frame.target_type, &checker.module.types) {
|
||||
id := source.addf(
|
||||
checker.diagnostics,
|
||||
expr.span,
|
||||
@@ -10892,9 +11144,16 @@ build_block :: proc(
|
||||
statement.assignment_op == .Shift_Left_Saturating {
|
||||
rhs_expected = types.U64
|
||||
}
|
||||
rhs_numeric_operation := false
|
||||
if statement.assignment_op != .Shift_Left &&
|
||||
statement.assignment_op != .Shift_Right &&
|
||||
statement.assignment_op != .Shift_Left_Saturating &&
|
||||
is_numeric_constant_expr(checker, statement.expr) {
|
||||
_, rhs_numeric_operation = types.distinct_scalar_backing(target_type, &checker.module.types)
|
||||
}
|
||||
value = build_expr(
|
||||
checker, statement.expr, ctx.locals^[:], ctx.global_reads, ctx.calls,
|
||||
rhs_expected, ctx.pkg, ctx.file,
|
||||
rhs_expected, ctx.pkg, ctx.file, rhs_numeric_operation,
|
||||
)
|
||||
if diagnostic, invalid := invalid_expr_diagnostic(checker, value); invalid {
|
||||
append(&body, hir.stmt_id(len(checker.module.statements)))
|
||||
@@ -10937,14 +11196,15 @@ build_block :: proc(
|
||||
is_bitwise := statement.assignment_op == .Bit_And ||
|
||||
statement.assignment_op == .Bit_Or ||
|
||||
statement.assignment_op == .Bit_Xor
|
||||
result_type := types.widest(target_type, rhs_type)
|
||||
operation, compatible := numeric_operation_type(checker, target_type, rhs_type)
|
||||
target_representation := types.runtime_representation(target_type, &checker.module.types)
|
||||
if is_shift {
|
||||
if !types.is_concrete_integer(target_type) || !types.is_unsigned(rhs_type, checker.target) {
|
||||
if !types.is_concrete_integer(target_representation) || !types.is_unsigned(rhs_type, checker.target) {
|
||||
id := source.add(checker.diagnostics, statement.span, "shift assignment requires an integer target and unsigned integer count")
|
||||
value = invalid_hir_expr(checker, statement.span, id, target_type)
|
||||
} else if constant := eval_integer_constant_in_context(checker, statement.expr, ctx.pkg, ctx.file);
|
||||
constant.kind == .Value && statement.assignment_op != .Shift_Left_Saturating &&
|
||||
constant.value >= i128(types.bits(target_type, checker.target)) {
|
||||
constant.value >= i128(types.bits(target_representation, checker.target)) {
|
||||
id := source.addf(
|
||||
checker.diagnostics, statement.span,
|
||||
"shift count %d exceeds %s width", constant.value, types.name(target_type),
|
||||
@@ -10952,21 +11212,21 @@ build_block :: proc(
|
||||
value = invalid_hir_expr(checker, statement.span, id, target_type)
|
||||
}
|
||||
} else if is_bitwise {
|
||||
if !types.is_concrete_integer(result_type) {
|
||||
if !compatible || !types.is_concrete_integer(operation.representation) {
|
||||
id := source.add(checker.diagnostics, statement.span, "bitwise assignment requires compatible concrete integer operands")
|
||||
value = invalid_hir_expr(checker, statement.span, id, target_type)
|
||||
} else {
|
||||
value = coerce_expr(checker, value, target_type, statement.span)
|
||||
}
|
||||
} else if statement.assignment_op == .Div && types.is_concrete_integer(result_type) {
|
||||
} else if statement.assignment_op == .Div &&
|
||||
compatible && types.is_concrete_integer(operation.representation) {
|
||||
id := source.add(
|
||||
checker.diagnostics,
|
||||
statement.span,
|
||||
"integer '/=' is not allowed; assign through an explicit division builtin",
|
||||
)
|
||||
value = invalid_hir_expr(checker, statement.span, id, target_type)
|
||||
} else if !types.is_concrete_scalar(result_type) ||
|
||||
types.is_bool(result_type) {
|
||||
} else if !compatible || types.is_bool(operation.representation) {
|
||||
id := source.add(
|
||||
checker.diagnostics,
|
||||
statement.span,
|
||||
|
||||
Reference in New Issue
Block a user