Skip to content

Commit 37fdcb4

Browse files
committed
Don't unnecessarily stringify paths in diagnostics
1 parent caefac0 commit 37fdcb4

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

Diff for: compiler/rustc_error_messages/locales/en-US/parser.ftl

+4-4
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,13 @@ parser_use_empty_block_not_semi = expected { "`{}`" }, found `;`
165165
.suggestion = try using { "`{}`" } instead
166166
167167
parser_comparison_interpreted_as_generic =
168-
`<` is interpreted as a start of generic arguments for `{$typename}`, not a comparison
168+
`<` is interpreted as a start of generic arguments for `{$type}`, not a comparison
169169
.label_args = interpreted as generic arguments
170170
.label_comparison = not interpreted as comparison
171171
.suggestion = try comparing the cast value
172172
173173
parser_shift_interpreted_as_generic =
174-
`<<` is interpreted as a start of generic arguments for `{$typename}`, not a shift
174+
`<<` is interpreted as a start of generic arguments for `{$type}`, not a shift
175175
.label_args = interpreted as generic arguments
176176
.label_comparison = not interpreted as shift
177177
.suggestion = try shifting the cast value
@@ -184,8 +184,8 @@ parser_leading_plus_not_supported = leading `+` is not supported
184184
.suggestion_remove_plus = try removing the `+`
185185
186186
parser_parentheses_with_struct_fields = invalid `struct` delimiters or `fn` call arguments
187-
.suggestion_braces_for_struct = if `{$name}` is a struct, use braces as delimiters
188-
.suggestion_no_fields_for_fn = if `{$name}` is a function, use the arguments directly
187+
.suggestion_braces_for_struct = if `{$type}` is a struct, use braces as delimiters
188+
.suggestion_no_fields_for_fn = if `{$type}` is a function, use the arguments directly
189189
190190
parser_labeled_loop_in_break = parentheses are required around this expression to avoid confusion with a labeled break expression
191191

Diff for: compiler/rustc_parse/src/errors.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use rustc_ast::Path;
12
use rustc_errors::{fluent, AddToDiagnostic, Applicability, EmissionGuarantee, IntoDiagnostic};
23
use rustc_macros::{Diagnostic, Subdiagnostic};
34
use rustc_session::errors::ExprParenthesesNeeded;
@@ -536,7 +537,7 @@ pub(crate) struct ComparisonInterpretedAsGeneric {
536537
#[primary_span]
537538
#[label(parser::label_comparison)]
538539
pub comparison: Span,
539-
pub typename: String,
540+
pub r#type: Path,
540541
#[label(parser::label_args)]
541542
pub args: Span,
542543
#[subdiagnostic]
@@ -549,7 +550,7 @@ pub(crate) struct ShiftInterpretedAsGeneric {
549550
#[primary_span]
550551
#[label(parser::label_comparison)]
551552
pub shift: Span,
552-
pub typename: String,
553+
pub r#type: Path,
553554
#[label(parser::label_args)]
554555
pub args: Span,
555556
#[subdiagnostic]
@@ -597,7 +598,7 @@ pub(crate) struct LeadingPlusNotSupported {
597598
pub(crate) struct ParenthesesWithStructFields {
598599
#[primary_span]
599600
pub span: Span,
600-
pub name: String,
601+
pub r#type: Path,
601602
#[subdiagnostic]
602603
pub braces_for_struct: BracesForStructLiteral,
603604
#[subdiagnostic]

Diff for: compiler/rustc_parse/src/parser/expr.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -756,11 +756,12 @@ impl<'a> Parser<'a> {
756756

757757
match self.parse_path(PathStyle::Expr) {
758758
Ok(path) => {
759-
let typename = pprust::path_to_string(&path);
760-
761759
let span_after_type = parser_snapshot_after_type.token.span;
762-
let expr =
763-
mk_expr(self, lhs, self.mk_ty(path.span, TyKind::Path(None, path)));
760+
let expr = mk_expr(
761+
self,
762+
lhs,
763+
self.mk_ty(path.span, TyKind::Path(None, path.clone())),
764+
);
764765

765766
let args_span = self.look_ahead(1, |t| t.span).to(span_after_type);
766767
let suggestion = ComparisonOrShiftInterpretedAsGenericSugg {
@@ -771,14 +772,14 @@ impl<'a> Parser<'a> {
771772
match self.token.kind {
772773
token::Lt => self.sess.emit_err(ComparisonInterpretedAsGeneric {
773774
comparison: self.token.span,
774-
typename,
775+
r#type: path,
775776
args: args_span,
776777
suggestion,
777778
}),
778779
token::BinOp(token::Shl) => {
779780
self.sess.emit_err(ShiftInterpretedAsGeneric {
780781
shift: self.token.span,
781-
typename,
782+
r#type: path,
782783
args: args_span,
783784
suggestion,
784785
})
@@ -1197,9 +1198,8 @@ impl<'a> Parser<'a> {
11971198
) -> Option<P<Expr>> {
11981199
match (seq.as_mut(), snapshot) {
11991200
(Err(err), Some((mut snapshot, ExprKind::Path(None, path)))) => {
1200-
let name = pprust::path_to_string(&path);
12011201
snapshot.bump(); // `(`
1202-
match snapshot.parse_struct_fields(path, false, Delimiter::Parenthesis) {
1202+
match snapshot.parse_struct_fields(path.clone(), false, Delimiter::Parenthesis) {
12031203
Ok((fields, ..))
12041204
if snapshot.eat(&token::CloseDelim(Delimiter::Parenthesis)) =>
12051205
{
@@ -1211,7 +1211,7 @@ impl<'a> Parser<'a> {
12111211
if !fields.is_empty() {
12121212
let mut replacement_err = ParenthesesWithStructFields {
12131213
span,
1214-
name,
1214+
r#type: path,
12151215
braces_for_struct: BracesForStructLiteral {
12161216
first: open_paren,
12171217
second: close_paren,

0 commit comments

Comments
 (0)