VB -> C#: Generic extension method inference widens element types when using a base-type comparer

Offen
#1,302 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Maintainer antworten meist innerhalb von 3 Tagen

Dieses Issue hat noch niemand übernommen.

Bewertung

Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Anfängerfreundlichkeit
55/100
Issue-Typ
Bug
Klarheit
Klar beschrieben
Aktivitätsstatus
Aktiv
Tech-Stack
csharp
Bereich
devtools, tooling

Rechercherichtung

Start from the VB-to-C# conversion path for generic extension-method calls and reproduce the issue with the VB input in the report. Compare the generated Distinct call with the expected output; done means it specifies DerivedValue and preserves the declared return type.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Beschreibung

VB -> C#
VB.Net input code
Imports System.Collections.Generic
Imports System.Linq

Public Class BaseValue
    Public Property Key As Integer
End Class

Public Class DerivedValue
    Inherits BaseValue
End Class

Public Module Helpers
    Public Function ToDictionary(values As IEnumerable(Of DerivedValue)) As Dictionary(Of Integer, DerivedValue)
        Return values.Distinct(EqualityComparer(Of BaseValue).Default).ToDictionary(Function(value) value.Key)
    End Function
End Module
Erroneous output

The converted method leaves the generic arguments of Distinct implicit:

public static Dictionary<int, DerivedValue> ToDictionary(IEnumerable<DerivedValue> values)
{
    return values.Distinct(EqualityComparer<BaseValue>.Default).ToDictionary(value => value.Key);
}

C# infers BaseValue as the element type of Distinct, so ToDictionary returns Dictionary<int, BaseValue>.

This produces:

CS0029: Cannot implicitly convert type
'System.Collections.Generic.Dictionary<int, BaseValue>'
to 'System.Collections.Generic.Dictionary<int, DerivedValue>'
Expected output

The converter should preserve the type argument inferred by VB.NET:

public static Dictionary<int, DerivedValue> ToDictionary(IEnumerable<DerivedValue> values)
{
    return values.Distinct<DerivedValue>(EqualityComparer<BaseValue>.Default).ToDictionary(value => value.Key);
}
Details
  • Product: Visual Studio extension, built locally from master.
  • Version: 10.0.1 (local development build).
  • Previous working version: No.

VB.NET and C# use different type inference rules for generic extension methods.

VB.NET first infers the relevant type parameters from the receiver. In this example, it fixes the element type as DerivedValue. The base-type comparer is then accepted through the contravariance of IEqualityComparer(Of T).

C# inference considers both the receiver and the comparer, resulting in BaseValue instead. Omitting explicit type arguments therefore changes the type of the resulting sequence.

Microsoft's VB team describes this receiver-first inference here:
https://devblogs.microsoft.com/vbteam/extension-methods-and-generics-extension-methods-part-5/

A workaround is to specify the type argument explicitly in the VB source:

Return values.Distinct(Of DerivedValue)(EqualityComparer(Of BaseValue).Default).ToDictionary(Function(value) value.Key)
Vorherrschende Sprache
C#
Sterne
917
Forks
243
Ø Merge
2 T. 15 Std.
Gemergte PRs (30 T.)
9

Entwicklungsumgebung

Erste Schritte

  1. Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
  3. Forken Sie das Repository und arbeiten Sie in einem Branch.
  4. Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.

Mehr aus icsharpcode/CodeConverter

Alle Issues in icsharpcode/CodeConverter

Ähnliche Issues

Weitere Issues zu C#

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.