Skip to content

Commit ece35d7

Browse files
authored
Rollup merge of rust-lang#56572 - kevgrasso:let_self_err_dev, r=estebank
Contexually dependent error message for E0424 when value is assigned to "self" This is an improvement for pull request rust-lang#54495 referencing issue rust-lang#54369. If the "self" keyword is assigned a value as though it were a valid identifier, it will now report: ``` let self = "self"; ^^^^ `self` value is a keyword and may not be bound to variables or shadowed ``` instead of ``` let self = "self"; ^^^^ `self` value is a keyword only available in methods with `self` parameter ``` If anyone has a better idea for what the error should be I'd be happy to modify it appropriately.
2 parents a6e98b3 + 5586c04 commit ece35d7

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

src/librustc_resolve/lib.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -3010,6 +3010,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
30103010
// Visit all direct subpatterns of this pattern.
30113011
let outer_pat_id = pat.id;
30123012
pat.walk(&mut |pat| {
3013+
debug!("resolve_pattern pat={:?} node={:?}", pat, pat.node);
30133014
match pat.node {
30143015
PatKind::Ident(bmode, ident, ref opt_pat) => {
30153016
// First try to resolve the identifier as some existing
@@ -3166,6 +3167,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
31663167
format!("not found in {}", mod_str),
31673168
item_span)
31683169
};
3170+
31693171
let code = DiagnosticId::Error(code.into());
31703172
let mut err = this.session.struct_span_err_with_code(base_span, &base_msg, code);
31713173

@@ -3189,11 +3191,22 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
31893191
return (err, Vec::new());
31903192
}
31913193
if is_self_value(path, ns) {
3194+
debug!("smart_resolve_path_fragment E0424 source:{:?}", source);
3195+
31923196
__diagnostic_used!(E0424);
31933197
err.code(DiagnosticId::Error("E0424".into()));
3194-
err.span_label(span, format!("`self` value is a keyword \
3195-
only available in \
3196-
methods with `self` parameter"));
3198+
err.span_label(span, match source {
3199+
PathSource::Pat => {
3200+
format!("`self` value is a keyword \
3201+
and may not be bound to \
3202+
variables or shadowed")
3203+
}
3204+
_ => {
3205+
format!("`self` value is a keyword \
3206+
only available in methods \
3207+
with `self` parameter")
3208+
}
3209+
});
31973210
return (err, Vec::new());
31983211
}
31993212

src/test/ui/error-codes/E0424.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ impl Foo {
1919
}
2020

2121
fn main () {
22+
let self = "self"; //~ ERROR E0424
2223
}

src/test/ui/error-codes/E0424.stderr

+7-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ error[E0424]: expected value, found module `self`
44
LL | self.bar(); //~ ERROR E0424
55
| ^^^^ `self` value is a keyword only available in methods with `self` parameter
66

7-
error: aborting due to previous error
7+
error[E0424]: expected unit struct/variant or constant, found module `self`
8+
--> $DIR/E0424.rs:22:9
9+
|
10+
LL | let self = "self"; //~ ERROR E0424
11+
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
12+
13+
error: aborting due to 2 previous errors
814

915
For more information about this error, try `rustc --explain E0424`.

src/tools/clippy

0 commit comments

Comments
 (0)