Skip to content

Using -switch may lead to dead code and the Go compiler complaining #181

@Necoro

Description

@Necoro

I noticed that when using -switch that the resulting code seems to be sub-par and does checks multiple times. For instance, I see

switch buffer[position] {
// [...]
case ' ':
	if buffer[position] != rune(' ') {
		goto l0
	}
	position++

The if is completely redundant (and slightly removes the point of switch if the ifs are just kept but now inside a switch :))

There are other, more involved examples:

switch buffer[position] {
case '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
	{
		position5, tokenIndex5 := position, tokenIndex
		if c := buffer[position]; c < rune('0') || c > rune('9') {
			goto l6
		}
		position++
		goto l5
	l6:
		position, tokenIndex = position5, tokenIndex5
		if c := buffer[position]; c < rune('0') || c > rune('9') {
			goto l0
		}
		position++
	}
	l5:
		break

Here the whole code except for position++ is superfluous.

Example PEG:

package minimal

type Parser Peg {
}

OtherAttr <- [[A-Z_ 0-9]]+ _ ':' [^\n]*

_ <- [ \t]*

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions