From c904c9178d7b8ea62f380132acdecd401861ffae Mon Sep 17 00:00:00 2001 From: Joe Conigliaro Date: Sat, 13 Feb 2021 02:49:22 +1100 Subject: [PATCH] checker: support ParExpr for trying to take address errors & simplify messages --- vlib/v/checker/checker.v | 28 ++---- vlib/v/checker/tests/prefix_err.out | 134 ++++++++++++++++++---------- vlib/v/checker/tests/prefix_err.vv | 9 ++ 3 files changed, 104 insertions(+), 67 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 308ebb41af..78f38fa694 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -5125,25 +5125,15 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) table.Type { node.right_type = right_type // TODO: testing ref/deref strategy if node.op == .amp && !right_type.is_ptr() { - right_expr := node.right - match right_expr { - ast.BoolLiteral { - c.error('cannot take the address of a bool literal', node.pos) - } - ast.CallExpr { - c.error('cannot take the address of $node.right', node.pos) - } - ast.CharLiteral { - c.error('cannot take the address of a char literal', node.pos) - } - ast.FloatLiteral { - c.error('cannot take the address of a float literal', node.pos) - } - ast.IntegerLiteral { - c.error('cannot take the address of an int literal', node.pos) - } - ast.StringLiteral, ast.StringInterLiteral { - c.error('cannot take the address of a string literal', node.pos) + mut expr := node.right + // if ParExpr get the innermost expr + for mut expr is ast.ParExpr { + expr = expr.expr + } + match expr { + ast.BoolLiteral, ast.CallExpr, ast.CharLiteral, ast.FloatLiteral, ast.IntegerLiteral, + ast.InfixExpr, ast.StringLiteral, ast.StringInterLiteral { + c.error('cannot take the address of $expr', node.pos) } else {} } diff --git a/vlib/v/checker/tests/prefix_err.out b/vlib/v/checker/tests/prefix_err.out index 04e0d0b4d0..58f32cc9b2 100644 --- a/vlib/v/checker/tests/prefix_err.out +++ b/vlib/v/checker/tests/prefix_err.out @@ -1,57 +1,95 @@ -vlib/v/checker/tests/prefix_err.vv:1:6: error: cannot take the address of a bool literal - 1 | b := &true +vlib/v/checker/tests/prefix_err.vv:5:6: error: cannot take the address of true + 3 | } + 4 | + 5 | b := &true | ^ - 2 | _ := &10 - 3 | _ := &"Hi" -vlib/v/checker/tests/prefix_err.vv:2:6: error: cannot take the address of an int literal - 1 | b := &true - 2 | _ := &10 + 6 | _ := &get() + 7 | _ := &(get()) +vlib/v/checker/tests/prefix_err.vv:6:6: error: cannot take the address of get() + 4 | + 5 | b := &true + 6 | _ := &get() | ^ - 3 | _ := &"Hi" - 4 | _ := &"${b}" -vlib/v/checker/tests/prefix_err.vv:3:6: error: cannot take the address of a string literal - 1 | b := &true - 2 | _ := &10 - 3 | _ := &"Hi" + 7 | _ := &(get()) + 8 | _ := &(get() + 1) +vlib/v/checker/tests/prefix_err.vv:7:6: error: cannot take the address of get() + 5 | b := &true + 6 | _ := &get() + 7 | _ := &(get()) | ^ - 4 | _ := &"${b}" - 5 | _ := &`c` -vlib/v/checker/tests/prefix_err.vv:4:6: error: cannot take the address of a string literal - 2 | _ := &10 - 3 | _ := &"Hi" - 4 | _ := &"${b}" + 8 | _ := &(get() + 1) + 9 | _ := &10 +vlib/v/checker/tests/prefix_err.vv:8:6: error: cannot take the address of get() + 1 + 6 | _ := &get() + 7 | _ := &(get()) + 8 | _ := &(get() + 1) | ^ - 5 | _ := &`c` - 6 | _ := 12.3 -vlib/v/checker/tests/prefix_err.vv:5:6: error: cannot take the address of a char literal - 3 | _ := &"Hi" - 4 | _ := &"${b}" - 5 | _ := &`c` + 9 | _ := &10 + 10 | _ := &"Hi" +vlib/v/checker/tests/prefix_err.vv:9:6: error: cannot take the address of 10 + 7 | _ := &(get()) + 8 | _ := &(get() + 1) + 9 | _ := &10 | ^ - 6 | _ := 12.3 - 7 | -vlib/v/checker/tests/prefix_err.vv:9:5: error: invalid indirect of `int` - 7 | - 8 | a := 1 - 9 | _ = *a + 10 | _ := &"Hi" + 11 | _ := &"${b}" +vlib/v/checker/tests/prefix_err.vv:10:6: error: cannot take the address of "Hi" + 8 | _ := &(get() + 1) + 9 | _ := &10 + 10 | _ := &"Hi" + | ^ + 11 | _ := &"${b}" + 12 | _ := &`c` +vlib/v/checker/tests/prefix_err.vv:11:6: error: cannot take the address of '$b' + 9 | _ := &10 + 10 | _ := &"Hi" + 11 | _ := &"${b}" + | ^ + 12 | _ := &`c` + 13 | _ := &1.2 +vlib/v/checker/tests/prefix_err.vv:12:6: error: cannot take the address of `c` + 10 | _ := &"Hi" + 11 | _ := &"${b}" + 12 | _ := &`c` + | ^ + 13 | _ := &1.2 + 14 | _ := &(1 + 2) +vlib/v/checker/tests/prefix_err.vv:13:6: error: cannot take the address of 1.2 + 11 | _ := &"${b}" + 12 | _ := &`c` + 13 | _ := &1.2 + | ^ + 14 | _ := &(1 + 2) + 15 | _ := 12.3 +vlib/v/checker/tests/prefix_err.vv:14:6: error: cannot take the address of 1 + 2 + 12 | _ := &`c` + 13 | _ := &1.2 + 14 | _ := &(1 + 2) + | ^ + 15 | _ := 12.3 + 16 | +vlib/v/checker/tests/prefix_err.vv:18:5: error: invalid indirect of `int` + 16 | + 17 | a := 1 + 18 | _ = *a | ^ - 10 | a <- 4 - 11 | -vlib/v/checker/tests/prefix_err.vv:10:1: error: cannot push on non-channel `int` - 8 | a := 1 - 9 | _ = *a - 10 | a <- 4 + 19 | a <- 4 + 20 | +vlib/v/checker/tests/prefix_err.vv:19:1: error: cannot push on non-channel `int` + 17 | a := 1 + 18 | _ = *a + 19 | a <- 4 | ^ - 11 | - 12 | _ = ~true -vlib/v/checker/tests/prefix_err.vv:12:5: error: operator ~ only defined on int types - 10 | a <- 4 - 11 | - 12 | _ = ~true + 20 | + 21 | _ = ~true +vlib/v/checker/tests/prefix_err.vv:21:5: error: operator ~ only defined on int types + 19 | a <- 4 + 20 | + 21 | _ = ~true | ^ - 13 | _ = !4 -vlib/v/checker/tests/prefix_err.vv:13:5: error: ! operator can only be used with bool types - 11 | - 12 | _ = ~true - 13 | _ = !4 + 22 | _ = !4 +vlib/v/checker/tests/prefix_err.vv:22:5: error: ! operator can only be used with bool types + 20 | + 21 | _ = ~true + 22 | _ = !4 | ^ diff --git a/vlib/v/checker/tests/prefix_err.vv b/vlib/v/checker/tests/prefix_err.vv index 2be959ef80..6472a0b734 100644 --- a/vlib/v/checker/tests/prefix_err.vv +++ b/vlib/v/checker/tests/prefix_err.vv @@ -1,8 +1,17 @@ +fn get() int { + return 1 +} + b := &true +_ := &get() +_ := &(get()) +_ := &(get() + 1) _ := &10 _ := &"Hi" _ := &"${b}" _ := &`c` +_ := &1.2 +_ := &(1 + 2) _ := 12.3 a := 1