Skip to content

Commit

Permalink
Rollup merge of rust-lang#67744 - Centril:reduce-diversity, r=petroch…
Browse files Browse the repository at this point in the history
…enkov

parser: reduce diversity in error handling mechanisms

Instead of having e.g. `span_err`, `fatal`, etc., we prefer to move towards uniformly using `struct_span_err` thus making it harder to emit fatal and/or unstructured diagnostics.

This PR also de-fatalizes some diagnostics.

r? @estebank
  • Loading branch information
Centril authored Dec 31, 2019
2 parents 50fb848 + 2e78061 commit 3cca3c6
Show file tree
Hide file tree
Showing 16 changed files with 147 additions and 142 deletions.
10 changes: 6 additions & 4 deletions src/librustc_expand/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,12 +890,12 @@ fn parse_nt_inner<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> PResult<'a,
Ok(match name {
sym::item => match p.parse_item()? {
Some(i) => token::NtItem(i),
None => return Err(p.fatal("expected an item keyword")),
None => return Err(p.struct_span_err(p.token.span, "expected an item keyword")),
},
sym::block => token::NtBlock(p.parse_block()?),
sym::stmt => match p.parse_stmt()? {
Some(s) => token::NtStmt(s),
None => return Err(p.fatal("expected a statement")),
None => return Err(p.struct_span_err(p.token.span, "expected a statement")),
},
sym::pat => token::NtPat(p.parse_pat(None)?),
sym::expr => token::NtExpr(p.parse_expr()?),
Expand All @@ -909,7 +909,8 @@ fn parse_nt_inner<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> PResult<'a,
token::NtIdent(Ident::new(name, span), is_raw)
} else {
let token_str = pprust::token_to_string(&p.token);
return Err(p.fatal(&format!("expected ident, found {}", &token_str)));
let msg = &format!("expected ident, found {}", &token_str);
return Err(p.struct_span_err(p.token.span, msg));
}
}
sym::path => token::NtPath(p.parse_path(PathStyle::Type)?),
Expand All @@ -920,7 +921,8 @@ fn parse_nt_inner<'a>(p: &mut Parser<'a>, sp: Span, name: Symbol) -> PResult<'a,
token::NtLifetime(p.expect_lifetime().ident)
} else {
let token_str = pprust::token_to_string(&p.token);
return Err(p.fatal(&format!("expected a lifetime, found `{}`", &token_str)));
let msg = &format!("expected a lifetime, found `{}`", &token_str);
return Err(p.struct_span_err(p.token.span, msg));
}
}
// this is not supposed to happen, since it has been checked
Expand Down
13 changes: 7 additions & 6 deletions src/librustc_parse/parser/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ impl<'a> Parser<'a> {
token::DocComment(s) => {
let attr = self.mk_doc_comment(s);
if attr.style != ast::AttrStyle::Outer {
let mut err = self.fatal("expected outer doc comment");
let span = self.token.span;
let mut err = self.struct_span_err(span, "expected outer doc comment");
err.note(
"inner doc comments like this (starting with \
`//!` or `/*!`) can only appear before items",
Expand Down Expand Up @@ -133,7 +134,7 @@ impl<'a> Parser<'a> {
"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 {
diagnostic
Expand All @@ -156,7 +157,8 @@ impl<'a> Parser<'a> {
}
_ => {
let token_str = pprust::token_to_string(&self.token);
return Err(self.fatal(&format!("expected `#`, found `{}`", token_str)));
let msg = &format!("expected `#`, found `{}`", token_str);
return Err(self.struct_span_err(self.token.span, msg));
}
};

Expand Down Expand Up @@ -231,8 +233,7 @@ impl<'a> Parser<'a> {

if !lit.kind.is_unsuffixed() {
let msg = "suffixed literals are not allowed in attributes";
self.diagnostic()
.struct_span_err(lit.span, msg)
self.struct_span_err(lit.span, msg)
.help(
"instead of using a suffixed literal \
(1u8, 1.0f32, etc.), use an unsuffixed version \
Expand Down Expand Up @@ -332,6 +333,6 @@ impl<'a> Parser<'a> {

let found = pprust::token_to_string(&self.token);
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))
}
}
47 changes: 15 additions & 32 deletions src/librustc_parse/parser/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,6 @@ crate enum ConsumeClosingDelim {
}

impl<'a> Parser<'a> {
pub fn fatal(&self, m: &str) -> DiagnosticBuilder<'a> {
self.span_fatal(self.token.span, m)
}

crate fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, m: &str) -> DiagnosticBuilder<'a> {
self.sess.span_diagnostic.struct_span_fatal(sp, m)
}

pub(super) fn span_fatal_err<S: Into<MultiSpan>>(
&self,
sp: S,
Expand All @@ -173,14 +165,6 @@ impl<'a> Parser<'a> {
err.span_err(sp, self.diagnostic())
}

pub(super) fn bug(&self, m: &str) -> ! {
self.sess.span_diagnostic.span_bug(self.token.span, m)
}

pub(super) fn span_err<S: Into<MultiSpan>>(&self, sp: S, m: &str) {
self.sess.span_diagnostic.span_err(sp, m)
}

pub fn struct_span_err<S: Into<MultiSpan>>(&self, sp: S, m: &str) -> DiagnosticBuilder<'a> {
self.sess.span_diagnostic.struct_span_err(sp, m)
}
Expand Down Expand Up @@ -298,7 +282,7 @@ impl<'a> Parser<'a> {
)
};
self.last_unexpected_token_span = Some(self.token.span);
let mut err = self.fatal(&msg_exp);
let mut err = self.struct_span_err(self.token.span, &msg_exp);
let sp = if self.token == token::Eof {
// This is EOF; don't want to point at the following char, but rather the last token.
self.prev_span
Expand Down Expand Up @@ -502,18 +486,17 @@ impl<'a> Parser<'a> {
let span = lo.until(self.token.span);

let total_num_of_gt = number_of_gt + number_of_shr * 2;
self.diagnostic()
.struct_span_err(
span,
&format!("unmatched angle bracket{}", pluralize!(total_num_of_gt)),
)
.span_suggestion(
span,
&format!("remove extra angle bracket{}", pluralize!(total_num_of_gt)),
String::new(),
Applicability::MachineApplicable,
)
.emit();
self.struct_span_err(
span,
&format!("unmatched angle bracket{}", pluralize!(total_num_of_gt)),
)
.span_suggestion(
span,
&format!("remove extra angle bracket{}", pluralize!(total_num_of_gt)),
String::new(),
Applicability::MachineApplicable,
)
.emit();
}
}

Expand Down Expand Up @@ -762,8 +745,7 @@ impl<'a> Parser<'a> {
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));
self.diagnostic()
.struct_span_err(path.span, "missing angle brackets in associated item path")
self.struct_span_err(path.span, "missing angle brackets in associated item path")
.span_suggestion(
// This is a best-effort recovery.
path.span,
Expand Down Expand Up @@ -1271,7 +1253,8 @@ impl<'a> Parser<'a> {

pub(super) fn expected_semi_or_open_brace<T>(&mut self) -> PResult<'a, T> {
let token_str = super::token_descr(&self.token);
let mut err = self.fatal(&format!("expected `;` or `{{`, found {}", token_str));
let msg = &format!("expected `;` or `{{`, found {}", token_str);
let mut err = self.struct_span_err(self.token.span, msg);
err.span_label(self.token.span, "expected `;` or `{`");
Err(err)
}
Expand Down
17 changes: 9 additions & 8 deletions src/librustc_parse/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl<'a> Parser<'a> {
self.mk_expr(span, aopexpr, AttrVec::new())
}
AssocOp::As | AssocOp::Colon | AssocOp::DotDot | AssocOp::DotDotEq => {
self.bug("AssocOp should have been handled by special case")
self.span_bug(span, "AssocOp should have been handled by special case")
}
};

Expand Down Expand Up @@ -822,7 +822,11 @@ impl<'a> Parser<'a> {
} else {
// Field access `expr.f`
if let Some(args) = segment.args {
self.span_err(args.span(), "field expressions may not have generic arguments");
self.struct_span_err(
args.span(),
"field expressions may not have generic arguments",
)
.emit();
}

let span = lo.to(self.prev_span);
Expand Down Expand Up @@ -1133,7 +1137,7 @@ impl<'a> Parser<'a> {
pub(super) fn parse_lit(&mut self) -> PResult<'a, Lit> {
self.parse_opt_lit().ok_or_else(|| {
let msg = format!("unexpected token: {}", super::token_descr(&self.token));
self.span_fatal(self.token.span, &msg)
self.struct_span_err(self.token.span, &msg)
})
}

Expand Down Expand Up @@ -1446,9 +1450,7 @@ impl<'a> Parser<'a> {
self.struct_span_err(sp, "missing condition for `if` expression")
.span_label(sp, "expected if condition here")
.emit();
let expr = self.mk_expr_err(span);
let stmt = self.mk_stmt(span, ast::StmtKind::Expr(expr));
self.mk_block(vec![stmt], BlockCheckMode::Default, span)
self.mk_block_err(span)
}

/// Parses the condition of a `if` or `while` expression.
Expand Down Expand Up @@ -1915,8 +1917,7 @@ impl<'a> Parser<'a> {
return;
}

self.diagnostic()
.struct_span_err(self.token.span, "expected `:`, found `=`")
self.struct_span_err(self.token.span, "expected `:`, found `=`")
.span_suggestion(
field_name.span.shrink_to_hi().to(self.token.span),
"replace equals symbol with a colon",
Expand Down
41 changes: 17 additions & 24 deletions src/librustc_parse/parser/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,8 +306,7 @@ impl<'a> Parser<'a> {
// possible public struct definition where `struct` was forgotten
let ident = self.parse_ident().unwrap();
let msg = format!("add `struct` here to parse `{}` as a public struct", ident);
let mut err =
self.diagnostic().struct_span_err(sp, "missing `struct` for struct definition");
let mut err = self.struct_span_err(sp, "missing `struct` for struct definition");
err.span_suggestion_short(
sp,
&msg,
Expand Down Expand Up @@ -335,7 +334,7 @@ impl<'a> Parser<'a> {
};

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 {
self.consume_block(token::Brace, ConsumeClosingDelim::Yes);
let suggestion =
Expand Down Expand Up @@ -375,7 +374,7 @@ impl<'a> Parser<'a> {
("fn` or `struct", "function or struct", true)
};
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 {
err.span_suggestion_short(
sp,
Expand Down Expand Up @@ -446,9 +445,7 @@ impl<'a> Parser<'a> {
// FAILURE TO PARSE ITEM
match visibility.node {
VisibilityKind::Inherited => {}
_ => {
return Err(self.span_fatal(self.prev_span, "unmatched visibility `pub`"));
}
_ => return Err(self.struct_span_err(self.prev_span, "unmatched visibility `pub`")),
}

if !attributes_allowed && !attrs.is_empty() {
Expand All @@ -466,7 +463,7 @@ impl<'a> Parser<'a> {
_ => "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() {
err.span_label(self.prev_span, "this doc comment doesn't document anything");
}
Expand Down Expand Up @@ -536,7 +533,6 @@ impl<'a> Parser<'a> {
// ^^ `sp` below will point to this
let sp = prev_span.between(self.prev_span);
let mut err = self
.diagnostic()
.struct_span_err(sp, &format!("{} for {}-item declaration", expected_kinds, item_type));
err.span_label(sp, expected_kinds);
err
Expand Down Expand Up @@ -619,7 +615,7 @@ impl<'a> Parser<'a> {
// This notably includes paths passed through `ty` macro fragments (#46438).
TyKind::Path(None, path) => path,
_ => {
self.span_err(ty_first.span, "expected a trait, found type");
self.struct_span_err(ty_first.span, "expected a trait, found type").emit();
err_path(ty_first.span)
}
};
Expand Down Expand Up @@ -1349,10 +1345,11 @@ impl<'a> Parser<'a> {
body
} else {
let token_str = super::token_descr(&self.token);
let mut err = self.fatal(&format!(
let msg = &format!(
"expected `where`, `{{`, `(`, or `;` after struct name, found {}",
token_str
));
);
let mut err = self.struct_span_err(self.token.span, msg);
err.span_label(self.token.span, "expected `where`, `{`, `(`, or `;` after struct name");
return Err(err);
};
Expand All @@ -1375,8 +1372,8 @@ impl<'a> Parser<'a> {
VariantData::Struct(fields, recovered)
} else {
let token_str = super::token_descr(&self.token);
let mut err = self
.fatal(&format!("expected `where` or `{{` after union name, found {}", token_str));
let msg = &format!("expected `where` or `{{` after union name, found {}", token_str);
let mut err = self.struct_span_err(self.token.span, msg);
err.span_label(self.token.span, "expected `where` or `{` after union name");
return Err(err);
};
Expand Down Expand Up @@ -1412,10 +1409,8 @@ impl<'a> Parser<'a> {
self.eat(&token::CloseDelim(token::Brace));
} else {
let token_str = super::token_descr(&self.token);
let mut err = self.fatal(&format!(
"expected `where`, or `{{` after struct name, found {}",
token_str
));
let msg = &format!("expected `where`, or `{{` after struct name, found {}", token_str);
let mut err = self.struct_span_err(self.token.span, msg);
err.span_label(self.token.span, "expected `where`, or `{` after struct name");
return Err(err);
}
Expand Down Expand Up @@ -1603,9 +1598,8 @@ impl<'a> Parser<'a> {
VisibilityKind::Inherited => {}
_ => {
let mut err = if self.token.is_keyword(sym::macro_rules) {
let mut err = self
.diagnostic()
.struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
let mut err =
self.struct_span_err(sp, "can't qualify macro_rules invocation with `pub`");
err.span_suggestion(
sp,
"try exporting the macro",
Expand All @@ -1614,9 +1608,8 @@ impl<'a> Parser<'a> {
);
err
} else {
let mut err = self
.diagnostic()
.struct_span_err(sp, "can't qualify macro invocation with `pub`");
let mut err =
self.struct_span_err(sp, "can't qualify macro invocation with `pub`");
err.help("try adjusting the macro to put `pub` inside the invocation");
err
};
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_parse/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -884,7 +884,8 @@ impl<'a> Parser<'a> {
pub fn bump(&mut self) {
if self.prev_token_kind == PrevTokenKind::Eof {
// Bumping after EOF is a bad sign, usually an infinite loop.
self.bug("attempted to bump the parser past EOF (may be stuck in a loop)");
let msg = "attempted to bump the parser past EOF (may be stuck in a loop)";
self.span_bug(self.token.span, msg);
}

self.prev_span = self.meta_var_span.take().unwrap_or(self.token.span);
Expand Down Expand Up @@ -1056,8 +1057,7 @@ impl<'a> Parser<'a> {
_ => unreachable!(),
};
let span = self.prev_span.to(self.token.span);
self.diagnostic()
.struct_span_fatal(span, &format!("unknown macro variable `{}`", name))
self.struct_span_err(span, &format!("unknown macro variable `{}`", name))
.span_label(span, "unknown macro variable")
.emit();
self.bump();
Expand Down
Loading

0 comments on commit 3cca3c6

Please sign in to comment.