Skip to content

Update GeneratedMessage== to accept Object instead of dynamic #675

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

Merged
merged 2 commits into from
Jun 14, 2022

Conversation

jakemac53
Copy link
Contributor

This matches the overridden == method, previously it was expanding the parameter type unnecessarily.

This also fixes Mockito codegen for fakes, which adds back an == method with type Object, which isn't allowed if the type being faked has widened it to dynamic.

Note that you can still do == null, that is special cased.

Copy link
Member

@osa1 osa1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

which isn't allowed if the type being faked has widened it to dynamic.

We discussed this with @mraleph. The inferred type of the argument here is already Object as this method overrides a superclass method, and in the superclass the argument type is Object. Here's an example of this behavior:

class X {
  void f(String s) {}
}

Type getType<T>(T t) {
  return T;
}

class Y extends X {
  @override
  void f(s) {
    print(getType(s));
  }
}

void main() {
  Y().f('a');  // prints "String"
}

(I remember reading this behavior in the language spec also, but I can't find it now)

Here's another way to check this:

import 'package:protobuf/protobuf.dart';

class Test extends GeneratedMessage {
  @override
  GeneratedMessage clone() {
    throw '';
  }

  @override
  GeneratedMessage createEmptyInstance() {
    throw '';
  }

  @override
  BuilderInfo get info_ {
    throw '';
  }

  @override
  bool operator ==(Test test) {
    throw '';
  }
}

The error message shows that the inferred argument type in GeneratedMessage.operator == is Object, not dynamic:

  error - protobuf_test.dart:20:17 - 'Test.==' ('bool Function(Test)') isn't a valid override of 'GeneratedMessage.==' ('bool Function(Object)'). -
          invalid_override
           - The member being overridden at protobuf_test.dart:27:3659.

So it seems to me that this reveals a bug in Mockito, or something that Mockito uses.

We can still merge this of course but we should update the changelog entry as it's wrong. Actually we should probably omit the changelog entry as this is not a user-visible change.

@jakemac53
Copy link
Contributor Author

Hmm interesting, I am not sure why this fixes the Mockito issue then. It doesn't hurt to land this either way, as it is just more explicit about the type. But I will leave that up to you.

I will reproduce the error I was getting and see if I can understand more the actual problem.

@srawlins
Copy link
Contributor

I thought the same thing. Parameter types get inherited. So strange. Perhaps a bug in mockito.

@osa1 osa1 merged commit c85b414 into google:master Jun 14, 2022
@osa1
Copy link
Member

osa1 commented Jun 14, 2022

Just for the record, here's where the language spec describes inference of return and argument types of overriding methods: https://github.com/dart-lang/language/blob/ee1135e0c22391cee17bf3ee262d6a04582d25de/resources/type-system/inference.md?plain=1#L69-L72

  1. Method override inference
    • If you omit a return type or parameter type from an overridden or
      implemented method, inference will try to fill in the missing type using the
      signature of the methods you are overriding.

@jakemac53 jakemac53 deleted the object== branch June 14, 2022 15:56
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.

4 participants