parser: disallow option alias with option parent type (#20769)

This commit is contained in:
Swastik Baranwal 2024-02-10 13:35:07 +05:30 committed by GitHub
parent 99579acaf2
commit 4d01a779b6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 0 deletions

View File

@ -534,6 +534,13 @@ fn (mut p Parser) parse_type() ast.Type {
if is_option && sym.info is ast.SumType && sym.info.is_anon { 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())) 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 { if is_option {
typ = typ.set_flag(.option) typ = typ.set_flag(.option)

View File

@ -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 | }

View File

@ -0,0 +1,5 @@
type Foo = ?bool
struct Bar {
a ?Foo
}