]> Cypherpunks repositories - gostls13.git/commitdiff
go/types: print "nil" rather than "untyped nil"
authorRobert Griesemer <gri@golang.org>
Tue, 23 Nov 2021 00:04:17 +0000 (16:04 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 24 Nov 2021 20:56:40 +0000 (20:56 +0000)
This is a port of CL 366276 from types2 to go/types
with minor adjustments due to the slightly different
handling of nil in go/types.

It uses some more detailed error strings in stmt0.src;
the same changes are made to the corresponding types2
file.

For #48852.

Change-Id: I2cdf258799bcbe2d12bbadaf67b8b4504b356bd0
Reviewed-on: https://go-review.googlesource.com/c/go/+/366277
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/testdata/check/stmt0.src
src/go/types/operand.go
src/go/types/testdata/check/stmt0.src
src/go/types/testdata/fixedbugs/issue49296.go2
src/go/types/testdata/spec/assignability.go2

index 353444f06880f78385536407604ca11bfbc61e89..8171c57d8b57e8c22432b94e08983366a6cf7941 100644 (file)
@@ -69,10 +69,10 @@ func assignments1() {
 
        // test cases for issue 5800
        var (
-               _ int = nil /* ERROR "nil" */
-               _ [10]int = nil /* ERROR "nil" */
+               _ int = nil /* ERROR "cannot use nil as int value in variable declaration" */
+               _ [10]int = nil /* ERROR "cannot use nil as \[10\]int value in variable declaration" */
                _ []byte = nil
-               _ struct{} = nil /* ERROR "nil" */
+               _ struct{} = nil /* ERROR "cannot use nil as struct{} value in variable declaration" */
                _ func() = nil
                _ map[int]string = nil
                _ chan int = nil
index 8cc5eda8669bc5b407e321fa55e8d6ebfb74e23f..c35b1650be264fd70016f4cc404de9fd4435ab2b 100644 (file)
@@ -105,6 +105,11 @@ func (x *operand) Pos() token.Pos {
 // cgofunc    <expr> (               <mode>       of type <typ>)
 //
 func operandString(x *operand, qf Qualifier) string {
+       // special-case nil
+       if x.mode == value && x.typ == Typ[UntypedNil] {
+               return "nil"
+       }
+
        var buf bytes.Buffer
 
        var expr string
index 15df37703c1cb052175b2f810103a74eddf88e03..2cce0b59b2b4843e8ac3f19b9b5f29551fc844f7 100644 (file)
@@ -69,10 +69,10 @@ func assignments1() {
 
        // test cases for issue 5800
        var (
-               _ int = nil /* ERROR "untyped nil value" */
-               _ [10]int = nil /* ERROR "untyped nil value" */
+               _ int = nil /* ERROR "cannot use nil as int value in variable declaration" */
+               _ [10]int = nil /* ERROR "cannot use nil as \[10\]int value in variable declaration" */
                _ []byte = nil
-               _ struct{} = nil /* ERROR "untyped nil value" */
+               _ struct{} = nil /* ERROR "cannot use nil as struct{} value in variable declaration" */
                _ func() = nil
                _ map[int]string = nil
                _ chan int = nil
index 8c6d0b678d04de1358fb1b5de6312beffd59ff43..0ad71ef4b2adb677f00ec677cb92a94536fdb1f3 100644 (file)
@@ -10,7 +10,7 @@ func _[
         T2 ~float64 | ~complex128 | chan int,
 ]() {
        // TODO(rfindley): the types2 error here is clearer.
-        _ = T0(nil /* ERROR cannot convert nil \(untyped nil value\) to T0 */ )
+        _ = T0(nil /* ERROR cannot convert nil to T0 */ )
         _ = T1(1 /* ERROR cannot convert 1 .* to T1 */ )
         _ = T2(2 /* ERROR cannot convert 2 .* to T2 */ )
 }
index a6e71aac817e340784a8c7cf5f9aecc2837d8873..d5f6ab4419a1aa7d5cac671477e9f417659ae7a6 100644 (file)
@@ -155,28 +155,28 @@ func _[
 // TODO(rfindley) error messages about untyped nil diverge from types2 here.
 // Consider aligning them.
 func _[TP Interface](X TP) {
-       b = nil // ERROR cannot use.*untyped nil
-       a = nil // ERROR cannot use.*untyped nil
+       b = nil // ERROR cannot use nil
+       a = nil // ERROR cannot use nil
        l = nil
-       s = nil // ERROR cannot use.*untyped nil
+       s = nil // ERROR cannot use nil
        p = nil
        f = nil
        i = nil
        m = nil
        c = nil
-       d = nil // ERROR cannot use.*untyped nil
+       d = nil // ERROR cannot use nil
 
-       B = nil // ERROR cannot use.*untyped nil
-       A = nil // ERROR cannot use.*untyped nil
+       B = nil // ERROR cannot use nil
+       A = nil // ERROR cannot use nil
        L = nil
-       S = nil // ERROR cannot use.*untyped nil
+       S = nil // ERROR cannot use nil
        P = nil
        F = nil
        I = nil
        M = nil
        C = nil
-       D = nil // ERROR cannot use.*untyped nil
-       X = nil // ERROR cannot use.*untyped nil
+       D = nil // ERROR cannot use nil
+       X = nil // ERROR cannot use nil
 }
 
 // "x is an untyped constant representable by a value of type T"