From d553071e6554567b7ed1126a86e9024dcddaed0e Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 20 Jan 2022 19:03:19 +0800 Subject: [PATCH] parser, checker: correct error message for a fixed array size using a non constant (fix #13219) (#13228) --- vlib/v/checker/containers.v | 2 +- vlib/v/parser/parse_type.v | 3 ++- .../tests/fixed_array_size_using_non_constant_err.out | 6 ++++++ .../tests/fixed_array_size_using_non_constant_err.vv | 10 ++++++++++ 4 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 vlib/v/parser/tests/fixed_array_size_using_non_constant_err.out create mode 100644 vlib/v/parser/tests/fixed_array_size_using_non_constant_err.vv diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index 2d95231c08..0946fdf82d 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -163,7 +163,7 @@ pub fn (mut c Checker) array_init(mut node ast.ArrayInit) ast.Type { } } else { - c.error('expecting `int` for fixed size', node.pos) + c.error('fixed array size cannot use non-constant value', init_expr.position()) } } if fixed_size <= 0 { diff --git a/vlib/v/parser/parse_type.v b/vlib/v/parser/parse_type.v index a0331e7c60..cd487b0d75 100644 --- a/vlib/v/parser/parse_type.v +++ b/vlib/v/parser/parse_type.v @@ -59,7 +59,8 @@ pub fn (mut p Parser) parse_array_type(expecting token.Kind) ast.Type { } } else { - p.error('expecting `int` for fixed size') + p.error_with_pos('fixed array size cannot use non-constant value', + size_expr.position()) } } } diff --git a/vlib/v/parser/tests/fixed_array_size_using_non_constant_err.out b/vlib/v/parser/tests/fixed_array_size_using_non_constant_err.out new file mode 100644 index 0000000000..e84198a0d2 --- /dev/null +++ b/vlib/v/parser/tests/fixed_array_size_using_non_constant_err.out @@ -0,0 +1,6 @@ +vlib/v/parser/tests/fixed_array_size_using_non_constant_err.vv:9:25: error: fixed array size cannot use non-constant value + 7 | println(typeof(t.len).name) + 8 | + 9 | mut score := [t.len][t.len]int{} + | ~~~ + 10 | } diff --git a/vlib/v/parser/tests/fixed_array_size_using_non_constant_err.vv b/vlib/v/parser/tests/fixed_array_size_using_non_constant_err.vv new file mode 100644 index 0000000000..054579a03c --- /dev/null +++ b/vlib/v/parser/tests/fixed_array_size_using_non_constant_err.vv @@ -0,0 +1,10 @@ +module main + +fn main() { + mut t := [0, 0] + + println(t.len) + println(typeof(t.len).name) + + mut score := [t.len][t.len]int{} +}