From b4207e1be79d35ad79527733fcbc7ecf637a0775 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Mon, 23 Sep 2019 02:14:10 +0300 Subject: [PATCH] compiler: allow "u64_var == 0" without casting the const literal --- compiler/parser.v | 11 +++++++---- compiler/table.v | 7 +++++++ vlib/builtin/int_test.v | 1 + 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/compiler/parser.v b/compiler/parser.v index e8dfd225cf..7713e3883c 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -62,6 +62,7 @@ mut: v_script bool // "V bash", import all os functions into global space var_decl_name string // To allow declaring the variable so that it can be used in the struct initialization is_alloc bool // Whether current expression resulted in an allocation + is_const_literal bool // `1`, `2.0` etc, so that `u64 == 0` works cur_gen_type string // "App" to replace "T" in current generic function is_vweb bool is_sql bool @@ -1463,6 +1464,7 @@ fn (p mut Parser) bterm() string { // also called on *, &, @, . (enum) fn (p mut Parser) name_expr() string { p.has_immutable_field = false + p.is_const_literal = false ph := p.cgen.add_placeholder() // amp ptr := p.tok == .amp @@ -2119,10 +2121,11 @@ fn (p mut Parser) indot_expr() string { // returns resulting type fn (p mut Parser) expression() string { - if p.scanner.file_path.contains('test_test') { - println('expression() pass=$p.pass tok=') - p.print_tok() - } + p.is_const_literal = true + //if p.scanner.file_path.contains('test_test') { + //println('expression() pass=$p.pass tok=') + //p.print_tok() + //} ph := p.cgen.add_placeholder() mut typ := p.indot_expr() is_str := typ=='string' diff --git a/compiler/table.v b/compiler/table.v index 3f4d5dc75e..992819f9f7 100644 --- a/compiler/table.v +++ b/compiler/table.v @@ -610,6 +610,13 @@ fn (p mut Parser) _check_types(got_, expected_ string, throw bool) bool { if expected=='void*' && got=='int' { return true } + // Allow `myu64 == 1` + //if p.fileis('_test') && is_number_type(got) && is_number_type(expected) { + //p.warn('got=$got exp=$expected $p.is_const_literal') + //} + if is_number_type(got) && is_number_type(expected) && p.is_const_literal { + return true + } expected = expected.replace('*', '') got = got.replace('*', '') if got != expected { diff --git a/vlib/builtin/int_test.v b/vlib/builtin/int_test.v index 56af9167cf..eae41cce39 100644 --- a/vlib/builtin/int_test.v +++ b/vlib/builtin/int_test.v @@ -8,6 +8,7 @@ fn test_const() { assert b == true assert a == 3 assert u == u64(1) + assert u == 1 // make sure this works without the cast } fn test_str_methods() {