From 7e323370b351ab138caa90cddb0e9811a4e8439d Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 11 Mar 2022 15:26:19 -0700 Subject: [PATCH 1/2] diagnostics: single colon within `<>` probably, not type ascription Fixes #94812 --- compiler/rustc_parse/src/parser/diagnostics.rs | 13 +++++++++++++ .../single-colon-path-not-const-generics.rs | 13 +++++++++++++ .../single-colon-path-not-const-generics.stderr | 11 +++++++++++ 3 files changed, 37 insertions(+) create mode 100644 src/test/ui/generics/single-colon-path-not-const-generics.rs create mode 100644 src/test/ui/generics/single-colon-path-not-const-generics.stderr diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 40daf4eb28fc1..5b398a513185a 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -2073,6 +2073,19 @@ impl<'a> Parser<'a> { let value = self.mk_expr_err(start.to(expr.span)); err.emit(); return Ok(GenericArg::Const(AnonConst { id: ast::DUMMY_NODE_ID, value })); + } else if token::Colon == snapshot.token.kind + && expr.span.lo() == snapshot.token.span.hi() + && matches!(expr.kind, ExprKind::Path(..)) + { + // Find a mistake like "foo::var:A". + err.span_suggestion( + snapshot.token.span, + "you might have meant to write a path", + "::".to_string(), + Applicability::MaybeIncorrect, + ); + err.emit(); + return Ok(GenericArg::Type(self.mk_ty(start.to(expr.span), TyKind::Err))); } else if token::Comma == self.token.kind || self.token.kind.should_end_const_arg() { // Avoid the following output by checking that we consumed a full const arg: diff --git a/src/test/ui/generics/single-colon-path-not-const-generics.rs b/src/test/ui/generics/single-colon-path-not-const-generics.rs new file mode 100644 index 0000000000000..d8ed8fbfe72f1 --- /dev/null +++ b/src/test/ui/generics/single-colon-path-not-const-generics.rs @@ -0,0 +1,13 @@ +pub mod foo { + pub mod bar { + pub struct A; + } +} + +pub struct Foo { + a: Vec, + //~^ ERROR expected + //~| HELP you might have meant to write a path +} + +fn main() {} diff --git a/src/test/ui/generics/single-colon-path-not-const-generics.stderr b/src/test/ui/generics/single-colon-path-not-const-generics.stderr new file mode 100644 index 0000000000000..d509fb7aeaa82 --- /dev/null +++ b/src/test/ui/generics/single-colon-path-not-const-generics.stderr @@ -0,0 +1,11 @@ +error: expected one of `,` or `>`, found `:` + --> $DIR/single-colon-path-not-const-generics.rs:8:18 + | +LL | a: Vec, + | ^ + | | + | expected one of `,` or `>` + | help: you might have meant to write a path: `::` + +error: aborting due to previous error + From 26e299a4a74e4526ee405476bf45559c434d28ce Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Sat, 12 Mar 2022 08:20:36 -0700 Subject: [PATCH 2/2] Use consistent prose for `::` Co-authored-by: Camille Gillot --- compiler/rustc_parse/src/parser/diagnostics.rs | 2 +- src/test/ui/generics/single-colon-path-not-const-generics.rs | 2 +- .../ui/generics/single-colon-path-not-const-generics.stderr | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs index 5b398a513185a..21d5bec65f03a 100644 --- a/compiler/rustc_parse/src/parser/diagnostics.rs +++ b/compiler/rustc_parse/src/parser/diagnostics.rs @@ -2080,7 +2080,7 @@ impl<'a> Parser<'a> { // Find a mistake like "foo::var:A". err.span_suggestion( snapshot.token.span, - "you might have meant to write a path", + "write a path separator here", "::".to_string(), Applicability::MaybeIncorrect, ); diff --git a/src/test/ui/generics/single-colon-path-not-const-generics.rs b/src/test/ui/generics/single-colon-path-not-const-generics.rs index d8ed8fbfe72f1..55a7ae0bb6d91 100644 --- a/src/test/ui/generics/single-colon-path-not-const-generics.rs +++ b/src/test/ui/generics/single-colon-path-not-const-generics.rs @@ -7,7 +7,7 @@ pub mod foo { pub struct Foo { a: Vec, //~^ ERROR expected - //~| HELP you might have meant to write a path + //~| HELP path separator } fn main() {} diff --git a/src/test/ui/generics/single-colon-path-not-const-generics.stderr b/src/test/ui/generics/single-colon-path-not-const-generics.stderr index d509fb7aeaa82..3eafa9fa5a9dd 100644 --- a/src/test/ui/generics/single-colon-path-not-const-generics.stderr +++ b/src/test/ui/generics/single-colon-path-not-const-generics.stderr @@ -5,7 +5,7 @@ LL | a: Vec, | ^ | | | expected one of `,` or `>` - | help: you might have meant to write a path: `::` + | help: write a path separator here: `::` error: aborting due to previous error