cmd/compile/internal/types2: change inference error message

Changes the type inference error message so that the position is
proceeded by a space. cmd/go rewrites the output of gc to replace
absolute paths at the beginning of lines and those proceeded by a
space or a tab to relative paths.

Updates testdir to do the same post processing on the output
of tests as cmd/go.

Fixes #68292

Change-Id: Ie109b51143e68f6e7ab4cd19064110db0e609a7e
Reviewed-on: https://go-review.googlesource.com/c/go/+/603097
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Robert Griesemer <gri@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Tim King 2024-08-05 12:57:33 -07:00
parent 206df8e7ad
commit 0253c0f4ac
4 changed files with 35 additions and 3 deletions

View File

@ -431,7 +431,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
for i, typ := range inferred { for i, typ := range inferred {
if typ == nil || isParameterized(tparams, typ) { if typ == nil || isParameterized(tparams, typ) {
obj := tparams[i].obj obj := tparams[i].obj
err.addf(pos, "cannot infer %s (%v)", obj.name, obj.pos) err.addf(pos, "cannot infer %s (declared at %v)", obj.name, obj.pos)
return nil return nil
} }
} }

View File

@ -1212,7 +1212,7 @@ func (t test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err
for i := range out { for i := range out {
for j := 0; j < len(fullshort); j += 2 { for j := 0; j < len(fullshort); j += 2 {
full, short := fullshort[j], fullshort[j+1] full, short := fullshort[j], fullshort[j+1]
out[i] = strings.Replace(out[i], full, short, -1) out[i] = replacePrefix(out[i], full, short)
} }
} }
@ -1962,3 +1962,23 @@ func splitQuoted(s string) (r []string, err error) {
} }
return args, err return args, err
} }
// replacePrefix is like strings.ReplaceAll, but only replaces instances of old
// that are preceded by ' ', '\t', or appear at the beginning of a line.
//
// This does the same kind of filename string replacement as cmd/go.
// Pilfered from src/cmd/go/internal/work/shell.go .
func replacePrefix(s, old, new string) string {
n := strings.Count(s, old)
if n == 0 {
return s
}
s = strings.ReplaceAll(s, " "+old, " "+new)
s = strings.ReplaceAll(s, "\n"+old, "\n"+new)
s = strings.ReplaceAll(s, "\n\t"+old, "\n\t"+new)
if strings.HasPrefix(s, old) {
s = new + s[len(old):]
}
return s
}

View File

@ -434,7 +434,7 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
for i, typ := range inferred { for i, typ := range inferred {
if typ == nil || isParameterized(tparams, typ) { if typ == nil || isParameterized(tparams, typ) {
obj := tparams[i].obj obj := tparams[i].obj
err.addf(posn, "cannot infer %s (%v)", obj.name, obj.pos) err.addf(posn, "cannot infer %s (declared at %v)", obj.name, obj.pos)
return nil return nil
} }
} }

View File

@ -0,0 +1,12 @@
// errorcheck
// Copyright 2024 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 main
func f[S any, T any](T) {}
func g() {
f(0) // ERROR "in call to f, cannot infer S \(declared at issue68292.go:9:8\)"
}