Skip to content

Commit f65a492

Browse files
committed
Point at enclosing function without self receiver
1 parent 1101101 commit f65a492

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

src/librustc_resolve/late.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,9 @@ struct LateResolutionVisitor<'a, 'b> {
345345
/// The current self item if inside an ADT (used for better errors).
346346
current_self_item: Option<NodeId>,
347347

348+
/// The current enclosing funciton (used for better errors).
349+
current_function: Option<Span>,
350+
348351
/// A list of labels as of yet unused. Labels will be removed from this map when
349352
/// they are used (in a `break` or `continue` statement)
350353
unused_labels: FxHashMap<NodeId, Span>,
@@ -415,7 +418,8 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
415418
}
416419
}
417420
}
418-
fn visit_fn(&mut self, fn_kind: FnKind<'tcx>, declaration: &'tcx FnDecl, _: Span, _: NodeId) {
421+
fn visit_fn(&mut self, fn_kind: FnKind<'tcx>, declaration: &'tcx FnDecl, sp: Span, _: NodeId) {
422+
let previous_value = replace(&mut self.current_function, Some(sp));
419423
debug!("(resolving function) entering function");
420424
let rib_kind = match fn_kind {
421425
FnKind::ItemFn(..) => FnItemRibKind,
@@ -441,6 +445,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LateResolutionVisitor<'a, '_> {
441445
debug!("(resolving function) leaving function");
442446
})
443447
});
448+
self.current_function = previous_value;
444449
}
445450

446451
fn visit_generics(&mut self, generics: &'tcx Generics) {
@@ -546,6 +551,7 @@ impl<'a, 'b> LateResolutionVisitor<'a, '_> {
546551
current_trait_assoc_types: Vec::new(),
547552
current_self_type: None,
548553
current_self_item: None,
554+
current_function: None,
549555
unused_labels: Default::default(),
550556
current_type_ascription: Vec::new(),
551557
}

src/librustc_resolve/late/diagnostics.rs

+3
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@ impl<'a> LateResolutionVisitor<'a, '_> {
134134
"`self` value is a keyword only available in methods with a `self` parameter",
135135
),
136136
});
137+
if let Some(span) = &self.current_function {
138+
err.span_label(*span, "this function doesn't have a `self` parameter");
139+
}
137140
return (err, Vec::new());
138141
}
139142

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

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
error[E0424]: expected value, found module `self`
22
--> $DIR/E0424.rs:7:9
33
|
4-
LL | self.bar();
5-
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
4+
LL | / fn foo() {
5+
LL | | self.bar();
6+
| | ^^^^ `self` value is a keyword only available in methods with a `self` parameter
7+
LL | | }
8+
| |_____- this function doesn't have a `self` parameter
69

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

1319
error: aborting due to 2 previous errors
1420

src/test/ui/resolve/issue-2356.stderr

+14-4
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ LL | purr();
6161
error[E0424]: expected value, found module `self`
6262
--> $DIR/issue-2356.rs:65:8
6363
|
64-
LL | if self.whiskers > 3 {
65-
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
64+
LL | / fn meow() {
65+
LL | | if self.whiskers > 3 {
66+
| | ^^^^ `self` value is a keyword only available in methods with a `self` parameter
67+
LL | |
68+
LL | | println!("MEOW");
69+
LL | | }
70+
LL | | }
71+
| |___- this function doesn't have a `self` parameter
6672

6773
error[E0425]: cannot find function `grow_older` in this scope
6874
--> $DIR/issue-2356.rs:72:5
@@ -97,8 +103,12 @@ LL | purr_louder();
97103
error[E0424]: expected value, found module `self`
98104
--> $DIR/issue-2356.rs:92:5
99105
|
100-
LL | self += 1;
101-
| ^^^^ `self` value is a keyword only available in methods with a `self` parameter
106+
LL | / fn main() {
107+
LL | | self += 1;
108+
| | ^^^^ `self` value is a keyword only available in methods with a `self` parameter
109+
LL | |
110+
LL | | }
111+
| |_- this function doesn't have a `self` parameter
102112

103113
error: aborting due to 17 previous errors
104114

0 commit comments

Comments
 (0)