checker: fix type mismatch in function argument for struct reference init

pull/5137/head
Uwe Krüger 2020-05-30 15:42:12 +02:00 committed by GitHub
parent a4de507c6d
commit b74e1bb05d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 1 deletions

View File

@ -168,7 +168,7 @@ pub fn (c &Checker) promote(left_type, right_type table.Type) table.Type {
if idx_lo in [table.int_type_idx, table.i64_type_idx, table.u32_type_idx, table.u64_type_idx] {
return table.void_type
} else {
return idx_hi
return type_hi
}
} else { // f64, any_flt
if idx_lo in [table.i64_type_idx, table.u64_type_idx] {

View File

@ -223,6 +223,8 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
mut i := 0
no_keys := p.peek_tok.kind != .colon && p.tok.kind != .rcbr // `Vec{a,b,c}
// p.warn(is_short_syntax.str())
saved_is_amp := p.is_amp
p.is_amp = false
for p.tok.kind != .rcbr && p.tok.kind != .rpar {
p.check_comment()
mut field_name := ''
@ -260,6 +262,7 @@ fn (mut p Parser) struct_init(short_syntax bool) ast.StructInit {
if !short_syntax {
p.check(.rcbr)
}
p.is_amp = saved_is_amp
node := ast.StructInit{
typ: typ
fields: fields

View File

@ -0,0 +1,69 @@
fn test_anyfloat() {
a := f64(5.765) + 45.75
b := 12.3 + f64(3)
c := f32(6.75) / 3.0
d := 16.5 / f32(2)
assert a == 51.515
assert typeof(a) == 'f64'
assert b == f64(15.3)
assert typeof(b) == 'f64'
assert c == 2.25
assert typeof(c) == 'f32'
assert d == 8.25
assert typeof(d) == 'f32'
}
fn g(x f32) f32 {
return x*x
}
fn fabs(x f32) f32 {
return if x >= 0.0 { x } else { -x }
}
fn test_call() {
c := 503
r := g(f32(c) / 255.0)
assert fabs(r - 3.890949634755863) <= 1.e-6
}
struct Tx {
x f32
}
fn (s Tx) get() f32 {
return s.x
}
fn test_struct_init() {
c := 503
d := Tx {
x: g(f32(c) / 255.0)
}
assert fabs(d.get() - 3.890949634755863) < 1.e-6
}
fn struct_init_return() Tx {
c := 503
return Tx {
x: g(f32(c) / 255.0)
}
}
fn test_struct_init_return() {
x := struct_init_return()
assert fabs(fabs(x.get()) - 3.890949634755863) < 1.e-6
}
fn struct_init_ref_return() &Tx {
c := 503
return &Tx {
x: g(f32(c) / 255.0)
}
}
fn test_struct_init_ref_return() {
x := struct_init_ref_return()
assert fabs(fabs(x.get()) - 3.890949634755863) < 1.e-6
}