From b7f83e2f50673658ec27971971f3f8af355439f6 Mon Sep 17 00:00:00 2001 From: StunxFS <56417208+StunxFS@users.noreply.github.com> Date: Sun, 3 Jan 2021 10:56:15 -0400 Subject: [PATCH] checker: more errors for illegal referencing (#7755) --- vlib/v/checker/checker.v | 9 +++-- vlib/v/checker/tests/prefix_err.out | 63 ++++++++++++++++------------- vlib/v/checker/tests/prefix_err.vv | 2 + 3 files changed, 43 insertions(+), 31 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 8218d9fb32..6f22880ab6 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4459,10 +4459,13 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) table.Type { // TODO: testing ref/deref strategy if node.op == .amp && !right_type.is_ptr() { match node.right { - ast.IntegerLiteral { c.error('cannot take the address of an int', node.pos) } - ast.BoolLiteral { c.error('cannot take the address of a bool', node.pos) } - ast.StringLiteral, ast.StringInterLiteral { c.error('cannot take the address of a string', + ast.IntegerLiteral { c.error('cannot take the address of an int literal', node.pos) } + ast.BoolLiteral { c.error('cannot take the address of a bool literal', node.pos) } + ast.StringLiteral, ast.StringInterLiteral { c.error('cannot take the address of a string literal', + node.pos) } + ast.FloatLiteral { c.error('cannot take the address of a float literal', node.pos) } + ast.CharLiteral { c.error('cannot take the address of a char literal', node.pos) } else {} } if mut node.right is ast.IndexExpr { diff --git a/vlib/v/checker/tests/prefix_err.out b/vlib/v/checker/tests/prefix_err.out index 14a8d6dfc5..04e0d0b4d0 100644 --- a/vlib/v/checker/tests/prefix_err.out +++ b/vlib/v/checker/tests/prefix_err.out @@ -1,50 +1,57 @@ -vlib/v/checker/tests/prefix_err.vv:1:6: error: cannot take the address of a bool +vlib/v/checker/tests/prefix_err.vv:1:6: error: cannot take the address of a bool literal 1 | b := &true | ^ 2 | _ := &10 3 | _ := &"Hi" -vlib/v/checker/tests/prefix_err.vv:2:6: error: cannot take the address of an int +vlib/v/checker/tests/prefix_err.vv:2:6: error: cannot take the address of an int literal 1 | b := &true 2 | _ := &10 | ^ 3 | _ := &"Hi" 4 | _ := &"${b}" -vlib/v/checker/tests/prefix_err.vv:3:6: error: cannot take the address of a string +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" | ^ 4 | _ := &"${b}" - 5 | -vlib/v/checker/tests/prefix_err.vv:4:6: error: cannot take the address of a string + 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}" | ^ - 5 | - 6 | a := 1 -vlib/v/checker/tests/prefix_err.vv:7:5: error: invalid indirect of `int` - 5 | - 6 | a := 1 - 7 | _ = *a + 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` + | ^ + 6 | _ := 12.3 + 7 | +vlib/v/checker/tests/prefix_err.vv:9:5: error: invalid indirect of `int` + 7 | + 8 | a := 1 + 9 | _ = *a | ^ - 8 | a <- 4 - 9 | -vlib/v/checker/tests/prefix_err.vv:8:1: error: cannot push on non-channel `int` - 6 | a := 1 - 7 | _ = *a - 8 | a <- 4 + 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 | ^ - 9 | - 10 | _ = ~true -vlib/v/checker/tests/prefix_err.vv:10:5: error: operator ~ only defined on int types - 8 | a <- 4 - 9 | - 10 | _ = ~true + 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 | ^ - 11 | _ = !4 -vlib/v/checker/tests/prefix_err.vv:11:5: error: ! operator can only be used with bool types - 9 | - 10 | _ = ~true - 11 | _ = !4 + 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 | ^ diff --git a/vlib/v/checker/tests/prefix_err.vv b/vlib/v/checker/tests/prefix_err.vv index c75d5550b7..2be959ef80 100644 --- a/vlib/v/checker/tests/prefix_err.vv +++ b/vlib/v/checker/tests/prefix_err.vv @@ -2,6 +2,8 @@ b := &true _ := &10 _ := &"Hi" _ := &"${b}" +_ := &`c` +_ := 12.3 a := 1 _ = *a