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

cmd/gofmt: comment in return params is misplaced #28433

Open
benesch opened this issue Oct 27, 2018 · 5 comments
Open

cmd/gofmt: comment in return params is misplaced #28433

benesch opened this issue Oct 27, 2018 · 5 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@benesch
Copy link
Contributor

benesch commented Oct 27, 2018

What version of Go are you using (go version)?

go version go1.11 darwin/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

Ran gofmt on this program (https://play.golang.org/p/1rASHggr4wx):

package foo

func bar() (
	// This return value is extremely subtle! Please make sure you understand
	// what it does.
	string,
) {
	return "42"
}

What did you expect to see?

The input, unchanged.

What did you see instead?

The program reformatted with the comment in a very strange place (https://play.golang.org/p/NujRVxVN6N5):

package foo

func bar() string {// This return value is extremely subtle! Please make sure you understand
	// what it does.

	return "42"
}

Arguably not that common to stick a comment there, but I find the resulting code extremely surprising. Note that this nearly identical program works just fine (https://play.golang.org/p/Hpfcc5HX00l) because the presence of bazzle makes the parens required and so gofmt doesn't attempt a reformatting:

package foo

func bar() (
	// This return value is extremely subtle! Please make sure you understand
	// what it does.
	bazzle string,
) {
	return "42"
}

/cc @griesemer since you seem to get pinged on all the gofmt issues eventually

@agnivade
Copy link
Contributor

Most probably related to #22631.

@agnivade agnivade added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Oct 27, 2018
@agnivade agnivade added this to the Unplanned milestone Oct 27, 2018
@griesemer
Copy link
Contributor

Seems to be a combination of problems here. I don't think this is related to #22631, this seems more an issue of not having all position information in the AST, and the fact that we don't print unnecessary ()'s around single, unnamed results.

Should be fixed at some point but there's no urgency. Also, independently, not a great place to put a comment in the first place (but that's just my personal taste).

@griesemer griesemer self-assigned this Oct 27, 2018
@bcmills
Copy link
Contributor

bcmills commented Oct 29, 2018

Dup of #23040?

@benesch
Copy link
Contributor Author

benesch commented Oct 29, 2018

Dup of #23040?

💯 thanks! (I did try to find an existing issue before I filed, but issues like this are more or less impossible to search for.)

@benesch benesch closed this as completed Oct 29, 2018
@griesemer
Copy link
Contributor

This is not a duplicate of #23040 - that one is about ast.CommentMaps. There are no comment maps involved here. Reopening.

@griesemer griesemer reopened this Oct 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants