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
Assignees
Milestone

Comments

@benesch
Copy link
Contributor

@benesch 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

@agnivade agnivade commented Oct 27, 2018

Most probably related to #22631.

@agnivade agnivade added this to the Unplanned milestone Oct 27, 2018
@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 27, 2018

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
Member

@bcmills bcmills commented Oct 29, 2018

Dup of #23040?

@benesch
Copy link
Contributor Author

@benesch 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 Oct 29, 2018
@griesemer
Copy link
Contributor

@griesemer griesemer commented Oct 29, 2018

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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.