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

Improve error message for explicit cast of collection literal to non-generic type #68862

Open
cston opened this issue Jul 3, 2023 · 2 comments
Labels
Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. help wanted The issue is "up for grabs" - add a comment if you are interested in working on it New Feature - Collection Expressions
Milestone

Comments

@cston
Copy link
Member

cston commented Jul 3, 2023

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 #68793 (comment)

Relates to test plan #66418

@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 3, 2023
@jcouv jcouv added this to the 17.8 milestone Jul 5, 2023
@jcouv jcouv added Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. help wanted The issue is "up for grabs" - add a comment if you are interested in working on it and removed untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 5, 2023
@cston cston modified the milestones: 17.8, Compiler.Next Jul 5, 2023
@jaredpar jaredpar modified the milestones: Compiler.Next, Backlog Sep 12, 2023
@cston cston removed their assignment Oct 16, 2023
@RikkiGibson
Copy link
Contributor

I thought we had made a decision that we would break this scenario and actually cause it to mean "cast collection literal [1] to type C", with the expectation that real-world code which intends for this to be an indexer access is overwhelmingly likely to be written as C[1] and not (C)[1].

@CyrusNajmabadi am I remembering this wrong?

@CyrusNajmabadi
Copy link
Member

I am not remembering any decision to make a breaking change here. That would be a fairly big deal, and we definitely weren't making breaking change decisions later in 12.

We might want to consider the breaking change in hte future though, esp if we have a breaking change mechanism finally decided upon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Compilers Concept-Diagnostic Clarity The issues deals with the ease of understanding of errors and warnings. help wanted The issue is "up for grabs" - add a comment if you are interested in working on it New Feature - Collection Expressions
Projects
None yet
Development

No branches or pull requests

5 participants