Skip to content

Context type intentionally different from parameter type in non-standard ==? #3098

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

Open
eernstg opened this issue May 22, 2023 · 2 comments
Open
Labels
type-inference Type inference, issues or improvements

Comments

@eernstg
Copy link
Member

eernstg commented May 22, 2023

Consider the following program:

X getContextType<X>() {
  print(X);
  throw 0;
}

class A {
  bool operator ==(covariant A other) => identical(this, other);
}

void main() {
  A() == getContextType(); // Prints 'dynamic'.
}

I would expect the context type to be A?.

@eernstg
Copy link
Member Author

eernstg commented May 22, 2023

@stereotype441, @johnniwinther, what would you expect?

@stereotype441
Copy link
Member

@stereotype441, @johnniwinther, what would you expect?

I can't find the behaviour specified anywhere (https://github.com/dart-lang/language/blob/main/resources/type-system/inference.md doesn't appear to say anything), but A? seems totally reasonable to me based on this reasoning:

  • In general, a op b is interpereted as a.operator op(b).
  • In general, for a.m(b), b is type inferred using a context that comes from the parameter type accepted by b.
  • For the special case of operator ==, we know that null can appear on the RHS, regardless of the signature of operator ==.
  • Therefore, since A.operator == has a parameter type of A, it makes sense for the context to be A?.

I ran some experiments with the analyzer and it looks like it behaves consistently with the CFE (it uses the context dynamic regardless of the type on the LHS of ==).

I would be open to changing this so that the context is A?. But since the analyzer and the CFE agree, I think we should consider this a language issue, and if we decide to address it, either make it a language-versioned change, or follow the breaking change process. So I'm going to go ahead and transfer this issue to the language repo.

Note that if we decide to change this behaviour so that the context type in Erik's example is A?, that begs the question of what we want the context type of the RHS of == to be when the parameter type of operator== is Object, Object?, or dynamic. For consistency, it would make sense to use dynamic when the parameter type is dynamic and Object? when the parameter type is Object or Object?. However, since Object.operator== is defined with a parameter type of Object, this would affect the type inference behaviour of nearly every equality check in all of Dart.

@stereotype441 stereotype441 transferred this issue from dart-lang/sdk May 22, 2023
@lrhn lrhn added type-inference Type inference, issues or improvements and removed inference labels Apr 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-inference Type inference, issues or improvements
Projects
None yet
Development

No branches or pull requests

3 participants