-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Description
Consider the following code:
class C {
bool operator==(Object other) => true;
}
class D extends C {
bool operator==(Object? other) => super == other;
}
main() {
print(D() == D());
}This is accepted by the analyzer, but rejected by the CFE. The CFE's error message is:
../../tmp/test.dart:5:46: Error: The argument type 'Object?' can't be assigned to the parameter type 'Object'.
- 'Object' is from 'dart:core'.
bool operator==(Object? other) => super == other;
@leafpetersen and I believe this code should be accepted, because the behavior of super == other has similar null-handling rules to the behavior of ordinary equality checks; that is, if other is null, then super.operator== is not invoked; therefore, it is ok to have a nullable type on the RHS of super == even if the corresponding operator== method declares its argument to be non-nullable.
Note that the NNBD spec is not entirely clear about this yet; I will shortly send a pull request to add this text (or something similar):
Similarly, consider an expression
eof the formsuper == e2, inside a class
C, where the static type ofe2isT2. LetSbe the type of the formal
parameter ofoperator ==in the super-interface ofC. It is a compile-time
error unlessT2is assignable toS?.
I'll also add some language tests if we don't have some already.