1
Fork 0

parser: call .struct_span_err directly

This commit is contained in:
Mazdak Farrokhzad 2019-12-30 14:56:57 +01:00
parent a9dd56ff9a
commit 2091062bf6
7 changed files with 56 additions and 68 deletions

View file

@ -133,7 +133,7 @@ impl<'a> Parser<'a> {
"previous outer attribute" "previous outer attribute"
}; };
let mut diagnostic = self.diagnostic().struct_span_err(attr_sp, reason); let mut diagnostic = self.struct_span_err(attr_sp, reason);
if let Some(prev_attr_sp) = prev_attr_sp { if let Some(prev_attr_sp) = prev_attr_sp {
diagnostic diagnostic
@ -231,8 +231,7 @@ impl<'a> Parser<'a> {
if !lit.kind.is_unsuffixed() { if !lit.kind.is_unsuffixed() {
let msg = "suffixed literals are not allowed in attributes"; let msg = "suffixed literals are not allowed in attributes";
self.diagnostic() self.struct_span_err(lit.span, msg)
.struct_span_err(lit.span, msg)
.help( .help(
"instead of using a suffixed literal \ "instead of using a suffixed literal \
(1u8, 1.0f32, etc.), use an unsuffixed version \ (1u8, 1.0f32, etc.), use an unsuffixed version \
@ -332,6 +331,6 @@ impl<'a> Parser<'a> {
let found = pprust::token_to_string(&self.token); let found = pprust::token_to_string(&self.token);
let msg = format!("expected unsuffixed literal or identifier, found `{}`", found); let msg = format!("expected unsuffixed literal or identifier, found `{}`", found);
Err(self.diagnostic().struct_span_err(self.token.span, &msg)) Err(self.struct_span_err(self.token.span, &msg))
} }
} }

View file

@ -502,18 +502,17 @@ impl<'a> Parser<'a> {
let span = lo.until(self.token.span); let span = lo.until(self.token.span);
let total_num_of_gt = number_of_gt + number_of_shr * 2; let total_num_of_gt = number_of_gt + number_of_shr * 2;
self.diagnostic() self.struct_span_err(
.struct_span_err( span,
span, &format!("unmatched angle bracket{}", pluralize!(total_num_of_gt)),
&format!("unmatched angle bracket{}", pluralize!(total_num_of_gt)), )
) .span_suggestion(
.span_suggestion( span,
span, &format!("remove extra angle bracket{}", pluralize!(total_num_of_gt)),
&format!("remove extra angle bracket{}", pluralize!(total_num_of_gt)), String::new(),
String::new(), Applicability::MachineApplicable,
Applicability::MachineApplicable, )
) .emit();
.emit();
} }
} }
@ -762,8 +761,7 @@ impl<'a> Parser<'a> {
path.span = ty_span.to(self.prev_span); path.span = ty_span.to(self.prev_span);
let ty_str = self.span_to_snippet(ty_span).unwrap_or_else(|_| pprust::ty_to_string(&ty)); let ty_str = self.span_to_snippet(ty_span).unwrap_or_else(|_| pprust::ty_to_string(&ty));
self.diagnostic() self.struct_span_err(path.span, "missing angle brackets in associated item path")
.struct_span_err(path.span, "missing angle brackets in associated item path")
.span_suggestion( .span_suggestion(
// This is a best-effort recovery. // This is a best-effort recovery.
path.span, path.span,

View file

@ -1915,8 +1915,7 @@ impl<'a> Parser<'a> {
return; return;
} }
self.diagnostic() self.struct_span_err(self.token.span, "expected `:`, found `=`")
.struct_span_err(self.token.span, "expected `:`, found `=`")
.span_suggestion( .span_suggestion(
field_name.span.shrink_to_hi().to(self.token.span), field_name.span.shrink_to_hi().to(self.token.span),
"replace equals symbol with a colon", "replace equals symbol with a colon",

View file

@ -306,8 +306,7 @@ impl<'a> Parser<'a> {
// possible public struct definition where `struct` was forgotten // possible public struct definition where `struct` was forgotten
let ident = self.parse_ident().unwrap(); let ident = self.parse_ident().unwrap();
let msg = format!("add `struct` here to parse `{}` as a public struct", ident); let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
let mut err = let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
self.diagnostic().struct_span_err(sp, "missing `struct` for struct definition");
err.span_suggestion_short( err.span_suggestion_short(
sp, sp,
&msg, &msg,
@ -335,7 +334,7 @@ impl<'a> Parser<'a> {
}; };
let msg = format!("missing `{}` for {} definition", kw, kw_name); let msg = format!("missing `{}` for {} definition", kw, kw_name);
let mut err = self.diagnostic().struct_span_err(sp, &msg); let mut err = self.struct_span_err(sp, &msg);
if !ambiguous { if !ambiguous {
self.consume_block(token::Brace, ConsumeClosingDelim::Yes); self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
let suggestion = let suggestion =
@ -375,7 +374,7 @@ impl<'a> Parser<'a> {
("fn` or `struct", "function or struct", true) ("fn` or `struct", "function or struct", true)
}; };
let msg = format!("missing `{}` for {} definition", kw, kw_name); let msg = format!("missing `{}` for {} definition", kw, kw_name);
let mut err = self.diagnostic().struct_span_err(sp, &msg); let mut err = self.struct_span_err(sp, &msg);
if !ambiguous { if !ambiguous {
err.span_suggestion_short( err.span_suggestion_short(
sp, sp,
@ -466,7 +465,7 @@ impl<'a> Parser<'a> {
_ => "expected item after attributes", _ => "expected item after attributes",
}; };
let mut err = self.diagnostic().struct_span_err(self.prev_span, message); let mut err = self.struct_span_err(self.prev_span, message);
if attrs.last().unwrap().is_doc_comment() { if attrs.last().unwrap().is_doc_comment() {
err.span_label(self.prev_span, "this doc comment doesn't document anything"); err.span_label(self.prev_span, "this doc comment doesn't document anything");
} }
@ -536,7 +535,6 @@ impl<'a> Parser<'a> {
// ^^ `sp` below will point to this // ^^ `sp` below will point to this
let sp = prev_span.between(self.prev_span); let sp = prev_span.between(self.prev_span);
let mut err = self let mut err = self
.diagnostic()
.struct_span_err(sp, &format!("{} for {}-item declaration", expected_kinds, item_type)); .struct_span_err(sp, &format!("{} for {}-item declaration", expected_kinds, item_type));
err.span_label(sp, expected_kinds); err.span_label(sp, expected_kinds);
err err
@ -1603,9 +1601,8 @@ impl<'a> Parser<'a> {
VisibilityKind::Inherited => {} VisibilityKind::Inherited => {}
_ => { _ => {
let mut err = if self.token.is_keyword(sym::macro_rules) { let mut err = if self.token.is_keyword(sym::macro_rules) {
let mut err = self let mut err =
.diagnostic() self.struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
.struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
err.span_suggestion( err.span_suggestion(
sp, sp,
"try exporting the macro", "try exporting the macro",
@ -1614,9 +1611,8 @@ impl<'a> Parser<'a> {
); );
err err
} else { } else {
let mut err = self let mut err =
.diagnostic() self.struct_span_err(sp, "can't qualify macro invocation with `pub`");
.struct_span_err(sp, "can't qualify macro invocation with `pub`");
err.help("try adjusting the macro to put `pub` inside the invocation"); err.help("try adjusting the macro to put `pub` inside the invocation");
err err
}; };

View file

@ -129,7 +129,7 @@ impl<'a> Parser<'a> {
DirectoryOwnership::UnownedViaBlock => { DirectoryOwnership::UnownedViaBlock => {
let msg = "Cannot declare a non-inline module inside a block \ let msg = "Cannot declare a non-inline module inside a block \
unless it has a path attribute"; unless it has a path attribute";
let mut err = self.diagnostic().struct_span_err(id_sp, msg); let mut err = self.struct_span_err(id_sp, msg);
if paths.path_exists { if paths.path_exists {
let msg = format!( let msg = format!(
"Maybe `use` the module `{}` instead of redeclaring it", "Maybe `use` the module `{}` instead of redeclaring it",
@ -140,9 +140,8 @@ impl<'a> Parser<'a> {
Err(err) Err(err)
} }
DirectoryOwnership::UnownedViaMod => { DirectoryOwnership::UnownedViaMod => {
let mut err = self let mut err =
.diagnostic() self.struct_span_err(id_sp, "cannot declare a new module at this location");
.struct_span_err(id_sp, "cannot declare a new module at this location");
if !id_sp.is_dummy() { if !id_sp.is_dummy() {
let src_path = self.sess.source_map().span_to_filename(id_sp); let src_path = self.sess.source_map().span_to_filename(id_sp);
if let FileName::Real(src_path) = src_path { if let FileName::Real(src_path) = src_path {

View file

@ -699,8 +699,7 @@ impl<'a> Parser<'a> {
let range_span = lo.to(end.span); let range_span = lo.to(end.span);
let begin = self.mk_expr(range_span, ExprKind::Err, AttrVec::new()); let begin = self.mk_expr(range_span, ExprKind::Err, AttrVec::new());
self.diagnostic() self.struct_span_err(range_span, &format!("`{}X` range patterns are not supported", form))
.struct_span_err(range_span, &format!("`{}X` range patterns are not supported", form))
.span_suggestion( .span_suggestion(
range_span, range_span,
"try using the minimum value for the type", "try using the minimum value for the type",
@ -722,18 +721,17 @@ impl<'a> Parser<'a> {
// Parsing e.g. `X..`. // Parsing e.g. `X..`.
let range_span = begin.span.to(self.prev_span); let range_span = begin.span.to(self.prev_span);
self.diagnostic() self.struct_span_err(
.struct_span_err( range_span,
range_span, &format!("`X{}` range patterns are not supported", form),
&format!("`X{}` range patterns are not supported", form), )
) .span_suggestion(
.span_suggestion( range_span,
range_span, "try using the maximum value for the type",
"try using the maximum value for the type", format!("{}{}MAX", pprust::expr_to_string(&begin), form),
format!("{}{}MAX", pprust::expr_to_string(&begin), form), Applicability::HasPlaceholders,
Applicability::HasPlaceholders, )
) .emit();
.emit();
Ok(self.mk_expr(range_span, ExprKind::Err, AttrVec::new())) Ok(self.mk_expr(range_span, ExprKind::Err, AttrVec::new()))
} }

View file

@ -325,24 +325,23 @@ impl<'a> Parser<'a> {
// Make a span over ${unmatched angle bracket count} characters. // Make a span over ${unmatched angle bracket count} characters.
let span = lo.with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count)); let span = lo.with_hi(lo.lo() + BytePos(snapshot.unmatched_angle_bracket_count));
self.diagnostic() self.struct_span_err(
.struct_span_err( span,
span, &format!(
&format!( "unmatched angle bracket{}",
"unmatched angle bracket{}", pluralize!(snapshot.unmatched_angle_bracket_count)
pluralize!(snapshot.unmatched_angle_bracket_count) ),
), )
) .span_suggestion(
.span_suggestion( span,
span, &format!(
&format!( "remove extra angle bracket{}",
"remove extra angle bracket{}", pluralize!(snapshot.unmatched_angle_bracket_count)
pluralize!(snapshot.unmatched_angle_bracket_count) ),
), String::new(),
String::new(), Applicability::MachineApplicable,
Applicability::MachineApplicable, )
) .emit();
.emit();
// Try again without unmatched angle bracket characters. // Try again without unmatched angle bracket characters.
self.parse_generic_args() self.parse_generic_args()