Skip to content

x/tools/internal/refactor/inline: avoid unnecessary interface conversions #68554

Closed
@adonovan

Description

@adonovan

This contrived example demonstrates that the source-level inliner (activated through gopls' refactor.inline code action) adds conversions to interface types even when they are not necessary:

func f(d discard) {
	g(d) // inlining this => fmt.Println(io.Writer(d)), where fmt.Println(d) would do
}

func g(w io.Writer) { fmt.Println(w) }

var d discard
type discard struct{}
func (discard) Write(p []byte) (int, error) { return len(p), nil }

Because fmt.Println's arguments are each converted to any, the implicit conversion from discard to io.Writer is not significant within the body of g and could be omitted. By contrast, were fmt.Println replaced by panic, the conversion would not be safe to remove, because in general panic(x) is not equivalent to panic(any(x)).

The inliner should detect, for each interface-typed parameter of the callee, whether all its uses in the callee's body are converted explicitly or implicitly to an interface type, so that the caller-side interface conversion may be safely omitted.

Googlers: see internal issue b/354664998

Metadata

Metadata

Assignees

Labels

FixPendingIssues that have a fix which has not yet been reviewed or submitted.NeedsFixThe path to resolution is known, but the work has not been done.RefactoringIssues related to refactoring toolsToolsThis label describes issues relating to any tools in the x/tools repository.

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions