]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: use compiler version error when configured for compiler
authorRobert Griesemer <gri@golang.org>
Fri, 5 Nov 2021 03:00:51 +0000 (20:00 -0700)
committerRobert Griesemer <gri@golang.org>
Mon, 8 Nov 2021 16:14:55 +0000 (16:14 +0000)
Fixes #49368.

Change-Id: I7c7575ae8bb6271160747e3f1888b144c3ab24c4
Reviewed-on: https://go-review.googlesource.com/c/go/+/361411
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/builtins.go
src/cmd/compile/internal/types2/call.go
src/cmd/compile/internal/types2/conversions.go
src/cmd/compile/internal/types2/decl.go
src/cmd/compile/internal/types2/errors.go
src/cmd/compile/internal/types2/expr.go
src/cmd/compile/internal/types2/typeset.go
src/cmd/compile/internal/types2/typexpr.go
src/cmd/compile/internal/types2/version.go
src/cmd/go/testdata/script/mod_edit_go.txt
test/fixedbugs/issue49368.go [new file with mode: 0644]

index 548d55e10c564330e8601890ae7b397d483bd5ba..ade4c0a49f2677d4acf2f9a499545cbaf05f746f 100644 (file)
@@ -574,7 +574,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
        case _Add:
                // unsafe.Add(ptr unsafe.Pointer, len IntegerType) unsafe.Pointer
                if !check.allowVersion(check.pkg, 1, 17) {
-                       check.error(call.Fun, "unsafe.Add requires go1.17 or later")
+                       check.versionErrorf(call.Fun, "go1.17", "unsafe.Add")
                        return
                }
 
@@ -700,7 +700,7 @@ func (check *Checker) builtin(x *operand, call *syntax.CallExpr, id builtinId) (
        case _Slice:
                // unsafe.Slice(ptr *T, len IntegerType) []T
                if !check.allowVersion(check.pkg, 1, 17) {
-                       check.error(call.Fun, "unsafe.Slice requires go1.17 or later")
+                       check.versionErrorf(call.Fun, "go1.17", "unsafe.Slice")
                        return
                }
 
index 49cae5a930d544debf09ef3252f43d0db88fc326..74edd4d4429d891fdbc9e69e5a9883226e795ed9 100644 (file)
@@ -16,7 +16,7 @@ import (
 // The operand x must be the evaluation of inst.X and its type must be a signature.
 func (check *Checker) funcInst(x *operand, inst *syntax.IndexExpr) {
        if !check.allowVersion(check.pkg, 1, 18) {
-               check.softErrorf(inst.Pos(), "function instantiation requires go1.18 or later")
+               check.versionErrorf(inst.Pos(), "go1.18", "function instantiation")
        }
 
        xlist := unpackExpr(inst.Index)
@@ -363,9 +363,9 @@ func (check *Checker) arguments(call *syntax.CallExpr, sig *Signature, targs []T
        if sig.TypeParams().Len() > 0 {
                if !check.allowVersion(check.pkg, 1, 18) {
                        if iexpr, _ := call.Fun.(*syntax.IndexExpr); iexpr != nil {
-                               check.softErrorf(iexpr.Pos(), "function instantiation requires go1.18 or later")
+                               check.versionErrorf(iexpr.Pos(), "go1.18", "function instantiation")
                        } else {
-                               check.softErrorf(call.Pos(), "implicit function instantiation requires go1.18 or later")
+                               check.versionErrorf(call.Pos(), "go1.18", "implicit function instantiation")
                        }
                }
                // TODO(gri) provide position information for targs so we can feed
index 44e8aad84f065b4730d903489a1b34d4f01254b8..ccabbaf0d740d751fd448066876171508410342a 100644 (file)
@@ -7,6 +7,7 @@
 package types2
 
 import (
+       "fmt"
        "go/constant"
        "unicode"
 )
@@ -181,11 +182,9 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
                                        }
                                        // check != nil
                                        if cause != nil {
+                                               *cause = "conversion of slices to array pointers requires go1.17 or later"
                                                if check.conf.CompilerErrorMessages {
-                                                       // compiler error message assumes a -lang flag
-                                                       *cause = "conversion of slices to array pointers only supported as of -lang=go1.17"
-                                               } else {
-                                                       *cause = "conversion of slices to array pointers requires go1.17 or later"
+                                                       *cause += fmt.Sprintf(" (-lang was set to %s; check go.mod)", check.conf.GoVersion)
                                                }
                                        }
                                        return false
index 5d2a6c531b41ec5bdb7890faa39b04b5999dcb7d..5219f7e7c5448ed8350024ff2521b3b96bb279d6 100644 (file)
@@ -555,7 +555,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
                check.validType(obj.typ, nil)
                // If typ is local, an error was already reported where typ is specified/defined.
                if check.isImportedConstraint(rhs) && !check.allowVersion(check.pkg, 1, 18) {
-                       check.errorf(tdecl.Type.Pos(), "using type constraint %s requires go1.18 or later", rhs)
+                       check.versionErrorf(tdecl.Type.Pos(), "go1.18", "using type constraint %s", rhs)
                }
        }).describef(obj, "validType(%s)", obj.Name())
 
@@ -570,11 +570,7 @@ func (check *Checker) typeDecl(obj *TypeName, tdecl *syntax.TypeDecl, def *Named
        // alias declaration
        if alias {
                if !check.allowVersion(check.pkg, 1, 9) {
-                       if check.conf.CompilerErrorMessages {
-                               check.error(tdecl, "type aliases only supported as of -lang=go1.9")
-                       } else {
-                               check.error(tdecl, "type aliases requires go1.9 or later")
-                       }
+                       check.versionErrorf(tdecl, "go1.9", "type aliases")
                }
 
                obj.typ = Typ[Invalid]
index b56d11a28bfdb007524f2e939d47d9adb4f1946b..c39652fe5e4378bc254151060afedee4feec8cd9 100644 (file)
@@ -230,6 +230,16 @@ func (check *Checker) softErrorf(at poser, format string, args ...interface{}) {
        check.err(at, check.sprintf(format, args...), true)
 }
 
+func (check *Checker) versionErrorf(at poser, goVersion string, format string, args ...interface{}) {
+       msg := check.sprintf(format, args...)
+       if check.conf.CompilerErrorMessages {
+               msg = fmt.Sprintf("%s requires %s or later (-lang was set to %s; check go.mod)", msg, goVersion, check.conf.GoVersion)
+       } else {
+               msg = fmt.Sprintf("%s requires %s or later", msg, goVersion)
+       }
+       check.err(at, msg, true)
+}
+
 // posFor reports the left (= start) position of at.
 func posFor(at poser) syntax.Pos {
        switch x := at.(type) {
index d618ebd3722683a8f4b72bee806811999ba47856..d24532d780d26ceea91c16bebb275c6922d710c8 100644 (file)
@@ -869,7 +869,7 @@ func (check *Checker) shift(x, y *operand, e syntax.Expr, op syntax.Operator) {
                x.mode = invalid
                return
        } else if !allUnsigned(y.typ) && !check.allowVersion(check.pkg, 1, 13) {
-               check.errorf(y, invalidOp+"signed shift count %s requires go1.13 or later", y)
+               check.versionErrorf(y, "go1.13", invalidOp+"signed shift count %s", y)
                x.mode = invalid
                return
        }
index 445a62f9e06280d5d34cfb047996e4369511f92d..c37a20e73e85448d83dd5fc49fcb2cf45c7386ae 100644 (file)
@@ -271,7 +271,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_
                        tset := computeInterfaceTypeSet(check, pos, u)
                        // If typ is local, an error was already reported where typ is specified/defined.
                        if check != nil && check.isImportedConstraint(typ) && !check.allowVersion(check.pkg, 1, 18) {
-                               check.errorf(pos, "embedding constraint interface %s requires go1.18 or later", typ)
+                               check.versionErrorf(pos, "go1.18", "embedding constraint interface %s", typ)
                                continue
                        }
                        if tset.comparable {
@@ -283,7 +283,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_
                        terms = tset.terms
                case *Union:
                        if check != nil && !check.allowVersion(check.pkg, 1, 18) {
-                               check.errorf(pos, "embedding interface element %s requires go1.18 or later", u)
+                               check.versionErrorf(pos, "go1.18", "embedding interface element %s", u)
                                continue
                        }
                        tset := computeUnionTypeSet(check, pos, u)
@@ -300,7 +300,7 @@ func computeInterfaceTypeSet(check *Checker, pos syntax.Pos, ityp *Interface) *_
                                continue
                        }
                        if check != nil && !check.allowVersion(check.pkg, 1, 18) {
-                               check.errorf(pos, "embedding non-interface type %s requires go1.18 or later", typ)
+                               check.versionErrorf(pos, "go1.18", "embedding non-interface type %s", typ)
                                continue
                        }
                        terms = termlist{{false, typ}}
index 95893fd1e1d50afb8ac9f5c453c87fd9e7096d08..dcd7cfebe829712cae03211787ed16d21a72776d 100644 (file)
@@ -264,7 +264,7 @@ func (check *Checker) typInternal(e0 syntax.Expr, def *Named) (T Type) {
 
        case *syntax.IndexExpr:
                if !check.allowVersion(check.pkg, 1, 18) {
-                       check.softErrorf(e.Pos(), "type instantiation requires go1.18 or later")
+                       check.versionErrorf(e.Pos(), "go1.18", "type instantiation")
                }
                return check.instantiatedType(e.X, unpackExpr(e.Index), def)
 
index d9d18b6f7a7f487e88320324fd7f457b5704b023..b649f09c3aa95ad333e1dcad2e4988ea0d549ff1 100644 (file)
@@ -21,7 +21,7 @@ func (check *Checker) langCompat(lit *syntax.BasicLit) {
        }
        // len(s) > 2
        if strings.Contains(s, "_") {
-               check.error(lit, "underscores in numeric literals requires go1.13 or later")
+               check.versionErrorf(lit, "go1.13", "underscores in numeric literals")
                return
        }
        if s[0] != '0' {
@@ -29,15 +29,15 @@ func (check *Checker) langCompat(lit *syntax.BasicLit) {
        }
        radix := s[1]
        if radix == 'b' || radix == 'B' {
-               check.error(lit, "binary literals requires go1.13 or later")
+               check.versionErrorf(lit, "go1.13", "binary literals")
                return
        }
        if radix == 'o' || radix == 'O' {
-               check.error(lit, "0o/0O-style octal literals requires go1.13 or later")
+               check.versionErrorf(lit, "go1.13", "0o/0O-style octal literals")
                return
        }
        if lit.Kind != syntax.IntLit && (radix == 'x' || radix == 'X') {
-               check.error(lit, "hexadecimal floating-point literals requires go1.13 or later")
+               check.versionErrorf(lit, "go1.13", "hexadecimal floating-point literals")
        }
 }
 
index 38321d071fb35e9adf1c8664f9c7de6edfab9549..7e9740fec43763a0727719f623c7ee708e98de97 100644 (file)
@@ -2,7 +2,7 @@
 
 env GO111MODULE=on
 ! go build
-stderr 'type aliases only supported as of'
+stderr ' type aliases requires'
 go mod edit -go=1.9
 grep 'go 1.9' go.mod
 go build
@@ -11,7 +11,7 @@ go build
 # the cached 1.9 build. (https://golang.org/issue/37804)
 go mod edit -go=1.8
 ! go build
-stderr 'type aliases only supported as of'
+stderr 'type aliases requires'
 
 
 -- go.mod --
diff --git a/test/fixedbugs/issue49368.go b/test/fixedbugs/issue49368.go
new file mode 100644 (file)
index 0000000..2339048
--- /dev/null
@@ -0,0 +1,11 @@
+// errorcheck -lang=go1.17
+
+// 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.
+
+package p
+
+type _ interface {
+       int // ERROR "embedding non-interface type int requires go1\.18 or later \(-lang was set to go1\.17; check go.mod\)"
+}