Skip to content

Commit 0253c0f

Browse files
committed
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 <[email protected]> Reviewed-by: Robert Griesemer <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 206df8e commit 0253c0f

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
431431
for i, typ := range inferred {
432432
if typ == nil || isParameterized(tparams, typ) {
433433
obj := tparams[i].obj
434-
err.addf(pos, "cannot infer %s (%v)", obj.name, obj.pos)
434+
err.addf(pos, "cannot infer %s (declared at %v)", obj.name, obj.pos)
435435
return nil
436436
}
437437
}

src/cmd/internal/testdir/testdir_test.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -1212,7 +1212,7 @@ func (t test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err
12121212
for i := range out {
12131213
for j := 0; j < len(fullshort); j += 2 {
12141214
full, short := fullshort[j], fullshort[j+1]
1215-
out[i] = strings.Replace(out[i], full, short, -1)
1215+
out[i] = replacePrefix(out[i], full, short)
12161216
}
12171217
}
12181218

@@ -1962,3 +1962,23 @@ func splitQuoted(s string) (r []string, err error) {
19621962
}
19631963
return args, err
19641964
}
1965+
1966+
// replacePrefix is like strings.ReplaceAll, but only replaces instances of old
1967+
// that are preceded by ' ', '\t', or appear at the beginning of a line.
1968+
//
1969+
// This does the same kind of filename string replacement as cmd/go.
1970+
// Pilfered from src/cmd/go/internal/work/shell.go .
1971+
func replacePrefix(s, old, new string) string {
1972+
n := strings.Count(s, old)
1973+
if n == 0 {
1974+
return s
1975+
}
1976+
1977+
s = strings.ReplaceAll(s, " "+old, " "+new)
1978+
s = strings.ReplaceAll(s, "\n"+old, "\n"+new)
1979+
s = strings.ReplaceAll(s, "\n\t"+old, "\n\t"+new)
1980+
if strings.HasPrefix(s, old) {
1981+
s = new + s[len(old):]
1982+
}
1983+
return s
1984+
}

src/go/types/infer.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/fixedbugs/issue68292.go

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

0 commit comments

Comments
 (0)