Skip to content

Commit 5d213a3

Browse files
griesemergopherbot
authored andcommitted
cmd/compile: handle go.mod error msg reference in noder, not type checker
Currently, for version errors, types2 adds the helpful hint (-lang was set to go1.xx; check go.mod) where 1.xx is the respective language version, to the error message. This requires that the type checker knows that it was invoked by the compiler, which is done through the Config.CompilerErrorMessages flag. This change looks for version errors being returned by the type checker and then adds the hint at that point, external to the type checker. This removes a dependency on the Config.CompilerErrorMessages. Once we have removed all dependencies on Config.CompilerErrorMessages we can remove it. For #55326. Change-Id: I1f9b2e472c49fe785a2075e26c4b3d9b8fcdbf4d Reviewed-on: https://go-review.googlesource.com/c/go/+/432559 Reviewed-by: Robert Griesemer <[email protected]> Run-TryBot: Robert Griesemer <[email protected]> Auto-Submit: Robert Griesemer <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 2f30083 commit 5d213a3

File tree

5 files changed

+11
-25
lines changed

5 files changed

+11
-25
lines changed

src/cmd/compile/internal/noder/irgen.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package noder
66

77
import (
88
"fmt"
9+
"regexp"
910
"sort"
1011

1112
"cmd/compile/internal/base"
@@ -18,6 +19,8 @@ import (
1819
"cmd/internal/src"
1920
)
2021

22+
var versionErrorRx = regexp.MustCompile(`requires go[0-9]+\.[0-9]+ or later`)
23+
2124
// checkFiles configures and runs the types2 checker on the given
2225
// parsed source files and then returns the result.
2326
func checkFiles(noders []*noder) (posMap, *types2.Package, *types2.Info) {
@@ -46,7 +49,12 @@ func checkFiles(noders []*noder) (posMap, *types2.Package, *types2.Info) {
4649
CompilerErrorMessages: true, // use error strings matching existing compiler errors
4750
Error: func(err error) {
4851
terr := err.(types2.Error)
49-
base.ErrorfAt(m.makeXPos(terr.Pos), "%s", terr.Msg)
52+
msg := terr.Msg
53+
// if we have a version error, hint at the -lang setting
54+
if versionErrorRx.MatchString(msg) {
55+
msg = fmt.Sprintf("%s (-lang was set to %s; check go.mod)", msg, base.Flag.Lang)
56+
}
57+
base.ErrorfAt(m.makeXPos(terr.Pos), "%s", msg)
5058
},
5159
Importer: &importer,
5260
Sizes: &gcSizes{},

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

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

99
import (
10-
"fmt"
1110
"go/constant"
1211
"unicode"
1312
)
@@ -201,9 +200,6 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
201200
if cause != nil {
202201
// TODO(gri) consider restructuring versionErrorf so we can use it here and below
203202
*cause = "conversion of slices to arrays requires go1.20 or later"
204-
if check.conf.CompilerErrorMessages {
205-
*cause += fmt.Sprintf(" (-lang was set to %s; check go.mod)", check.conf.GoVersion)
206-
}
207203
}
208204
return false
209205
}
@@ -216,9 +212,6 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
216212
// check != nil
217213
if cause != nil {
218214
*cause = "conversion of slices to array pointers requires go1.17 or later"
219-
if check.conf.CompilerErrorMessages {
220-
*cause += fmt.Sprintf(" (-lang was set to %s; check go.mod)", check.conf.GoVersion)
221-
}
222215
}
223216
return false
224217
}

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,7 @@ func (check *Checker) softErrorf(at poser, code errorCode, format string, args .
286286

287287
func (check *Checker) versionErrorf(at poser, goVersion string, format string, args ...interface{}) {
288288
msg := check.sprintf(format, args...)
289-
if check.conf.CompilerErrorMessages {
290-
msg = fmt.Sprintf("%s requires %s or later (-lang was set to %s; check go.mod)", msg, goVersion, check.conf.GoVersion)
291-
} else {
292-
msg = fmt.Sprintf("%s requires %s or later", msg, goVersion)
293-
}
289+
msg = fmt.Sprintf("%s requires %s or later", msg, goVersion)
294290
check.err(at, _UnsupportedFeature, msg, true)
295291
}
296292

src/go/types/conversions.go

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

99
import (
10-
"fmt"
1110
"go/constant"
1211
"go/token"
1312
"unicode"
@@ -201,9 +200,6 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
201200
if cause != nil {
202201
// TODO(gri) consider restructuring versionErrorf so we can use it here and below
203202
*cause = "conversion of slices to arrays requires go1.20 or later"
204-
if compilerErrorMessages {
205-
*cause += fmt.Sprintf(" (-lang was set to %s; check go.mod)", check.conf.GoVersion)
206-
}
207203
}
208204
return false
209205
}
@@ -216,9 +212,6 @@ func (x *operand) convertibleTo(check *Checker, T Type, cause *string) bool {
216212
// check != nil
217213
if cause != nil {
218214
*cause = "conversion of slices to array pointers requires go1.17 or later"
219-
if compilerErrorMessages {
220-
*cause += fmt.Sprintf(" (-lang was set to %s; check go.mod)", check.conf.GoVersion)
221-
}
222215
}
223216
return false
224217
}

src/go/types/errors.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,7 @@ func (check *Checker) softErrorf(at positioner, code errorCode, format string, a
295295
func (check *Checker) versionErrorf(at positioner, code errorCode, goVersion string, format string, args ...interface{}) {
296296
msg := check.sprintf(format, args...)
297297
var err *error_
298-
if compilerErrorMessages {
299-
err = newErrorf(at, code, "%s requires %s or later (-lang was set to %s; check go.mod)", msg, goVersion, check.conf.GoVersion)
300-
} else {
301-
err = newErrorf(at, code, "%s requires %s or later", msg, goVersion)
302-
}
298+
err = newErrorf(at, code, "%s requires %s or later", msg, goVersion)
303299
check.report(err)
304300
}
305301

0 commit comments

Comments
 (0)