From be8be3d3192be0ee89673245877cd1d401999e8f Mon Sep 17 00:00:00 2001 From: shadowninja55 <49539636+shadowninja55@users.noreply.github.com> Date: Fri, 25 Jun 2021 06:08:56 -0400 Subject: [PATCH] v.parser: prohibit redeclaration of builtin types (#10563) --- vlib/eventbus/README.md | 8 +- vlib/v/ast/table.v | 81 +++++++++++-------- ...rohibit_redeclaration_of_builtin_types.out | 3 + ...prohibit_redeclaration_of_builtin_types.vv | 1 + 4 files changed, 59 insertions(+), 34 deletions(-) create mode 100644 vlib/v/parser/tests/prohibit_redeclaration_of_builtin_types.out create mode 100644 vlib/v/parser/tests/prohibit_redeclaration_of_builtin_types.vv diff --git a/vlib/eventbus/README.md b/vlib/eventbus/README.md index 90d2e3d1d..93047db53 100644 --- a/vlib/eventbus/README.md +++ b/vlib/eventbus/README.md @@ -85,18 +85,22 @@ fn on_error(receiver voidptr, e &Error, work &Work) { ```v oksyntax module main +import eventbus + +const eb = eventbus.new() + struct Work { hours int } -struct Error { +struct AnError { message string } fn do_work() { work := Work{20} // get a mutable Params instance & put some data into it - error := &Error{'Error: no internet connection.'} + error := &AnError{'Error: no internet connection.'} // publish the event eb.publish('error', work, error) } diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index ff4376f53..966423217 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -542,44 +542,61 @@ pub fn (t &Table) unalias_num_type(typ Type) Type { return typ } -[inline] -pub fn (mut t Table) register_type_symbol(typ TypeSymbol) int { - // println('register_type_symbol( $typ.name )') - existing_idx := t.type_idxs[typ.name] - if existing_idx > 0 { - ex_type := t.type_symbols[existing_idx] - match ex_type.kind { - .placeholder { - // override placeholder - // println('overriding type placeholder `$typ.name`') - t.type_symbols[existing_idx] = { - ...typ - methods: ex_type.methods - } - return existing_idx +fn (mut t Table) check_for_already_registered_symbol(typ TypeSymbol, existing_idx int) int { + ex_type := t.type_symbols[existing_idx] + match ex_type.kind { + .placeholder { + // override placeholder + // println('overriding type placeholder `$typ.name`') + t.type_symbols[existing_idx] = { + ...typ + methods: ex_type.methods } - else { - // builtin - // this will override the already registered builtin types - // with the actual v struct declaration in the source - if (existing_idx >= string_type_idx && existing_idx <= map_type_idx) - || existing_idx == error_type_idx { - if existing_idx == string_type_idx { - // existing_type := t.type_symbols[existing_idx] - t.type_symbols[existing_idx] = TypeSymbol{ - ...typ - kind: ex_type.kind - } - } else { - t.type_symbols[existing_idx] = typ + return existing_idx + } + else { + // builtin + // this will override the already registered builtin types + // with the actual v struct declaration in the source + if (existing_idx >= string_type_idx && existing_idx <= map_type_idx) + || existing_idx == error_type_idx { + if existing_idx == string_type_idx { + // existing_type := t.type_symbols[existing_idx] + t.type_symbols[existing_idx] = TypeSymbol{ + ...typ + kind: ex_type.kind } - return existing_idx + } else { + t.type_symbols[existing_idx] = typ } - return -1 + return existing_idx + } + return -1 + } + } + return -2 +} + +[inline] +pub fn (mut t Table) register_type_symbol(typ TypeSymbol) int { + mut typ_idx := -2 + mut existing_idx := t.type_idxs[typ.name] + if existing_idx > 0 { + typ_idx = t.check_for_already_registered_symbol(typ, existing_idx) + if typ_idx != -2 { + return typ_idx + } + } + if typ.mod == 'main' { + existing_idx = t.type_idxs[typ.name.trim_prefix('main.')] + if existing_idx > 0 { + typ_idx = t.check_for_already_registered_symbol(typ, existing_idx) + if typ_idx != -2 { + return typ_idx } } } - typ_idx := t.type_symbols.len + typ_idx = t.type_symbols.len t.type_symbols << typ t.type_idxs[typ.name] = typ_idx return typ_idx diff --git a/vlib/v/parser/tests/prohibit_redeclaration_of_builtin_types.out b/vlib/v/parser/tests/prohibit_redeclaration_of_builtin_types.out new file mode 100644 index 000000000..e292e0a01 --- /dev/null +++ b/vlib/v/parser/tests/prohibit_redeclaration_of_builtin_types.out @@ -0,0 +1,3 @@ +vlib/v/parser/tests/prohibit_redeclaration_of_builtin_types.vv:1:8: error: cannot register struct `Option`, another type with this name exists + 1 | struct Option {} + | ~~~~~~ diff --git a/vlib/v/parser/tests/prohibit_redeclaration_of_builtin_types.vv b/vlib/v/parser/tests/prohibit_redeclaration_of_builtin_types.vv new file mode 100644 index 000000000..26669cca0 --- /dev/null +++ b/vlib/v/parser/tests/prohibit_redeclaration_of_builtin_types.vv @@ -0,0 +1 @@ +struct Option {} -- 2.30.2