Browse Source

Merge pull request #3896 from sailro/fix-3894-span-concat

Fix #3895: keep byref-like ToString in string concatenation
pull/3911/head
Siegfried Pammer 2 months ago committed by GitHub
parent
commit
3a6b074930
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs
  2. 35
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/StringConcatenation.cs
  3. 5
      ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs

6
ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs

@ -338,6 +338,12 @@ namespace ICSharpCode.Decompiler.Tests
await RunForLibrary(cscOptions: cscOptions); await RunForLibrary(cscOptions: cscOptions);
} }
[Test]
public async Task StringConcatenation([ValueSource(nameof(roslyn3OrNewerOptions))] CompilerOptions cscOptions)
{
await RunForLibrary(cscOptions: cscOptions);
}
[Test] [Test]
public async Task Async([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions) public async Task Async([ValueSource(nameof(defaultOptions))] CompilerOptions cscOptions)
{ {

35
ICSharpCode.Decompiler.Tests/TestCases/Pretty/StringConcatenation.cs

@ -0,0 +1,35 @@
// Copyright (c) 2026 Sebastien Lebreton
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
// software and associated documentation files (the "Software"), to deal in the Software
// without restriction, including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
// to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or
// substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
using System;
namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
{
public static class StringConcatenation
{
public static string WithSpan(Span<char> value)
{
return "prefix" + value.ToString();
}
public static string WithReadOnlySpan(ReadOnlySpan<char> value)
{
return "prefix" + value.ToString();
}
}
}

5
ICSharpCode.Decompiler/CSharp/Transforms/ReplaceMethodCallsWithOperators.cs

@ -366,6 +366,11 @@ namespace ICSharpCode.Decompiler.CSharp.Transforms
var toStringMethod = m.Get<Expression>("call").Single().GetSymbol() as IMethod; var toStringMethod = m.Get<Expression>("call").Single().GetSymbol() as IMethod;
var target = m.Get<Expression>("target").Single(); var target = m.Get<Expression>("target").Single();
var type = target.GetResolveResult().Type; var type = target.GetResolveResult().Type;
if (type.IsByRefLike)
{
// ref structs cannot be converted to object for use with +
return expr;
}
if (!(isLastArgument || ToStringIsKnownEffectFree(type))) if (!(isLastArgument || ToStringIsKnownEffectFree(type)))
{ {
// ToString() order of evaluation matters, see CheckArgumentsForStringConcat(). // ToString() order of evaluation matters, see CheckArgumentsForStringConcat().

Loading…
Cancel
Save