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

cgen: fix error for casting int to interface (#13790)

This commit is contained in:
yuyi 2022-03-22 03:18:11 +08:00 committed by GitHub
parent 9ad64b0bd8
commit c0437afbcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 7 deletions

View File

@ -2045,7 +2045,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
&& !expected_type.has_flag(.optional) {
if expr is ast.StructInit && !got_type.is_ptr() {
g.inside_cast_in_heap++
got_styp := g.cc_type(got_type.ref(), true)
got_styp := g.cc_type(got_type_raw.ref(), true)
// TODO: why does cc_type even add this in the first place?
exp_styp := exp_sym.cname
mut fname := 'I_${got_styp}_to_Interface_$exp_styp'
@ -2056,12 +2056,7 @@ fn (mut g Gen) expr_with_cast(expr ast.Expr, got_type_raw ast.Type, expected_typ
got_styp)
g.inside_cast_in_heap--
} else {
mut got_styp := g.cc_type(got_type, true)
got_styp = match got_styp {
'int' { 'int_literal' }
'f64' { 'float_literal' }
else { got_styp }
}
got_styp := g.cc_type(got_type_raw, true)
got_is_shared := got_type.has_flag(.shared_f)
exp_styp := if got_is_shared { '__shared__$exp_sym.cname' } else { exp_sym.cname }
// If it's shared, we need to use the other caster:

View File

@ -0,0 +1,19 @@
interface Any {}
fn return_any(val Any) ?Any {
return val
}
fn test_cast_int_to_interface() {
code := 200
if an := return_any(code) {
if an is int {
println('an is an int!')
} else {
println('an is not an int!')
}
assert '$an' == 'Any(200)'
} else {
assert false
}
}