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

C# compiler chooses inaccessible extension method over accessible one #25813

Closed
LeonidVasilyev opened this issue Mar 29, 2018 · 6 comments · Fixed by #26345
Closed

C# compiler chooses inaccessible extension method over accessible one #25813

LeonidVasilyev opened this issue Mar 29, 2018 · 6 comments · Fixed by #26345
Assignees
Milestone

Comments

@LeonidVasilyev
Copy link

LeonidVasilyev commented Mar 29, 2018

Version Used:
Microsoft (R) Visual C# Compiler version 2.6.0.62329 (5429b35)

Steps to Reproduce:

  1. Create new console application with following code in Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    static void Main(string[] args)
    {
        var a = new[] { 0 };
        var b = new[] { 1, 2, 3 };
        Console.WriteLine(b.Count(a.Contains));
    }
}

public static class Extensions
{
    // NOTE: private access modifier simulates internal class public method in referenced assembly.
    private static bool Contains<T>(this IEnumerable<T> a, T value) =>
        throw new NotImplementedException();
}
  1. Build project or wait full solution analysis to complete

Expected Behavior:
Project compiled successfully. System.Linq.Enumerable.Contains() used as an argument of System.Linq.Enumerable.Count() function.

Actual Behavior:
Compiler error CS0122: 'Extensions.Contains(IEnumerable, T)' is inaccessible due to its protection level

Original case is described in C# compiler chooses wrong extension method question on StackOverflow. CS0122 wrong member inaccessible when overload does not exist issue looks related.

@Joe4evr
Copy link

Joe4evr commented Mar 29, 2018

Should be something to be improved as part of dotnet/csharplang#98

@gafter It doesn't seem like this is part of those improvements. Is this perhaps a regression? If not, can this still be added as part of the improved overload resolution?

@jcouv jcouv added this to the 15.7 milestone Mar 29, 2018
@jcouv
Copy link
Member

jcouv commented Mar 29, 2018

Assigning to 15.7 milestone for now, we should confirm whether this is a 15.7 regression.
Never mind, the repro is in 15.5 (compiler version 2.6).
I'll let @gafter triage to appropriate milestone.

@gafter
Copy link
Member

gafter commented Mar 30, 2018

VS2015 behaves exactly the same way.

@gafter gafter modified the milestones: 15.7, 16.0 Mar 30, 2018
@gafter
Copy link
Member

gafter commented Mar 30, 2018

Looks like a bug. Inaccessible members should not affect overload resolution.

@gafter gafter added the Bug label Mar 30, 2018
@gafter
Copy link
Member

gafter commented Apr 18, 2018

See https://github.com/dotnet/csharplang/blob/master/spec/expressions.md#extension-method-invocations . In the terminology of the spec, we are letting non-eligible candidates short-circuit finding the right method group.

@gafter
Copy link
Member

gafter commented Apr 23, 2018

In review in #26345

@gafter gafter removed the 4 - In Review A fix for the issue is submitted for review. label Apr 27, 2018
gafter added a commit that referenced this issue Apr 27, 2018
…idates. (#26345)

* Inaccessible extension methods should not be considered eligible candidates.
Fixes #25813
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants