8000 [Issue-1317][Mock solution] Enable validation between methods defined via inheritance and mixins by iking96 · Pull Request #8766 · sorbet/sorbet · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Issue-1317][Mock solution] Enable validation between methods defined via inheritance and mixins #8766

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

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

iking96
Copy link
@iking96 iking96 commented Apr 14, 2025
# typed: true

class Parent
  extend T::Helpers
  abstract!
  extend T::Sig
  sig {abstract.params(arg: Integer).void}
  def foo(arg); end
end

module M
  def foo; end
end

class Child < Parent
  extend T::Sig
  include M
end

In the above code snippet I would like the Child class to report that the implementation of foo it has received from M does not satisfy the abstract method from Parent (I would also like an error reported if M were the abstract class).

Changes

  • Largely:
    • Allow validateCompatibleOverride() to accept a "receiverKlass" and "loc_for_error", so it can report on errors that may not just be relevant to the methods owner.
    • Call validateOverriding in validateAbstract
Screenshot 2025-04-17 at 2 02 18 PM

Motivation

This is a response to the issue raised here. I would like to follow a Ruby pattern similar to the one outlined in the issue and would like to have sorbet raise relevant errors.

Test plan

The plan is to add new tests! See included automated tests.

@iking96 iking96 changed the title Iking96 issue 1317 mock [Issue 1317] Mock Apr 14, 2025
@iking96 iking96 changed the title [Issue 1317] Mock [Issue 1317] Mock solution Apr 14, 2025
@iking96 iking96 force-pushed the iking96_issue_1317_mock branch 2 times, most recently from 1134c93 to a2f8d56 Compare April 16, 2025 06:48
iking96 added 2 commits April 17, 2025 07:01
NOTE: This allows checkSubtype() to be called from the perspective of a receiver rather than the perspective of the owner of a submethod (the owner of a submethod and receiver may be different in the case of abstract methods).
@iking96 iking96 force-pushed the iking96_issue_1317_mock branch from 1eff8d5 to 8ce1495 Compare April 17, 2025 13:13
iking96 added 2 commits April 17, 2025 10:33
…() and validateOverridingForMethodInClass()

NOTE: The purpose of validateOverridingForMethodInClass() is to allow for validation of overriding of a method from the view of the certain class
@iking96 iking96 force-pushed the iking96_issue_1317_mock branch 2 times, most recently from e5dfe09 to d23a55f Compare April 17, 2025 17:04
iking96 added 3 commits April 17, 2025 13:32
NOTE:
- This allows the error reports in validateCompatibleOverride() to point to some location other then the method which is passed in
- This commit updates the error location to be the relevant class in the case that class does not own the method causing the failure
…om requirement to use 'override' tag

NOTE: this is the current behavior, but also it seems like there would be no way for a programmer to respect this requirement in the case an abstract method is implemented by a mixin. Perhaps they could re-define the method, call super and update the sig to have an 'override', but that seems cumbersome.
@iking96 iking96 force-pushed the iking96_issue_1317_mock branch from d23a55f to 838263e Compare April 17, 2025 19:36
iking96 added 4 commits April 17, 2025 13:42
…e the case where the initial super class method found has the overload name

NOTE: Implemented as a result of failures see in rewriter/struct_initialize test (and others).
… was not considered when evaluating type compatibility of 'attached_class'

NOTE: Implemented as a result of failures seein in initialize_class_bound test (on ChildGoodThing class complaining about T.attached_class). Likely due to abstract T.attached_class mixed with inheritance.
…method implementation which conflicts with types on initial definition
@iking96 iking96 force-pushed the iking96_issue_1317_mock branch from 838263e to 20e6fe2 Compare April 17, 2025 19:44
8000
Comment on lines +343 to +353
// If no direct match, try to find through inheritance chain
for (auto &el : params) {
// Check if el's owner derives from this->definition's owner or vice versa
if (el.data(gs)->owner.asClassOrModuleRef().data(gs)->derivesFrom(gs, this->definition.data(gs)->owner.asClassOrModuleRef()) ||
this->definition.data(gs)->owner.asClassOrModuleRef().data(gs)->derivesFrom(gs, el.data(gs)->owner.asClassOrModuleRef())) {
// If they're related, check if they represent the same type parameter
if (el.data(gs)->name == this->definition.data(gs)->name) {
return targs[&el - &params.front()];
}
}
}
Copy link
Author

Choose a reason for hiding this comment

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

NOTE: Error log which prompted this change. test.log

}

ENFORCE(superMethod.exists());
Copy link
Author

Choose a reason for hiding this comment

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

NOTE: Error log which prompted this change. test.log

@iking96 iking96 changed the title [Issue 1317] Mock solution [Issue-1317] Mock solution Apr 17, 2025
@jez
Copy link
Collaborator
jez commented Apr 17, 2025

Hey thanks for taking an interest in Sorbet!

Note that #1317 is likely the sort of task that will not be able to be completed without substantial involvement from the Sorbet team to fix the issues that arise on Stripe's codebase.

You're welcome to tinker with this for your own curiosity, but it's unlikely that this change will land until possibly many months or years in the future when someone from our team can prioritize giving it the attention it will need.

@iking96
Copy link
Author
iking96 commented Apr 17, 2025

@jez Thanks for chiming in and for sharing the reality that this will be difficult to land in practice.

If you have time (and if it matches how you'd usually engage with these things), would you be able to provide any feedback on the approach taken here? My changes seem to pass tests locally and seem to provide correct-looking error outputs for the examples shared in #1317.

Of course I'd love to get this in (I have a use case here at Gusto that I would like to have Sorbet report on), but I understand that actually getting this landed would take investment beyond just a review. 🙏

@iking96 iking96 changed the title [Issue-1317] Mock solution [Issue-1317][Mock solution] Enable validation between methods defined via inheritance and mixins Apr 17, 2025
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.

2 participants
0