1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

compiler: allow "u64_var == 0" without casting the const literal

This commit is contained in:
Alexander Medvednikov 2019-09-23 02:14:10 +03:00
parent a585c8c22c
commit b4207e1be7
3 changed files with 15 additions and 4 deletions

View File

@ -62,6 +62,7 @@ mut:
v_script bool // "V bash", import all os functions into global space 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 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_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 cur_gen_type string // "App" to replace "T" in current generic function
is_vweb bool is_vweb bool
is_sql bool is_sql bool
@ -1463,6 +1464,7 @@ fn (p mut Parser) bterm() string {
// also called on *, &, @, . (enum) // also called on *, &, @, . (enum)
fn (p mut Parser) name_expr() string { fn (p mut Parser) name_expr() string {
p.has_immutable_field = false p.has_immutable_field = false
p.is_const_literal = false
ph := p.cgen.add_placeholder() ph := p.cgen.add_placeholder()
// amp // amp
ptr := p.tok == .amp ptr := p.tok == .amp
@ -2119,10 +2121,11 @@ fn (p mut Parser) indot_expr() string {
// returns resulting type // returns resulting type
fn (p mut Parser) expression() string { fn (p mut Parser) expression() string {
if p.scanner.file_path.contains('test_test') { p.is_const_literal = true
println('expression() pass=$p.pass tok=') //if p.scanner.file_path.contains('test_test') {
p.print_tok() //println('expression() pass=$p.pass tok=')
} //p.print_tok()
//}
ph := p.cgen.add_placeholder() ph := p.cgen.add_placeholder()
mut typ := p.indot_expr() mut typ := p.indot_expr()
is_str := typ=='string' is_str := typ=='string'

View File

@ -610,6 +610,13 @@ fn (p mut Parser) _check_types(got_, expected_ string, throw bool) bool {
if expected=='void*' && got=='int' { if expected=='void*' && got=='int' {
return true 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('*', '') expected = expected.replace('*', '')
got = got.replace('*', '') got = got.replace('*', '')
if got != expected { if got != expected {

View File

@ -8,6 +8,7 @@ fn test_const() {
assert b == true assert b == true
assert a == 3 assert a == 3
assert u == u64(1) assert u == u64(1)
assert u == 1 // make sure this works without the cast
} }
fn test_str_methods() { fn test_str_methods() {