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: doesn't simplify slices if there is a dot import #15153

Closed
dominikh opened this issue Apr 6, 2016 · 7 comments
Closed

cmd/gofmt: doesn't simplify slices if there is a dot import #15153

dominikh opened this issue Apr 6, 2016 · 7 comments
Milestone

Comments

@dominikh
Copy link
Member

dominikh commented Apr 6, 2016

Please answer these questions before submitting your issue. Thanks!

What version of Go are you using (go version)?
go version go1.6 linux/amd64

What operating system and processor architecture are you using (go env)?
/

What did you do?
Run gofmt -s on http://play.golang.org/p/9NhvjR24S1

What did you expect to see?
I expected s[:len(s)] to be simplified to s{;}

What did you see instead?
It wasn't simplified.

gofmt has a special check for dot imports and refuses to simplify slices in that case, claiming that it's impossible to know whether len is referring to the built-in function or not. I don't see how that could be true. Even a dot import will only pull in exported identifiers from a package, and no exported identifier can be called "len". Am I missing something here?

@bradfitz
Copy link
Contributor

bradfitz commented Apr 6, 2016

/cc @griesemer

@bradfitz bradfitz added this to the Unplanned milestone Apr 6, 2016
@griesemer
Copy link
Contributor

@dominikh You're absolutely correct. In fact, even w/o the dot-import we actually can't be certain whether len is the builtin len since it might be declared in another file of the package, and gofmt is by design only working on a file-by-file basis and thus cannot type-check the package and resolve an identifier for certain.

So in some sense this was always wrong, but in practice, nobody in his/her right mind is going to declare a len function (as opposed to a len method which might be quite common).

I'm not sure how I got to this code - for sure it's pretty old code. Would need to track down the history of that line to figure it out. Let's blame it on lack of coffee. Fix coming shortly.

@griesemer griesemer self-assigned this Apr 6, 2016
@griesemer griesemer modified the milestones: Go1.7, Unplanned Apr 6, 2016
@dominikh
Copy link
Member Author

dominikh commented Apr 6, 2016

Great, thanks.

P.S.: Anyone know where the description of the issue went? It certainly wasn't empty earlier :-)

@griesemer
Copy link
Contributor

@dominikh No idea what happened here - I certainly didn't touch it. Hmm...

@gopherbot
Copy link

CL https://golang.org/cl/21613 mentions this issue.

@dominikh
Copy link
Member Author

dominikh commented Apr 6, 2016

I've restored the description… let's hope GitHub doesn't eat it again.

@martisch
Copy link
Contributor

martisch commented Apr 6, 2016

btw origin of the code:
0cbca26

@golang golang locked and limited conversation to collaborators Apr 6, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants