From 37fdcb4b364aeb8164a21a39dac984eb1e772872 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Wed, 14 Sep 2022 20:11:42 +0200 Subject: [PATCH] Don't unnecessarily stringify paths in diagnostics --- .../locales/en-US/parser.ftl | 8 ++++---- compiler/rustc_parse/src/errors.rs | 7 ++++--- compiler/rustc_parse/src/parser/expr.rs | 18 +++++++++--------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/compiler/rustc_error_messages/locales/en-US/parser.ftl b/compiler/rustc_error_messages/locales/en-US/parser.ftl index e45094d72bc..dea85d8e7f4 100644 --- a/compiler/rustc_error_messages/locales/en-US/parser.ftl +++ b/compiler/rustc_error_messages/locales/en-US/parser.ftl @@ -165,13 +165,13 @@ parser_use_empty_block_not_semi = expected { "`{}`" }, found `;` .suggestion = try using { "`{}`" } instead parser_comparison_interpreted_as_generic = - `<` is interpreted as a start of generic arguments for `{$typename}`, not a comparison + `<` is interpreted as a start of generic arguments for `{$type}`, not a comparison .label_args = interpreted as generic arguments .label_comparison = not interpreted as comparison .suggestion = try comparing the cast value parser_shift_interpreted_as_generic = - `<<` is interpreted as a start of generic arguments for `{$typename}`, not a shift + `<<` is interpreted as a start of generic arguments for `{$type}`, not a shift .label_args = interpreted as generic arguments .label_comparison = not interpreted as shift .suggestion = try shifting the cast value @@ -184,8 +184,8 @@ parser_leading_plus_not_supported = leading `+` is not supported .suggestion_remove_plus = try removing the `+` parser_parentheses_with_struct_fields = invalid `struct` delimiters or `fn` call arguments - .suggestion_braces_for_struct = if `{$name}` is a struct, use braces as delimiters - .suggestion_no_fields_for_fn = if `{$name}` is a function, use the arguments directly + .suggestion_braces_for_struct = if `{$type}` is a struct, use braces as delimiters + .suggestion_no_fields_for_fn = if `{$type}` is a function, use the arguments directly parser_labeled_loop_in_break = parentheses are required around this expression to avoid confusion with a labeled break expression diff --git a/compiler/rustc_parse/src/errors.rs b/compiler/rustc_parse/src/errors.rs index f10b178049b..3d364a956d3 100644 --- a/compiler/rustc_parse/src/errors.rs +++ b/compiler/rustc_parse/src/errors.rs @@ -1,3 +1,4 @@ +use rustc_ast::Path; use rustc_errors::{fluent, AddToDiagnostic, Applicability, EmissionGuarantee, IntoDiagnostic}; use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_session::errors::ExprParenthesesNeeded; @@ -536,7 +537,7 @@ pub(crate) struct ComparisonInterpretedAsGeneric { #[primary_span] #[label(parser::label_comparison)] pub comparison: Span, - pub typename: String, + pub r#type: Path, #[label(parser::label_args)] pub args: Span, #[subdiagnostic] @@ -549,7 +550,7 @@ pub(crate) struct ShiftInterpretedAsGeneric { #[primary_span] #[label(parser::label_comparison)] pub shift: Span, - pub typename: String, + pub r#type: Path, #[label(parser::label_args)] pub args: Span, #[subdiagnostic] @@ -597,7 +598,7 @@ pub(crate) struct LeadingPlusNotSupported { pub(crate) struct ParenthesesWithStructFields { #[primary_span] pub span: Span, - pub name: String, + pub r#type: Path, #[subdiagnostic] pub braces_for_struct: BracesForStructLiteral, #[subdiagnostic] diff --git a/compiler/rustc_parse/src/parser/expr.rs b/compiler/rustc_parse/src/parser/expr.rs index ee9676c8054..02cee75a0f5 100644 --- a/compiler/rustc_parse/src/parser/expr.rs +++ b/compiler/rustc_parse/src/parser/expr.rs @@ -756,11 +756,12 @@ fn parse_assoc_op_cast( match self.parse_path(PathStyle::Expr) { Ok(path) => { - let typename = pprust::path_to_string(&path); - let span_after_type = parser_snapshot_after_type.token.span; - let expr = - mk_expr(self, lhs, self.mk_ty(path.span, TyKind::Path(None, path))); + let expr = mk_expr( + self, + lhs, + self.mk_ty(path.span, TyKind::Path(None, path.clone())), + ); let args_span = self.look_ahead(1, |t| t.span).to(span_after_type); let suggestion = ComparisonOrShiftInterpretedAsGenericSugg { @@ -771,14 +772,14 @@ fn parse_assoc_op_cast( match self.token.kind { token::Lt => self.sess.emit_err(ComparisonInterpretedAsGeneric { comparison: self.token.span, - typename, + r#type: path, args: args_span, suggestion, }), token::BinOp(token::Shl) => { self.sess.emit_err(ShiftInterpretedAsGeneric { shift: self.token.span, - typename, + r#type: path, args: args_span, suggestion, }) @@ -1197,9 +1198,8 @@ fn maybe_recover_struct_lit_bad_delims( ) -> Option> { match (seq.as_mut(), snapshot) { (Err(err), Some((mut snapshot, ExprKind::Path(None, path)))) => { - let name = pprust::path_to_string(&path); snapshot.bump(); // `(` - match snapshot.parse_struct_fields(path, false, Delimiter::Parenthesis) { + match snapshot.parse_struct_fields(path.clone(), false, Delimiter::Parenthesis) { Ok((fields, ..)) if snapshot.eat(&token::CloseDelim(Delimiter::Parenthesis)) => { @@ -1211,7 +1211,7 @@ fn maybe_recover_struct_lit_bad_delims( if !fields.is_empty() { let mut replacement_err = ParenthesesWithStructFields { span, - name, + r#type: path, braces_for_struct: BracesForStructLiteral { first: open_paren, second: close_paren, -- 2.44.0