parser: add error for self aliasing (#7347)
parent
3fdff93c3f
commit
52f908839e
|
@ -2015,6 +2015,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
first_type := p.parse_type() // need to parse the first type before we can check if it's `type A = X | Y`
|
first_type := p.parse_type() // need to parse the first type before we can check if it's `type A = X | Y`
|
||||||
|
type_alias_pos := p.tok.position()
|
||||||
if p.tok.kind == .pipe {
|
if p.tok.kind == .pipe {
|
||||||
mut type_end_pos := p.prev_tok.position()
|
mut type_end_pos := p.prev_tok.position()
|
||||||
type_pos = type_pos.extend(type_end_pos)
|
type_pos = type_pos.extend(type_end_pos)
|
||||||
|
@ -2061,7 +2062,7 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
||||||
comments: comments
|
comments: comments
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 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_name := p.table.get_type_symbol(parent_type).name
|
||||||
pid := parent_type.idx()
|
pid := parent_type.idx()
|
||||||
|
@ -2087,6 +2088,10 @@ fn (mut p Parser) type_decl() ast.TypeDecl {
|
||||||
is_public: is_pub
|
is_public: is_pub
|
||||||
})
|
})
|
||||||
comments = p.eat_line_end_comments()
|
comments = p.eat_line_end_comments()
|
||||||
|
if prepend_mod_name == parent_name {
|
||||||
|
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{
|
return ast.AliasTypeDecl{
|
||||||
name: name
|
name: name
|
||||||
is_pub: is_pub
|
is_pub: is_pub
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
vlib/v/parser/tests/type_alias_same_type_err.vv:3:1: error: a type alias can not refer to itself: Foo
|
||||||
|
1 | struct Foo{}
|
||||||
|
2 |
|
||||||
|
3 | type Foo = Foo
|
||||||
|
| ~~~~~~~~~~~~~~
|
||||||
|
4 |
|
||||||
|
5 | fn main() {
|
|
@ -0,0 +1,7 @@
|
||||||
|
struct Foo{}
|
||||||
|
|
||||||
|
type Foo = Foo
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue