dotnet / sdk

Core functionality needed to create .NET Core projects, that is shared between Visual Studio and CLI
https://dot.net/core
MIT License
2.59k stars 1.03k forks source link

msbuild throws CS9215, but dotnet build doesn't #38777

Open virzak opened 4 months ago

virzak commented 4 months ago

Description

Using MSBuild version 17.10.0-preview-24081-01+97651a25d

Getting an MSBuild error:

error CS9215: Collection expression type must have an applicable instance or extension method 'Add' that can be called with an argument of iteration type 'object'. The best overloaded method is 'MyClass.Add(int)'

Reproduction Steps

Here is a sharplab link

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

Console.WriteLine("🌄");

class MyClass<T> : IEnumerable
{
    readonly List<object> list = [];
    public void Add(T p)
    {
        list.Add(p);
    }

    public IEnumerator GetEnumerator()
    {
        throw new NotImplementedException();
    }
}

class MyClass2
{
    MyClass<int> myClass = [4];
}

Expected behavior

Compiles just fine using dotnet build

Actual behavior

Although this is still .NET 8, Visual Studio

Regression?

Yes. Used to work with previous versions.

Known Workarounds

No response

Configuration

No response

Other information

This happens when using @davidwengier 's Xunit.SerializedTheoryData library.

image

KalleOlaviNiemitalo commented 4 months ago

The collection expression conversion rules regarding iteration type were changed in https://github.com/dotnet/csharplang/pull/7783. That's why the compiler no longer allows the conversion.