From 11011013f2b609b6cf58c96555b9e31dfd19d7ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 17 Oct 2019 19:00:19 -0700 Subject: [PATCH 1/5] Refer to "associated functions" instead of "static methods" --- src/librustc_resolve/error_codes.rs | 22 ++++++++++++++-------- src/librustc_resolve/late/diagnostics.rs | 22 ++++++++++------------ src/libsyntax_ext/deriving/clone.rs | 22 +++++++++------------- src/libsyntax_ext/deriving/default.rs | 2 +- src/libsyntax_ext/deriving/generic/mod.rs | 4 +--- src/test/ui/error-codes/E0424.stderr | 2 +- src/test/ui/resolve/issue-2356.stderr | 4 ++-- 7 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/librustc_resolve/error_codes.rs b/src/librustc_resolve/error_codes.rs index ab3d95dd8edfe..29ca797fc065c 100644 --- a/src/librustc_resolve/error_codes.rs +++ b/src/librustc_resolve/error_codes.rs @@ -1013,7 +1013,12 @@ fn h1() -> i32 { "##, E0424: r##" -The `self` keyword was used in a static method. +The `self` keyword was used inside of an associated function instead of inside +of a method. Associated functions have no "`self` receiver" argument, and are +equivalent to regular functions which exist in the namespace of a trait. +Methods, on the other hand, have a `self` reciver argument, like `self`, +`&self`, `&mut self` or `self: &mut Pin` (this last one is an example of +an ["abitrary `self` type"](https://github.com/rust-lang/rust/issues/44874)). Erroneous code example: @@ -1021,25 +1026,26 @@ Erroneous code example: struct Foo; impl Foo { - fn bar(self) {} + // `bar` is a method, because it has a receiver argument. + fn bar(&self) {} + // `foo` is an associated function, because it has no receiver argument. fn foo() { - self.bar(); // error: `self` is not available in a static method. + self.bar(); // error: `self` is not available in an associated function } } ``` -Please check if the method's argument list should have contained `self`, -`&self`, or `&mut self` (in case you didn't want to create a static -method), and add it if so. Example: +Check if the associated function's argument list should have contained a `self` +receiver for it to be a method, and add it if so. Example: ``` struct Foo; impl Foo { - fn bar(self) {} + fn bar(&self) {} - fn foo(self) { + fn foo(self) { // `foo` is now a method. self.bar(); // ok! } } diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index 412734eabe05b..dba4226e98363 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -115,8 +115,10 @@ impl<'a> LateResolutionVisitor<'a, '_> { if is_self_type(path, ns) { syntax::diagnostic_used!(E0411); err.code(DiagnosticId::Error("E0411".into())); - err.span_label(span, format!("`Self` is only available in impls, traits, \ - and type definitions")); + err.span_label( + span, + format!("`Self` is only available in impls, traits, and type definitions"), + ); return (err, Vec::new()); } if is_self_value(path, ns) { @@ -125,16 +127,12 @@ impl<'a> LateResolutionVisitor<'a, '_> { syntax::diagnostic_used!(E0424); err.code(DiagnosticId::Error("E0424".into())); 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") - } + 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 a `self` parameter", + ), }); return (err, Vec::new()); } diff --git a/src/libsyntax_ext/deriving/clone.rs b/src/libsyntax_ext/deriving/clone.rs index eb7d480aa9831..67ef69babdc0a 100644 --- a/src/libsyntax_ext/deriving/clone.rs +++ b/src/libsyntax_ext/deriving/clone.rs @@ -174,14 +174,12 @@ fn cs_clone(name: &str, all_fields = af; vdata = &variant.data; } - EnumNonMatchingCollapsed(..) => { - cx.span_bug(trait_span, - &format!("non-matching enum variants in \ - `derive({})`", - name)) - } + EnumNonMatchingCollapsed(..) => cx.span_bug(trait_span, &format!( + "non-matching enum variants in `derive({})`", + name, + )), StaticEnum(..) | StaticStruct(..) => { - cx.span_bug(trait_span, &format!("static method in `derive({})`", name)) + cx.span_bug(trait_span, &format!("associated function in `derive({})`", name)) } } @@ -191,12 +189,10 @@ fn cs_clone(name: &str, .map(|field| { let ident = match field.name { Some(i) => i, - None => { - cx.span_bug(trait_span, - &format!("unnamed field in normal struct in \ - `derive({})`", - name)) - } + None => cx.span_bug(trait_span, &format!( + "unnamed field in normal struct in `derive({})`", + name, + )), }; let call = subcall(cx, field); cx.field_imm(field.span, ident, call) diff --git a/src/libsyntax_ext/deriving/default.rs b/src/libsyntax_ext/deriving/default.rs index 6176791c31b18..cfc0f3cd6cbf9 100644 --- a/src/libsyntax_ext/deriving/default.rs +++ b/src/libsyntax_ext/deriving/default.rs @@ -75,6 +75,6 @@ fn default_substructure(cx: &mut ExtCtxt<'_>, // let compilation continue DummyResult::raw_expr(trait_span, true) } - _ => cx.span_bug(trait_span, "Non-static method in `derive(Default)`"), + _ => cx.span_bug(trait_span, "method in `derive(Default)`"), }; } diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index 1886a5154b7b6..fd2c1552d32a8 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -1055,9 +1055,7 @@ impl<'a> MethodDef<'a> { }) .collect() } else { - cx.span_bug(trait_.span, - "no self arguments to non-static method in generic \ - `derive`") + cx.span_bug(trait_.span, "no self arguments for method in generic `derive`") }; // body of the inner most destructuring match diff --git a/src/test/ui/error-codes/E0424.stderr b/src/test/ui/error-codes/E0424.stderr index d67a2660dac38..99b5e01abb192 100644 --- a/src/test/ui/error-codes/E0424.stderr +++ b/src/test/ui/error-codes/E0424.stderr @@ -2,7 +2,7 @@ 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 `self` parameter + | ^^^^ `self` value is a keyword only available in methods with a `self` parameter error[E0424]: expected unit struct/variant or constant, found module `self` --> $DIR/E0424.rs:12:9 diff --git a/src/test/ui/resolve/issue-2356.stderr b/src/test/ui/resolve/issue-2356.stderr index 7790383843e17..e0a2088ab8b98 100644 --- a/src/test/ui/resolve/issue-2356.stderr +++ b/src/test/ui/resolve/issue-2356.stderr @@ -62,7 +62,7 @@ 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 `self` parameter + | ^^^^ `self` value is a keyword only available in methods with a `self` parameter error[E0425]: cannot find function `grow_older` in this scope --> $DIR/issue-2356.rs:72:5 @@ -98,7 +98,7 @@ 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 `self` parameter + | ^^^^ `self` value is a keyword only available in methods with a `self` parameter error: aborting due to 17 previous errors From f65a492afcd9cd892a1a5591f938efd41b5e0d24 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Thu, 17 Oct 2019 20:26:21 -0700 Subject: [PATCH 2/5] Point at enclosing function without `self` receiver --- src/librustc_resolve/late.rs | 8 +++++++- src/librustc_resolve/late/diagnostics.rs | 3 +++ src/test/ui/error-codes/E0424.stderr | 14 ++++++++++---- src/test/ui/resolve/issue-2356.stderr | 18 ++++++++++++++---- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/librustc_resolve/late.rs b/src/librustc_resolve/late.rs index bb9f895c5f39b..73a282b1a0ec1 100644 --- a/src/librustc_resolve/late.rs +++ b/src/librustc_resolve/late.rs @@ -345,6 +345,9 @@ struct LateResolutionVisitor<'a, 'b> { /// The current self item if inside an ADT (used for better errors). current_self_item: Option, + /// The current enclosing funciton (used for better errors). + current_function: Option, + /// 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, @@ -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, @@ -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) { @@ -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(), } diff --git a/src/librustc_resolve/late/diagnostics.rs b/src/librustc_resolve/late/diagnostics.rs index dba4226e98363..2721df4c68763 100644 --- a/src/librustc_resolve/late/diagnostics.rs +++ b/src/librustc_resolve/late/diagnostics.rs @@ -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()); } diff --git a/src/test/ui/error-codes/E0424.stderr b/src/test/ui/error-codes/E0424.stderr index 99b5e01abb192..567d1b3cc75f4 100644 --- a/src/test/ui/error-codes/E0424.stderr +++ b/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 diff --git a/src/test/ui/resolve/issue-2356.stderr b/src/test/ui/resolve/issue-2356.stderr index e0a2088ab8b98..329543114a610 100644 --- a/src/test/ui/resolve/issue-2356.stderr +++ b/src/test/ui/resolve/issue-2356.stderr @@ -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 @@ -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 From 865c4bcff6b52f69b4122e3a0f02f647cf588553 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 18 Oct 2019 08:36:46 -0700 Subject: [PATCH 3/5] review comments: help wording --- src/librustc_resolve/error_codes.rs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/librustc_resolve/error_codes.rs b/src/librustc_resolve/error_codes.rs index 29ca797fc065c..ee207025e0c32 100644 --- a/src/librustc_resolve/error_codes.rs +++ b/src/librustc_resolve/error_codes.rs @@ -1013,12 +1013,12 @@ fn h1() -> i32 { "##, E0424: r##" -The `self` keyword was used inside of an associated function instead of inside -of a method. Associated functions have no "`self` receiver" argument, and are -equivalent to regular functions which exist in the namespace of a trait. -Methods, on the other hand, have a `self` reciver argument, like `self`, -`&self`, `&mut self` or `self: &mut Pin` (this last one is an example of -an ["abitrary `self` type"](https://github.com/rust-lang/rust/issues/44874)). +The `self` keyword was used inside of an associated function without a "`self` +receiver" parameter. The `self` keyword can only be used inside methods, which +are associated functions (functions defined inside of a `trait` or `impl` block) +that have a `self` receiver as its first parameter, like `self`, `&self`, +`&mut self` or `self: &mut Pin` (this last one is an example of an +["abitrary `self` type"](https://github.com/rust-lang/rust/issues/44874)). Erroneous code example: @@ -1026,17 +1026,18 @@ Erroneous code example: struct Foo; impl Foo { - // `bar` is a method, because it has a receiver argument. + // `bar` is a method, because it has a receiver parameter. fn bar(&self) {} - // `foo` is an associated function, because it has no receiver argument. + // `foo` is not a method, because it has no receiver parameter. fn foo() { - self.bar(); // error: `self` is not available in an associated function + self.bar(); // error: `self` value is a keyword only available in + // methods with a `self` parameter } } ``` -Check if the associated function's argument list should have contained a `self` +Check if the associated function's parameter list should have contained a `self` receiver for it to be a method, and add it if so. Example: ``` From bd813bf1acbef7643a06278af48603397c7eb5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 18 Oct 2019 08:38:08 -0700 Subject: [PATCH 4/5] review comment: span bug label --- src/libsyntax_ext/deriving/generic/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libsyntax_ext/deriving/generic/mod.rs b/src/libsyntax_ext/deriving/generic/mod.rs index fd2c1552d32a8..216338c1a8861 100644 --- a/src/libsyntax_ext/deriving/generic/mod.rs +++ b/src/libsyntax_ext/deriving/generic/mod.rs @@ -1055,7 +1055,7 @@ impl<'a> MethodDef<'a> { }) .collect() } else { - cx.span_bug(trait_.span, "no self arguments for method in generic `derive`") + cx.span_bug(trait_.span, "no `self` parameter for method in generic `derive`") }; // body of the inner most destructuring match From 2b76c8b95ff866d8806fdc82fe270c4df02efc0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 18 Oct 2019 13:00:14 -0700 Subject: [PATCH 5/5] review comments --- src/librustc_resolve/error_codes.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/librustc_resolve/error_codes.rs b/src/librustc_resolve/error_codes.rs index ee207025e0c32..2288aab3c27a6 100644 --- a/src/librustc_resolve/error_codes.rs +++ b/src/librustc_resolve/error_codes.rs @@ -1014,11 +1014,7 @@ fn h1() -> i32 { E0424: r##" The `self` keyword was used inside of an associated function without a "`self` -receiver" parameter. The `self` keyword can only be used inside methods, which -are associated functions (functions defined inside of a `trait` or `impl` block) -that have a `self` receiver as its first parameter, like `self`, `&self`, -`&mut self` or `self: &mut Pin` (this last one is an example of an -["abitrary `self` type"](https://github.com/rust-lang/rust/issues/44874)). +receiver" parameter. Erroneous code example: @@ -1037,6 +1033,12 @@ impl Foo { } ``` +The `self` keyword can only be used inside methods, which are associated +functions (functions defined inside of a `trait` or `impl` block) that have a +`self` receiver as its first parameter, like `self`, `&self`, `&mut self` or +`self: &mut Pin` (this last one is an example of an ["abitrary `self` +type"](https://github.com/rust-lang/rust/issues/44874)). + Check if the associated function's parameter list should have contained a `self` receiver for it to be a method, and add it if so. Example: