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

go/parser: goto; is incorrectly valid #70957

Open
mateusz834 opened this issue Dec 22, 2024 · 4 comments · May be fixed by #70958
Open

go/parser: goto; is incorrectly valid #70957

mateusz834 opened this issue Dec 22, 2024 · 4 comments · May be fixed by #70958
Assignees
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@mateusz834
Copy link
Member

mateusz834 commented Dec 22, 2024

While poking around the go/types internals, i noticed that nothing really "type-checks" gotos without labels.

First pass:

go/src/go/types/stmt.go

Lines 578 to 608 in 500675a

case *ast.BranchStmt:
if s.Label != nil {
check.hasLabel = true
return // checked in 2nd pass (check.labels)
}
switch s.Tok {
case token.BREAK:
if ctxt&breakOk == 0 {
check.error(s, MisplacedBreak, "break not in for, switch, or select statement")
}
case token.CONTINUE:
if ctxt&continueOk == 0 {
check.error(s, MisplacedContinue, "continue not in for statement")
}
case token.FALLTHROUGH:
if ctxt&fallthroughOk == 0 {
var msg string
switch {
case ctxt&finalSwitchCase != 0:
msg = "cannot fallthrough final case in switch"
case ctxt&inTypeSwitch != 0:
msg = "cannot fallthrough in type switch"
default:
msg = "fallthrough statement out of place"
}
check.error(s, MisplacedFallthrough, msg)
}
default:
check.errorf(s, InvalidSyntaxTree, "branch statement: %s", s.Tok)
}

Second pass:

go/src/go/types/labels.go

Lines 173 to 176 in 500675a

case *ast.BranchStmt:
if s.Label == nil {
return // checked in 1st pass (check.stmt)
}

Also it also turns out that:

func test() {
    goto;
}

Is accepted by the go/parser. The spec disallows gotos without labels, so we should either return an appropriate error in go/types or go/parser. Currently go/types returns an InvalidSyntaxTree error.

go/src/go/types/stmt.go

Lines 605 to 607 in 500675a

default:
check.errorf(s, InvalidSyntaxTree, "branch statement: %s", s.Tok)
}

CC @griesemer @adonovan

@mateusz834 mateusz834 added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Dec 22, 2024
@mateusz834
Copy link
Member Author

Actually, this looks like a bug in go/parser:

go/src/go/parser/parser.go

Lines 2051 to 2056 in 500675a

pos := p.expect(tok)
var label *ast.Ident
if tok != token.FALLTHROUGH && p.tok == token.IDENT {
label = p.parseIdent()
}
p.expectSemi()

Will send a CL.

@mateusz834 mateusz834 self-assigned this Dec 22, 2024
@mateusz834
Copy link
Member Author

cmd/compile/internal/syntax currently reports this as an error:

case _Goto:
s := new(BranchStmt)
s.pos = p.pos()
s.Tok = _Goto
p.next()
s.Label = p.name()
return s

@mateusz834 mateusz834 linked a pull request Dec 22, 2024 that will close this issue
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/638395 mentions this issue: go/parser: require label after goto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants