Skip to content

Fix #3895: keep byref-like ToString in string concatenation#3896

Open
sailro wants to merge 1 commit into
icsharpcode:masterfrom
sailro:fix-3894-span-concat
Open

Fix #3895: keep byref-like ToString in string concatenation#3896
sailro wants to merge 1 commit into
icsharpcode:masterfrom
sailro:fix-3894-span-concat

Conversation

@sailro

@sailro sailro commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

Fixes #3895.

Problem

String-concatenation reconstruction removes explicit ToString() calls when the C# compiler
would normally reproduce an equivalent call through the built-in + operator.

That assumption does not hold for byref-like values. Span<T> and ReadOnlySpan<T> cannot be
converted to object, so removing the call from "prefix" + span.ToString() produces
"prefix" + span, which fails with CS0019.

Solution

Keep the explicit ToString() call when its target is byref-like.

The surrounding string.Concat reconstruction still proceeds, so the result remains the
natural and compilable "prefix" + span.ToString() form.

Tests

Added a Roslyn 3+ Pretty fixture covering both Span<char> and ReadOnlySpan<char>.

Validation:

  • StringConcatenation Pretty matrix: 6 passed

  • Release solution build: 0 warnings, 0 errors

  • Full solution test suite: 4,891 passed, 15 existing skips

  • At least one test covering the code changed

String concatenation cannot box a byref-like value, so removing its explicit
ToString call produces invalid C#. Preserve that call while retaining the
surrounding string-concatenation reconstruction.

Assisted-by: Copilot:gpt-5.6-sol:GitHub Copilot CLI
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 86d2918e-5a24-48b4-9a86-41d331ec3720
@sailro sailro changed the title Fix #3894: keep byref-like ToString in string concatenation Fix #3895: keep byref-like ToString in string concatenation Jul 19, 2026
@sailro
sailro force-pushed the fix-3894-span-concat branch from c0f326f to 675fca8 Compare July 19, 2026 23:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

String concatenation drops Span<char>.ToString() and does not recompile (CS0019)

1 participant