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

markused: fix generic fn mark as used (fix #15387) (#15406)

This commit is contained in:
yuyi 2022-08-11 13:27:20 +08:00 committed by GitHub
parent f54e45b77e
commit 32fa475316
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 1 deletions

View File

@ -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

View File

@ -0,0 +1 @@
OK!!

View File

@ -0,0 +1 @@
OK!!

View File

@ -0,0 +1,54 @@
struct Calc<S> {
mut:
typ S
}
struct TypeA {}
struct TypeB {}
fn (mut c Calc<S>) next<T>(input T) f64 {
$if S is TypeA || S is TypeB {
return c.typ.next(input)
} $else {
return 99.0
}
}
fn (mut t TypeA) next<T>(input T) f64 {
return 10
}
fn (mut t TypeB) next<T>(input T) f64 {
return 11
}
fn new<S>() Calc<S> {
$if S is TypeA {
return Calc<TypeA>{
typ: TypeA{}
}
} $else $if S is TypeB {
return Calc<TypeB>{
typ: TypeB{}
}
} $else {
panic('unknown type $S.name')
}
}
fn main() {
{
mut c := Calc<TypeA>{
typ: TypeA{}
}
assert c.next(100) == 10.0
}
{
mut c := Calc<TypeB>{
typ: TypeB{}
}
assert c.next(100) == 11.0
}
println('OK!!')
}