Skip to content

Commit d1ed866

Browse files
authored
parser: check invalid struct name in struct_init() (fix #26030) (#26093)
1 parent 57cecdb commit d1ed866

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
vlib/v/checker/tests/wrong_struct_init_err.vv:4:33: error: struct name must begin with capital letter
2+
2 |
3+
3 | import net.http
4+
4 | _ := http.Response{header: http.new_header{} }
5+
| ~~~~~~~~~~
6+
5 |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module main
2+
3+
import net.http
4+
_ := http.Response{header: http.new_header{} }
5+

vlib/v/parser/struct.v

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,13 @@ fn (mut p Parser) struct_init(typ_str string, kind ast.StructInitKind, is_option
553553
first_pos := (if kind == .short_syntax && p.prev_tok.kind == .lcbr { p.prev_tok } else { p.tok }).pos()
554554
p.init_generic_types = []ast.Type{}
555555
mut typ := if kind == .short_syntax { ast.void_type } else { p.parse_type() }
556+
sym := p.table.sym(typ)
557+
struct_name := sym.name.all_after_last('.')
558+
if sym.kind == .placeholder && struct_name.len > 0 && !struct_name[0].is_capital()
559+
&& !sym.name.starts_with('C.') {
560+
p.error_with_pos('struct name must begin with capital letter', first_pos)
561+
return ast.StructInit{}
562+
}
556563
struct_init_generic_types := p.init_generic_types.clone()
557564
if is_option {
558565
typ = typ.set_flag(.option)

0 commit comments

Comments
 (0)