Skip to content

Commit 3e41b18

Browse files
committed
cmd/compile/internal/types2: use compiler version error when configured for compiler
Fixes #49368. Change-Id: I7c7575ae8bb6271160747e3f1888b144c3ab24c4 Reviewed-on: https://go-review.googlesource.com/c/go/+/361411 Trust: Robert Griesemer <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent ab31dbc commit 3e41b18

File tree

11 files changed

+42
-26
lines changed

11 files changed

+42
-26
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
574574
case _Add:
575575
// unsafe.Add(ptr unsafe.Pointer, len IntegerType) unsafe.Pointer
576576
if !check.allowVersion(check.pkg, 1, 17) {
577-
check.error(call.Fun, "unsafe.Add requires go1.17 or later")
577+
check.versionErrorf(call.Fun, "go1.17", "unsafe.Add")
578578
return
579579
}
580580

@@ -700,7 +700,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
700700
case _Slice:
701701
// unsafe.Slice(ptr *T, len IntegerType) []T
702702
if !check.allowVersion(check.pkg, 1, 17) {
703-
check.error(call.Fun, "unsafe.Slice requires go1.17 or later")
703+
check.versionErrorf(call.Fun, "go1.17", "unsafe.Slice")
704704
return
705705
}
706706

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
// The operand x must be the evaluation of inst.X and its type must be a signature.
1717
func (check *Checker) funcInst(x *operand, inst *syntax.IndexExpr) {
1818
if !check.allowVersion(check.pkg, 1, 18) {
19-
check.softErrorf(inst.Pos(), "function instantiation requires go1.18 or later")
19+
check.versionErrorf(inst.Pos(), "go1.18", "function instantiation")
2020
}
2121

2222
xlist := unpackExpr(inst.Index)
@@ -363,9 +363,9 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
363363
if sig.TypeParams().Len() > 0 {
364364
if !check.allowVersion(check.pkg, 1, 18) {
365365
if iexpr, _ := call.Fun.(*syntax.IndexExpr); iexpr != nil {
366-
check.softErrorf(iexpr.Pos(), "function instantiation requires go1.18 or later")
366+
check.versionErrorf(iexpr.Pos(), "go1.18", "function instantiation")
367367
} else {
368-
check.softErrorf(call.Pos(), "implicit function instantiation requires go1.18 or later")
368+
check.versionErrorf(call.Pos(), "go1.18", "implicit function instantiation")
369369
}
370370
}
371371
// TODO(gri) provide position information for targs so we can feed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package types2
88

99
import (
10+
"fmt"
1011
"go/constant"
1112
"unicode"
1213
)
@@ -181,11 +182,9 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
181182
}
182183
// check != nil
183184
if cause != nil {
185+
*cause = "conversion of slices to array pointers requires go1.17 or later"
184186
if check.conf.CompilerErrorMessages {
185-
// compiler error message assumes a -lang flag
186-
*cause = "conversion of slices to array pointers only supported as of -lang=go1.17"
187-
} else {
188-
*cause = "conversion of slices to array pointers requires go1.17 or later"
187+
*cause += fmt.Sprintf(" (-lang was set to %s; check go.mod)", check.conf.GoVersion)
189188
}
190189
}
191190
return false

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
555555
check.validType(obj.typ, nil)
556556
// If typ is local, an error was already reported where typ is specified/defined.
557557
if check.isImportedConstraint(rhs) && !check.allowVersion(check.pkg, 1, 18) {
558-
check.errorf(tdecl.Type.Pos(), "using type constraint %s requires go1.18 or later", rhs)
558+
check.versionErrorf(tdecl.Type.Pos(), "go1.18", "using type constraint %s", rhs)
559559
}
560560
}).describef(obj, "validType(%s)", obj.Name())
561561

@@ -570,11 +570,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
570570
// alias declaration
571571
if alias {
572572
if !check.allowVersion(check.pkg, 1, 9) {
573-
if check.conf.CompilerErrorMessages {
574-
check.error(tdecl, "type aliases only supported as of -lang=go1.9")
575-
} else {
576-
check.error(tdecl, "type aliases requires go1.9 or later")
577-
}
573+
check.versionErrorf(tdecl, "go1.9", "type aliases")
578574
}
579575

580576
obj.typ = Typ[Invalid]

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,16 @@ func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
230230
check.err(at, check.sprintf(format, args...), true)
231231
}
232232

233+
func (check *Checker) versionErrorf(at poser, goVersion string, format string, args ...interface{}) {
234+
msg := check.sprintf(format, args...)
235+
if check.conf.CompilerErrorMessages {
236+
msg = fmt.Sprintf("%s requires %s or later (-lang was set to %s; check go.mod)", msg, goVersion, check.conf.GoVersion)
237+
} else {
238+
msg = fmt.Sprintf("%s requires %s or later", msg, goVersion)
239+
}
240+
check.err(at, msg, true)
241+
}
242+
233243
// posFor reports the left (= start) position of at.
234244
func posFor(at poser) syntax.Pos {
235245
switch x := at.(type) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,7 @@ func (check *Checker) shift(x, y *operand, e syntax.Expr, op syntax.Operator) {
869869
x.mode = invalid
870870
return
871871
} else if !allUnsigned(y.typ) && !check.allowVersion(check.pkg, 1, 13) {
872-
check.errorf(y, invalidOp+"signed shift count %s requires go1.13 or later", y)
872+
check.versionErrorf(y, "go1.13", invalidOp+"signed shift count %s", y)
873873
x.mode = invalid
874874
return
875875
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_
271271
tset := computeInterfaceTypeSet(check, pos, u)
272272
// If typ is local, an error was already reported where typ is specified/defined.
273273
if check != nil && check.isImportedConstraint(typ) && !check.allowVersion(check.pkg, 1, 18) {
274-
check.errorf(pos, "embedding constraint interface %s requires go1.18 or later", typ)
274+
check.versionErrorf(pos, "go1.18", "embedding constraint interface %s", typ)
275275
continue
276276
}
277277
if tset.comparable {
@@ -283,7 +283,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_
283283
terms = tset.terms
284284
case *Union:
285285
if check != nil && !check.allowVersion(check.pkg, 1, 18) {
286-
check.errorf(pos, "embedding interface element %s requires go1.18 or later", u)
286+
check.versionErrorf(pos, "go1.18", "embedding interface element %s", u)
287287
continue
288288
}
289289
tset := computeUnionTypeSet(check, pos, u)
@@ -300,7 +300,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_
300300
continue
301301
}
302302
if check != nil && !check.allowVersion(check.pkg, 1, 18) {
303-
check.errorf(pos, "embedding non-interface type %s requires go1.18 or later", typ)
303+
check.versionErrorf(pos, "go1.18", "embedding non-interface type %s", typ)
304304
continue
305305
}
306306
terms = termlist{{false, typ}}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) {
264264

265265
case *syntax.IndexExpr:
266266
if !check.allowVersion(check.pkg, 1, 18) {
267-
check.softErrorf(e.Pos(), "type instantiation requires go1.18 or later")
267+
check.versionErrorf(e.Pos(), "go1.18", "type instantiation")
268268
}
269269
return check.instantiatedType(e.X, unpackExpr(e.Index), def)
270270

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@ func (check *Checker) langCompat(lit *syntax.BasicLit) {
2121
}
2222
// len(s) > 2
2323
if strings.Contains(s, "_") {
24-
check.error(lit, "underscores in numeric literals requires go1.13 or later")
24+
check.versionErrorf(lit, "go1.13", "underscores in numeric literals")
2525
return
2626
}
2727
if s[0] != '0' {
2828
return
2929
}
3030
radix := s[1]
3131
if radix == 'b' || radix == 'B' {
32-
check.error(lit, "binary literals requires go1.13 or later")
32+
check.versionErrorf(lit, "go1.13", "binary literals")
3333
return
3434
}
3535
if radix == 'o' || radix == 'O' {
36-
check.error(lit, "0o/0O-style octal literals requires go1.13 or later")
36+
check.versionErrorf(lit, "go1.13", "0o/0O-style octal literals")
3737
return
3838
}
3939
if lit.Kind != syntax.IntLit && (radix == 'x' || radix == 'X') {
40-
check.error(lit, "hexadecimal floating-point literals requires go1.13 or later")
40+
check.versionErrorf(lit, "go1.13", "hexadecimal floating-point literals")
4141
}
4242
}
4343

src/cmd/go/testdata/script/mod_edit_go.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
env GO111MODULE=on
44
! go build
5-
stderr 'type aliases only supported as of'
5+
stderr ' type aliases requires'
66
go mod edit -go=1.9
77
grep 'go 1.9' go.mod
88
go build
@@ -11,7 +11,7 @@ go build
1111
# the cached 1.9 build. (https://golang.org/issue/37804)
1212
go mod edit -go=1.8
1313
! go build
14-
stderr 'type aliases only supported as of'
14+
stderr 'type aliases requires'
1515

1616

1717
-- go.mod --

test/fixedbugs/issue49368.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// errorcheck -lang=go1.17
2+
3+
// Copyright 2021 The Go Authors. All rights reserved.
4+
// Use of this source code is governed by a BSD-style
5+
// license that can be found in the LICENSE file.
6+
7+
package p
8+
9+
type _ interface {
10+
int // ERROR "embedding non-interface type int requires go1\.18 or later \(-lang was set to go1\.17; check go.mod\)"
11+
}

0 commit comments

Comments
 (0)