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

parser: detect wrong usage of Option as concrete type (#18334)

This commit is contained in:
Felipe Pena 2023-06-03 08:24:25 -03:00 committed by GitHub
parent 0fc33c6fa3
commit 4e21b2ab4e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 1 deletions

View File

@ -0,0 +1,5 @@
vlib/v/checker/tests/option_concrete_type_err.vv:5:8: error: cannot use Option type name as concrete type
3 | }
4 |
5 | r := f[?int]()
| ^

View File

@ -0,0 +1,5 @@
fn f[T]() T {
return none
}
r := f[?int]()

View File

@ -485,7 +485,10 @@ fn (mut p Parser) expr_with_left(left ast.Expr, precedence int, is_stmt_ident bo
} else if left !is ast.IntegerLiteral && p.tok.kind in [.lsbr, .nilsbr] } else if left !is ast.IntegerLiteral && p.tok.kind in [.lsbr, .nilsbr]
&& (p.tok.line_nr == p.prev_tok.line_nr || (p.prev_tok.kind == .string && (p.tok.line_nr == p.prev_tok.line_nr || (p.prev_tok.kind == .string
&& p.tok.line_nr == p.prev_tok.line_nr + p.prev_tok.lit.count('\n'))) { && p.tok.line_nr == p.prev_tok.line_nr + p.prev_tok.lit.count('\n'))) {
if p.tok.kind == .nilsbr { if p.peek_tok.kind == .question && p.peek_token(2).kind == .name {
p.next()
p.error_with_pos('cannot use Option type name as concrete type', p.tok.pos())
} else if p.tok.kind == .nilsbr {
node = p.index_expr(node, true) node = p.index_expr(node, true)
} else { } else {
node = p.index_expr(node, false) node = p.index_expr(node, false)