Skip to content

Call to super == x should allow null, even if the resolved operator == method does not allow it. #42603

@stereotype441

Description

@stereotype441

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 e of the form super == e2, inside a class
C, where the static type of e2 is T2. Let S be the type of the formal
parameter of operator == in the super-interface of C. It is a compile-time
error unless T2 is assignable to S?.

I'll also add some language tests if we don't have some already.

Metadata

Metadata

Assignees

No one assigned

    Labels

    NNBDIssues related to NNBD Releaselegacy-area-front-endLegacy: Use area-dart-model instead.type-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions