From 55d9464890e95337005043df85f8739c9f52bd1e Mon Sep 17 00:00:00 2001 From: yuyi Date: Wed, 30 Mar 2022 06:33:10 +0800 Subject: [PATCH] checker: fix error for marking as referenced (#13859) --- vlib/v/checker/checker.v | 3 +++ vlib/v/tests/mark_as_referenced_test.v | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 vlib/v/tests/mark_as_referenced_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 84614732f0..fbe56ccaa9 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3556,6 +3556,9 @@ pub fn (mut c Checker) mark_as_referenced(mut node ast.Expr, as_interface bool) if c.fn_scope != voidptr(0) { obj = c.fn_scope.find_var(node.obj.name) or { obj } } + if obj.typ == 0 { + return + } type_sym := c.table.sym(obj.typ.set_nr_muls(0)) if obj.is_stack_obj && !type_sym.is_heap() && !c.pref.translated && !c.file.is_translated { diff --git a/vlib/v/tests/mark_as_referenced_test.v b/vlib/v/tests/mark_as_referenced_test.v new file mode 100644 index 0000000000..db60cefaa9 --- /dev/null +++ b/vlib/v/tests/mark_as_referenced_test.v @@ -0,0 +1,24 @@ +module main + +fn test_mark_as_referenced() { + if true { + a := Type{} + ret := f(a) + println(ret) + assert ret == 'Interface(Type{})' + } + a := Type{} + ret := f(a) + println(ret) + assert ret == 'Interface(Type{})' +} + +struct Type { +} + +interface Interface { +} + +fn f(b Interface) string { + return '$b' +}