mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
checker: make << shifts work with custom number types
This commit is contained in:
parent
2dd82748e0
commit
1a9dba0005
@ -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}',
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user