From 2785a5bf65246c8dcdb3cf7aa74cb053e1e6a326 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 15 Jun 2020 13:10:45 +0800 Subject: [PATCH] checker: check return_duplicate_with_none (closes #5363) --- vlib/v/ast/ast.v | 6 +++++- vlib/v/checker/checker.v | 2 +- .../v/checker/tests/return_duplicate_with_none_err_a.out | 7 +++++++ vlib/v/checker/tests/return_duplicate_with_none_err_a.vv | 9 +++++++++ .../v/checker/tests/return_duplicate_with_none_err_b.out | 7 +++++++ vlib/v/checker/tests/return_duplicate_with_none_err_b.vv | 9 +++++++++ vlib/v/parser/pratt.v | 5 ++++- 7 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 vlib/v/checker/tests/return_duplicate_with_none_err_a.out create mode 100644 vlib/v/checker/tests/return_duplicate_with_none_err_a.vv create mode 100644 vlib/v/checker/tests/return_duplicate_with_none_err_b.out create mode 100644 vlib/v/checker/tests/return_duplicate_with_none_err_b.vv diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 8ae1bc4966..abd456400d 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -798,7 +798,8 @@ pub mut: pub struct None { pub: - foo int // todo + pos token.Position + foo int // todo } [inline] @@ -874,6 +875,9 @@ pub fn (expr Expr) position() token.Position { MatchExpr { return it.pos } + None { + return it.pos + } PostfixExpr { return it.pos } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 9434dd9fd4..4db86be2e4 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1793,7 +1793,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) { c.stmt(stmt) } if unreachable.line_nr >= 0 { - c.warn('unreachable code', unreachable) + c.error('unreachable code', unreachable) } c.scope_returns = false c.expected_type = table.void_type diff --git a/vlib/v/checker/tests/return_duplicate_with_none_err_a.out b/vlib/v/checker/tests/return_duplicate_with_none_err_a.out new file mode 100644 index 0000000000..5907a63114 --- /dev/null +++ b/vlib/v/checker/tests/return_duplicate_with_none_err_a.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/return_duplicate_with_none_err_a.v:3:2: error: unreachable code + 1 | fn f() ?bool { + 2 | return true + 3 | return none + | ~~~~~~~~~~~ + 4 | } + 5 | fn main() { diff --git a/vlib/v/checker/tests/return_duplicate_with_none_err_a.vv b/vlib/v/checker/tests/return_duplicate_with_none_err_a.vv new file mode 100644 index 0000000000..c356eb89fa --- /dev/null +++ b/vlib/v/checker/tests/return_duplicate_with_none_err_a.vv @@ -0,0 +1,9 @@ +fn f() ?bool { + return true + return none +} +fn main() { + f() or { + println('fail') + } +} diff --git a/vlib/v/checker/tests/return_duplicate_with_none_err_b.out b/vlib/v/checker/tests/return_duplicate_with_none_err_b.out new file mode 100644 index 0000000000..c3f15574c9 --- /dev/null +++ b/vlib/v/checker/tests/return_duplicate_with_none_err_b.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/return_duplicate_with_none_err_b.v:3:2: error: unreachable code + 1 | fn f() ?bool { + 2 | return none + 3 | return true + | ~~~~~~~~~~~ + 4 | } + 5 | fn main() { diff --git a/vlib/v/checker/tests/return_duplicate_with_none_err_b.vv b/vlib/v/checker/tests/return_duplicate_with_none_err_b.vv new file mode 100644 index 0000000000..aaf39a1525 --- /dev/null +++ b/vlib/v/checker/tests/return_duplicate_with_none_err_b.vv @@ -0,0 +1,9 @@ +fn f() ?bool { + return none + return true +} +fn main() { + f() or { + println('fail') + } +} diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index e9a7e0eb63..e43d9aa6a5 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -73,8 +73,11 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { } } .key_none { + pos := p.tok.position() p.next() - node = ast.None{} + node = ast.None{ + pos: pos + } } .key_sizeof { p.next() // sizeof