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
fix: resolve Self type references in delegate method assist #15705
fix: resolve Self type references in delegate method assist #15705
Conversation
Looking at the assist, I'm surprised we aren't using the |
Yeah that sounds like a good idea, I could give it a try! macro_rules! test_method {
() => {
fn test(self) -> Self {
self
}
};
}
struct Bar;
impl Bar {
test_method!();
}
struct Foo {
bar: $0Bar,
} |
Why wouldn't it work there? We can take a scope from a macro expansion's node just fine |
c6487b5
to
5b2cd56
Compare
Ah I was running into an issue where it said the lookup failed in this instance of I added the change to |
@@ -106,7 +107,10 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<' | |||
|edit| { | |||
// Create the function | |||
let method_source = match method.source(ctx.db()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let method_source = match method.source(ctx.db()) { | |
let method_source = match ctx.sema.source(method) { |
We can do this instead of the extra parse_or_expand
, this will do the caching for us then
PathTransform::generic_transformation( | ||
&ctx.sema.scope(strukt.syntax()).unwrap(), | ||
&ctx.sema.scope(method_source.syntax()).unwrap(), | ||
) | ||
.apply(f.syntax()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PathTransform::generic_transformation( | |
&ctx.sema.scope(strukt.syntax()).unwrap(), | |
&ctx.sema.scope(method_source.syntax()).unwrap(), | |
) | |
.apply(f.syntax()); | |
if let Some((target, source)) = | |
ctx.sema.scope(strukt.syntax()).zip(ctx.sema.scope(method_source.syntax())) | |
{ | |
PathTransform::generic_transformation(&target, &source).apply(f.syntax()); | |
} |
The unwraps shouldn't really be possible to hit here, but we are trying to not use unwraps in assist code if possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah okay, sorry about that!
} | ||
} | ||
|
||
ted::replace(path.syntax(), ast_ty.syntax()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably wanna prefer keeping Self
if it does not need to be replaced. So a check if the source and target scopes have the same Self
(and in that case, skip this transformation here entirely). We can give Ctx
a new bool field for that and set it accordingly in build_ctx
by giving SemanticsScope
a function like fn has_same_self_type(&self, other: &SemanticsScope<'_>) -> bool
which just checks if the top ImplDefScope
s of each scope's resolver has the same def in them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yeah good catch, thanks :)
Thanks! |
…=Veykril fix: resolve Self type references in delegate method assist This PR makes the delegate method assist resolve any `Self` type references in the parameters or return type. It also works across macros such as the `uint_impl!` macro used for `saturating_mul` in the issue example. Closes #14485
💔 Test failed - checks-actions |
d8f0a9d
to
7e768cb
Compare
rebased |
☀️ Test successful - checks-actions |
This PR makes the delegate method assist resolve any
Self
type references in the parameters or return type. It also works across macros such as theuint_impl!
macro used forsaturating_mul
in the issue example.Closes #14485