Closed thesilentg closed 5 months ago
CC @golang/tools-team
In this case, the LHS type, mock.Testing.T
, is a named interface type, so I think the code is working as intended. (It would be very surprising if it were not, as the only place in the code that inserts an entry into the Results set is Finder.assign, and it is dominated by an if isInterface(lhs)
check; the isInterface predicate strips off types.Named
and types.Alias
constructors as needed.)
When we describe something as "an interface type", we usually intend that to include aliases and named types whose underlying type is an interface type, though it sometimes depends on the context, and the difference is often significant. In this instance, the fact that Constraint.LHS has type Type
, not *Interface
, is a tiny hint that it may be a named or alias type too. Preserving the Named/Alias constructors leads to much better error messages in tool such as gopls' Rename operation.
I hope that makes sense.
Thanks for the context and clarification, that makes sense.
Go version
go version go1.22.0 darwin/amd64
Output of
go env
in your module/workspace:What did you do?
go.mod
example2/main.go
I ran the
refactor/satisfy
tool against thisexample2
package in thescratchpad
module. The code looks roughly as below:What did you see happen?
From the refactor/satisfy docs:
However, one of the constraints returned is actually the opposite (LHS = types.Named, RHS = types.Interface) as compared to the expected (LHS = types.Interface, RHS = types.Named).
Printing this returns:
The LHS is:
*types.Named
The RHS is:
*types.Interface
What did you expect to see?
Honestly, I'm not sure.
mock.TestingT
is actually an interface (and you can see this via a call toUnderlying()
on the named type).I see two options:
*types.Interface
was correct, and the code forrefactor/satisfy
should be updated to ensure that this holds.*types.Interface
was unfounded, and I should update my code to account for potentially receiving atypes.Named
for LHS and then accessing the underlying interface.