From f76eaece4e6d1c516564fe1d9677aa6a58fe53e4 Mon Sep 17 00:00:00 2001 From: Phlosioneer Date: Mon, 26 Mar 2018 00:34:58 -0400 Subject: [PATCH 1/3] Better error message when trying to write default impls Previously, if you tried to write this (using the specialization feature flag): default impl PartialEq { ... } The compiler would give you the mysterious warning "inherent impls cannot be default". What it really means is that you're trying to write an impl for a Structure or *Trait Object*, and that cannot be "default". However, one of the ways to encounter this error (as shown by the above example) is when you forget to write "for MyType". This PR adds a help message that reads "maybe missing a `for` keyword?" This is useful, actionable advice that will help any user identify their mistake, and doesn't get in the way or mislead any user that really meant to use the "default" keyword for this weird purpose. In particular, this help message will be useful for any users who don't know the "inherent impl" terminology, and/or users who forget that inherent impls CAN be written for traits (they apply to the trait objects). Both of these are somewhat confusing, seldom- used concepts; a one-line error message without any error number for longer explanation is NOT the place to introduce these ideas. --- src/librustc_passes/ast_validation.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index 37274d1fc4479..1c73ced806ec3 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -272,7 +272,9 @@ impl<'a> Visitor<'a> for AstValidator<'a> { self.err_handler().span_err(item.span, "inherent impls cannot be negative"); } if defaultness == Defaultness::Default { - self.err_handler().span_err(item.span, "inherent impls cannot be default"); + self.err_handler() + .struct_span_err(item.span, "inherent impls cannot be default") + .help("maybe a missing `for` keyword?"); } } ItemKind::ForeignMod(..) => { From 686682deb33d9003958889834fed92ced98828d0 Mon Sep 17 00:00:00 2001 From: Phlosioneer Date: Tue, 27 Mar 2018 16:01:02 -0400 Subject: [PATCH 2/3] Add emit call to error message --- src/librustc_passes/ast_validation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index 1c73ced806ec3..3386d41bc6300 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -274,7 +274,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if defaultness == Defaultness::Default { self.err_handler() .struct_span_err(item.span, "inherent impls cannot be default") - .help("maybe a missing `for` keyword?"); + .help("maybe a missing `for` keyword?").emit(); } } ItemKind::ForeignMod(..) => { From c61e641ed6462fb3a74b033a5db96f877256a021 Mon Sep 17 00:00:00 2001 From: Phlosioneer Date: Mon, 16 Apr 2018 16:56:24 -0400 Subject: [PATCH 3/3] Changed help message to note --- src/librustc_passes/ast_validation.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_passes/ast_validation.rs b/src/librustc_passes/ast_validation.rs index 3386d41bc6300..10951de8b94d7 100644 --- a/src/librustc_passes/ast_validation.rs +++ b/src/librustc_passes/ast_validation.rs @@ -274,7 +274,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { if defaultness == Defaultness::Default { self.err_handler() .struct_span_err(item.span, "inherent impls cannot be default") - .help("maybe a missing `for` keyword?").emit(); + .note("only trait implementations may be annotated with default").emit(); } } ItemKind::ForeignMod(..) => {