Skip to content

Commit

Permalink
parser: disallow option alias with option parent type
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Feb 9, 2024
1 parent 1544546 commit c007766
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
5 changes: 5 additions & 0 deletions vlib/v/parser/parse_type.v
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,11 @@ 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) {
p.error_with_pos('alias type cannot be option as parent type is already option',
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
vlib/v/parser/tests/option_alias_option_type_err.vv:4:11: error: alias type cannot be option as parent type is already option
2 |
3 | struct Bar {
4 | a ?Foo
| ~~~~
5 | }
5 changes: 5 additions & 0 deletions vlib/v/parser/tests/option_alias_option_type_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type Foo = ?bool

struct Bar {
a ?Foo
}

0 comments on commit c007766

Please sign in to comment.