From f59119485af01d1da73e3ae40366669febef7022 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Wed, 4 Aug 2021 17:14:16 +0200 Subject: [PATCH] v.parser: add check for existing type on sum type declaration (#11054) --- vlib/v/parser/parser.v | 8 +++++++- vlib/v/parser/tests/sum_type_exists_err.out | 3 +++ vlib/v/parser/tests/sum_type_exists_err.vv | 1 + vlib/v/parser/tests/type_alias_existing_type_err.out | 8 ++++---- 4 files changed, 15 insertions(+), 5 deletions(-) create mode 100644 vlib/v/parser/tests/sum_type_exists_err.out create mode 100644 vlib/v/parser/tests/sum_type_exists_err.vv diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index e17295e67c..ba75af9f07 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -3123,6 +3123,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl { p.check(.key_type) end_pos := p.tok.position() decl_pos := start_pos.extend(end_pos) + name_pos := p.tok.position() name := p.check_name() if name.len == 1 && name[0].is_capital() { p.error_with_pos('single letter capital names are reserved for generic template types.', @@ -3197,6 +3198,11 @@ fn (mut p Parser) type_decl() ast.TypeDecl { } is_public: is_pub }) + if typ == -1 { + p.error_with_pos('cannot register sum type `$name`, another type with this name exists', + name_pos) + return ast.SumTypeDecl{} + } comments = p.eat_comments(same_line: true) return ast.SumTypeDecl{ name: name @@ -3233,7 +3239,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl { type_end_pos := p.prev_tok.position() if idx == -1 { p.error_with_pos('cannot register alias `$name`, another type with this name exists', - decl_pos.extend(type_alias_pos)) + name_pos) return ast.AliasTypeDecl{} } if idx == pidx { diff --git a/vlib/v/parser/tests/sum_type_exists_err.out b/vlib/v/parser/tests/sum_type_exists_err.out new file mode 100644 index 0000000000..99bbeafa8d --- /dev/null +++ b/vlib/v/parser/tests/sum_type_exists_err.out @@ -0,0 +1,3 @@ +vlib/v/parser/tests/sum_type_exists_err.vv:1:6: error: cannot register sum type `Option`, another type with this name exists + 1 | type Option = string | int + | ~~~~~~ diff --git a/vlib/v/parser/tests/sum_type_exists_err.vv b/vlib/v/parser/tests/sum_type_exists_err.vv new file mode 100644 index 0000000000..b9634c1bf2 --- /dev/null +++ b/vlib/v/parser/tests/sum_type_exists_err.vv @@ -0,0 +1 @@ +type Option = string | int diff --git a/vlib/v/parser/tests/type_alias_existing_type_err.out b/vlib/v/parser/tests/type_alias_existing_type_err.out index 6ea695f473..e3ada576e9 100644 --- a/vlib/v/parser/tests/type_alias_existing_type_err.out +++ b/vlib/v/parser/tests/type_alias_existing_type_err.out @@ -1,7 +1,7 @@ -vlib/v/parser/tests/type_alias_existing_type_err.vv:3:1: error: cannot register alias `Foo`, another type with this name exists +vlib/v/parser/tests/type_alias_existing_type_err.vv:3:6: error: cannot register alias `Foo`, another type with this name exists 1 | struct Foo{} - 2 | + 2 | 3 | type Foo = Foo - | ~~~~~~~~~~~~~~ - 4 | + | ~~~ + 4 | 5 | fn main() {