diff --git a/vlib/v/checker/tests/option_concrete_type_err.out b/vlib/v/checker/tests/option_concrete_type_err.out new file mode 100644 index 0000000000..ea502ea40c --- /dev/null +++ b/vlib/v/checker/tests/option_concrete_type_err.out @@ -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]() + | ^ diff --git a/vlib/v/checker/tests/option_concrete_type_err.vv b/vlib/v/checker/tests/option_concrete_type_err.vv new file mode 100644 index 0000000000..d643789ce4 --- /dev/null +++ b/vlib/v/checker/tests/option_concrete_type_err.vv @@ -0,0 +1,5 @@ +fn f[T]() T { + return none +} + +r := f[?int]() diff --git a/vlib/v/parser/expr.v b/vlib/v/parser/expr.v index 9cf284db10..9e61b664d6 100644 --- a/vlib/v/parser/expr.v +++ b/vlib/v/parser/expr.v @@ -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] && (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'))) { - 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) } else { node = p.index_expr(node, false)