Skip to content

Commit

Permalink
compiler/parser: make 'used as value' error in 'if' init the same of gc
Browse files Browse the repository at this point in the history
It also fixed a typo in a comment.

For #385
  • Loading branch information
gazerro committed Jun 25, 2021
1 parent 7e4919e commit 068b3b6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiler/checker_expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,7 @@ func (tc *typechecker) checkCompositeLiteral(node *ast.CompositeLiteral, typ ref
var elemTi *typeInfo
if cl, ok := kv.Value.(*ast.CompositeLiteral); ok {
if ti.Type.Elem().Kind() == reflect.Ptr {
// The slice (or array) as element *T, so the value '{..}' must be
// The slice (or array) has element *T, so the value '{..}' must be
// replaced with '&T{..}'.
kv.Value = ast.NewUnaryOperator(cl.Pos(), ast.OperatorAddress, cl)
tc.checkCompositeLiteral(cl, ti.Type.Elem().Elem()) // []*T -> T (or [n]*T -> T)
Expand Down
2 changes: 1 addition & 1 deletion compiler/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ LABEL:
if a, ok := init.(*ast.Assignment); ok && a.Type == ast.AssignmentSimple {
panic(syntaxError(tok.pos, "assignment %s used as value", init))
}
panic(syntaxError(tok.pos, "%s used as value", init))
panic(syntaxError(tok.pos, "cannot use %s as value", init))
}
init = nil
}
Expand Down
10 changes: 5 additions & 5 deletions test/compare/testdata/syntax/if_statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func main() {
if f() {}
if <-ch {} // ERROR `non-bool <-ch (type int) used as if condition`
if <-chb {}
if ch <- 3 {} // ERROR `ch <- 3 used as value`
if i++ for {} // ERROR `syntax error: i++ used as value`
if i++ {} // ERROR `syntax error: i++ used as value`
if i-- {} // ERROR `syntax error: i-- used as value`
if ch <- 3 {} // ERROR `cannot use ch <- 3 as value`
if i++ for {} // ERROR `syntax error: cannot use i++ as value`
if i++ {} // ERROR `syntax error: cannot use i++ as value`
if i-- {} // ERROR `syntax error: cannot use i-- as value`
if i = 3 {} // ERROR `syntax error: assignment i = 3 used as value`
if i := 3 { _ = i } // ERROR `syntax error: i := 3 used as value`
if i := 3 { _ = i } // ERROR `syntax error: cannot use i := 3 as value`
if print(); {} // ERROR `syntax error: missing condition in if statement`
if <-ch; {} // ERROR `syntax error: missing condition in if statement`
if ch <- 3; {} // ERROR `syntax error: missing condition in if statement`
Expand Down

0 comments on commit 068b3b6

Please sign in to comment.