]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: change inference error message
authorTim King <taking@google.com>
Mon, 5 Aug 2024 19:57:33 +0000 (12:57 -0700)
committerTim King <taking@google.com>
Tue, 13 Aug 2024 17:25:36 +0000 (17:25 +0000)
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>

src/cmd/compile/internal/types2/infer.go
src/cmd/internal/testdir/testdir_test.go
src/go/types/infer.go
test/fixedbugs/issue68292.go [new file with mode: 0644]

index 122ac9e04fce3edbc7ed64843c4701ea2d949f35..219942862f17c4dc1cfd5a823ed5bf4854a53394 100644 (file)
@@ -431,7 +431,7 @@ func (check *Checker) infer(pos syntax.Pos, tparams []*TypeParam, targs []Type,
        for i, typ := range inferred {
                if typ == nil || isParameterized(tparams, typ) {
                        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
                }
        }
index c494f2c4c0125e93d2ccf2385b9de6c1a4b83366..68fbdffb1dc5d4bdd337d64453e5ab1b3a7dae22 100644 (file)
@@ -1212,7 +1212,7 @@ func (t test) errorCheck(outStr string, wantAuto bool, fullshort ...string) (err
        for i := range out {
                for j := 0; j < len(fullshort); j += 2 {
                        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
 }
+
+// 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
+}
index d0f1c1caf48f2a4d79ac6cdf1e9e4a2d550ed557..4da4513c7b5a8342778a446b2dea4337612b98d9 100644 (file)
@@ -434,7 +434,7 @@ func (check *Checker) infer(posn positioner, tparams []*TypeParam, targs []Type,
        for i, typ := range inferred {
                if typ == nil || isParameterized(tparams, typ) {
                        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
                }
        }
diff --git a/test/fixedbugs/issue68292.go b/test/fixedbugs/issue68292.go
new file mode 100644 (file)
index 0000000..2a0d826
--- /dev/null
@@ -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\)"
+}