Skip to content

Commit

Permalink
checker: fix missing concrete type checking on a generic type specifi…
Browse files Browse the repository at this point in the history
…er (#21614)
  • Loading branch information
felipensp authored Jun 1, 2024
1 parent 7cff1f7 commit 2e567ff
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/missing_concrete_type_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/missing_concrete_type_err.vv:4:6: error: missing concrete type on generic type
2 |
3 | fn main() {
4 | bar[Foo]()
| ~~~
5 | }
6 |
9 changes: 9 additions & 0 deletions vlib/v/checker/tests/missing_concrete_type_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
struct Foo[T] {}

fn main() {
bar[Foo]()
}

fn bar[T]() {

}
5 changes: 5 additions & 0 deletions vlib/v/parser/parse_type.v
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,11 @@ fn (mut p Parser) parse_type() ast.Type {
return 0
}
sym := p.table.sym(typ)
if p.inside_fn_concrete_type && sym.info is ast.Struct {
if !typ.has_flag(.generic) && sym.info.generic_types.len > 0 {
p.error_with_pos('missing concrete type on generic type', option_pos.extend(p.prev_tok.pos()))
}
}
if is_option && sym.info is ast.SumType && sym.info.is_anon {
p.error_with_pos('an inline sum type cannot be an Option', option_pos.extend(p.prev_tok.pos()))
}
Expand Down
5 changes: 5 additions & 0 deletions vlib/v/parser/parser.v
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mut:
inside_for_expr bool
inside_fn bool // true even with implicit main
inside_fn_return bool
inside_fn_concrete_type bool // parsing fn_name[concrete_type]() call expr
inside_call_args bool // true inside f( .... )
inside_unsafe_fn bool
inside_str_interp bool
Expand Down Expand Up @@ -3369,6 +3370,10 @@ fn (mut p Parser) parse_concrete_types() []ast.Type {
if p.tok.kind !in [.lt, .lsbr] {
return types
}
p.inside_fn_concrete_type = true
defer {
p.inside_fn_concrete_type = false
}
end_kind := if p.tok.kind == .lt { token.Kind.gt } else { token.Kind.rsbr }
p.next() // `<`
mut first_done := false
Expand Down

0 comments on commit 2e567ff

Please sign in to comment.