Skip to content

go/token: MergeLine(0) should be a no-op, not a panic #9921

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

Closed
rsc opened this issue Feb 18, 2015 · 2 comments
Closed

go/token: MergeLine(0) should be a no-op, not a panic #9921

rsc opened this issue Feb 18, 2015 · 2 comments
Milestone

Comments

@rsc
Copy link
Contributor

rsc commented Feb 18, 2015

This seems to be an idiom (found in ast.SortImports):

fset.File(x.Pos()).MergeLine(fset.Position(x.Pos()).Line)

If x.Pos returns token.NoPos (0), then fset.Position(x.Pos()).Line == 0, and MergeLine panics. Instead of panicking I believe MergeLine should just return silently. This will make code doing things like this much more robust. Otherwise every call site has to guard against x.Pos() == 0.

@rsc rsc added this to the Go1.5 milestone Feb 18, 2015
@josharian
Copy link
Contributor

Calling MergeLine on a line derived from token.NoPos looks like a programmer error to me, and thus a panic seems appropriate.

@griesemer what do you think? If you want it not to panic, I'll send a CL.

@griesemer
Copy link
Contributor

I am also leaning towards leaving this as is.

Note that if x.Pos() returns token.NoPos, then fset.File(x.Pos()) returns nil as well. It's not clear how MergeLine should be changed: I don't think we want to silently do nothing if the *File receiver is nil as that would hide errors. So we could only silently return if both the *File receiver is nil and the line number is 0. But that seems odd, too.

Please re-open if you have strong counterarguments. I don't know that MergeLine is used all that frequently, and having callers making sure that the incoming *File and line are correct seems ok.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
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

4 participants