From 4bcb45e7b3c8a32e1fef501d58b48a374d65c39d Mon Sep 17 00:00:00 2001 From: MoonlightSentinel Date: Sat, 2 May 2020 14:01:55 +0200 Subject: [PATCH 1/2] Fix 383 - Formatting AST including `@trusted` delegate produces invalid output `do`was written unconditionally even though `hasDo` was false. --- src/dparse/formatter.d | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/dparse/formatter.d b/src/dparse/formatter.d index d22bedbe..10c2899a 100644 --- a/src/dparse/formatter.d +++ b/src/dparse/formatter.d @@ -2678,7 +2678,8 @@ class Formatter(Sink) { foreach (contract; functionContracts) format(contract); - put("do"); + if (specifiedFunctionBody.hasDo) + put("do"); if (blockStatement) format(blockStatement); } @@ -4144,4 +4145,10 @@ do{} testFormatNode!(AliasDeclaration)("alias f(T) = void(int, int, int) const pure nothrow;"); testFormatNode!(AliasDeclaration)("alias MT = mixin (strEnum1, strEnum2);"); + + testFormatNode!(Module)("static assert(() @trusted { return bar()(3); }() == 4);", +`static assert (() @trusted +{ return bar()(3); +}() == 4)` +); } From 6448b28cfb68869b63d97772b5afe0e902981c4b Mon Sep 17 00:00:00 2001 From: MoonlightSentinel Date: Sat, 2 May 2020 15:26:35 +0200 Subject: [PATCH 2/2] Ensure proper newline/indentation for in/out/do --- src/dparse/formatter.d | 96 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 92 insertions(+), 4 deletions(-) diff --git a/src/dparse/formatter.d b/src/dparse/formatter.d index 10c2899a..f84c1ce8 100644 --- a/src/dparse/formatter.d +++ b/src/dparse/formatter.d @@ -1878,7 +1878,7 @@ class Formatter(Sink) void format(const InContractExpression expression) { debug(verbose) writeln("InContractExpression"); - + newlineIndent(); put("in ("); format(expression.assertArguments); put(")"); @@ -1904,7 +1904,6 @@ class Formatter(Sink) { if (inContractExpression) format(inContractExpression); if (outContractExpression) format(outContractExpression); - newline(); } } @@ -1922,6 +1921,7 @@ class Formatter(Sink) void format(const InStatement inStatement) { debug(verbose) writeln("InStatement"); + newlineIndent(); put("in"); format(inStatement.blockStatement); } @@ -2346,7 +2346,7 @@ class Formatter(Sink) void format(const OutContractExpression expression) { debug(verbose) writeln("OutContractExpression"); - + newlineIndent(); put("out ("); if (expression.parameter != tok!"") format(expression.parameter); @@ -2363,7 +2363,7 @@ class Formatter(Sink) Token parameter; BlockStatement blockStatement; **/ - + newlineIndent(); put("out"); if (stmnt.parameter != tok!"") { @@ -2679,7 +2679,10 @@ class Formatter(Sink) foreach (contract; functionContracts) format(contract); if (specifiedFunctionBody.hasDo) + { + newlineIndent(); put("do"); + } if (blockStatement) format(blockStatement); } @@ -4150,5 +4153,90 @@ do{} `static assert (() @trusted { return bar()(3); }() == 4)` +); + testFormatNode!(Module)("int foo() { return 2; }", +`int foo() +{ + return 2; +}` +); + + testFormatNode!(Module)("int foo() do { return 2; }", +`int foo() +do +{ + return 2; +}` +); + + testFormatNode!(Module)("int foo(int i) in(i > 0) do { return 2; }", +`int foo(int i) +in (i > 0) +do +{ + return 2; +}` +); + + testFormatNode!(Module)("int foo(int i) in { assert(i > 0); } do { return 2; }", +`int foo(int i) +in +{ + assert (i > 0); +} +do +{ + return 2; +}` +); + + testFormatNode!(Module)("int foo() out(j; j > 0) do { return 2; }", +`int foo() +out (j; j > 0) +do +{ + return 2; +}` +); + + testFormatNode!(Module)("int foo() out(j) { assert(j > 0); } do { return 2; }", +`int foo() +out (j) +{ + assert (j > 0); +} +do +{ + return 2; +}` +); + + testFormatNode!(Module)("version(BAR) { + int foo(int i) + in(i > 0) in { assert(i > 0); } + out(j; j > 0) out(j) { assert(j>0); } + do { return 2; } int i; } + ", +`version (BAR) +{ + + int foo(int i) + in (i > 0) + in + { + assert (i > 0); + } + out (j; j > 0) + out (j) + { + assert (j > 0); + } + do + { + return 2; + } + + int i; +}` ); }