From 58e150df1258d69dc07340aed7ba26d7bb9b5c27 Mon Sep 17 00:00:00 2001 From: Swastik Baranwal Date: Mon, 28 Nov 2022 14:07:12 +0530 Subject: [PATCH] parser: add a better error msg for using `...`, instead of `..` in `for a in 1...10 {` (#16547) --- vlib/v/checker/tests/for_loop_range_inclusive_err.out | 5 +++++ vlib/v/checker/tests/for_loop_range_inclusive_err.vv | 3 +++ vlib/v/parser/for.v | 5 ++++- 3 files changed, 12 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/for_loop_range_inclusive_err.out create mode 100644 vlib/v/checker/tests/for_loop_range_inclusive_err.vv diff --git a/vlib/v/checker/tests/for_loop_range_inclusive_err.out b/vlib/v/checker/tests/for_loop_range_inclusive_err.out new file mode 100644 index 0000000000..856021b471 --- /dev/null +++ b/vlib/v/checker/tests/for_loop_range_inclusive_err.out @@ -0,0 +1,5 @@ +vlib/v/checker/tests/for_loop_range_inclusive_err.vv:1:11: error: for loop only supports exclusive (`..`) ranges, not inclusive (`...`) + 1 | for a in 1...10 { + | ~~~ + 2 | println(a) + 3 | } diff --git a/vlib/v/checker/tests/for_loop_range_inclusive_err.vv b/vlib/v/checker/tests/for_loop_range_inclusive_err.vv new file mode 100644 index 0000000000..c89c6e8fb7 --- /dev/null +++ b/vlib/v/checker/tests/for_loop_range_inclusive_err.vv @@ -0,0 +1,3 @@ +for a in 1...10 { + println(a) +} diff --git a/vlib/v/parser/for.v b/vlib/v/parser/for.v index bb995da7a3..843cbed200 100644 --- a/vlib/v/parser/for.v +++ b/vlib/v/parser/for.v @@ -150,7 +150,10 @@ fn (mut p Parser) for_stmt() ast.Stmt { // TODO use RangeExpr mut high_expr := ast.empty_expr mut is_range := false - if p.tok.kind == .dotdot { + if p.tok.kind == .ellipsis { + p.error_with_pos('for loop only supports exclusive (`..`) ranges, not inclusive (`...`)', + p.tok.pos()) + } else if p.tok.kind == .dotdot { is_range = true p.next() high_expr = p.expr(0)