Commit 6013052e authored by Daniel Martí's avatar Daniel Martí

cmd/compile: eliminate some lineno uses

Focused on ranges, selects and switches for this one.

While at it, simplify some vars in typecheckselect.

Updates #19683.

Change-Id: Ib6aabe0f6826cb1930483aeb4bb2de1ff8052d9e
Reviewed-on: https://go-review.googlesource.com/69690
Run-TryBot: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent 07f7db3e
...@@ -52,7 +52,7 @@ func typecheckrange(n *Node) { ...@@ -52,7 +52,7 @@ func typecheckrange(n *Node) {
toomany = 0 toomany = 0
switch t.Etype { switch t.Etype {
default: default:
yyerror("cannot range over %L", n.Right) yyerrorl(n.Pos, "cannot range over %L", n.Right)
goto out goto out
case TARRAY, TSLICE: case TARRAY, TSLICE:
...@@ -65,7 +65,7 @@ func typecheckrange(n *Node) { ...@@ -65,7 +65,7 @@ func typecheckrange(n *Node) {
case TCHAN: case TCHAN:
if !t.ChanDir().CanRecv() { if !t.ChanDir().CanRecv() {
yyerror("invalid operation: range %v (receive from send-only type %v)", n.Right, n.Right.Type) yyerrorl(n.Pos, "invalid operation: range %v (receive from send-only type %v)", n.Right, n.Right.Type)
goto out goto out
} }
...@@ -81,7 +81,7 @@ func typecheckrange(n *Node) { ...@@ -81,7 +81,7 @@ func typecheckrange(n *Node) {
} }
if n.List.Len() > 2 || toomany != 0 { if n.List.Len() > 2 || toomany != 0 {
yyerror("too many variables in range") yyerrorl(n.Pos, "too many variables in range")
} }
v1 = nil v1 = nil
...@@ -108,7 +108,7 @@ func typecheckrange(n *Node) { ...@@ -108,7 +108,7 @@ func typecheckrange(n *Node) {
if v1.Name != nil && v1.Name.Defn == n { if v1.Name != nil && v1.Name.Defn == n {
v1.Type = t1 v1.Type = t1
} else if v1.Type != nil && assignop(t1, v1.Type, &why) == 0 { } else if v1.Type != nil && assignop(t1, v1.Type, &why) == 0 {
yyerror("cannot assign type %v to %L in range%s", t1, v1, why) yyerrorl(n.Pos, "cannot assign type %v to %L in range%s", t1, v1, why)
} }
checkassign(n, v1) checkassign(n, v1)
} }
...@@ -117,7 +117,7 @@ func typecheckrange(n *Node) { ...@@ -117,7 +117,7 @@ func typecheckrange(n *Node) {
if v2.Name != nil && v2.Name.Defn == n { if v2.Name != nil && v2.Name.Defn == n {
v2.Type = t2 v2.Type = t2
} else if v2.Type != nil && assignop(t2, v2.Type, &why) == 0 { } else if v2.Type != nil && assignop(t2, v2.Type, &why) == 0 {
yyerror("cannot assign type %v to %L in range%s", t2, v2, why) yyerrorl(n.Pos, "cannot assign type %v to %L in range%s", t2, v2, why)
} }
checkassign(n, v2) checkassign(n, v2)
} }
......
...@@ -8,39 +8,32 @@ import "cmd/compile/internal/types" ...@@ -8,39 +8,32 @@ import "cmd/compile/internal/types"
// select // select
func typecheckselect(sel *Node) { func typecheckselect(sel *Node) {
var ncase *Node
var n *Node
var def *Node var def *Node
lno := setlineno(sel) lno := setlineno(sel)
count := 0
typecheckslice(sel.Ninit.Slice(), Etop) typecheckslice(sel.Ninit.Slice(), Etop)
for _, n1 := range sel.List.Slice() { for _, ncase := range sel.List.Slice() {
count++
ncase = n1
setlineno(ncase)
if ncase.Op != OXCASE { if ncase.Op != OXCASE {
setlineno(ncase)
Fatalf("typecheckselect %v", ncase.Op) Fatalf("typecheckselect %v", ncase.Op)
} }
if ncase.List.Len() == 0 { if ncase.List.Len() == 0 {
// default // default
if def != nil { if def != nil {
yyerror("multiple defaults in select (first at %v)", def.Line()) yyerrorl(ncase.Pos, "multiple defaults in select (first at %v)", def.Line())
} else { } else {
def = ncase def = ncase
} }
} else if ncase.List.Len() > 1 { } else if ncase.List.Len() > 1 {
yyerror("select cases cannot be lists") yyerrorl(ncase.Pos, "select cases cannot be lists")
} else { } else {
ncase.List.SetFirst(typecheck(ncase.List.First(), Etop)) ncase.List.SetFirst(typecheck(ncase.List.First(), Etop))
n = ncase.List.First() n := ncase.List.First()
ncase.Left = n ncase.Left = n
ncase.List.Set(nil) ncase.List.Set(nil)
setlineno(n)
switch n.Op { switch n.Op {
default: default:
yyerror("select case must be receive, send or assign recv") yyerrorl(n.Pos, "select case must be receive, send or assign recv")
// convert x = <-c into OSELRECV(x, <-c). // convert x = <-c into OSELRECV(x, <-c).
// remove implicit conversions; the eventual assignment // remove implicit conversions; the eventual assignment
...@@ -51,7 +44,7 @@ func typecheckselect(sel *Node) { ...@@ -51,7 +44,7 @@ func typecheckselect(sel *Node) {
} }
if n.Right.Op != ORECV { if n.Right.Op != ORECV {
yyerror("select assignment must have receive on right hand side") yyerrorl(n.Pos, "select assignment must have receive on right hand side")
break break
} }
...@@ -60,7 +53,7 @@ func typecheckselect(sel *Node) { ...@@ -60,7 +53,7 @@ func typecheckselect(sel *Node) {
// convert x, ok = <-c into OSELRECV2(x, <-c) with ntest=ok // convert x, ok = <-c into OSELRECV2(x, <-c) with ntest=ok
case OAS2RECV: case OAS2RECV:
if n.Rlist.First().Op != ORECV { if n.Rlist.First().Op != ORECV {
yyerror("select assignment must have receive on right hand side") yyerrorl(n.Pos, "select assignment must have receive on right hand side")
break break
} }
...@@ -72,7 +65,7 @@ func typecheckselect(sel *Node) { ...@@ -72,7 +65,7 @@ func typecheckselect(sel *Node) {
// convert <-c into OSELRECV(N, <-c) // convert <-c into OSELRECV(N, <-c)
case ORECV: case ORECV:
n = nod(OSELRECV, nil, n) n = nodl(n.Pos, OSELRECV, nil, n)
n.SetTypecheck(1) n.SetTypecheck(1)
ncase.Left = n ncase.Left = n
...@@ -85,7 +78,7 @@ func typecheckselect(sel *Node) { ...@@ -85,7 +78,7 @@ func typecheckselect(sel *Node) {
typecheckslice(ncase.Nbody.Slice(), Etop) typecheckslice(ncase.Nbody.Slice(), Etop)
} }
sel.Xoffset = int64(count) sel.Xoffset = int64(sel.List.Len())
lineno = lno lineno = lno
} }
......
...@@ -393,7 +393,7 @@ func casebody(sw *Node, typeswvar *Node) { ...@@ -393,7 +393,7 @@ func casebody(sw *Node, typeswvar *Node) {
case 0: case 0:
// default // default
if def != nil { if def != nil {
yyerror("more than one default case") yyerrorl(n.Pos, "more than one default case")
} }
// reuse original default case // reuse original default case
n.Right = jmp n.Right = jmp
...@@ -673,14 +673,13 @@ func (s *typeSwitch) walk(sw *Node) { ...@@ -673,14 +673,13 @@ func (s *typeSwitch) walk(sw *Node) {
return return
} }
if cond.Right == nil { if cond.Right == nil {
setlineno(sw) yyerrorl(sw.Pos, "type switch must have an assignment")
yyerror("type switch must have an assignment")
return return
} }
cond.Right = walkexpr(cond.Right, &sw.Ninit) cond.Right = walkexpr(cond.Right, &sw.Ninit)
if !cond.Right.Type.IsInterface() { if !cond.Right.Type.IsInterface() {
yyerror("type switch must be on an interface") yyerrorl(sw.Pos, "type switch must be on an interface")
return return
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment