Skip to content

Commit 83bfed9

Browse files
committed
cmd/compile/internal/types2: print "nil" rather than "untyped nil"
When we have a typed nil, we already say so; thus it is sufficient to use "nil" in all the other cases. This is closer to (1.17) compiler behavior. In cases where the 1.17 compiler prints "untyped nil" (e.g., wrong uses of "copy"), we already print a different message. We can do better in those cases as well; will be addressed in a separate CL (see #49735). Fixes #48852. Change-Id: I9a7a72e0f99185b00f80040c5510a693b1ea80f6 Reviewed-on: https://go-review.googlesource.com/c/go/+/366276 Trust: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 9678f79 commit 83bfed9

File tree

8 files changed

+25
-28
lines changed

8 files changed

+25
-28
lines changed

src/cmd/compile/internal/types2/assignments.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ func (check *Checker) assignVar(lhs syntax.Expr, x *operand) Type {
220220
return nil
221221
case variable, mapindex:
222222
// ok
223-
case nilvalue:
224-
check.error(&z, "cannot assign to nil") // default would print "untyped nil"
225-
return nil
226223
default:
227224
if sel, ok := z.expr.(*syntax.SelectorExpr); ok {
228225
var op operand

src/cmd/compile/internal/types2/operand.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func operandString(x *operand, qf Qualifier) string {
116116
case nil, Typ[Invalid]:
117117
return "nil (with invalid type)"
118118
case Typ[UntypedNil]:
119-
return "untyped nil"
119+
return "nil"
120120
default:
121121
return fmt.Sprintf("nil (of type %s)", TypeString(x.typ, qf))
122122
}

src/cmd/compile/internal/types2/testdata/check/stmt0.src

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ func assignments1() {
6969

7070
// test cases for issue 5800
7171
var (
72-
_ int = nil /* ERROR "untyped nil" */
73-
_ [10]int = nil /* ERROR "untyped nil" */
72+
_ int = nil /* ERROR "nil" */
73+
_ [10]int = nil /* ERROR "nil" */
7474
_ []byte = nil
75-
_ struct{} = nil /* ERROR "untyped nil" */
75+
_ struct{} = nil /* ERROR "nil" */
7676
_ func() = nil
7777
_ map[int]string = nil
7878
_ chan int = nil

src/cmd/compile/internal/types2/testdata/fixedbugs/issue49296.go2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ func _[
99
T1 []int,
1010
T2 ~float64 | ~complex128 | chan int,
1111
]() {
12-
_ = T0(nil /* ERROR cannot convert untyped nil to T0 */ )
12+
_ = T0(nil /* ERROR cannot convert nil to T0 */ )
1313
_ = T1(1 /* ERROR cannot convert 1 .* to T1 */ )
1414
_ = T2(2 /* ERROR cannot convert 2 .* to T2 */ )
1515
}

src/cmd/compile/internal/types2/testdata/spec/assignability.go2

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -153,28 +153,28 @@ func _[
153153

154154
// "x is the predeclared identifier nil and T is a pointer, function, slice, map, channel, or interface type"
155155
func _[TP Interface](X TP) {
156-
b = nil // ERROR cannot use untyped nil
157-
a = nil // ERROR cannot use untyped nil
156+
b = nil // ERROR cannot use nil
157+
a = nil // ERROR cannot use nil
158158
l = nil
159-
s = nil // ERROR cannot use untyped nil
159+
s = nil // ERROR cannot use nil
160160
p = nil
161161
f = nil
162162
i = nil
163163
m = nil
164164
c = nil
165-
d = nil // ERROR cannot use untyped nil
165+
d = nil // ERROR cannot use nil
166166

167-
B = nil // ERROR cannot use untyped nil
168-
A = nil // ERROR cannot use untyped nil
167+
B = nil // ERROR cannot use nil
168+
A = nil // ERROR cannot use nil
169169
L = nil
170-
S = nil // ERROR cannot use untyped nil
170+
S = nil // ERROR cannot use nil
171171
P = nil
172172
F = nil
173173
I = nil
174174
M = nil
175175
C = nil
176-
D = nil // ERROR cannot use untyped nil
177-
X = nil // ERROR cannot use untyped nil
176+
D = nil // ERROR cannot use nil
177+
X = nil // ERROR cannot use nil
178178
}
179179

180180
// "x is an untyped constant representable by a value of type T"

test/fixedbugs/issue6004.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
package main
88

99
func main() {
10-
_ = nil // ERROR "use of untyped nil"
11-
_, _ = nil, 1 // ERROR "use of untyped nil"
12-
_, _ = 1, nil // ERROR "use of untyped nil"
13-
_ = append(nil, 1, 2, 3) // ERROR "untyped nil"
10+
_ = nil // ERROR "use of untyped nil"
11+
_, _ = nil, 1 // ERROR "use of untyped nil"
12+
_, _ = 1, nil // ERROR "use of untyped nil"
13+
_ = append(nil, 1, 2, 3) // ERROR "untyped nil|nil"
1414
}
15-

test/fixedbugs/issue6402.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
package p
1010

1111
func f() uintptr {
12-
return nil // ERROR "cannot use nil as type uintptr in return argument|incompatible type|cannot use untyped nil"
12+
return nil // ERROR "cannot use nil as type uintptr in return argument|incompatible type|cannot use nil"
1313
}

test/fixedbugs/issue7223.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
package main
88

99
var bits1 uint = 10
10+
1011
const bits2 uint = 10
1112

1213
func main() {
1314
_ = make([]byte, 1<<bits1)
1415
_ = make([]byte, 1<<bits2)
15-
_ = make([]byte, nil) // ERROR "non-integer.*len|untyped nil"
16-
_ = make([]byte, nil, 2) // ERROR "non-integer.*len|untyped nil"
17-
_ = make([]byte, 1, nil) // ERROR "non-integer.*cap|untyped nil"
18-
_ = make([]byte, true) // ERROR "non-integer.*len|untyped bool"
19-
_ = make([]byte, "abc") // ERROR "non-integer.*len|untyped string"
16+
_ = make([]byte, nil) // ERROR "non-integer.*len|nil"
17+
_ = make([]byte, nil, 2) // ERROR "non-integer.*len|nil"
18+
_ = make([]byte, 1, nil) // ERROR "non-integer.*cap|nil"
19+
_ = make([]byte, true) // ERROR "non-integer.*len|untyped bool"
20+
_ = make([]byte, "abc") // ERROR "non-integer.*len|untyped string"
2021
}

0 commit comments

Comments
 (0)