Skip to content

Concatenate adjacent string literals in deprecated attribute messages#728

Merged
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:fix-deprecated-adjacent-strings
Jul 13, 2026
Merged

Concatenate adjacent string literals in deprecated attribute messages#728
tannergooding merged 1 commit into
dotnet:mainfrom
tannergooding:fix-deprecated-adjacent-strings

Conversation

@tannergooding

Copy link
Copy Markdown
Member

Fixes #694.

A C deprecated message can be written as adjacent string literals, which the compiler concatenates into a single value:

[[deprecated("Use Bar() or "
             "Baz() instead")]]
void MyFunction();

The generator extracted the message as the substring between the first " (via IndexOf) and the last " (via LastIndexOf) and emitted it verbatim. For adjacent literals that span text includes the interior boundary quotes and inter-literal whitespace/newlines, so it emitted invalid C#:

[Obsolete("Use Bar() or "
    "Baz() instead")]

The fix replaces the fragile first/last-quote slice with a proper scan (GetDeprecatedMessage) that walks the attribute source, gathers the contents of every string literal (respecting escaped quotes so \" isn't treated as a terminator), and joins them into a single value:

[Obsolete("Use Bar() or Baz() instead")]

The common single-literal path is byte-identical (verified: no golden output changed), escaped quotes are preserved, and a missing/empty message still emits a bare [Obsolete].

Added DeprecatedAdjacentStringTest covering the adjacent-literal case. Build 0 warning / 0 error (Release, TreatWarningsAsErrors + EnforceCodeStyleInBuild); full suite green (3713 passed / 0 failed).

Note

This PR description and the code change were drafted by Copilot on my behalf.

A C deprecated message can be written as adjacent string literals, which
the compiler concatenates into a single value. The generator extracted the
text between the first and last quote verbatim, so the emitted Obsolete
attribute contained the raw boundary quotes and inter-literal whitespace,
producing invalid C#. Scan the attribute source for each literal and join
their contents into a single valid Obsolete message.

Fixes dotnet#694

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@tannergooding tannergooding merged commit 6e0bf24 into dotnet:main Jul 13, 2026
14 checks passed
@tannergooding tannergooding deleted the fix-deprecated-adjacent-strings branch July 13, 2026 05:22
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.

Invalid C# codegen for C adjacent string literals.

1 participant