From 1a9dba0005898f16ac4b4e8ef0effe629c98c0e5 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 11 Aug 2020 01:05:34 +0200 Subject: [PATCH] checker: make << shifts work with custom number types --- vlib/v/checker/check_types.v | 8 ++++++-- vlib/v/tests/shift_test.v | 5 +++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/vlib/v/checker/check_types.v b/vlib/v/checker/check_types.v index 795348f87f..6c7a15bfda 100644 --- a/vlib/v/checker/check_types.v +++ b/vlib/v/checker/check_types.v @@ -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}', diff --git a/vlib/v/tests/shift_test.v b/vlib/v/tests/shift_test.v index 597e8389a0..f5ef181685 100644 --- a/vlib/v/tests/shift_test.v +++ b/vlib/v/tests/shift_test.v @@ -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 }