From 09630dd0bc33bbc059e014c63f624bcc713441c4 Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 28 Jun 2022 13:29:23 +0800 Subject: [PATCH] parser: check interface name using single letter capital (#14878) --- vlib/v/parser/struct.v | 5 +++++ vlib/v/parser/tests/interface_name_err.out | 5 +++++ vlib/v/parser/tests/interface_name_err.vv | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 vlib/v/parser/tests/interface_name_err.out create mode 100644 vlib/v/parser/tests/interface_name_err.vv diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index c12a4437f1..affd50a570 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -493,6 +493,11 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl { return ast.InterfaceDecl{} } modless_name := p.check_name() + if modless_name.len == 1 && modless_name[0].is_capital() { + p.error_with_pos('single letter capital names are reserved for generic template types.', + name_pos) + return ast.InterfaceDecl{} + } if modless_name == 'IError' && p.mod != 'builtin' { p.error_with_pos('cannot register interface `IError`, it is builtin interface type', name_pos) diff --git a/vlib/v/parser/tests/interface_name_err.out b/vlib/v/parser/tests/interface_name_err.out new file mode 100644 index 0000000000..34d5e71d9a --- /dev/null +++ b/vlib/v/parser/tests/interface_name_err.out @@ -0,0 +1,5 @@ +vlib/v/parser/tests/interface_name_err.vv:1:11: error: single letter capital names are reserved for generic template types. + 1 | interface A { + | ^ + 2 | a int + 3 | b() int diff --git a/vlib/v/parser/tests/interface_name_err.vv b/vlib/v/parser/tests/interface_name_err.vv new file mode 100644 index 0000000000..f67cfdac85 --- /dev/null +++ b/vlib/v/parser/tests/interface_name_err.vv @@ -0,0 +1,19 @@ +interface A { + a int + b() int +} + +struct Ba { + a int +} + +fn (b &Ba) b() int { + return b.a +} + +fn main() { + a := &Ba{ + a: 1 + } + b := A(a) +}