parser: differentiate recursive alias & existing type err + rune fix

pull/7355/head
joe-conigliaro 2020-12-16 18:03:49 +11:00
parent 7c8fa62cc2
commit a7879ce77e
No known key found for this signature in database
GPG Key ID: C12F7136C08206F1
7 changed files with 41 additions and 31 deletions

View File

@ -2,7 +2,10 @@
// Use of this source code is governed by an MIT license that can be found in the LICENSE file. // Use of this source code is governed by an MIT license that can be found in the LICENSE file.
module builtin module builtin
type rune = int // This was never working correctly, the issue is now
// fixed however the type checks in checker need to be
// updated. if you uncomment it you will see the issue
// type rune = int
pub fn (c rune) str() string { pub fn (c rune) str() string {
return utf32_to_str(u32(c)) return utf32_to_str(u32(c))

View File

@ -2064,34 +2064,31 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
} }
// type MyType = int // type MyType = int
parent_type := first_type parent_type := first_type
parent_name := p.table.get_type_symbol(parent_type).name parent_sym := p.table.get_type_symbol(parent_type)
pid := parent_type.idx() pidx := parent_type.idx()
mut language := table.Language.v p.check_for_impure_v(parent_sym.language, decl_pos)
if parent_name.len > 2 && parent_name.starts_with('C.') {
language = table.Language.c
p.check_for_impure_v(language, decl_pos)
} else if parent_name.len > 2 && parent_name.starts_with('JS.') {
language = table.Language.js
p.check_for_impure_v(language, decl_pos)
}
prepend_mod_name := p.prepend_mod(name) prepend_mod_name := p.prepend_mod(name)
p.table.register_type_symbol(table.TypeSymbol{ idx := p.table.register_type_symbol(table.TypeSymbol{
kind: .alias kind: .alias
name: prepend_mod_name name: prepend_mod_name
cname: util.no_dots(prepend_mod_name) cname: util.no_dots(prepend_mod_name)
mod: p.mod mod: p.mod
parent_idx: pid parent_idx: pidx
info: table.Alias{ info: table.Alias{
parent_type: parent_type parent_type: parent_type
language: language language: parent_sym.language
} }
is_public: is_pub is_public: is_pub
}) })
comments = p.eat_line_end_comments() if idx == -1 {
if prepend_mod_name == parent_name { p.error_with_pos('cannot register alias `$name`, another type with this name exists', decl_pos.extend(type_alias_pos))
return ast.AliasTypeDecl{}
}
if idx == pidx {
p.error_with_pos('a type alias can not refer to itself: $name', decl_pos.extend(type_alias_pos)) p.error_with_pos('a type alias can not refer to itself: $name', decl_pos.extend(type_alias_pos))
return ast.AliasTypeDecl{} return ast.AliasTypeDecl{}
} }
comments = p.eat_line_end_comments()
return ast.AliasTypeDecl{ return ast.AliasTypeDecl{
name: name name: name
is_pub: is_pub is_pub: is_pub

View File

@ -0,0 +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
1 | struct Foo{}
2 |
3 | type Foo = Foo
| ~~~~~~~~~~~~~~
4 |
5 | fn main() {

View File

@ -0,0 +1,7 @@
struct Foo{}
type Foo = Foo
fn main() {
}

View File

@ -1,7 +1,5 @@
vlib/v/parser/tests/type_alias_same_type_err.vv:3:1: error: a type alias can not refer to itself: Foo vlib/v/parser/tests/type_alias_same_type_err.vv:1:1: error: a type alias can not refer to itself: Foo
1 | struct Foo{} 1 | type Foo = Foo
2 |
3 | type Foo = Foo
| ~~~~~~~~~~~~~~ | ~~~~~~~~~~~~~~
4 | 2 |
5 | fn main() { 3 | fn main() {

View File

@ -1,5 +1,3 @@
struct Foo{}
type Foo = Foo type Foo = Foo
fn main() { fn main() {

View File

@ -309,11 +309,11 @@ pub const (
none_type_idx = 17 none_type_idx = 17
string_type_idx = 18 string_type_idx = 18
ustring_type_idx = 19 ustring_type_idx = 19
array_type_idx = 20 rune_type_idx = 20
map_type_idx = 21 array_type_idx = 21
chan_type_idx = 22 map_type_idx = 22
sizet_type_idx = 23 chan_type_idx = 23
rune_type_idx = 24 sizet_type_idx = 24
any_type_idx = 25 any_type_idx = 25
any_flt_type_idx = 26 any_flt_type_idx = 26
any_int_type_idx = 27 any_int_type_idx = 27
@ -353,10 +353,10 @@ pub const (
none_type = new_type(none_type_idx) none_type = new_type(none_type_idx)
string_type = new_type(string_type_idx) string_type = new_type(string_type_idx)
ustring_type = new_type(ustring_type_idx) ustring_type = new_type(ustring_type_idx)
rune_type = new_type(rune_type_idx)
array_type = new_type(array_type_idx) array_type = new_type(array_type_idx)
map_type = new_type(map_type_idx) map_type = new_type(map_type_idx)
chan_type = new_type(chan_type_idx) chan_type = new_type(chan_type_idx)
rune_type = new_type(rune_type_idx)
any_type = new_type(any_type_idx) any_type = new_type(any_type_idx)
any_flt_type = new_type(any_flt_type_idx) any_flt_type = new_type(any_flt_type_idx)
any_int_type = new_type(any_int_type_idx) any_int_type = new_type(any_int_type_idx)
@ -517,11 +517,11 @@ pub fn (mut t Table) register_builtin_type_symbols() {
t.register_type_symbol(kind: .none_, name: 'none', cname: 'none', mod: 'builtin') t.register_type_symbol(kind: .none_, name: 'none', cname: 'none', mod: 'builtin')
t.register_type_symbol(kind: .string, name: 'string', cname: 'string', mod: 'builtin') t.register_type_symbol(kind: .string, name: 'string', cname: 'string', mod: 'builtin')
t.register_type_symbol(kind: .ustring, name: 'ustring', cname: 'ustring', mod: 'builtin') t.register_type_symbol(kind: .ustring, name: 'ustring', cname: 'ustring', mod: 'builtin')
t.register_type_symbol(kind: .rune, name: 'rune', cname: 'rune', mod: 'builtin')
t.register_type_symbol(kind: .array, name: 'array', cname: 'array', mod: 'builtin') t.register_type_symbol(kind: .array, name: 'array', cname: 'array', mod: 'builtin')
t.register_type_symbol(kind: .map, name: 'map', cname: 'map', mod: 'builtin') t.register_type_symbol(kind: .map, name: 'map', cname: 'map', mod: 'builtin')
t.register_type_symbol(kind: .chan, name: 'chan', cname: 'chan', mod: 'builtin') t.register_type_symbol(kind: .chan, name: 'chan', cname: 'chan', mod: 'builtin')
t.register_type_symbol(kind: .size_t, name: 'size_t', cname: 'size_t', mod: 'builtin') t.register_type_symbol(kind: .size_t, name: 'size_t', cname: 'size_t', mod: 'builtin')
t.register_type_symbol(kind: .rune, name: 'rune', cname: 'rune', mod: 'builtin')
t.register_type_symbol(kind: .any, name: 'any', cname: 'any', mod: 'builtin') t.register_type_symbol(kind: .any, name: 'any', cname: 'any', mod: 'builtin')
t.register_type_symbol( t.register_type_symbol(
kind: .any_float kind: .any_float