Skip to content

Commit 6013052

Browse files
committed
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í <[email protected]> Reviewed-by: Matthew Dempsky <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 07f7db3 commit 6013052

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

src/cmd/compile/internal/gc/range.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func typecheckrange(n *Node) {
5252
toomany = 0
5353
switch t.Etype {
5454
default:
55-
yyerror("cannot range over %L", n.Right)
55+
yyerrorl(n.Pos, "cannot range over %L", n.Right)
5656
goto out
5757

5858
case TARRAY, TSLICE:
@@ -65,7 +65,7 @@ func typecheckrange(n *Node) {
6565

6666
case TCHAN:
6767
if !t.ChanDir().CanRecv() {
68-
yyerror("invalid operation: range %v (receive from send-only type %v)", n.Right, n.Right.Type)
68+
yyerrorl(n.Pos, "invalid operation: range %v (receive from send-only type %v)", n.Right, n.Right.Type)
6969
goto out
7070
}
7171

@@ -81,7 +81,7 @@ func typecheckrange(n *Node) {
8181
}
8282

8383
if n.List.Len() > 2 || toomany != 0 {
84-
yyerror("too many variables in range")
84+
yyerrorl(n.Pos, "too many variables in range")
8585
}
8686

8787
v1 = nil
@@ -108,7 +108,7 @@ func typecheckrange(n *Node) {
108108
if v1.Name != nil && v1.Name.Defn == n {
109109
v1.Type = t1
110110
} else if v1.Type != nil && assignop(t1, v1.Type, &why) == 0 {
111-
yyerror("cannot assign type %v to %L in range%s", t1, v1, why)
111+
yyerrorl(n.Pos, "cannot assign type %v to %L in range%s", t1, v1, why)
112112
}
113113
checkassign(n, v1)
114114
}
@@ -117,7 +117,7 @@ func typecheckrange(n *Node) {
117117
if v2.Name != nil && v2.Name.Defn == n {
118118
v2.Type = t2
119119
} else if v2.Type != nil && assignop(t2, v2.Type, &why) == 0 {
120-
yyerror("cannot assign type %v to %L in range%s", t2, v2, why)
120+
yyerrorl(n.Pos, "cannot assign type %v to %L in range%s", t2, v2, why)
121121
}
122122
checkassign(n, v2)
123123
}

src/cmd/compile/internal/gc/select.go

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,32 @@ import "cmd/compile/internal/types"
88

99
// select
1010
func typecheckselect(sel *Node) {
11-
var ncase *Node
12-
var n *Node
13-
1411
var def *Node
1512
lno := setlineno(sel)
16-
count := 0
1713
typecheckslice(sel.Ninit.Slice(), Etop)
18-
for _, n1 := range sel.List.Slice() {
19-
count++
20-
ncase = n1
21-
setlineno(ncase)
14+
for _, ncase := range sel.List.Slice() {
2215
if ncase.Op != OXCASE {
16+
setlineno(ncase)
2317
Fatalf("typecheckselect %v", ncase.Op)
2418
}
2519

2620
if ncase.List.Len() == 0 {
2721
// default
2822
if def != nil {
29-
yyerror("multiple defaults in select (first at %v)", def.Line())
23+
yyerrorl(ncase.Pos, "multiple defaults in select (first at %v)", def.Line())
3024
} else {
3125
def = ncase
3226
}
3327
} else if ncase.List.Len() > 1 {
34-
yyerror("select cases cannot be lists")
28+
yyerrorl(ncase.Pos, "select cases cannot be lists")
3529
} else {
3630
ncase.List.SetFirst(typecheck(ncase.List.First(), Etop))
37-
n = ncase.List.First()
31+
n := ncase.List.First()
3832
ncase.Left = n
3933
ncase.List.Set(nil)
40-
setlineno(n)
4134
switch n.Op {
4235
default:
43-
yyerror("select case must be receive, send or assign recv")
36+
yyerrorl(n.Pos, "select case must be receive, send or assign recv")
4437

4538
// convert x = <-c into OSELRECV(x, <-c).
4639
// remove implicit conversions; the eventual assignment
@@ -51,7 +44,7 @@ func typecheckselect(sel *Node) {
5144
}
5245

5346
if n.Right.Op != ORECV {
54-
yyerror("select assignment must have receive on right hand side")
47+
yyerrorl(n.Pos, "select assignment must have receive on right hand side")
5548
break
5649
}
5750

@@ -60,7 +53,7 @@ func typecheckselect(sel *Node) {
6053
// convert x, ok = <-c into OSELRECV2(x, <-c) with ntest=ok
6154
case OAS2RECV:
6255
if n.Rlist.First().Op != ORECV {
63-
yyerror("select assignment must have receive on right hand side")
56+
yyerrorl(n.Pos, "select assignment must have receive on right hand side")
6457
break
6558
}
6659

@@ -72,7 +65,7 @@ func typecheckselect(sel *Node) {
7265

7366
// convert <-c into OSELRECV(N, <-c)
7467
case ORECV:
75-
n = nod(OSELRECV, nil, n)
68+
n = nodl(n.Pos, OSELRECV, nil, n)
7669

7770
n.SetTypecheck(1)
7871
ncase.Left = n
@@ -85,7 +78,7 @@ func typecheckselect(sel *Node) {
8578
typecheckslice(ncase.Nbody.Slice(), Etop)
8679
}
8780

88-
sel.Xoffset = int64(count)
81+
sel.Xoffset = int64(sel.List.Len())
8982
lineno = lno
9083
}
9184

src/cmd/compile/internal/gc/swt.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func casebody(sw *Node, typeswvar *Node) {
393393
case 0:
394394
// default
395395
if def != nil {
396-
yyerror("more than one default case")
396+
yyerrorl(n.Pos, "more than one default case")
397397
}
398398
// reuse original default case
399399
n.Right = jmp
@@ -673,14 +673,13 @@ func (s *typeSwitch) walk(sw *Node) {
673673
return
674674
}
675675
if cond.Right == nil {
676-
setlineno(sw)
677-
yyerror("type switch must have an assignment")
676+
yyerrorl(sw.Pos, "type switch must have an assignment")
678677
return
679678
}
680679

681680
cond.Right = walkexpr(cond.Right, &sw.Ninit)
682681
if !cond.Right.Type.IsInterface() {
683-
yyerror("type switch must be on an interface")
682+
yyerrorl(sw.Pos, "type switch must be on an interface")
684683
return
685684
}
686685

0 commit comments

Comments
 (0)