From c9c95f97fda18f56ab4c8c406e9f48a4cb3b3d7e Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Fri, 5 Jan 2024 14:54:16 -0500 Subject: [PATCH] internal/refactor/inline: improve a confusing error message Change-Id: I31e587105ffd16dbe32436ce5a090180f5186200 Reviewed-on: https://go-review.googlesource.com/c/tools/+/554061 Auto-Submit: Alan Donovan LUCI-TryBot-Result: Go LUCI Reviewed-by: Robert Findley --- internal/refactor/inline/inline.go | 7 ++++--- internal/refactor/inline/testdata/err-shadow-builtin.txtar | 6 +++--- internal/refactor/inline/testdata/err-shadow-pkg.txtar | 4 ++-- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/internal/refactor/inline/inline.go b/internal/refactor/inline/inline.go index 06f64013c79..7eaa6bff3f5 100644 --- a/internal/refactor/inline/inline.go +++ b/internal/refactor/inline/inline.go @@ -485,7 +485,7 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu // check not shadowed at caller. found := caller.lookup(obj.Name) // always finds something if found.Pos().IsValid() { - return nil, fmt.Errorf("cannot inline because built-in %q is shadowed in caller by a %s (line %d)", + return nil, fmt.Errorf("cannot inline, because the callee refers to built-in %q, which in the caller is shadowed by a %s (declared at line %d)", obj.Name, objectKind(found), caller.Fset.PositionFor(found.Pos(), false).Line) } @@ -505,8 +505,9 @@ func inline(logf func(string, ...any), caller *Caller, callee *gobCallee) (*resu // around the refactored signature. found := caller.lookup(obj.Name) if found != nil && !isPkgLevel(found) { - return nil, fmt.Errorf("cannot inline because %q is shadowed in caller by a %s (line %d)", - obj.Name, objectKind(found), + return nil, fmt.Errorf("cannot inline, because the callee refers to %s %q, which in the caller is shadowed by a %s (declared at line %d)", + obj.Kind, obj.Name, + objectKind(found), caller.Fset.PositionFor(found.Pos(), false).Line) } } else { diff --git a/internal/refactor/inline/testdata/err-shadow-builtin.txtar b/internal/refactor/inline/testdata/err-shadow-builtin.txtar index 543d38fe540..34ea586ab3e 100644 --- a/internal/refactor/inline/testdata/err-shadow-builtin.txtar +++ b/internal/refactor/inline/testdata/err-shadow-builtin.txtar @@ -10,7 +10,7 @@ package a func _() { const nil = 1 - _ = f() //@ inline(re"f", re"nil.*shadowed.*by.*const .line 4") + _ = f() //@ inline(re"f", re"nil.*shadowed.*by.*const.*line 4") } func f() *int { return nil } @@ -20,7 +20,7 @@ package a func _() { type append int - g(nil) //@ inline(re"g", re"append.*shadowed.*by.*typename .line 4") + g(nil) //@ inline(re"g", re"append.*shadowed.*by.*typename.*line 4") } func g(x []int) { _ = append(x, x...) } @@ -30,7 +30,7 @@ package a func _() { type int uint8 - _ = h(0) //@ inline(re"h", re"int.*shadowed.*by.*typename .line 4") + _ = h(0) //@ inline(re"h", re"int.*shadowed.*by.*typename.*line 4") } func h(x int) int { return x + 1 } diff --git a/internal/refactor/inline/testdata/err-shadow-pkg.txtar b/internal/refactor/inline/testdata/err-shadow-pkg.txtar index 4338b8b31cd..792418dd453 100644 --- a/internal/refactor/inline/testdata/err-shadow-pkg.txtar +++ b/internal/refactor/inline/testdata/err-shadow-pkg.txtar @@ -15,7 +15,7 @@ package a func _() { f() //@ inline(re"f", result) const v = 1 - f() //@ inline(re"f", re"v.*shadowed.*by.*const .line 5") + f() //@ inline(re"f", re"v.*shadowed.*by.*const.*line 5") } func f() int { return v } @@ -28,7 +28,7 @@ package a func _() { _ = v //@ inline(re"f", result) const v = 1 - f() //@ inline(re"f", re"v.*shadowed.*by.*const .line 5") + f() //@ inline(re"f", re"v.*shadowed.*by.*const.*line 5") } func f() int { return v }