Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/compiler/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,14 @@ func (c *codegen) Visit(node ast.Node) ast.Visitor {
c.currentSwitch = label
c.pushStackLabel(label, 1)

last := len(n.Body.List) - 1
for i := range last {
if n.Body.List[i].(*ast.CaseClause).List == nil { // early default
n.Body.List[i], n.Body.List[last] = n.Body.List[last], n.Body.List[i]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to check if this substitution affects generated debug info. Lines info stored in debug info for substituted default should match the source code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how can i check this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See how saveSequencePoint works, ensure that proper statement boundaries will be stored in the debug info file on compilation:

func (c *codegen) saveSequencePoint(n ast.Node) {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break
}
}

startLabels := make([]uint16, len(n.Body.List))
for i := range startLabels {
startLabels[i] = c.newLabel()
Expand Down
40 changes: 40 additions & 0 deletions pkg/compiler/switch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,46 @@ var switchTestCases = []testCase{
`,
big.NewInt(1),
},
{
"case after first default",
`func F%d() int {
a := 5
switch a {
default: return 4
case 5: return 2
}
}
`,
big.NewInt(2),
},
{
"case after intermediate default",
`func F%d() int {
a := 6
switch a {
case 5: return 2
default: return 4
case 6:
}
return 1
}
`,
big.NewInt(1),
},
{
"intermediate default",
`func F%d() int {
a := 3
switch a {
case 5: return 2
default: return 4
case 6:
}
return 1
}
`,
big.NewInt(4),
},
{
"expression in case clause",
`func F%d() int {
Expand Down
Loading