checker: make << shifts work with custom number types

pull/6105/head
Alexander Medvednikov 2020-08-11 01:05:34 +02:00
parent 2dd82748e0
commit 1a9dba0005
2 changed files with 11 additions and 2 deletions

View File

@ -144,8 +144,12 @@ pub fn (mut c Checker) check_matching_function_symbols(got_type_sym, exp_type_sy
[inline]
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() {
c.error('invalid operation: shift of type `${c.table.get_type_symbol(left_type).name}`',
left_pos)
// maybe it's an int alias? TODO move this to is_int() ?
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
} 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}',

View File

@ -1,3 +1,5 @@
type MyInt int
fn test_shift_operators() {
// check that shift works with all integer types
// as the right-hand side operand
@ -63,4 +65,7 @@ fn test_shift_operators() {
assert e3 == b
e3 >>= u64(i)
assert e3 == a
// Test shifts with custom int types
x := MyInt(2)
assert x << 2 == 8
}