1
Fork 0

Don't unnecessarily stringify paths in diagnostics

This commit is contained in:
Xiretza 2022-09-14 20:11:42 +02:00
parent caefac034e
commit 37fdcb4b36
3 changed files with 17 additions and 16 deletions

View file

@ -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

View file

@ -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]

View file

@ -756,11 +756,12 @@ impl<'a> Parser<'a> {
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 @@ impl<'a> Parser<'a> {
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 @@ impl<'a> Parser<'a> {
) -> Option<P<Expr>> {
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 @@ impl<'a> Parser<'a> {
if !fields.is_empty() {
let mut replacement_err = ParenthesesWithStructFields {
span,
name,
r#type: path,
braces_for_struct: BracesForStructLiteral {
first: open_paren,
second: close_paren,