Skip to content

Commit c65d1c8

Browse files
dejlekWebFreak001
authored andcommitted
Fix @attr+do/body concatenation and expand issue0601 tests
Handle `do` and `body` after @-attribute identifiers (e.g. `@safe do`, `@nogc body`) by routing them through formatKeyword(). `do` is a keyword so any `identifier do` must be a contract. `body` is an identifier that can also be a variable name, so it needs the additional guard peekIs(tok!"{") to distinguish contract `body` from variable declarations like `@custom body = 3;`. Also add regression tests for `@custom body = 3;` and `SomeStruct body = foo();`.
1 parent dca6a1c commit c65d1c8

5 files changed

Lines changed: 132 additions & 1 deletion

File tree

src/dfmt/formatter.d

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,9 @@ private:
305305
else
306306
formatBlockHeader();
307307
}
308-
else if ((current.text == "body" || current == tok!"do") && peekBackIsFunctionDeclarationEnding())
308+
else if ((current.text == "body" || current == tok!"do") && peekBackIsFunctionDeclarationEnding()
309+
|| current == tok!"do" && peekBackIs(tok!"identifier")
310+
|| current.text == "body" && peekBackIs(tok!"identifier") && peekIs(tok!"{"))
309311
{
310312
formatKeyword();
311313
}

tests/allman/issue0601.d.ref

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,40 @@ struct S
1111
void h() @nogc @trusted return
1212
{
1313
}
14+
15+
void i() @system return
16+
{
17+
}
18+
19+
void j() pure @safe return
20+
{
21+
}
22+
23+
void k() @nogc return
24+
{
25+
}
26+
27+
void l() @trusted
28+
do
29+
{
30+
}
31+
32+
void m() @safe
33+
do
34+
{
35+
}
36+
37+
void n() @nogc @system
38+
do
39+
{
40+
}
41+
42+
void o() pure @safe
43+
do
44+
{
45+
}
46+
47+
// Regression: 'body' as variable name must not be treated as contract keyword
48+
@custom body = 3;
49+
SomeStruct body = foo();
1450
}

tests/issue0601.d

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,36 @@ struct S
1111
void h() @nogc @trusted return
1212
{
1313
}
14+
15+
void i() @system return
16+
{
17+
}
18+
19+
void j() pure @safe return
20+
{
21+
}
22+
23+
void k() @nogc return
24+
{
25+
}
26+
27+
void l() @trusted do
28+
{
29+
}
30+
31+
void m() @safe do
32+
{
33+
}
34+
35+
void n() @nogc @system do
36+
{
37+
}
38+
39+
void o() pure @safe do
40+
{
41+
}
42+
43+
// Regression: 'body' as variable name must not be treated as contract keyword
44+
@custom body = 3;
45+
SomeStruct body = foo();
1446
}

tests/knr/issue0601.d.ref

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,36 @@ struct S {
1010
void h() @nogc @trusted return
1111
{
1212
}
13+
14+
void i() @system return
15+
{
16+
}
17+
18+
void j() pure @safe return
19+
{
20+
}
21+
22+
void k() @nogc return
23+
{
24+
}
25+
26+
void l() @trusted
27+
do {
28+
}
29+
30+
void m() @safe
31+
do {
32+
}
33+
34+
void n() @nogc @system
35+
do {
36+
}
37+
38+
void o() pure @safe
39+
do {
40+
}
41+
42+
// Regression: 'body' as variable name must not be treated as contract keyword
43+
@custom body = 3;
44+
SomeStruct body = foo();
1345
}

tests/otbs/issue0601.d.ref

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,33 @@ struct S {
77

88
void h() @nogc @trusted return {
99
}
10+
11+
void i() @system return {
12+
}
13+
14+
void j() pure @safe return {
15+
}
16+
17+
void k() @nogc return {
18+
}
19+
20+
void l() @trusted
21+
do {
22+
}
23+
24+
void m() @safe
25+
do {
26+
}
27+
28+
void n() @nogc @system
29+
do {
30+
}
31+
32+
void o() pure @safe
33+
do {
34+
}
35+
36+
// Regression: 'body' as variable name must not be treated as contract keyword
37+
@custom body = 3;
38+
SomeStruct body = foo();
1039
}

0 commit comments

Comments
 (0)