dotnet/roslyn

Roslyn compiler does not round-trip attribute arguments that contain unmatched surrogates

Open

#41,764 opened on Feb 18, 2020

View on GitHub
 (0 comments) (0 reactions) (0 assignees)C# (4,257 forks)batch import
Area-CompilersBughelp wanted

Repository metrics

Stars
 (20,414 stars)
PR merge metrics
 (Avg merge 6d 17h) (256 merged PRs in 30d)

Description

See also https://github.com/dotnet/roslyn/issues/41280

The following test passes on netcore 3.1, but the test demonstrates that

  1. an attribute in source with a string argument containing an unmatched surrogate is seen properly (unedited) through the symbol APIs from the source assembly
  2. results in a different attribute value when viewed through the compiler APIs on the emitted assembly
  3. results in a yet different value when viewed at runtime using reflection

The same value should be seen through all three mechanisms.

        [Fact]
        public void TEMP()
        {
            const string UnicodeHighSurrogate = "\uD800";
            const string UnicodeReplacementCharacter = "\uFFFD";

            var source =
@"using System;
 
[Obsolete(UnicodeHighSurrogate)]
class C
{
    public const string UnicodeHighSurrogate = ""\uD800"";
    public const string UnicodeReplacementCharacter = ""\uFFFD"";
 
    static void Main()
    {
        string message = ((ObsoleteAttribute)typeof(C).GetCustomAttributes(false)[0]).Message;
 
        Console.WriteLine(message == UnicodeReplacementCharacter + UnicodeReplacementCharacter);
    }
}";
            Func<bool, Action<ModuleSymbol>> validator = isFromSource => (ModuleSymbol module) =>
            {
                var C = module.GlobalNamespace.GetMember<NamedTypeSymbol>("C");
                var obs = C.GetAttributes()[0];
                var tc = obs.ConstructorArguments.First();
                Assert.Equal(isFromSource ? UnicodeHighSurrogate : UnicodeReplacementCharacter + UnicodeReplacementCharacter + UnicodeReplacementCharacter, (string)tc.Value);
            };
            CompileAndVerify(source, sourceSymbolValidator: validator(true), symbolValidator: validator(false), expectedOutput: "True");
        }

Contributor guide