From 830cf4645c373a0015323f9a8c7c2c334f3d8236 Mon Sep 17 00:00:00 2001 From: shadowninja55 <49539636+shadowninja55@users.noreply.github.com> Date: Mon, 28 Jun 2021 03:26:09 -0400 Subject: [PATCH] v.parser: prohibit registering selectively imported (structs / enums / aliases / interfaces) (#10579) --- vlib/v/parser/parser.v | 10 ++++++++++ vlib/v/parser/struct.v | 13 ++++++++++++- vlib/v/parser/tests/register_imported_alias.out | 4 ++++ vlib/v/parser/tests/register_imported_alias.vv | 2 ++ vlib/v/parser/tests/register_imported_enum.out | 4 ++++ vlib/v/parser/tests/register_imported_enum.vv | 2 ++ vlib/v/parser/tests/register_imported_interface.out | 4 ++++ vlib/v/parser/tests/register_imported_interface.vv | 2 ++ vlib/v/parser/tests/register_imported_struct.out | 4 ++++ vlib/v/parser/tests/register_imported_struct.vv | 2 ++ 10 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 vlib/v/parser/tests/register_imported_alias.out create mode 100644 vlib/v/parser/tests/register_imported_alias.vv create mode 100644 vlib/v/parser/tests/register_imported_enum.out create mode 100644 vlib/v/parser/tests/register_imported_enum.vv create mode 100644 vlib/v/parser/tests/register_imported_interface.out create mode 100644 vlib/v/parser/tests/register_imported_interface.vv create mode 100644 vlib/v/parser/tests/register_imported_struct.out create mode 100644 vlib/v/parser/tests/register_imported_struct.vv diff --git a/vlib/v/parser/parser.v b/vlib/v/parser/parser.v index e7b35aed94..772f59c7e9 100644 --- a/vlib/v/parser/parser.v +++ b/vlib/v/parser/parser.v @@ -2962,6 +2962,11 @@ fn (mut p Parser) enum_decl() ast.EnumDecl { end_pos) return ast.EnumDecl{} } + if enum_name in p.imported_symbols { + p.error_with_pos('cannot register enum `$enum_name`, this type was already imported', + end_pos) + return ast.EnumDecl{} + } name := p.prepend_mod(enum_name) p.check(.lcbr) enum_decl_comments := p.eat_comments({}) @@ -3059,6 +3064,11 @@ fn (mut p Parser) type_decl() ast.TypeDecl { decl_pos) return ast.FnTypeDecl{} } + if name in p.imported_symbols { + p.error_with_pos('cannot register alias `$name`, this type was already imported', + end_pos) + return ast.AliasTypeDecl{} + } mut sum_variants := []ast.TypeNode{} p.check(.assign) mut type_pos := p.tok.position() diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index 9d5cf73fc7..e50f145152 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -73,6 +73,11 @@ fn (mut p Parser) struct_decl() ast.StructDecl { p.error_with_pos('struct names must have more than one character', name_pos) return ast.StructDecl{} } + if name in p.imported_symbols { + p.error_with_pos('cannot register struct `$name`, this type was already imported', + name_pos) + return ast.StructDecl{} + } mut orig_name := name if language == .c { name = 'C.$name' @@ -449,10 +454,16 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { } name_pos := p.tok.position() p.check_for_impure_v(language, name_pos) - interface_name := p.prepend_mod(p.check_name()).clone() + modless_name := p.check_name() + interface_name := p.prepend_mod(modless_name).clone() // println('interface decl $interface_name') p.check(.lcbr) pre_comments := p.eat_comments({}) + if modless_name in p.imported_symbols { + p.error_with_pos('cannot register interface `$interface_name`, this type was already imported', + name_pos) + return ast.InterfaceDecl{} + } // Declare the type reg_idx := p.table.register_type_symbol( is_public: is_pub diff --git a/vlib/v/parser/tests/register_imported_alias.out b/vlib/v/parser/tests/register_imported_alias.out new file mode 100644 index 0000000000..3cc0365c7c --- /dev/null +++ b/vlib/v/parser/tests/register_imported_alias.out @@ -0,0 +1,4 @@ +vlib/v/parser/tests/register_imported_alias.vv:2:6: error: cannot register alias `Duration`, this type was already imported + 1 | import time { Duration } + 2 | type Duration = bool + | ~~~~~~~~ diff --git a/vlib/v/parser/tests/register_imported_alias.vv b/vlib/v/parser/tests/register_imported_alias.vv new file mode 100644 index 0000000000..0200e5d026 --- /dev/null +++ b/vlib/v/parser/tests/register_imported_alias.vv @@ -0,0 +1,2 @@ +import time { Duration } +type Duration = bool diff --git a/vlib/v/parser/tests/register_imported_enum.out b/vlib/v/parser/tests/register_imported_enum.out new file mode 100644 index 0000000000..89f888be81 --- /dev/null +++ b/vlib/v/parser/tests/register_imported_enum.out @@ -0,0 +1,4 @@ +vlib/v/parser/tests/register_imported_enum.vv:2:6: error: cannot register enum `Method`, this type was already imported + 1 | import net.http { Method } + 2 | enum Method { foo bar } + | ~~~~~~ diff --git a/vlib/v/parser/tests/register_imported_enum.vv b/vlib/v/parser/tests/register_imported_enum.vv new file mode 100644 index 0000000000..bb1bb6ea19 --- /dev/null +++ b/vlib/v/parser/tests/register_imported_enum.vv @@ -0,0 +1,2 @@ +import net.http { Method } +enum Method { foo bar } diff --git a/vlib/v/parser/tests/register_imported_interface.out b/vlib/v/parser/tests/register_imported_interface.out new file mode 100644 index 0000000000..58b20293ef --- /dev/null +++ b/vlib/v/parser/tests/register_imported_interface.out @@ -0,0 +1,4 @@ +vlib/v/parser/tests/register_imported_interface.vv:2:11: error: cannot register interface `Reader`, this type was already imported + 1 | import io { Reader } + 2 | interface Reader {} + | ~~~~~~ diff --git a/vlib/v/parser/tests/register_imported_interface.vv b/vlib/v/parser/tests/register_imported_interface.vv new file mode 100644 index 0000000000..c659327dae --- /dev/null +++ b/vlib/v/parser/tests/register_imported_interface.vv @@ -0,0 +1,2 @@ +import io { Reader } +interface Reader {} diff --git a/vlib/v/parser/tests/register_imported_struct.out b/vlib/v/parser/tests/register_imported_struct.out new file mode 100644 index 0000000000..259b831382 --- /dev/null +++ b/vlib/v/parser/tests/register_imported_struct.out @@ -0,0 +1,4 @@ +vlib/v/parser/tests/register_imported_struct.vv:2:8: error: cannot register struct `File`, this type was already imported + 1 | import os { File } + 2 | struct File {} + | ~~~~ diff --git a/vlib/v/parser/tests/register_imported_struct.vv b/vlib/v/parser/tests/register_imported_struct.vv new file mode 100644 index 0000000000..a9c54dbc95 --- /dev/null +++ b/vlib/v/parser/tests/register_imported_struct.vv @@ -0,0 +1,2 @@ +import os { File } +struct File {}