From 0383e47b3392b939cd5331fc9bf4d2d351be3b49 Mon Sep 17 00:00:00 2001 From: Swastik Date: Sat, 11 Nov 2023 23:13:10 +0530 Subject: [PATCH] checker: disallow `$for i in struct.values` and `$for i in enum.fields` --- vlib/v/checker/comptime.v | 8 ++++++++ vlib/v/checker/tests/for_comptime_enum_fields_err.out | 7 +++++++ vlib/v/checker/tests/for_comptime_enum_fields_err.vv | 8 ++++++++ vlib/v/checker/tests/for_comptime_struct_values_err.out | 7 +++++++ vlib/v/checker/tests/for_comptime_struct_values_err.vv | 7 +++++++ 5 files changed, 37 insertions(+) create mode 100644 vlib/v/checker/tests/for_comptime_enum_fields_err.out create mode 100644 vlib/v/checker/tests/for_comptime_enum_fields_err.vv create mode 100644 vlib/v/checker/tests/for_comptime_struct_values_err.out create mode 100644 vlib/v/checker/tests/for_comptime_struct_values_err.vv diff --git a/vlib/v/checker/comptime.v b/vlib/v/checker/comptime.v index fb1a98d1e4d58a..1908059307bc77 100644 --- a/vlib/v/checker/comptime.v +++ b/vlib/v/checker/comptime.v @@ -287,6 +287,10 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) { } c.comptime_for_field_var = '' c.inside_comptime_for_field = false + } else { + c.error('comptime field lookup supports only structs and interfaces, and ${sym.name} is neither', + node.typ_pos) + return } } else if node.kind == .values { if sym.kind == .enum_ { @@ -301,6 +305,10 @@ fn (mut c Checker) comptime_for(mut node ast.ComptimeFor) { c.comptime_fields_type[node.val_var] = node.typ c.stmts(mut node.stmts) } + } else { + c.error('comptime value lookup supports only enum, and ${sym.name} isn\'t', + node.typ_pos) + return } } else { c.stmts(mut node.stmts) diff --git a/vlib/v/checker/tests/for_comptime_enum_fields_err.out b/vlib/v/checker/tests/for_comptime_enum_fields_err.out new file mode 100644 index 00000000000000..7abc2bbbaf5099 --- /dev/null +++ b/vlib/v/checker/tests/for_comptime_enum_fields_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/for_comptime_enum_fields_err.vv:6:11: error: comptime field lookup supports only structs and interfaces currently, and Test is neither + 4 | } + 5 | + 6 | $for i in Test.fields { + | ~~~~ + 7 | println(i) + 8 | } diff --git a/vlib/v/checker/tests/for_comptime_enum_fields_err.vv b/vlib/v/checker/tests/for_comptime_enum_fields_err.vv new file mode 100644 index 00000000000000..07b90cf3b0e8d9 --- /dev/null +++ b/vlib/v/checker/tests/for_comptime_enum_fields_err.vv @@ -0,0 +1,8 @@ +enum Test { + one + two +} + +$for i in Test.fields { + println(i) +} diff --git a/vlib/v/checker/tests/for_comptime_struct_values_err.out b/vlib/v/checker/tests/for_comptime_struct_values_err.out new file mode 100644 index 00000000000000..e9fed3c1790bbc --- /dev/null +++ b/vlib/v/checker/tests/for_comptime_struct_values_err.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/for_comptime_struct_values_err.vv:5:11: error: comptime value lookup supports only enum, and Foo isn't + 3 | fn (_ Foo) hello() {} + 4 | + 5 | $for i in Foo.values { + | ~~~ + 6 | println(i) + 7 | } diff --git a/vlib/v/checker/tests/for_comptime_struct_values_err.vv b/vlib/v/checker/tests/for_comptime_struct_values_err.vv new file mode 100644 index 00000000000000..e41918b2c7a9df --- /dev/null +++ b/vlib/v/checker/tests/for_comptime_struct_values_err.vv @@ -0,0 +1,7 @@ +struct Foo{} + +fn (_ Foo) hello() {} + +$for i in Foo.values { + println(i) +}