Skip to content

Commit 41a9213

Browse files
committed
go/packages: report type errors unconditionally
Ensure that when types.Checker.Files returns an error some error is always reported and the package is illTyped. Adds an additional hint to recompile the tool when types returns an error "package requires newer Go version 1.22" or similar. Updates golang/go#65608 Updates golang/go#66525 Change-Id: I131ee3e668815f88d16a18c6e92f002220284a03 Reviewed-on: https://go-review.googlesource.com/c/tools/+/574255 Reviewed-by: Zvonimir Pavlinovic <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Michael Matloob <[email protected]> Reviewed-by: Alan Donovan <[email protected]>
1 parent 7204363 commit 41a9213

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

go/packages/packages.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1129,10 +1129,24 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
11291129
return
11301130
}
11311131
}
1132-
types.NewChecker(tc, ld.Fset, lpkg.Types, lpkg.TypesInfo).Files(lpkg.Syntax)
11331132

1133+
typErr := types.NewChecker(tc, ld.Fset, lpkg.Types, lpkg.TypesInfo).Files(lpkg.Syntax)
11341134
lpkg.importErrors = nil // no longer needed
11351135

1136+
// In go/types go1.21 and go1.22, Checker.Files failed fast with a
1137+
// a "too new" error, without calling tc.Error and without
1138+
// proceeding to type-check the package (#66525).
1139+
// We rely on the runtimeVersion error to give the suggested remedy.
1140+
if typErr != nil && len(lpkg.Errors) == 0 && len(lpkg.Syntax) > 0 {
1141+
if msg := typErr.Error(); strings.HasPrefix(msg, "package requires newer Go version") {
1142+
appendError(types.Error{
1143+
Fset: ld.Fset,
1144+
Pos: lpkg.Syntax[0].Package,
1145+
Msg: msg,
1146+
})
1147+
}
1148+
}
1149+
11361150
// If !Cgo, the type-checker uses FakeImportC mode, so
11371151
// it doesn't invoke the importer for import "C",
11381152
// nor report an error for the import,
@@ -1154,6 +1168,12 @@ func (ld *loader) loadPackage(lpkg *loaderPackage) {
11541168
}
11551169
}
11561170

1171+
// If types.Checker.Files had an error that was unreported,
1172+
// make sure to report the unknown error so the package is illTyped.
1173+
if typErr != nil && len(lpkg.Errors) == 0 {
1174+
appendError(typErr)
1175+
}
1176+
11571177
// Record accumulated errors.
11581178
illTyped := len(lpkg.Errors) > 0
11591179
if !illTyped {

0 commit comments

Comments
 (0)