1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: disallow enum initalization (#17361)

This commit is contained in:
Swastik Baranwal 2023-02-20 02:52:07 +05:30 committed by GitHub
parent 79b2c34f97
commit e9a3817aed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 27 additions and 7 deletions

View File

@ -142,7 +142,7 @@ fn (mut en EpollNotifier) close() ! {
// event_mask_to_flag is a helper function that converts a bitmask // event_mask_to_flag is a helper function that converts a bitmask
// returned by epoll_wait to FdEventType // returned by epoll_wait to FdEventType
fn event_mask_to_flag(mask u32) FdEventType { fn event_mask_to_flag(mask u32) FdEventType {
mut flags := FdEventType{} mut flags := FdEventType.read
if mask & notify.epoll_read != 0 { if mask & notify.epoll_read != 0 {
flags.set(.read) flags.set(.read)

View File

@ -174,7 +174,7 @@ fn (mut ctx Context) parse_events() {
else { unsafe { KeyCode(ascii) } } else { unsafe { KeyCode(ascii) } }
} }
mut modifiers := Modifiers{} mut modifiers := Modifiers.ctrl
if e.dwControlKeyState & (0x1 | 0x2) != 0 { if e.dwControlKeyState & (0x1 | 0x2) != 0 {
modifiers.set(.alt) modifiers.set(.alt)
} }
@ -204,7 +204,7 @@ fn (mut ctx Context) parse_events() {
} }
x := e.dwMousePosition.X + 1 x := e.dwMousePosition.X + 1
y := int(e.dwMousePosition.Y) - sb_info.srWindow.Top + 1 y := int(e.dwMousePosition.Y) - sb_info.srWindow.Top + 1
mut modifiers := Modifiers{} mut modifiers := Modifiers.ctrl
if e.dwControlKeyState & (0x1 | 0x2) != 0 { if e.dwControlKeyState & (0x1 | 0x2) != 0 {
modifiers.set(.alt) modifiers.set(.alt)
} }

View File

@ -433,7 +433,7 @@ fn escape_sequence(buf_ string) (&Event, int) {
lo := typ & 0b00011 lo := typ & 0b00011
hi := typ & 0b11100 hi := typ & 0b11100
mut modifiers := Modifiers{} mut modifiers := Modifiers.ctrl
if hi & 4 != 0 { if hi & 4 != 0 {
modifiers.set(.shift) modifiers.set(.shift)
} }
@ -503,7 +503,7 @@ fn escape_sequence(buf_ string) (&Event, int) {
// ---------------------------- // ----------------------------
mut code := KeyCode.null mut code := KeyCode.null
mut modifiers := Modifiers{} mut modifiers := Modifiers.ctrl
match buf { match buf {
'[A', 'OA' { code = .up } '[A', 'OA' { code = .up }
'[B', 'OB' { code = .down } '[B', 'OB' { code = .down }

View File

@ -360,6 +360,9 @@ fn (mut c Checker) struct_init(mut node ast.StructInit, is_field_zero_struct_ini
&& type_sym.kind != .placeholder { && type_sym.kind != .placeholder {
c.error('cannot initialize builtin type `${type_sym.name}`', node.pos) 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 { if type_sym.kind == .sum_type && node.fields.len == 1 {
sexpr := node.fields[0].expr.str() sexpr := node.fields[0].expr.str()

View File

@ -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 | }

View File

@ -0,0 +1,10 @@
enum Hello {
aaa
bbb
ccc
}
fn main() {
a := Hello{}
println(a)
}

View File

@ -26,7 +26,7 @@ fn (mut g Gen) str_format(node ast.StringInterLiteral, i int) (u64, string) {
} }
mut remove_tail_zeros := false mut remove_tail_zeros := false
fspec := node.fmts[i] fspec := node.fmts[i]
mut fmt_type := StrIntpType{} mut fmt_type := StrIntpType.si_no_str
g.write('/*${fspec} ${sym}*/') g.write('/*${fspec} ${sym}*/')
// upper cases // upper cases
if (fspec - `A`) <= (`Z` - `A`) { if (fspec - `A`) <= (`Z` - `A`) {

View File

@ -142,7 +142,7 @@ enum FileType {
} }
fn test_enum_instance() { fn test_enum_instance() {
mut filetype := FileType{} mut filetype := FileType.unknown
eprintln(filetype) eprintln(filetype)
s := 'x ${filetype} z' s := 'x ${filetype} z'
assert s == 'x unknown z' assert s == 'x unknown z'