Skip to content

Commit

Permalink
Rollup merge of rust-lang#56572 - kevgrasso:let_self_err_dev, r=estebank
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kennytm committed Dec 12, 2018
2 parents e8ab05b + 5586c04 commit 3df2e7f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
19 changes: 16 additions & 3 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3010,6 +3010,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
// Visit all direct subpatterns of this pattern.
let outer_pat_id = pat.id;
pat.walk(&mut |pat| {
debug!("resolve_pattern pat={:?} node={:?}", pat, pat.node);
match pat.node {
PatKind::Ident(bmode, ident, ref opt_pat) => {
// First try to resolve the identifier as some existing
Expand Down Expand Up @@ -3166,6 +3167,7 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
format!("not found in {}", mod_str),
item_span)
};

let code = DiagnosticId::Error(code.into());
let mut err = this.session.struct_span_err_with_code(base_span, &base_msg, code);

Expand All @@ -3189,11 +3191,22 @@ impl<'a, 'crateloader: 'a> Resolver<'a, 'crateloader> {
return (err, Vec::new());
}
if is_self_value(path, ns) {
debug!("smart_resolve_path_fragment E0424 source:{:?}", source);

__diagnostic_used!(E0424);
err.code(DiagnosticId::Error("E0424".into()));
err.span_label(span, format!("`self` value is a keyword \
only available in \
methods with `self` parameter"));
err.span_label(span, match source {
PathSource::Pat => {
format!("`self` value is a keyword \
and may not be bound to \
variables or shadowed")
}
_ => {
format!("`self` value is a keyword \
only available in methods \
with `self` parameter")
}
});
return (err, Vec::new());
}

Expand Down
1 change: 1 addition & 0 deletions src/test/ui/error-codes/E0424.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ impl Foo {
}

fn main () {
let self = "self"; //~ ERROR E0424
}
8 changes: 7 additions & 1 deletion src/test/ui/error-codes/E0424.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ error[E0424]: expected value, found module `self`
LL | self.bar(); //~ ERROR E0424
| ^^^^ `self` value is a keyword only available in methods with `self` parameter

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

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0424`.
2 changes: 1 addition & 1 deletion src/tools/clippy

0 comments on commit 3df2e7f

Please sign in to comment.