diff --git a/vlib/context/empty_test.v b/vlib/context/empty_test.v index b752d66bbacec7..433d8c8ad08b78 100644 --- a/vlib/context/empty_test.v +++ b/vlib/context/empty_test.v @@ -2,7 +2,7 @@ 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') } @@ -10,7 +10,7 @@ fn test_background() { fn test_todo() { ctx := todo() - assert '&context.TODO' == ctx.str() + assert 'context.TODO' == ctx.str() if _ := ctx.value('') { panic('This should never happen') } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 6ba7795bafd1b7..cdf262bd5c7057 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 + } } } } @@ -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 }) diff --git a/vlib/v/tests/empty_interface_test.v b/vlib/v/tests/empty_interface_test.v index a9cf858a97a4f1..9ef82674ea2389 100644 --- a/vlib/v/tests/empty_interface_test.v +++ b/vlib/v/tests/empty_interface_test.v @@ -10,5 +10,5 @@ fn print_out(x Any) string { fn test_empty_interface() { ret := print_out('12345') - assert ret == '&12345' + assert ret == '12345' } diff --git a/vlib/v/tests/print_interface_variable_in_smartcast_test.v b/vlib/v/tests/print_interface_variable_in_smartcast_test.v new file mode 100644 index 00000000000000..a1f8594ccec02e --- /dev/null +++ b/vlib/v/tests/print_interface_variable_in_smartcast_test.v @@ -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' +}