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:
parent
a585c8c22c
commit
b4207e1be7
@ -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'
|
||||
|
@ -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 {
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user