-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Remove false label when self
resolve failure does not relate to macro
#143177
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
Open
xizheyin
wants to merge
3
commits into
rust-lang:master
Choose a base branch
from
xizheyin:143134
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1183,15 +1183,23 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { | |
_ => "`self` value is a keyword only available in methods with a `self` parameter", | ||
}, | ||
); | ||
|
||
// using `let self` is wrong even if we're not in an associated method or if we're in a macro expansion. | ||
// So, we should return early if we're in a pattern, see issue #143134. | ||
if matches!(source, PathSource::Pat) { | ||
return true; | ||
} | ||
|
||
let is_assoc_fn = self.self_type_is_available(); | ||
let self_from_macro = "a `self` parameter, but a macro invocation can only \ | ||
access identifiers it receives from parameters"; | ||
if let Some((fn_kind, span)) = &self.diag_metadata.current_function { | ||
if let Some((fn_kind, fn_span)) = &self.diag_metadata.current_function { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I keep the |
||
// The current function has a `self` parameter, but we were unable to resolve | ||
// a reference to `self`. This can only happen if the `self` identifier we | ||
// are resolving came from a different hygiene context. | ||
// are resolving came from a different hygiene context or a variable binding. | ||
// But variable binding error is returned early above. | ||
if fn_kind.decl().inputs.get(0).is_some_and(|p| p.is_self()) { | ||
err.span_label(*span, format!("this function has {self_from_macro}")); | ||
err.span_label(*fn_span, format!("this function has {self_from_macro}")); | ||
} else { | ||
let doesnt = if is_assoc_fn { | ||
let (span, sugg) = fn_kind | ||
|
@@ -1204,7 +1212,7 @@ impl<'ast, 'ra, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> { | |
// This avoids placing the suggestion into the visibility specifier. | ||
let span = fn_kind | ||
.ident() | ||
.map_or(*span, |ident| span.with_lo(ident.span.hi())); | ||
.map_or(*fn_span, |ident| fn_span.with_lo(ident.span.hi())); | ||
( | ||
self.r | ||
.tcx | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,8 +40,6 @@ LL | fn qux(&self) { | |
error[E0424]: expected unit struct, unit variant or constant, found module `self` | ||
--> $DIR/E0424.rs:20:9 | ||
| | ||
LL | fn main () { | ||
| ---- this function can't have a `self` parameter | ||
Comment on lines
-43
to
-44
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It indeed bring some benefits. :) |
||
LL | let self = "self"; | ||
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
trait T { | ||
fn f(self); | ||
} | ||
impl T for () { | ||
fn f(self) { | ||
let self = (); //~ ERROR expected unit struct, unit variant or constant, found local variable `self` | ||
} | ||
} | ||
|
||
fn main() {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0424]: expected unit struct, unit variant or constant, found local variable `self` | ||
--> $DIR/false-self-in-macro-issue-143134.rs:6:13 | ||
| | ||
LL | let self = (); | ||
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0424`. |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I return early when the source is
Pat
.