From be3bd520f69ba67429aa82be38aa6f2e04328b1d Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 20 Apr 2020 06:21:16 +0800 Subject: [PATCH] checker: fix for in range type mismatch --- vlib/v/checker/checker.v | 10 ++++++++++ .../tests/inout/for_in_range_not_match_type.out | 6 ++++++ .../checker/tests/inout/for_in_range_not_match_type.vv | 5 +++++ .../v/checker/tests/inout/for_in_range_string_type.out | 6 ++++++ vlib/v/checker/tests/inout/for_in_range_string_type.vv | 5 +++++ 5 files changed, 32 insertions(+) create mode 100644 vlib/v/checker/tests/inout/for_in_range_not_match_type.out create mode 100644 vlib/v/checker/tests/inout/for_in_range_not_match_type.vv create mode 100644 vlib/v/checker/tests/inout/for_in_range_string_type.out create mode 100644 vlib/v/checker/tests/inout/for_in_range_string_type.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 1016e91b2a..0cc5e2f009 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1105,6 +1105,16 @@ fn (c mut Checker) stmt(node ast.Stmt) { c.in_for_count++ typ := c.expr(it.cond) if it.is_range { + high_type := c.expr(it.high) + if typ in table.integer_type_idxs && high_type !in table.integer_type_idxs { + c.error('range types do not match', it.cond.position()) + } else if typ in table.float_type_idxs || high_type in table.float_type_idxs { + c.error('range type can not be float', it.cond.position()) + } else if typ == table.bool_type_idx || high_type == table.bool_type_idx { + c.error('range type can not be bool', it.cond.position()) + } else if typ == table.string_type_idx || high_type == table.string_type_idx { + c.error('range type can not be string', it.cond.position()) + } c.expr(it.high) } else { mut scope := c.file.scope.innermost(it.pos.pos) diff --git a/vlib/v/checker/tests/inout/for_in_range_not_match_type.out b/vlib/v/checker/tests/inout/for_in_range_not_match_type.out new file mode 100644 index 0000000000..ea364b372f --- /dev/null +++ b/vlib/v/checker/tests/inout/for_in_range_not_match_type.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/inout/for_in_range_not_match_type.v:2:11: error: range types do not match + 1| fn main() { + 2| for i in 10..10.5 { + ~~ + 3| println(i) + 4| } \ No newline at end of file diff --git a/vlib/v/checker/tests/inout/for_in_range_not_match_type.vv b/vlib/v/checker/tests/inout/for_in_range_not_match_type.vv new file mode 100644 index 0000000000..8b158639f9 --- /dev/null +++ b/vlib/v/checker/tests/inout/for_in_range_not_match_type.vv @@ -0,0 +1,5 @@ +fn main() { + for i in 10..10.5 { + println(i) + } +} diff --git a/vlib/v/checker/tests/inout/for_in_range_string_type.out b/vlib/v/checker/tests/inout/for_in_range_string_type.out new file mode 100644 index 0000000000..f00ce32b10 --- /dev/null +++ b/vlib/v/checker/tests/inout/for_in_range_string_type.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/inout/for_in_range_string_type.v:2:11: error: range type can not be string + 1| fn main() { + 2| for i in 'a'..'b' { + ~~~ + 3| println(i) + 4| } \ No newline at end of file diff --git a/vlib/v/checker/tests/inout/for_in_range_string_type.vv b/vlib/v/checker/tests/inout/for_in_range_string_type.vv new file mode 100644 index 0000000000..a459398c1d --- /dev/null +++ b/vlib/v/checker/tests/inout/for_in_range_string_type.vv @@ -0,0 +1,5 @@ +fn main() { + for i in 'a'..'b' { + println(i) + } +}