dotnet/roslyn
GitHub で見るImprove error message for explicit cast of collection literal to non-generic type
Open
#68,862 opened on 2023年7月3日
Area-CompilersConcept-Diagnostic ClarityFeature - Collection Expressionshelp wanted
Repository metrics
- Stars
- (20,414 stars)
- PR merge metrics
- (平均マージ 6d 17h) (30d で 256 merged PRs)
説明
Compile:
using System;
using System.Collections;
using System.Collections.Generic;
class C : IEnumerable<int>
{
public void Add(int i) { }
public IEnumerator<int> GetEnumerator() => null;
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
class Program
{
static void Main()
{
var c = (C)[1];
}
}
Actual Behavior:
(16,18): error CS0119: 'C' is a type, which is not valid in the given context
Expected Behavior: Diagnostic above, with suggestion to parenthesize the collection literal if a cast is intended.
See https://github.com/dotnet/roslyn/pull/68793#discussion_r1245560632
Relates to test plan https://github.com/dotnet/roslyn/issues/66418