about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorMichael Howell <michael@notriddle.com>2021-09-28 12:48:58 -0700
committerMichael Howell <michael@notriddle.com>2021-09-28 13:02:45 -0700
commitbefdfb5c7113b3d82d973b1acfdcc2b0d9c14ab1 (patch)
tree7f534baa92c6a38aabb23987876c0e5a4fb520f3 /compiler
parent8a7c1306b43895a97c0675c6d854afa468a22aad (diff)
downloadrust-befdfb5c7113b3d82d973b1acfdcc2b0d9c14ab1.tar.gz
rust-befdfb5c7113b3d82d973b1acfdcc2b0d9c14ab1.zip
Improve error messages for bad type constraints
Co-authored-by: Esteban Kuber <esteban@kuber.com.ar>
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_parse/src/parser/path.rs31
1 files changed, 20 insertions, 11 deletions
diff --git a/compiler/rustc_parse/src/parser/path.rs b/compiler/rustc_parse/src/parser/path.rs
index 4e4130dfc23..c7d080a80fe 100644
--- a/compiler/rustc_parse/src/parser/path.rs
+++ b/compiler/rustc_parse/src/parser/path.rs
@@ -495,25 +495,28 @@ impl<'a> Parser<'a> {
             None => {
                 let after_eq = eq.shrink_to_hi();
                 let before_next = self.token.span.shrink_to_lo();
-                let the_type_placeholder = if matches!(self.token.kind, token::Comma | token::Gt) {
-                    " TheType"
-                } else {
-                    " TheType "
-                };
-                self.struct_span_err(after_eq.to(before_next), "missing type to the right of `=`")
-                    .span_suggestion(
+                let mut err = self
+                    .struct_span_err(after_eq.to(before_next), "missing type to the right of `=`");
+                if matches!(self.token.kind, token::Comma | token::Gt) {
+                    err.span_suggestion(
                         self.sess.source_map().next_point(eq).to(before_next),
                         "to constrain the associated type, add a type after `=`",
-                        the_type_placeholder.to_string(),
+                        " TheType".to_string(),
                         Applicability::HasPlaceholders,
-                    )
-                    .span_suggestion(
+                    );
+                    err.span_suggestion(
                         eq.to(before_next),
                         &format!("remove the `=` if `{}` is a type", ident),
                         String::new(),
                         Applicability::MaybeIncorrect,
                     )
-                    .emit();
+                } else {
+                    err.span_label(
+                        self.token.span,
+                        &format!("expected type, found {}", super::token_descr(&self.token)),
+                    )
+                };
+                return Err(err);
             }
         }
         Ok(self.mk_ty(span, ast::TyKind::Err))
@@ -584,6 +587,12 @@ impl<'a> Parser<'a> {
                 "expected lifetime, type, or constant, found keyword `const`",
             );
             if self.check_const_arg() {
+                err.span_suggestion_verbose(
+                    start.until(self.token.span),
+                    "the `const` keyword is only needed in the definition of the type",
+                    String::new(),
+                    Applicability::MaybeIncorrect,
+                );
                 err.emit();
                 GenericArg::Const(self.parse_const_arg()?)
             } else {