Skip to content

Commit a9705e1

Browse files
committed
cmd/compile/internal/types2: slice-to-array-pointer conversion requires go1.17
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 <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent e99dfb0 commit a9705e1

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ func (check *Checker) conversion(x *operand, T Type) {
3838
}
3939

4040
if !ok {
41-
check.errorf(x, "cannot convert %s to %s", x, T)
42-
x.mode = invalid
41+
if x.mode != invalid {
42+
check.errorf(x, "cannot convert %s to %s", x, T)
43+
x.mode = invalid
44+
}
4345
return
4446
}
4547

@@ -141,7 +143,16 @@ func (x *operand) convertibleTo(check *Checker, T Type) bool {
141143
if p := asPointer(T); p != nil {
142144
if a := asArray(p.Elem()); a != nil {
143145
if check.identical(s.Elem(), a.Elem()) {
144-
return true
146+
if check == nil || check.allowVersion(check.pkg, 1, 17) {
147+
return true
148+
}
149+
// check != nil
150+
if check.conf.CompilerErrorMessages {
151+
check.error(x, "conversion of slices to array pointers only supported as of -lang=go1.17")
152+
} else {
153+
check.error(x, "conversion of slices to array pointers requires go1.17 or later")
154+
}
155+
x.mode = invalid // avoid follow-up error
145156
}
146157
}
147158
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
601601

602602
if alias {
603603
// type alias declaration
604-
if !check.allowVersion(obj.pkg, 1, 9) {
604+
if !check.allowVersion(check.pkg, 1, 9) {
605605
if check.conf.CompilerErrorMessages {
606606
check.error(tdecl, "type aliases only supported as of -lang=go1.9")
607607
} else {
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
// Check Go language version-specific errors.
6+
7+
package go1_16 // go1.16
8+
9+
type Slice []byte
10+
type Array [8]byte
11+
12+
var s Slice
13+
var p = (*Array)(s /* ERROR requires go1.17 or later */ )

0 commit comments

Comments
 (0)