Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
<Compile Include="TestCases\ILPretty\Issue3442.cs" />
<Compile Include="TestCases\ILPretty\MonoFixed.cs" />
<Compile Include="TestCases\Pretty\Comparisons.cs" />
<Compile Include="TestCases\Pretty\GloballyQualifiedTypeInStringInterpolation.cs" />
<Compile Include="TestCases\Pretty\Issue3406.cs" />
<Compile Include="TestCases\Pretty\PointerArithmetic.cs" />
<Compile Include="TestCases\Pretty\Issue3439.cs" />
Expand Down
13 changes: 13 additions & 0 deletions ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,19 @@ public async Task UsingVariables([ValueSource(nameof(roslyn3OrNewerWithNet40Opti
await RunForLibrary(cscOptions: cscOptions);
}

[Test]
public async Task GloballyQualifiedTypeInStringInterpolation([ValueSource(nameof(roslynOnlyWithNet40Options))] CompilerOptions cscOptions)
{
// https://github.com/icsharpcode/ILSpy/issues/3447
await RunForLibrary(
cscOptions: cscOptions,
configureDecompiler: settings => {
settings.UsingDeclarations = false;
settings.AlwaysUseGlobal = true;
}
);
}

[Test]
public async Task LiftedOperators([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
public static class GloballyQualifiedTypeInStringInterpolation
{
public static string Root => $"Prefix {(global::System.DateTime.Now)} suffix";
public static string Cast => $"Prefix {((int)global::System.DateTime.Now.Ticks)} suffix";
#if CS100 && NET60
public static string Lambda1 => $"Prefix {(() => global::System.DateTime.Now)} suffix";
#else
public static string Lambda1 => $"Prefix {(global::System.Func<global::System.DateTime>)(() => global::System.DateTime.Now)} suffix";
#endif
public static string Lambda2 => $"Prefix {((global::System.Func<global::System.DateTime>)(() => global::System.DateTime.Now))()} suffix";
public static string Method1 => $"Prefix {M(global::System.DateTime.Now)} suffix";
public static string Method2 => $"Prefix {(global::System.DateTime.Now.Ticks)} suffix";
public static string Method3 => $"Prefix {(global::System.DateTime.Equals(global::System.DateTime.Now, global::System.DateTime.Now))} suffix";
public static string ConditionalExpression1 => $"Prefix {(Boolean ? global::System.DateTime.Now : global::System.DateTime.UtcNow)} suffix";
public static string ConditionalExpression2 => $"Prefix {(Boolean ? global::System.DateTime.Now : global::System.DateTime.UtcNow).Ticks} suffix";

private static bool Boolean => false;
private static long M(global::System.DateTime time)
{
return time.Ticks;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,38 @@ public override void VisitAsExpression(AsExpression asExpression)
base.VisitAsExpression(asExpression);
}

public override void VisitInterpolation(Interpolation interpolation)
{
// Need to do this first, in case the descendents parenthesize themselves.
base.VisitInterpolation(interpolation);

// If an interpolation contains global::, we need to parenthesize the expression.
if (InterpolationNeedsParenthesis(interpolation))
Parenthesize(interpolation.Expression);

static bool InterpolationNeedsParenthesis(AstNode node)
{
if (node is MemberType { IsDoubleColon: true })
return true;

if (node is ParenthesizedExpression)
return false;
if (node is AnonymousMethodExpression or LambdaExpression { Body: BlockStatement })
return false;
if (node is InvocationExpression invocation)
return InterpolationNeedsParenthesis(invocation.Target);
if (node is CastExpression cast)
return InterpolationNeedsParenthesis(cast.Expression);

foreach (var child in node.Children)
{
if (InterpolationNeedsParenthesis(child))
return true;
}
return false;
}
}

// Conditional operator
public override void VisitConditionalExpression(ConditionalExpression conditionalExpression)
{
Expand Down
Loading