Skip to content

Conversation

@A4-Tacks
Copy link
Member

Using this instead of self in a closure is a common pattern

Example

struct Foo { field: i32 }
impl Foo {
    fn foo(&mut self) {
        let f: fn(&mut Self) = |this| { $0 };
        f(self)
    }
}

Before this PR

fd self.field           i32
me self.foo() fn(&mut self)
lc self            &mut Foo
lc this            &mut Foo
md core
sp Self                 Foo
st Foo                  Foo
tt Fn
tt FnMut
tt FnOnce
bt u32                  u32

After this PR

fd this.field           i32
me this.foo() fn(&mut self)
lc self            &mut Foo
lc this            &mut Foo
md core
sp Self                 Foo
st Foo                  Foo
tt Fn
tt FnMut
tt FnOnce
bt u32                  u32

Using `this` instead of `self` in a closure is a common pattern

Example
---
```rust
struct Foo { field: i32 }
impl Foo {
    fn foo(&mut self) {
        let f: fn(&mut Self) = |this| { $0 };
        f(self)
    }
}
```

**Before this PR**

```text
fd self.field           i32
me self.foo() fn(&mut self)
lc self            &mut Foo
lc this            &mut Foo
md core
sp Self                 Foo
st Foo                  Foo
tt Fn
tt FnMut
tt FnOnce
bt u32                  u32
```

**After this PR**

```text
fd this.field           i32
me this.foo() fn(&mut self)
lc self            &mut Foo
lc this            &mut Foo
md core
sp Self                 Foo
st Foo                  Foo
tt Fn
tt FnMut
tt FnOnce
bt u32                  u32
```
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 29, 2025
Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

Thanks, this is nice!

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Dec 21, 2025
@ChayimFriedman2
Copy link
Contributor

I wonder, when this is used this is usually because using self will be an error (usually a borrowck error), so perhaps we should punish self's rank a bit in this case, to nudge users towards this?

@A4-Tacks
Copy link
Member Author

I am not familiar with ranking related codes.

Is it implemented on the client-side extension or the LSP server?

Merged via the queue into rust-lang:master with commit b5fbcc6 Dec 21, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 21, 2025
@ChayimFriedman2
Copy link
Contributor

In the server,

pub fn score(self) -> u32 {

@A4-Tacks
Copy link
Member Author

If self has a special ranking, it may be feasible to make this score higher

@A4-Tacks A4-Tacks deleted the fly-closure-this-param branch December 21, 2025 05: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.

3 participants