fix(format): never peel a stacked label that names a macro

Peeling a stacked label whose name is a macro moves it onto a line of
its own, where its expansion decides the line's shape: an empty body
leaves a bare colon behind, a line the parser rejects.  The peel loops
now hold such labels back with the rest of the line, and the crashing
input joins the corpus.

Assisted-by: GLM 5.3
This commit is contained in:
petrbalvin committed 2026-10-07 13:54:42 +02:00
1 parent b54d2b4520
commit f932c5811c
3 files changed
+18 -3

No files matched your search

+10
View File
@@ -117,6 +117,16 @@ func TestFormatPreservesExpansionSemantics(t *testing.T) {
"\tA: M\n" +
"\tRET\n",
},
{
// A stacked label naming a macro: its expansion decides what
// the line becomes (an empty body makes it vanish), so it is
// never peeled onto a line of its own.
name: "stacked label naming a macro",
src: "#define M\n" +
"TEXT ·f(SB), $0\n" +
"\tA: M:\n" +
"\tRET\n",
},
{
// A use above its define: the name is not a macro at that point
// of the file, so splitting would be safe; the whole-file set
+6 -3
View File
@@ -70,10 +70,11 @@ func Source(src string) string {
// instruction after the last one is rendered at the
// function's alignment width, so its mnemonic counts here,
// unless it names a macro and never reaches a line of its
// own.
// own. A stacked label naming a macro is never peeled
// either: its expansion decides what the line becomes.
rest := line[2:]
for len(rest) >= 2 && rest[0].Kind == token.Ident && rest[1].Kind == token.Colon &&
!isDirective(rest[0].Text) {
!isDirective(rest[0].Text) && !macros[rest[0].Text] {
rest = rest[2:]
}
if len(rest) > 0 && rest[0].Kind == token.Ident && !isDirective(rest[0].Text) &&
@@ -146,8 +147,10 @@ func Source(src string) string {
// honest about what it is looking at.
outs = append(outs, outLine{kind: kLabel, text: line[0].Text + ":"})
rest := line[2:]
// A stacked label naming a macro is never peeled: its expansion
// decides what the line becomes, exactly as the first label's.
for len(rest) >= 2 && rest[0].Kind == token.Ident && rest[1].Kind == token.Colon &&
!isDirective(rest[0].Text) {
!isDirective(rest[0].Text) && !macros[rest[0].Text] {
outs = append(outs, outLine{kind: kLabel, text: rest[0].Text + ":"})
rest = rest[2:]
}
@@ -0,0 +1,2 @@
go test fuzz v1
string("#define M \nA:M:")