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

parser: check error for generic struct parameter (#14362)

This commit is contained in:
yuyi 2022-05-11 18:24:54 +08:00 committed by GitHub
parent cf536b848b
commit 3afc7c4c6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 0 deletions

View File

@ -661,11 +661,17 @@ pub fn (mut p Parser) parse_generic_inst_type(name string) ast.Type {
mut concrete_types := []ast.Type{}
mut is_instance := false
for p.tok.kind != .eof {
mut type_pos := p.tok.pos()
gt := p.parse_type()
type_pos = type_pos.extend(p.prev_tok.pos())
if !gt.has_flag(.generic) {
is_instance = true
}
gts := p.table.sym(gt)
if !is_instance && gts.name.len > 1 {
p.error_with_pos('generic struct parameter name needs to be exactly one char',
type_pos)
}
bs_name += gts.name
bs_cname += gts.cname
concrete_types << gt

View File

@ -0,0 +1,7 @@
vlib/v/parser/tests/generic_struct_parameter_err.vv:10:17: error: generic struct parameter name needs to be exactly one char
8 | struct MyContainer<T> {
9 | mut:
10 | lst LinkedList<MyNode<T>>
| ~~~~~~~~~
11 | }
12 |

View File

@ -0,0 +1,23 @@
import datatypes { LinkedList }
struct MyNode<T> {
mut:
data T
}
struct MyContainer<T> {
mut:
lst LinkedList<MyNode<T>>
}
fn (mut c MyContainer<T>) push(data T) {
node := MyNode<T>{
data: data
}
c.lst.push<T>(node)
}
fn main() {
mut c := MyContainer<string>{}
println(c)
}