Skip to content

Suggest to use self for fake-self from other languages #54694

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

Merged
merged 4 commits into from
Oct 2, 2018

Conversation

csmoe
Copy link
Member

@csmoe csmoe commented Sep 30, 2018

Closes #54019
r? @estebank

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 30, 2018
@rust-highfive

This comment has been minimized.


fn baz(&self) -> i32 {
my.bar()
//~^ ERROR cannot find value `this` in this scope
Copy link
Member

Choose a reason for hiding this comment

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

s/this/my

span,
"do you mean",
"self".to_string(),
Applicability::MachineApplicable,
Copy link
Member

Choose a reason for hiding this comment

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

Hm, is this truly machine applicable in the sense that I can run cargo fix, it will apply this, and we can guarantee this fixes the code?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would keep this as MaybeIncorrect unless we added a check to see if the binding is available in self to begin with (which I don't see in this PR and shouldn't necessarily block it).

|s| Ident::from_str(*s)
).collect();
if fake_self.contains(&item_str)
&& this.self_value_is_available(path[0].span, span) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we ever get here in case that this or my are defiend? If so, maybe we should add a check here to only suggest this when they are undefined and the author actually meant what we call self?

A test case to consider adding would be

struct Foo { x: i32 };
impl Foo {
    fn foo(&mut self) -> i32 {
        let this = 13;
        // ... a hundred complicated lines of code ...
        this.x
    }
}

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, it's covered. I tested this locally(but deleted this lines from test case):

let this = self;
this.x

it works fine and doesn't trigger the err.
I'll recover this as you point here.

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

This is pretty neat! Could you quickly draft out how hard it'd be to see if self.foo actually exists when the user wrote this.foo? Not only that, if that is the case then it'd be a great idea to actually accept the code (while emitting the error) so that we avoid knock down effects from the incorrect code, although it seems that the scope is already marked as TyErr, so the output already looks good enough.

r=me with the test that @killercup pointed out fixed.

span,
"do you mean",
"self".to_string(),
Applicability::MachineApplicable,
Copy link
Contributor

Choose a reason for hiding this comment

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

I would keep this as MaybeIncorrect unless we added a check to see if the binding is available in self to begin with (which I don't see in this PR and shouldn't necessarily block it).

x: 2
};
let this = a;
this.x
Copy link
Member Author

@csmoe csmoe Oct 1, 2018

Choose a reason for hiding this comment

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

@estebank the testcases this1 this2 passed, should it be marked as Applicable?
oh, no, as you pointed out, if it's UnApplicable with this.fo for struct T { foo: i32 }.

Copy link
Member Author

@csmoe csmoe Oct 1, 2018

Choose a reason for hiding this comment

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

@estebank I guess it won't be hard as there alread has methods like check_field

Copy link
Contributor

Choose a reason for hiding this comment

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

That's great! In that case, could you make a follow up PR incorporating that change? At that point, if the check fails it might need to be a note instead of a suggestion, given that the likelihood of the suggestion (even MaybeIncorrect) plummets... I'm not sure.

Copy link
Member Author

Choose a reason for hiding this comment

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

yep, added an entry to TODO.

@csmoe

This comment has been minimized.

@bors

This comment has been minimized.

@csmoe

This comment has been minimized.

@bors

This comment has been minimized.

@rust-lang rust-lang deleted a comment from bors Oct 1, 2018
@estebank

This comment has been minimized.

@bors

This comment has been minimized.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 1, 2018
@csmoe
Copy link
Member Author

csmoe commented Oct 1, 2018

@estebank sorry for rebasing PR, I misspelled did you mean as do you mean. latest commits fixed that.

@killercup
Copy link
Member

@bors r=estebank

@bors
Copy link
Collaborator

bors commented Oct 1, 2018

📌 Commit 4470b1c has been approved by estebank

@bors
Copy link
Collaborator

bors commented Oct 2, 2018

⌛ Testing commit 4470b1c with merge 2d1065b...

bors added a commit that referenced this pull request Oct 2, 2018
Suggest to use self for fake-self from other languages

Closes #54019
r? @estebank
@bors
Copy link
Collaborator

bors commented Oct 2, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: estebank
Pushing 2d1065b to master...

@bors bors merged commit 4470b1c into rust-lang:master Oct 2, 2018
@csmoe csmoe deleted the self_this branch October 2, 2018 04:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants