Skip to content

Calling interpolated string handler method on ref parameter (or ref local) causes compilation error #56624

@addabis

Description

@addabis

A regression that breaks an existing code that uses StringBuilder.

Version Used:
.NET 6.0 RC1 (Roslyn 4.0)

Steps to Reproduce:

Our codebase uses StringBuilder, but it is reproducible with any [InterpolatedStringHandler].

Use the following:

using System.Runtime.CompilerServices;

namespace Test;

class Program
{
    static void Main()
    {
        Test test = new Test();
        Foo("test", ref test);
    }

    static void Foo(string name, /* CAUSE! */ ref Test state)
    {
        state.Append($"{name}");
    }
}

class Test
{
    public void Append([InterpolatedStringHandlerArgument("")] ref MyInterpolatedStringHandler handler)
    {
    }
}

[InterpolatedStringHandler]
struct MyInterpolatedStringHandler
{
    public MyInterpolatedStringHandler(int literalLength, int formattedCount, Test stringBuilder)
    {
    }

    public void AppendFormatted<T>(T value)
    {
        Console.WriteLine("Hit");
    }
}

Expected Behavior:

Should compile and execute the AppendFormatted method, which is the behavior when ref is removed from the state parameter.

Actual Behavior:

Compilation error:

error CS1615: Argument 3 may not be passed with the 'ref' keyword

(this means argument 3 of the MyInterpolatedStringHandler ctor, i.e. the instance of Test, )

The same happens if the method is invoked on a ref local.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions