From 5fb3c0e3a8a5ff1742cf0f7240a87728fc55f0b8 Mon Sep 17 00:00:00 2001 From: joe-conigliaro Date: Mon, 30 Sep 2019 20:41:11 +1000 Subject: [PATCH] compiler: add error when embedding struct in itself --- compiler/parser.v | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/compiler/parser.v b/compiler/parser.v index 3415e76893..12422a62f4 100644 --- a/compiler/parser.v +++ b/compiler/parser.v @@ -722,6 +722,7 @@ fn (p mut Parser) struct_decl() { // p.next() // } // Check if reserved name + field_name_token_idx := p.cur_tok_index() field_name := if name != 'Option' { p.table.var_cgen_name(p.check_name()) } else { p.check_name() } // Check dups if field_name in names { @@ -744,6 +745,9 @@ fn (p mut Parser) struct_decl() { access_mod := if is_pub{AccessMod.public} else { AccessMod.private} p.fgen(' ') field_type := p.get_type() + if field_type == name { + p.error_with_token_index( 'cannot embed struct `$name` in itself (field `$field_name`)', field_name_token_idx) + } p.check_and_register_used_imported_type(field_type) is_atomic := p.tok == .key_atomic if is_atomic {