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

CS8619 is reported on anonymous types #62500

Closed
Genbox opened this issue Jul 8, 2022 · 9 comments
Closed

CS8619 is reported on anonymous types #62500

Genbox opened this issue Jul 8, 2022 · 9 comments
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead

Comments

@Genbox
Copy link

Genbox commented Jul 8, 2022

Version Used:

  • .NET 6.0.301
  • Visual C# Compiler version 4.2.0-4.22281.5 (8d3180e)

Steps to Reproduce:
I am unable to create a small project that reproduces the compiler warning.
Instead, I'll give what I can so you can investigate the issue.

This is our data class:

  public class DataType
  {
    public string ModuleId { get; set; }
    public Module Module { get; set; }
    public string Name { get; set; }
    public string TypeName { get; set; }
    public string DisplayName { get; set; }
    public string? Description { get; set; }
    public string? BaseTypes { get; set; }
  }

The code that uses it, and have the CS8619 warning:

var dbTypes = await _db.DataTypes
    .Where(s => typeIds.Contains(s.Id))
    .Select(s => new
    {
        s.Id,
        s.BaseTypes, //<-- here is the type that generates the warning
        ModuleName = s.Module.Name,
    })
    .ToListAsync(token)
    .ConfigureAwait(false);

When we compile it, we get the following warning:

DataTypeInfoManager.cs(43, 26): [CS8619] Nullability of reference types in value of type '<anonymous type: string Id, string Name, string TypeName, string DisplayName, string BaseTypes, string ModuleName, ICollection<DataField> DataFields>' doesn't match target type '<anonymous type: string Id, string Name, string TypeName, string DisplayName, string? BaseTypes, string ModuleName, ICollection<DataField> DataFields>'.

Note how the signatures mismatch on string/string? for BaseTypes.

I've put the code into sharplab.io and looked at the compiler output. It seems that anonymous types are not generated with nullable reference types, so s.BaseTypes above becomes a string, instead of string?. The compiler detects this string -> string? mismatch and report CS8619.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 8, 2022
@Genbox
Copy link
Author

Genbox commented Jul 8, 2022

The code I used on SharpLab:

#nullable enable
using System.Collections.Generic;
using System.Linq;

public static class Program
{
    public class MyClass
    {
        public MyClass(string? value1, string value2)
        {
            Value1 = value1;
            Value2 = value2;
        }

        public string? Value1 { get; set; }
        public string Value2 { get; set; }
    }

    public static void Main()
    {
        List<MyClass> list = new List<MyClass>();
        list.Add(new MyClass(null, "Value2"));

        var list2 = list.Select(x => new
        {
            x.Value1,
            Value2New = x.Value2
        });
    }
}

Note that this does not generate the compiler warning. Unsure as to why.

@Genbox
Copy link
Author

Genbox commented Jul 8, 2022

Also, note that I can apparently mute the warning with an explicit cast.

Before:
image

Note: I've set CS8619 to a severity of "error" in a .editorconfig file like this: dotnet_diagnostic.cs8619.severity = error. That is why it shows up as an error in the picture above.

After:
Error is gone - only warnings in other files remain.
image

@Youssef1313
Copy link
Member

@RikkiGibson At a first glance, this looks like #51886 ? I'm not quite sure.

@Genbox, can you comment the code at lines from 55 to the end of the method, and see if this still reproduces?

If it no longer produces, do you have a call somewhere in the commented out code that passes 'default' (either explicitly or as a default parameter value? e.g, _db.SaveChangesAsync() or _db.SaveChangesAsync(default)? Does replacing that with _db.SaveChangesAsync(token) fix the warning?

@Genbox
Copy link
Author

Genbox commented Jul 8, 2022

Looks a lot like #51886.

I can confirm that the issue disappears when I comment out the code.
I don't see anywhere we pass a default from the get-go - however, I think I can create a reproducible test with this new info.

@Genbox
Copy link
Author

Genbox commented Jul 8, 2022

Hahaha, this is kinda funny. I've copied out the method and all classes it depends on, but still no dice. Can't reproduce it.
I'm going to try and reduce the project where the error is reproduced. However, it is quite large and complex, so it will take some time.

@Genbox
Copy link
Author

Genbox commented Jul 8, 2022

Ah, I managed to create a reproduction. I've uploaded it here: https://github.com/Genbox/Repo-62500

Simply run "dotnet build" in the folder and it should give the error:

DataTypeInfoManager.cs(16,36): error CS8619: Nullability of reference types in value of type '<anonymous type: string 
BaseTypes, ICollection<DataField> DataFields>' doesn't match target type '<anonymous type: string? BaseTypes, 
ICollection<DataField> DataFields>'

@Youssef1313
Copy link
Member

There is also #61516

@jcouv
Copy link
Member

jcouv commented Jul 8, 2022

Thanks for reporting this.
Closing as duplicate of #51886

@jcouv jcouv closed this as not planned Won't fix, can't repro, duplicate, stale Jul 8, 2022
@Youssef1313
Copy link
Member

Ah, I managed to create a reproduction. I've uploaded it here: https://github.com/Genbox/Repo-62500

Simply run "dotnet build" in the folder and it should give the error:

DataTypeInfoManager.cs(16,36): error CS8619: Nullability of reference types in value of type '<anonymous type: string 
BaseTypes, ICollection<DataField> DataFields>' doesn't match target type '<anonymous type: string? BaseTypes, 
ICollection<DataField> DataFields>'

I minimized your repro to this:

#nullable enable

using System.Collections.Generic;
using System.Linq;

public class DataType
{
    public string? BaseTypes { get; set; }
}

internal class DataTypeInfoManager
{
    private async void GetTypesByIdsAsync()
    {
        List<DataType> dataTypes = new List<DataType>();

        var dbTypes = dataTypes
                      .Select(s => new
                      {
                          s.BaseTypes,
                      });

        foreach (var field in new[] { "" })
        {
            #nullable disable
            string x = null; // comment this out. Warning disappear
            _ = x;
            #nullable enable
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

No branches or pull requests

3 participants