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

Invalid override is generated when overridden method has covariant #506

Closed
srawlins opened this issue Jan 21, 2022 · 1 comment
Closed
Assignees
Labels
P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@srawlins
Copy link
Member

From @athomas :

class A {
  void foo(num n) {}
  void bar(int n) {}
}

class B extends A {
  @override
  void foo(covariant int n) {}
  @override
  void bar(int n) {}
}

// Roughly what mockito generates:
class MockitoFake extends B {
  @override
  void foo(int? n) {} // <-- invalid override
  @override
  void bar(int? n) {}
}

This foo override is invalid.

@srawlins srawlins added type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) P1 A high priority bug; for example, a single project is unusable or has many test failures labels Jan 21, 2022
@srawlins
Copy link
Member Author

I think when we encounter a covariant parameter, we must walk up inheritance and get the LUB of all the corresponding parameters. Testing considerations:

  • chain of two covariant parameters which successively narrow the type
  • covariant parameter in method overriding method from mixin
  • positional parameters
  • named parameters
  • parameters with default values (the AST plays tricks here)
  • covariant optional parameter with corresponding super-parameter with an overridden method which does not feature a corresponding parameter.
  • covariant fields... drowning in test cases

@srawlins srawlins self-assigned this Jan 21, 2022
srawlins added a commit that referenced this issue Feb 8, 2022
Fixes #506

A covariant parameter may tighten its type. In this case, mockito must
consider the parameter types in overridden methods and choose the LUB as the
type of the parameter it is implementing.

PiperOrigin-RevId: 423409713
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
P1 A high priority bug; for example, a single project is unusable or has many test failures type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

1 participant