Skip to content

Commit

Permalink
parser: disallow option alias with option parent type (#20769)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Feb 10, 2024
1 parent 99579ac commit 4d01a77
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
7 changes: 7 additions & 0 deletions vlib/v/parser/parse_type.v
Expand Up @@ -534,6 +534,13 @@ fn (mut p Parser) parse_type() ast.Type {
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()))
}

if is_option && sym.info is ast.Alias && sym.info.parent_type.has_flag(.option) {
alias_type_str := p.table.type_to_str(typ)
parent_type_str := p.table.type_to_str(sym.info.parent_type)
p.error_with_pos('cannot use double options like `?${parent_type_str}`, `?${alias_type_str}` is a double option. use `${alias_type_str}` instead',
option_pos.extend(p.prev_tok.pos()))
}
}
if is_option {
typ = typ.set_flag(.option)
Expand Down
6 changes: 6 additions & 0 deletions vlib/v/parser/tests/option_alias_option_type_err.out
@@ -0,0 +1,6 @@
vlib/v/parser/tests/option_alias_option_type_err.vv:4:11: error: cannot use double options like `??bool`, `?Foo` is a double option. use `Foo` instead
2 |
3 | struct Bar {
4 | a ?Foo
| ~~~~
5 | }
5 changes: 5 additions & 0 deletions vlib/v/parser/tests/option_alias_option_type_err.vv
@@ -0,0 +1,5 @@
type Foo = ?bool

struct Bar {
a ?Foo
}

0 comments on commit 4d01a77

Please sign in to comment.