summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_parse/src/parser/diagnostics.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_parse/src/parser/diagnostics.rs')
-rw-r--r--compiler/rustc_parse/src/parser/diagnostics.rs41
1 files changed, 34 insertions, 7 deletions
diff --git a/compiler/rustc_parse/src/parser/diagnostics.rs b/compiler/rustc_parse/src/parser/diagnostics.rs
index c14540396..0ce6a570d 100644
--- a/compiler/rustc_parse/src/parser/diagnostics.rs
+++ b/compiler/rustc_parse/src/parser/diagnostics.rs
@@ -605,6 +605,22 @@ impl<'a> Parser<'a> {
}
}
+ if let TokenKind::Ident(prev, _) = &self.prev_token.kind
+ && let TokenKind::Ident(cur, _) = &self.token.kind
+ {
+ let concat = Symbol::intern(&format!("{}{}", prev, cur));
+ let ident = Ident::new(concat, DUMMY_SP);
+ if ident.is_used_keyword() || ident.is_reserved() || ident.is_raw_guess() {
+ let span = self.prev_token.span.to(self.token.span);
+ err.span_suggestion_verbose(
+ span,
+ format!("consider removing the space to spell keyword `{}`", concat),
+ concat,
+ Applicability::MachineApplicable,
+ );
+ }
+ }
+
// `pub` may be used for an item or `pub(crate)`
if self.prev_token.is_ident_named(sym::public)
&& (self.token.can_begin_item()
@@ -751,13 +767,24 @@ impl<'a> Parser<'a> {
tail.could_be_bare_literal = true;
if maybe_struct_name.is_ident() && can_be_struct_literal {
// Account for `if Example { a: one(), }.is_pos() {}`.
- Err(self.sess.create_err(StructLiteralNeedingParens {
- span: maybe_struct_name.span.to(expr.span),
- sugg: StructLiteralNeedingParensSugg {
- before: maybe_struct_name.span.shrink_to_lo(),
- after: expr.span.shrink_to_hi(),
- },
- }))
+ // expand `before` so that we take care of module path such as:
+ // `foo::Bar { ... } `
+ // we expect to suggest `(foo::Bar { ... })` instead of `foo::(Bar { ... })`
+ let sm = self.sess.source_map();
+ let before = maybe_struct_name.span.shrink_to_lo();
+ if let Ok(extend_before) = sm.span_extend_prev_while(before, |t| {
+ t.is_alphanumeric() || t == ':' || t == '_'
+ }) {
+ Err(self.sess.create_err(StructLiteralNeedingParens {
+ span: maybe_struct_name.span.to(expr.span),
+ sugg: StructLiteralNeedingParensSugg {
+ before: extend_before.shrink_to_lo(),
+ after: expr.span.shrink_to_hi(),
+ },
+ }))
+ } else {
+ return None;
+ }
} else {
self.sess.emit_err(StructLiteralBodyWithoutPath {
span: expr.span,