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

Conversion error (CS0029) during compilation in .NET SDK 8.0.300 #40893

Open
nikmons opened this issue May 15, 2024 · 2 comments
Open

Conversion error (CS0029) during compilation in .NET SDK 8.0.300 #40893

nikmons opened this issue May 15, 2024 · 2 comments
Assignees
Labels
Area-NetSDK untriaged Request triage from a team member

Comments

@nikmons
Copy link

nikmons commented May 15, 2024

Describe the bug

Hi everyone,

Today we came across a really strange issue during a scheduled pipeline run (AzDO) for a dotnet core repo.
A project that was compiled without any issues yesterday, threw the following error - although there were no code changes.

[...]\SampleClass.cs(262,16): error CS0029: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JProperty' to 'System.Collections.Generic.KeyValuePair<string, Newtonsoft.Json.Linq.JToken?>

The lines in question are the following

using Newtonsoft.Json.Linq;
[...]
List<JObject> sqlColumnValues =
         [
            [
               new JProperty(TAB_NAME.Fields.COL_CODE_1, -1),
               new JProperty(TAB_NAME.Fields.COL_CODE_2, val2),
               new JProperty(TAB_NAME.Fields.COL_CODE_3, val3),
               new JProperty(TAB_NAME.Fields.COL_CODE_4, val4),
               new JProperty(TAB_NAME.Fields.COL_CODE_5, val5),
               new JProperty(TAB_NAME.Fields.COL_CODE_6, val6),
               new JProperty(TAB_NAME.Fields.COL_CODE_7, val7),
               new JProperty(TAB_NAME.Fields.COL_CODE_8, val8)
            ]
         ];

Upon further investigation we noticed that the automated Azure DevOps pipeline used the latest dotnet SDK version today (v8.0.300),
instead of v8.0.204 that was used up until yesterday. We downgraded the SDK version in the build environment to v8.0.204 and the project was compiled successfully without further issues.

To Reproduce

The issue was reproduced on dev environments running 8.0.300.

  • Use dotnet SDK 8.0.300 to build a project that utilizes C# 12 syntactic sugar for array initialization.
  • Sample list provided above.

Exceptions (if any)

[...]\SampleClass.cs(262,16): error CS0029: Cannot implicitly convert type 'Newtonsoft.Json.Linq.JProperty' to 'System.Collections.Generic.KeyValuePair<string, Newtonsoft.Json.Linq.JToken?>

Further technical details

  • dotnet --info (dev machine)
    dotnet_8_0_300

  • CLI build

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-NetSDK untriaged Request triage from a team member labels May 15, 2024
@KalleOlaviNiemitalo
Copy link

This seems related to the design change dotnet/csharplang#7783.

The following demonstrates a difference between compiler versions, without referencing Newtonsoft.Json:

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

class Collection : IEnumerable<Item1>
{
    IEnumerator<Item1> IEnumerable<Item1>.GetEnumerator() => throw new NotImplementedException();

    IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();

    public IEnumerator<Item2> GetEnumerator() => throw new NotImplementedException();

    public void Add(object obj) => throw new NotImplementedException();
}

struct Item1
{
}

struct Item2
{
}

class Demo
{
    Collection collection = [ new Item1(), "hoh" ];
}

.NET SDK 8.0.205:

Class1.cs(26,44): error CS0029: Cannot implicitly convert type 'string' to 'Item1'

.NET SDK 8.0.300:

Class1.cs(26,31): error CS0029: Cannot implicitly convert type 'Item1' to 'Item2'
Class1.cs(26,44): error CS0029: Cannot implicitly convert type 'string' to 'Item2'

The older SDK deduces the collection element type from the IEnumerable<Item1> interface, but the newer SDK deduces it from public IEnumerator<Item2> GetEnumerator() instead.

In the Newtonsoft.Json case, JObject has a public IEnumerator<KeyValuePair<string, JToken>> GetEnumerator() method and implements IEnumerable<KeyValuePair<string, JToken>>, but its base class JContainer implements IEnumerable<JToken>. It's not clear to me which version of the speclet .NET SDK 8.0.204 implements.

@bording
Copy link

bording commented May 17, 2024

We had code that was impacted by this change as well, and it was rather frustrating to understand why it was happening due to the fact that the 8.0.300 SDK has to be installed manually and doesn't work with Visual Studio 17.9.7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-NetSDK untriaged Request triage from a team member
Projects
None yet
Development

No branches or pull requests

5 participants
@bording @nikmons @joeloff @KalleOlaviNiemitalo and others