Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/internal/refactor/inline: semantic change when inlining variadic functions #69441

Closed
lfolger opened this issue Sep 13, 2024 · 6 comments
Assignees
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@lfolger
Copy link
Contributor

lfolger commented Sep 13, 2024

Steps to reproduce:

func TestInlineVariadics(t *testing.T) {
	files := map[string]string{
		"foo/foo.go": `
			package foo
			import "fmt"
				func F(is ...int) {
					if is == nil {
						fmt.Println("is is nil")
					} else {
						fmt.Println("is is not nil")
					}
				}

				//inlineme
				func G(is ...int) { F(is...) }

				func main() {
					G() // want "inline call of foo.G"
				}
		`,
		"foo/foo.go.golden": `
			package foo
			import "fmt"
				func F(is ...int) {
					if is == nil {
						fmt.Println("is is nil")
					} else {
						fmt.Println("is is not nil")
					}
				}

				func G(is ...int) { F(is...) }

				func main() {
					F() // want "inline call of foo.G"
				}
		`,
	}
	dir, cleanup, err := analysistest.WriteFiles(files)
	if err != nil {
		t.Fatal(err)
	}
	analysistest.RunWithSuggestedFixes(t, dir, analyzer.Analyzer, "foo")
	cleanup()
}

The test fails with:

        --- ..../foo/foo.go.golden
        +++ actual
        @@ -9,9 +9,10 @@

         func main() {
        -       F() // want "inline call of foo.G"
        +       F([]int{}...) // want "inline call of foo.G"
         }

Before the inling the program was printing is is nil after the inlining it is printing is is not nil.

@lfolger lfolger added the Tools This label describes issues relating to any tools in the x/tools repository. label Sep 13, 2024
@gopherbot gopherbot added this to the Unreleased milestone Sep 13, 2024
@gabyhelp
Copy link

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

@lfolger
Copy link
Contributor Author

lfolger commented Sep 13, 2024

cc: @findleyr @adonovan

@timothy-king timothy-king added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Sep 13, 2024
@timothy-king timothy-king modified the milestones: Unreleased, Backlog Sep 13, 2024
@findleyr findleyr added the Refactoring Issues related to refactoring tools label Sep 18, 2024
@adonovan
Copy link
Member

adonovan commented Nov 5, 2024

Possibly the same underlying cause as #63717, and the same fix.

While you are correct that the inliner makes a semantically observable change, I think it is bad form for variadic functions to assume anything about the nilness of the argument list.

@lfolger
Copy link
Contributor Author

lfolger commented Nov 11, 2024

The primary reason, I reported this is because it leads to undesirable rewrites.

It always adds an argument for the variadic parameter, even if that argument is an empty slice.

It would be great if the inliner would only add the argument when it is actually needed.

@adonovan
Copy link
Member

It would be great if the inliner would only add the argument when it is actually needed.

I agree; that is #63717.

@findleyr findleyr modified the milestones: Backlog, gopls/v0.17.0 Nov 19, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/629136 mentions this issue: internal/refactor: undo variadic elimination during substitution

@findleyr findleyr added the FixPending Issues that have a fix which has not yet been reviewed or submitted. label Nov 19, 2024
@findleyr findleyr self-assigned this Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

6 participants