Skip to content

Commit

Permalink
checker: disallow enum initalization (#17361)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Feb 19, 2023
1 parent 79b2c34 commit e9a3817
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 7 deletions.
2 changes: 1 addition & 1 deletion vlib/os/notify/backend_linux.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ fn (mut en EpollNotifier) close() ! {
// event_mask_to_flag is a helper function that converts a bitmask
// returned by epoll_wait to FdEventType
fn event_mask_to_flag(mask u32) FdEventType {
mut flags := FdEventType{}
mut flags := FdEventType.read

if mask & notify.epoll_read != 0 {
flags.set(.read)
Expand Down
4 changes: 2 additions & 2 deletions vlib/term/ui/input_windows.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn (mut ctx Context) parse_events() {
else { unsafe { KeyCode(ascii) } }
}

mut modifiers := Modifiers{}
mut modifiers := Modifiers.ctrl
if e.dwControlKeyState & (0x1 | 0x2) != 0 {
modifiers.set(.alt)
}
Expand Down Expand Up @@ -204,7 +204,7 @@ fn (mut ctx Context) parse_events() {
}
x := e.dwMousePosition.X + 1
y := int(e.dwMousePosition.Y) - sb_info.srWindow.Top + 1
mut modifiers := Modifiers{}
mut modifiers := Modifiers.ctrl
if e.dwControlKeyState & (0x1 | 0x2) != 0 {
modifiers.set(.alt)
}
Expand Down
4 changes: 2 additions & 2 deletions vlib/term/ui/termios_nix.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ fn escape_sequence(buf_ string) (&Event, int) {
lo := typ & 0b00011
hi := typ & 0b11100

mut modifiers := Modifiers{}
mut modifiers := Modifiers.ctrl
if hi & 4 != 0 {
modifiers.set(.shift)
}
Expand Down Expand Up @@ -503,7 +503,7 @@ fn escape_sequence(buf_ string) (&Event, int) {
// ----------------------------

mut code := KeyCode.null
mut modifiers := Modifiers{}
mut modifiers := Modifiers.ctrl
match buf {
'[A', 'OA' { code = .up }
'[B', 'OB' { code = .down }
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,9 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
&& type_sym.kind != .placeholder {
c.error('cannot initialize builtin type `${type_sym.name}`', node.pos)
}
if type_sym.kind == .enum_ && !c.pref.translated && !c.file.is_translated {
c.error('cannot initialize enums', node.pos)
}
}
if type_sym.kind == .sum_type && node.fields.len == 1 {
sexpr := node.fields[0].expr.str()
Expand Down
7 changes: 7 additions & 0 deletions vlib/v/checker/tests/enum_init_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
vlib/v/checker/tests/enum_init_err.vv:8:7: error: cannot initialize enums
6 |
7 | fn main() {
8 | a := Hello{}
| ~~~~~~~
9 | println(a)
10 | }
10 changes: 10 additions & 0 deletions vlib/v/checker/tests/enum_init_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
enum Hello {
aaa
bbb
ccc
}

fn main() {
a := Hello{}
println(a)
}
2 changes: 1 addition & 1 deletion vlib/v/gen/c/str_intp.v
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn (mut g Gen) str_format(node ast.StringInterLiteral, i int) (u64, string) {
}
mut remove_tail_zeros := false
fspec := node.fmts[i]
mut fmt_type := StrIntpType{}
mut fmt_type := StrIntpType.si_no_str
g.write('/*${fspec} ${sym}*/')
// upper cases
if (fspec - `A`) <= (`Z` - `A`) {
Expand Down
2 changes: 1 addition & 1 deletion vlib/v/tests/enum_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ enum FileType {
}

fn test_enum_instance() {
mut filetype := FileType{}
mut filetype := FileType.unknown
eprintln(filetype)
s := 'x ${filetype} z'
assert s == 'x unknown z'
Expand Down

0 comments on commit e9a3817

Please sign in to comment.