checker: make << shifts work with custom number types
parent
2dd82748e0
commit
1a9dba0005
|
@ -144,8 +144,12 @@ pub fn (mut c Checker) check_matching_function_symbols(got_type_sym, exp_type_sy
|
||||||
[inline]
|
[inline]
|
||||||
fn (mut c Checker) check_shift(left_type, right_type table.Type, left_pos, right_pos token.Position) table.Type {
|
fn (mut c Checker) check_shift(left_type, right_type table.Type, left_pos, right_pos token.Position) table.Type {
|
||||||
if !left_type.is_int() {
|
if !left_type.is_int() {
|
||||||
c.error('invalid operation: shift of type `${c.table.get_type_symbol(left_type).name}`',
|
// maybe it's an int alias? TODO move this to is_int() ?
|
||||||
left_pos)
|
sym := c.table.get_type_symbol(left_type)
|
||||||
|
if sym.kind == .alias && (sym.info as table.Alias).parent_type.is_int() {
|
||||||
|
return left_type
|
||||||
|
}
|
||||||
|
c.error('invalid operation: shift of type `$sym.name`', left_pos)
|
||||||
return table.void_type
|
return table.void_type
|
||||||
} else if !right_type.is_int() {
|
} else if !right_type.is_int() {
|
||||||
c.error('cannot shift non-integer type ${c.table.get_type_symbol(right_type).name} into type ${c.table.get_type_symbol(left_type).name}',
|
c.error('cannot shift non-integer type ${c.table.get_type_symbol(right_type).name} into type ${c.table.get_type_symbol(left_type).name}',
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
type MyInt int
|
||||||
|
|
||||||
fn test_shift_operators() {
|
fn test_shift_operators() {
|
||||||
// check that shift works with all integer types
|
// check that shift works with all integer types
|
||||||
// as the right-hand side operand
|
// as the right-hand side operand
|
||||||
|
@ -63,4 +65,7 @@ fn test_shift_operators() {
|
||||||
assert e3 == b
|
assert e3 == b
|
||||||
e3 >>= u64(i)
|
e3 >>= u64(i)
|
||||||
assert e3 == a
|
assert e3 == a
|
||||||
|
// Test shifts with custom int types
|
||||||
|
x := MyInt(2)
|
||||||
|
assert x << 2 == 8
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue