Skip to content

Commit

Permalink
Point at enclosing function without self receiver
Browse files Browse the repository at this point in the history
  • Loading branch information
estebank committed Oct 18, 2019
1 parent 1101101 commit f65a492
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/librustc_resolve/late.rs
Expand Up @@ -345,6 +345,9 @@ struct LateResolutionVisitor<'a, 'b> {
/// The current self item if inside an ADT (used for better errors).
current_self_item: Option<NodeId>,

/// The current enclosing funciton (used for better errors).
current_function: Option<Span>,

/// A list of labels as of yet unused. Labels will be removed from this map when
/// they are used (in a `break` or `continue` statement)
unused_labels: FxHashMap<NodeId, Span>,
Expand Down Expand Up @@ -415,7 +418,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
}
}
}
fn visit_fn(&mut self, fn_kind: FnKind<'tcx>, declaration: &'tcx FnDecl, _: Span, _: NodeId) {
fn visit_fn(&mut self, fn_kind: FnKind<'tcx>, declaration: &'tcx FnDecl, sp: Span, _: NodeId) {
let previous_value = replace(&mut self.current_function, Some(sp));
debug!("(resolving function) entering function");
let rib_kind = match fn_kind {
FnKind::ItemFn(..) => FnItemRibKind,
Expand All @@ -441,6 +445,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
debug!("(resolving function) leaving function");
})
});
self.current_function = previous_value;
}

fn visit_generics(&mut self, generics: &'tcx Generics) {
Expand Down Expand Up @@ -546,6 +551,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
current_trait_assoc_types: Vec::new(),
current_self_type: None,
current_self_item: None,
current_function: None,
unused_labels: Default::default(),
current_type_ascription: Vec::new(),
}
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_resolve/late/diagnostics.rs
Expand Up @@ -134,6 +134,9 @@ impl<'a> LateResolutionVisitor<'a, '_> {
"`self` value is a keyword only available in methods with a `self` parameter",
),
});
if let Some(span) = &self.current_function {
err.span_label(*span, "this function doesn't have a `self` parameter");
}
return (err, Vec::new());
}

Expand Down
14 changes: 10 additions & 4 deletions src/test/ui/error-codes/E0424.stderr
@@ -1,14 +1,20 @@
error[E0424]: expected value, found module `self`
--> $DIR/E0424.rs:7:9
|
LL | self.bar();
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
LL | / fn foo() {
LL | | self.bar();
| | ^^^^ `self` value is a keyword only available in methods with a `self` parameter
LL | | }
| |_____- this function doesn't have a `self` parameter

error[E0424]: expected unit struct/variant or constant, found module `self`
--> $DIR/E0424.rs:12:9
|
LL | let self = "self";
| ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
LL | / fn main () {
LL | | let self = "self";
| | ^^^^ `self` value is a keyword and may not be bound to variables or shadowed
LL | | }
| |_- this function doesn't have a `self` parameter

error: aborting due to 2 previous errors

Expand Down
18 changes: 14 additions & 4 deletions src/test/ui/resolve/issue-2356.stderr
Expand Up @@ -61,8 +61,14 @@ LL | purr();
error[E0424]: expected value, found module `self`
--> $DIR/issue-2356.rs:65:8
|
LL | if self.whiskers > 3 {
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
LL | / fn meow() {
LL | | if self.whiskers > 3 {
| | ^^^^ `self` value is a keyword only available in methods with a `self` parameter
LL | |
LL | | println!("MEOW");
LL | | }
LL | | }
| |___- this function doesn't have a `self` parameter

error[E0425]: cannot find function `grow_older` in this scope
--> $DIR/issue-2356.rs:72:5
Expand Down Expand Up @@ -97,8 +103,12 @@ LL | purr_louder();
error[E0424]: expected value, found module `self`
--> $DIR/issue-2356.rs:92:5
|
LL | self += 1;
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
LL | / fn main() {
LL | | self += 1;
| | ^^^^ `self` value is a keyword only available in methods with a `self` parameter
LL | |
LL | | }
| |_- this function doesn't have a `self` parameter

error: aborting due to 17 previous errors

Expand Down

0 comments on commit f65a492

Please sign in to comment.