From 13cd7e88ef78277d4ae9d3e79af40db4c799501d Mon Sep 17 00:00:00 2001 From: StunxFS <56417208+StunxFS@users.noreply.github.com> Date: Wed, 30 Dec 2020 12:21:15 -0400 Subject: [PATCH] checker: error taking the address of a boolean literal (#7716) --- vlib/v/checker/checker.v | 11 ++--- vlib/v/checker/tests/prefix_err.out | 66 ++++++++++++++++++++--------- vlib/v/checker/tests/prefix_err.vv | 5 +++ 3 files changed, 57 insertions(+), 25 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 372f345363..9257b0939e 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -4367,11 +4367,12 @@ 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() { - if node.right is ast.IntegerLiteral { - c.error('cannot take the address of an int', node.pos) - } - if node.right is ast.StringLiteral || node.right is ast.StringInterLiteral { - c.error('cannot take the address of a string', node.pos) + 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', + node.pos) } + else {} } if mut node.right is ast.IndexExpr { typ_sym := c.table.get_type_symbol(node.right.left_type) diff --git a/vlib/v/checker/tests/prefix_err.out b/vlib/v/checker/tests/prefix_err.out index c82b31df5a..14a8d6dfc5 100644 --- a/vlib/v/checker/tests/prefix_err.out +++ b/vlib/v/checker/tests/prefix_err.out @@ -1,24 +1,50 @@ -vlib/v/checker/tests/prefix_err.vv:2:5: error: invalid indirect of `int` - 1 | a := 1 - 2 | _ = *a +vlib/v/checker/tests/prefix_err.vv:1:6: error: cannot take the address of a bool + 1 | b := &true + | ^ + 2 | _ := &10 + 3 | _ := &"Hi" +vlib/v/checker/tests/prefix_err.vv:2:6: error: cannot take the address of an int + 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 + 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 + 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 | ^ - 3 | a <- 4 - 4 | -vlib/v/checker/tests/prefix_err.vv:3:1: error: cannot push on non-channel `int` - 1 | a := 1 - 2 | _ = *a - 3 | a <- 4 + 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 | ^ - 4 | - 5 | _ = ~true -vlib/v/checker/tests/prefix_err.vv:5:5: error: operator ~ only defined on int types - 3 | a <- 4 - 4 | - 5 | _ = ~true + 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 | ^ - 6 | _ = !4 -vlib/v/checker/tests/prefix_err.vv:6:5: error: ! operator can only be used with bool types - 4 | - 5 | _ = ~true - 6 | _ = !4 + 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 | ^ diff --git a/vlib/v/checker/tests/prefix_err.vv b/vlib/v/checker/tests/prefix_err.vv index 6d5629f08a..c75d5550b7 100644 --- a/vlib/v/checker/tests/prefix_err.vv +++ b/vlib/v/checker/tests/prefix_err.vv @@ -1,3 +1,8 @@ +b := &true +_ := &10 +_ := &"Hi" +_ := &"${b}" + a := 1 _ = *a a <- 4