Skip to content

binding: report ambiguous members of variant base interfaces - #56

Merged
rameel merged 2 commits into
mainfrom
binding/variant-interface-member-hiding
Nov 22, 2026
Merged

rameel merged 2 commits into
mainfrom
binding/variant-interface-member-hiding

Conversation

@rameel

@rameel rameel commented Nov 22, 2026

Copy link
Copy Markdown
Owner

A field or property hid the members with the same name declared in any type assignable from its declaring type, and assignability takes variance into account. For a covariant IProducer<out T> with T Value { get; } and IBothProducers : IProducer<object>, IProducer<string>, IProducer<object> is assignable from IProducer<string>, so P.Value silently bound to IProducer<string>.Value and got the type string. C# reports CS0229: neither instantiation is a base interface of the other. Classes were not affected, since they cannot be variant.

This PR moves the base type check that MethodResolver has used for method hiding since #54 to ReflectionExtensions.IsBaseTypeOf, which finds base types by inheritance, and uses it in BindToMember as well. MethodResolver keeps only the rule specific to method invocation: a method of a class other than Object removes the methods of interfaces. The member filter no longer needs a separate check for the same declaring type.

HidesMethodsOf in MethodResolver combined two rules: finding base
types by inheritance and letting a method of a class other than
Object remove the methods of interfaces. Move the first rule to
ReflectionExtensions.IsBaseTypeOf, so that only the rule specific to
method invocation stays in MethodResolver and member lookup can use
the same check. No behavior change.
A field or property hid the members with the same name declared in
any type assignable from its declaring type, and assignability takes
variance into account. With a covariant IProducer<out T> declaring
T Value and IBothProducers : IProducer<object>, IProducer<string>,
IProducer<object> is assignable from IProducer<string>, so P.Value
silently bound to IProducer<string>.Value. C# reports an ambiguity
(CS0229): neither instantiation is a base interface of the other.

Use IsBaseTypeOf, which finds base types by inheritance, as for
methods. It never treats a type as its own base type, so the separate
check for the same declaring type is no longer needed. Classes were
not affected, since they cannot be variant.
@rameel
rameel merged commit adcd985 into main Nov 22, 2026
1 check passed
@rameel
rameel deleted the binding/variant-interface-member-hiding branch November 22, 2026 14:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant