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

Partial class implementing identically named file interfaces with identically named members should not confuse them #68219

Closed
mkane91301 opened this issue May 16, 2023 · 1 comment · Fixed by #68331
Assignees
Labels
Milestone

Comments

@mkane91301
Copy link

Version Used: .NET 7

(Before you think this is a crazy scenario, the use case is generated code.)

Steps to Reproduce:
(Unfortunately, this can't be demonstrated in a gist, because it requires multiple files.)

  1. In one file:
file interface IFoo
{
    IReadOnlyDictionary<int, string> Bar { get; }
}

internal partial class Foo : IFoo
{
    private readonly Dictionary<int, string> _bar = new() { { 1, "one" }, { 2, "two" } };
    IReadOnlyDictionary<int, string> IFoo.Bar => _bar;
}
  1. In a second file:
file interface IFoo
{
    IReadOnlyDictionary<int, string> Bar { get; }
}

internal partial class Foo : IFoo
{
    IReadOnlyDictionary<int, string> IFoo.Bar => _bar;
}
  1. After getting no complaints in Visual Studio, compile.

Diagnostic Id: CS0102

  1. Change second file:
file interface IFoo
{
    IReadOnlyDictionary<int, string> Bar { get; }
}

internal partial class Foo : IFoo
{
}

(Foo needs to implement IFoo because, elsewhere in the file, an instance of Foo will be passed to a method taking IFoo.)

  1. Visual Studio complains that Foo doesn't implement IFoo.

  2. Compile.

Diagnostic Id: CS0535

Expected Behavior: The compiler recognizes that IFoo.Bar in the part of Foo in one file is different from IFoo.Bar in a different part because each IFoo is file scoped and is therefore a different type. Seeing these as different types, the compiler does not emit CS0102 and compiles the code.

Actual Behavior: The compiler confuses file scoped types with the same name and emits CS0102 and doesn't compile the code.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels May 16, 2023
@RikkiGibson
Copy link
Contributor

Thanks for reporting! Adding the full error message here, as we don't have the error codes memorized 😉:

Class2.cs(8,43,8,46): error CS0102: The type 'Foo' already contains a definition for 'IFoo.Bar'

@RikkiGibson RikkiGibson self-assigned this May 17, 2023
@RikkiGibson RikkiGibson added Bug Feature - File-Local Types File-local types (file types) and removed untriaged Issues and PRs which have not yet been triaged by a lead labels May 17, 2023
@RikkiGibson RikkiGibson added this to the C# 12.0 milestone May 17, 2023
@jcouv jcouv modified the milestones: C# 12.0, 17.7 May 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants