Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roslyn fails to detect nullability problems in C# 10 interpolated string handlers #57412

Closed
TessenR opened this issue Oct 27, 2021 · 1 comment
Assignees
Labels
Area-Compilers Bug Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue
Milestone

Comments

@TessenR
Copy link

TessenR commented Oct 27, 2021

Version Used:

Branch main (26 Oct 2021)
Latest commit a4ec465 by Gen Lu:
Merge pull request #57338 from genlu/FixWarning

Fix 'AssemblyVersion overridden by auto-generated version' warning in build

Steps to Reproduce:

Compile and run the following code:

using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;

#nullable enable

string? s = null;
M(s, $"");

void M<T>(T t1, [InterpolatedStringHandlerArgument("t1")] CustomHandler c) {}

public partial struct CustomHandler
{
  public CustomHandler(int literalLength, int formattedCount, string t , out bool success) : this()
  {
    t.ToString();
      
    _builder = new();
    success = true;
  }
}

[System.Runtime.CompilerServices.InterpolatedStringHandler]
public partial struct CustomHandler
{
  private readonly StringBuilder _builder;
  public CustomHandler(int literalLength, int formattedCount, out bool success)
  {
    success = true;
    _builder = new();
  }
  public bool AppendLiteral(string literal)
  {
    _builder.AppendLine("literal:" + literal);
    return true;
  }
  public bool AppendFormatted(object o, int alignment = 0, string? format = null)
  {
    _builder.AppendLine("value:" + o?.ToString());
    _builder.AppendLine("alignment:" + alignment.ToString());
    _builder.AppendLine("format:" + format);
    return true;
  }
  public override string ToString() => _builder.ToString();
}

namespace System.Runtime.CompilerServices
{
  [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = false, Inherited = false)]
  public sealed class InterpolatedStringHandlerAttribute : Attribute
  {
    public InterpolatedStringHandlerAttribute()
    {
    }
  }
}

public class CultureInfoNormalizer
{
  public static void Normalize()
  {
    CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
  }
}

namespace System.Runtime.CompilerServices
{
  [AttributeUsage(AttributeTargets.Parameter, AllowMultiple = false, Inherited = false)]
  public sealed class InterpolatedStringHandlerArgumentAttribute : Attribute
  {
    public InterpolatedStringHandlerArgumentAttribute(string argument) => Arguments = new string[] { argument };
    public InterpolatedStringHandlerArgumentAttribute(params string[] arguments) => Arguments = arguments;
    public string[] Arguments { get; }
  }
}

namespace System.Diagnostics.CodeAnalysis
{
  [AttributeUsage(AttributeTargets.Field | AttributeTargets.Parameter | AttributeTargets.Property | AttributeTargets.ReturnValue)]
  public sealed class MaybeNullAttribute : Attribute
  {
  }
}

Expected Behavior:
Warning for a nulalble argument to the CustomHandler's non-nullable parameter string t

Actual Behavior:
No warnings at all in the program above. It crashes with a NullReferenceException at runtime

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Oct 27, 2021
@TessenR TessenR changed the title Roslyn fails to detect nullability problem in C# 10 interpolated string handlers Roslyn fails to detect nullability problems in C# 10 interpolated string handlers Oct 27, 2021
@jaredpar jaredpar added Feature - Nullable Reference Types Nullable Reference Types and removed Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Nov 4, 2021
@jaredpar jaredpar added this to the 17.1 milestone Nov 4, 2021
@333fred
Copy link
Member

333fred commented Nov 4, 2021

Duplicate of #54583.

@333fred 333fred closed this as completed Nov 4, 2021
@333fred 333fred added the Resolution-Duplicate The described behavior is tracked in another issue label Nov 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Bug Feature - Nullable Reference Types Nullable Reference Types Resolution-Duplicate The described behavior is tracked in another issue
Projects
None yet
Development

No branches or pull requests

3 participants