From 32fa475316d7c8c3055faaaf5324bf3ec255b9f4 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 11 Aug 2022 13:27:20 +0800 Subject: [PATCH] markused: fix generic fn mark as used (fix #15387) (#15406) --- vlib/v/markused/markused.v | 2 +- .../nested_generics_method.run.out | 1 + ...nested_generics_method.skip_unused.run.out | 1 + .../skip_unused/nested_generics_method.vv | 54 +++++++++++++++++++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/skip_unused/nested_generics_method.run.out create mode 100644 vlib/v/tests/skip_unused/nested_generics_method.skip_unused.run.out create mode 100644 vlib/v/tests/skip_unused/nested_generics_method.vv diff --git a/vlib/v/markused/markused.v b/vlib/v/markused/markused.v index 5ed906565..af57e6b7d 100644 --- a/vlib/v/markused/markused.v +++ b/vlib/v/markused/markused.v @@ -204,7 +204,7 @@ pub fn mark_used(mut table ast.Table, pref &pref.Preferences, ast_files []&ast.F all_fn_root_names << k continue } - if mfn.receiver.typ != ast.void_type && mfn.receiver.typ.has_flag(.generic) { + if mfn.receiver.typ != ast.void_type && mfn.generic_names.len > 0 { // generic methods may be used in cgen after specialisation :-| // TODO: move generic method specialisation from cgen to before markused all_fn_root_names << k diff --git a/vlib/v/tests/skip_unused/nested_generics_method.run.out b/vlib/v/tests/skip_unused/nested_generics_method.run.out new file mode 100644 index 000000000..cd3dace03 --- /dev/null +++ b/vlib/v/tests/skip_unused/nested_generics_method.run.out @@ -0,0 +1 @@ +OK!! diff --git a/vlib/v/tests/skip_unused/nested_generics_method.skip_unused.run.out b/vlib/v/tests/skip_unused/nested_generics_method.skip_unused.run.out new file mode 100644 index 000000000..cd3dace03 --- /dev/null +++ b/vlib/v/tests/skip_unused/nested_generics_method.skip_unused.run.out @@ -0,0 +1 @@ +OK!! diff --git a/vlib/v/tests/skip_unused/nested_generics_method.vv b/vlib/v/tests/skip_unused/nested_generics_method.vv new file mode 100644 index 000000000..d13bfdaae --- /dev/null +++ b/vlib/v/tests/skip_unused/nested_generics_method.vv @@ -0,0 +1,54 @@ +struct Calc { +mut: + typ S +} + +struct TypeA {} + +struct TypeB {} + +fn (mut c Calc) next(input T) f64 { + $if S is TypeA || S is TypeB { + return c.typ.next(input) + } $else { + return 99.0 + } +} + +fn (mut t TypeA) next(input T) f64 { + return 10 +} + +fn (mut t TypeB) next(input T) f64 { + return 11 +} + +fn new() Calc { + $if S is TypeA { + return Calc{ + typ: TypeA{} + } + } $else $if S is TypeB { + return Calc{ + typ: TypeB{} + } + } $else { + panic('unknown type $S.name') + } +} + +fn main() { + { + mut c := Calc{ + typ: TypeA{} + } + assert c.next(100) == 10.0 + } + { + mut c := Calc{ + typ: TypeB{} + } + assert c.next(100) == 11.0 + } + println('OK!!') +} -- 2.30.2