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

VM instantiated tear-offs of instance methods ignores the instantiation type in equality checks. #45890

Closed
lrhn opened this issue May 3, 2021 · 4 comments
Assignees
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.

Comments

@lrhn
Copy link
Member

lrhn commented May 3, 2021

Example:

class C {
  T foo<T>(T value) => value;
}
void main() {
  var c = C();
  num Function(num) f0 = c.foo;
  int Function(int) f1 = c.foo;
  print(f0 == f1); // true
  print(f0.runtimeType); // (num) => num
  print(f1.runtimeType); // (int) => int
}

It seems inconsistent that two functions with different types should be equal. It's true that they have the same underlying behavior, and calling them with the same valid argument will give the same result, but it allows surprising equalities to be true, and if code does different things depending on the run-time type of the function they're passed, then it might make a difference whether a set of function can hold both or not.

@lrhn lrhn added the area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends. label May 3, 2021
@eernstg
Copy link
Member

eernstg commented May 3, 2021

We do specify that equality must hold (according to operator ==) when the generic function instantiation is applied to the same function declaration with the same type arguments (whether constant or not), and argue that equality cannot hold with different type arguments. A similar rule is given for generic method instantiation.

@lrhn
Copy link
Member Author

lrhn commented May 3, 2021

That text specifies when instantiated instance method tear-offs must be equal (allowing them to be identical, which no-one does).
It doesn't actually say when they must not be equal, so the VM is technically correct. It just says that it wouldn't make sense to say that they are equal when type arguments are different, but that's in the rationale, not normative text.
(Also, it depends on the phrase "the same type" which is historically underspecified. I think that got better with null safety, now it probably means "normalizes to the syntactically same type, up to alpha equivalence of bound type variables").

(Rule of thumb, when you need to prefix something with "technically", it really means "not actually").

@eernstg
Copy link
Member

eernstg commented May 3, 2021

It doesn't actually say when they must not be equal

When we specify that the function objects must compare equal according to operator == with "the same" type arguments, and mention in rationale that it would not make sense to consider them equal when the type arguments are "not the same", it's a really long shot to say that it's OK to return true from that operator == when the type arguments are different.

(.. when you need to prefix something with "technically", it really means "not actually").

Exactly! 😄

@a-siva
Copy link
Contributor

a-siva commented May 3, 2021

/cc @crelier

@crelier crelier self-assigned this May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-vm Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.
Projects
None yet
Development

No branches or pull requests

4 participants