Skip to content

Commit

Permalink
checker: fix printing interface variable in smartcast
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyi98 committed Sep 13, 2023
1 parent a0490f2 commit 2fb616b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
4 changes: 2 additions & 2 deletions vlib/context/empty_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module context

fn test_background() {
ctx := background()
assert '&context.Background' == ctx.str()
assert 'context.Background' == ctx.str()
if _ := ctx.value('') {
panic('This should never happen')
}
}

fn test_todo() {
ctx := todo()
assert '&context.TODO' == ctx.str()
assert 'context.TODO' == ctx.str()
if _ := ctx.value('') {
panic('This should never happen')
}
Expand Down
6 changes: 5 additions & 1 deletion vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -1441,7 +1441,10 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
if node.expr.is_auto_deref_var() {
if mut node.expr is ast.Ident {
if mut node.expr.obj is ast.Var {
typ = node.expr.obj.typ
if node.expr.obj.orig_type == 0 || (node.expr.obj.orig_type != 0
&& c.table.sym(node.expr.obj.orig_type).kind != .interface_) {
typ = node.expr.obj.typ
}
}
}
}
Expand Down Expand Up @@ -3798,6 +3801,7 @@ fn (mut c Checker) smartcast(mut expr ast.Expr, cur_type ast.Type, to_type_ ast.
is_used: true
is_mut: expr.is_mut
is_inherited: is_inherited
is_auto_deref: sym.kind == .interface_
smartcasts: smartcasts
orig_type: orig_type
})
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/tests/empty_interface_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ fn print_out(x Any) string {

fn test_empty_interface() {
ret := print_out('12345')
assert ret == '&12345'
assert ret == '12345'
}
25 changes: 25 additions & 0 deletions vlib/v/tests/print_interface_variable_in_smartcast_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
interface Any {}

fn do(v Any) string {
match v {
int {
println('> integer answer: ${2 * v}')
return '${2 * v}'
}
string {
println('> string answer: ${v}, len: ${v.len}')
return '${v}'
}
else {
return ''
}
}
}

fn test_printing_interface_variable_in_smartcast() {
s1 := do(123)
assert s1 == '246'

s2 := do('abc')
assert s2 == 'abc'
}

0 comments on commit 2fb616b

Please sign in to comment.