]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: slice-to-array-pointer conversion requires go1.17
authorRobert Griesemer <gri@golang.org>
Thu, 29 Apr 2021 17:02:01 +0000 (10:02 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 29 Apr 2021 23:41:22 +0000 (23:41 +0000)
Add missing version check. Even though this is a new types2 error
we separate between the compiler and the types2 error message: we
have the compiler error message to match the compiler style, and
we have a types2-specific error message to match the types2 style
for these kinds of errors (for now).

Eventually we need to decide which style we like better and clean
this up.

Follow-up on https://golang.org/cl/301650.

Updates #395.

Change-Id: I5b779f345994c66b1f4a4db466466f98b7d3c491
Reviewed-on: https://go-review.googlesource.com/c/go/+/315169
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/conversions.go
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/testdata/check/go1_16.src [new file with mode: 0644]

index 51be50e9ad1d2c0794b7f5ea7e9438b044497513..30201e2b7f4232a147d17a2bdbc8cbb5bc301be3 100644 (file)
@@ -38,8 +38,10 @@ func (check *Checker) conversion(x *operand, T Type) {
        }
 
        if !ok {
-               check.errorf(x, "cannot convert %s to %s", x, T)
-               x.mode = invalid
+               if x.mode != invalid {
+                       check.errorf(x, "cannot convert %s to %s", x, T)
+                       x.mode = invalid
+               }
                return
        }
 
@@ -141,7 +143,16 @@ func (x *operand) convertibleTo(check *Checker, T Type) bool {
                if p := asPointer(T); p != nil {
                        if a := asArray(p.Elem()); a != nil {
                                if check.identical(s.Elem(), a.Elem()) {
-                                       return true
+                                       if check == nil || check.allowVersion(check.pkg, 1, 17) {
+                                               return true
+                                       }
+                                       // check != nil
+                                       if check.conf.CompilerErrorMessages {
+                                               check.error(x, "conversion of slices to array pointers only supported as of -lang=go1.17")
+                                       } else {
+                                               check.error(x, "conversion of slices to array pointers requires go1.17 or later")
+                                       }
+                                       x.mode = invalid // avoid follow-up error
                                }
                        }
                }
index 4966f198921de386f1f169d98c72fea17d3bc88a..1333e4c0eca323c2725b8c24ea733d7919258ff6 100644 (file)
@@ -601,7 +601,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
 
        if alias {
                // type alias declaration
-               if !check.allowVersion(obj.pkg, 1, 9) {
+               if !check.allowVersion(check.pkg, 1, 9) {
                        if check.conf.CompilerErrorMessages {
                                check.error(tdecl, "type aliases only supported as of -lang=go1.9")
                        } else {
diff --git a/src/cmd/compile/internal/types2/testdata/check/go1_16.src b/src/cmd/compile/internal/types2/testdata/check/go1_16.src
new file mode 100644 (file)
index 0000000..fdf5c99
--- /dev/null
@@ -0,0 +1,13 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Check Go language version-specific errors.
+
+package go1_16 // go1.16
+
+type Slice []byte
+type Array [8]byte
+
+var s Slice
+var p = (*Array)(s /* ERROR requires go1.17 or later */ )