Skip to content

Add a fix that inserts a missing method #19950

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 8 commits into
base: master
Choose a base branch
from

Conversation

MatrixFrog
Copy link
Contributor

@MatrixFrog MatrixFrog commented Jun 9, 2025

Work in progress! Not ready for review yet.

Often I will call a method even though I know it doesn't exist yet, because I'm planning to add that method.

struct Dog {}

impl Dog {
  bark(&self) {}
}

fn main() {
  let dog = Dog {};
  dog.bark();
  dog.play_dead();  // errors because the method doesn't exist yet
}

This PR adds a fix that inserts a stub for the play_dead method.

  • For now, the receiver type is &self but maybe we can figure out whether it should be self or &mut self instead?
  • Return type is not added but it could be inferred if the calling code looks like let lhs: SomeType = dog.play_dead()
  • Should the method body be todo!() or unimplemented!() or something else? What is the most consistent with how r-a does things elsewhere?
  • Need to do more testing
  • Don't try to add a method to crates that aren't in the current workspace. (I've been trying this out locally and I accidentally got it to try and add a method to std::fs::File)

@ChayimFriedman2
Copy link
Contributor

Should the method body be todo!() or unimplemented!() or something else? What is the most consistent with how r-a does things elsewhere?

There is a config for what to insert in e.g. fill match arms, I think it's appropriate to use it here as well.

@MatrixFrog MatrixFrog force-pushed the add_missing_method branch from 6e541ae to 4d54aab Compare June 22, 2025 06:41
@@ -289,6 +333,117 @@ fn main() {
);
}

#[test]
fn test_add_method_fix_ref_self() {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this test currently fails. we're calling a non-existent method on &Dolphin (not Dolphin) so no fix is generated. In such a case we should look for an impl Dolphin block and add a method that takes &self, or look for an impl &Dolphin block and add a method that takes self. What if both exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haven't yet tested cases with &mut SomeType but probably need to handle that too.

Copy link
Member

@Veykril Veykril left a comment

Choose a reason for hiding this comment

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

There is an assist that can create functions from unresolved ones,


Naturally, that should turn into a diagnostic in the long run (when we have more confident diagnostics for this).

Would be good to deduplicate code here, especially as the assist already supports this use case better (being signature aware)

@MatrixFrog MatrixFrog force-pushed the add_missing_method branch from 7f05496 to 6f77fd3 Compare July 8, 2025 04:01
@MatrixFrog MatrixFrog force-pushed the add_missing_method branch from d98b4ab to b59a82d Compare July 14, 2025 22:31
.enumerate()
.map(|(i, ty)| {
let name = format!("arg{}", i + 1);
let ty = ty.display_source_code(db, module_id.into(), true).unwrap();
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this unwrap panicked on some real code. if there's an error just put in '()' or something for the type

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.

3 participants