dotnet / dotnet/roslyn

Emit RVA fields for constant collection expressions with spreads of `u8` literals.

Open
#84,145 3 comments 0 reactions 0 assignees View on GitHub
Area-Compilers Code Gen Quality Feature - Collection Expressions Feature - Utf8StringLiterals
Dominant language
C#
Stars
20.7k
Forks
4.3k
PR merge metrics
PR metrics pending

Description

Expressions like `(ReadOnlySpan)[.."Hello"u8, 1, 2, 3, 4, .."World"u8]` are emitted by constructing a `List`, adding its elements, calling `ToArray()`, and getting a span from it. However, the compiler can statically determine the expression's content, and should be able to directly emit an RVA blob.

This is an extension of #60896, and can help in expressions where text interleaves with binary content, or the expression does not entirely contain valid UTF-8 text. Without this, people have to write `[(byte)'H', (byte)'e', (byte)'l', (byte)'l', (byte)'o', …]` to get an optimized RVA blob (an analyzer and fixer could be added in the future).

Contributor guide

Open the contributing guide

Research direction

Start by tracing the compiler implementation and tests for constant collection expressions, using the behavior described here and the related issue #60896 as entry points. Verify how spreads of u8 literals are currently emitted, then add coverage for the mixed "Hello"u8 and "World"u8 example and confirm that the completed expression emits an RVA blob rather than the current collection-building path.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.