Use Session::diagnostic in more places.

This commit is contained in:
Nicholas Nethercote 2023-12-01 13:45:29 +11:00
parent a179a53565
commit 31ac4efb31
7 changed files with 33 additions and 40 deletions

View file

@ -56,7 +56,7 @@ impl<'a> Parser<'a> {
} else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind { } else if let token::DocComment(comment_kind, attr_style, data) = self.token.kind {
if attr_style != ast::AttrStyle::Outer { if attr_style != ast::AttrStyle::Outer {
let span = self.token.span; let span = self.token.span;
let mut err = self.sess.span_diagnostic.struct_span_err_with_code( let mut err = self.diagnostic().struct_span_err_with_code(
span, span,
fluent::parse_inner_doc_comment_not_permitted, fluent::parse_inner_doc_comment_not_permitted,
error_code!(E0753), error_code!(E0753),
@ -418,7 +418,7 @@ impl<'a> Parser<'a> {
} }
Err(InvalidMetaItem { span: self.token.span, token: self.token.clone() } Err(InvalidMetaItem { span: self.token.span, token: self.token.clone() }
.into_diagnostic(&self.sess.span_diagnostic)) .into_diagnostic(self.diagnostic()))
} }
} }

View file

@ -266,8 +266,7 @@ impl<'a> Parser<'a> {
if let Some(attr_range) = self.capture_state.inner_attr_ranges.remove(&inner_attr.id) { if let Some(attr_range) = self.capture_state.inner_attr_ranges.remove(&inner_attr.id) {
inner_attr_replace_ranges.push(attr_range); inner_attr_replace_ranges.push(attr_range);
} else { } else {
self.sess self.diagnostic()
.span_diagnostic
.span_delayed_bug(inner_attr.span, "Missing token range for attribute"); .span_delayed_bug(inner_attr.span, "Missing token range for attribute");
} }
} }

View file

@ -247,11 +247,11 @@ impl<'a> Parser<'a> {
sp: S, sp: S,
m: impl Into<DiagnosticMessage>, m: impl Into<DiagnosticMessage>,
) -> DiagnosticBuilder<'a, ErrorGuaranteed> { ) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
self.sess.span_diagnostic.struct_span_err(sp, m) self.diagnostic().struct_span_err(sp, m)
} }
pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, m: impl Into<String>) -> ! { pub fn span_bug<S: Into<MultiSpan>>(&self, sp: S, m: impl Into<String>) -> ! {
self.sess.span_diagnostic.span_bug(sp, m) self.diagnostic().span_bug(sp, m)
} }
pub(super) fn diagnostic(&self) -> &'a Handler { pub(super) fn diagnostic(&self) -> &'a Handler {
@ -285,7 +285,7 @@ impl<'a> Parser<'a> {
span: self.prev_token.span, span: self.prev_token.span,
missing_comma: None, missing_comma: None,
} }
.into_diagnostic(&self.sess.span_diagnostic)); .into_diagnostic(self.diagnostic()));
} }
let valid_follow = &[ let valid_follow = &[
@ -348,7 +348,7 @@ impl<'a> Parser<'a> {
suggest_remove_comma, suggest_remove_comma,
help_cannot_start_number, help_cannot_start_number,
}; };
let mut err = err.into_diagnostic(&self.sess.span_diagnostic); let mut err = err.into_diagnostic(self.diagnostic());
// if the token we have is a `<` // if the token we have is a `<`
// it *might* be a misplaced generic // it *might* be a misplaced generic
@ -1426,7 +1426,7 @@ impl<'a> Parser<'a> {
// Not entirely sure now, but we bubble the error up with the // Not entirely sure now, but we bubble the error up with the
// suggestion. // suggestion.
self.restore_snapshot(snapshot); self.restore_snapshot(snapshot);
Err(err.into_diagnostic(&self.sess.span_diagnostic)) Err(err.into_diagnostic(self.diagnostic()))
} }
} }
} else if token::OpenDelim(Delimiter::Parenthesis) == self.token.kind { } else if token::OpenDelim(Delimiter::Parenthesis) == self.token.kind {
@ -1441,7 +1441,7 @@ impl<'a> Parser<'a> {
} }
// Consume the fn call arguments. // Consume the fn call arguments.
match self.consume_fn_args() { match self.consume_fn_args() {
Err(()) => Err(err.into_diagnostic(&self.sess.span_diagnostic)), Err(()) => Err(err.into_diagnostic(self.diagnostic())),
Ok(()) => { Ok(()) => {
self.sess.emit_err(err); self.sess.emit_err(err);
// FIXME: actually check that the two expressions in the binop are // FIXME: actually check that the two expressions in the binop are
@ -1467,7 +1467,7 @@ impl<'a> Parser<'a> {
mk_err_expr(self, inner_op.span.to(self.prev_token.span)) mk_err_expr(self, inner_op.span.to(self.prev_token.span))
} else { } else {
// These cases cause too many knock-down errors, bail out (#61329). // These cases cause too many knock-down errors, bail out (#61329).
Err(err.into_diagnostic(&self.sess.span_diagnostic)) Err(err.into_diagnostic(self.diagnostic()))
} }
}; };
} }
@ -2522,8 +2522,7 @@ impl<'a> Parser<'a> {
Ok(Some(GenericArg::Const(self.parse_const_arg()?))) Ok(Some(GenericArg::Const(self.parse_const_arg()?)))
} else { } else {
let after_kw_const = self.token.span; let after_kw_const = self.token.span;
self.recover_const_arg(after_kw_const, err.into_diagnostic(&self.sess.span_diagnostic)) self.recover_const_arg(after_kw_const, err.into_diagnostic(self.diagnostic())).map(Some)
.map(Some)
} }
} }
@ -2886,7 +2885,7 @@ impl<'a> Parser<'a> {
span: path.span.shrink_to_hi(), span: path.span.shrink_to_hi(),
between: between_span, between: between_span,
} }
.into_diagnostic(&self.sess.span_diagnostic)); .into_diagnostic(self.diagnostic()));
} }
} }
} }

View file

@ -1269,7 +1269,7 @@ impl<'a> Parser<'a> {
.collect(), .collect(),
}, },
} }
.into_diagnostic(&self.sess.span_diagnostic); .into_diagnostic(self.diagnostic());
replacement_err.emit(); replacement_err.emit();
let old_err = mem::replace(err, replacement_err); let old_err = mem::replace(err, replacement_err);
@ -1691,7 +1691,7 @@ impl<'a> Parser<'a> {
err: impl FnOnce(&Self) -> DiagnosticBuilder<'a, ErrorGuaranteed>, err: impl FnOnce(&Self) -> DiagnosticBuilder<'a, ErrorGuaranteed>,
) -> L { ) -> L {
if let Some(mut diag) = if let Some(mut diag) =
self.sess.span_diagnostic.steal_diagnostic(lifetime.span, StashKey::LifetimeIsChar) self.diagnostic().steal_diagnostic(lifetime.span, StashKey::LifetimeIsChar)
{ {
diag.span_suggestion_verbose( diag.span_suggestion_verbose(
lifetime.span.shrink_to_hi(), lifetime.span.shrink_to_hi(),
@ -1882,7 +1882,7 @@ impl<'a> Parser<'a> {
let Some((ident, false)) = self.token.ident() else { let Some((ident, false)) = self.token.ident() else {
let err = errors::ExpectedBuiltinIdent { span: self.token.span } let err = errors::ExpectedBuiltinIdent { span: self.token.span }
.into_diagnostic(&self.sess.span_diagnostic); .into_diagnostic(self.diagnostic());
return Err(err); return Err(err);
}; };
self.sess.gated_spans.gate(sym::builtin_syntax, ident.span); self.sess.gated_spans.gate(sym::builtin_syntax, ident.span);
@ -1893,7 +1893,7 @@ impl<'a> Parser<'a> {
Ok(res) Ok(res)
} else { } else {
let err = errors::UnknownBuiltinConstruct { span: lo.to(ident.span), name: ident.name } let err = errors::UnknownBuiltinConstruct { span: lo.to(ident.span), name: ident.name }
.into_diagnostic(&self.sess.span_diagnostic); .into_diagnostic(self.diagnostic());
return Err(err); return Err(err);
}; };
self.expect(&TokenKind::CloseDelim(Delimiter::Parenthesis))?; self.expect(&TokenKind::CloseDelim(Delimiter::Parenthesis))?;
@ -1957,7 +1957,7 @@ impl<'a> Parser<'a> {
&& matches!(e.kind, ExprKind::Err) && matches!(e.kind, ExprKind::Err)
{ {
let mut err = errors::InvalidInterpolatedExpression { span: self.token.span } let mut err = errors::InvalidInterpolatedExpression { span: self.token.span }
.into_diagnostic(&self.sess.span_diagnostic); .into_diagnostic(self.diagnostic());
err.downgrade_to_delayed_bug(); err.downgrade_to_delayed_bug();
return Err(err); return Err(err);
} }
@ -2169,7 +2169,7 @@ impl<'a> Parser<'a> {
return Err(errors::MissingSemicolonBeforeArray { return Err(errors::MissingSemicolonBeforeArray {
open_delim: open_delim_span, open_delim: open_delim_span,
semicolon: prev_span.shrink_to_hi(), semicolon: prev_span.shrink_to_hi(),
}.into_diagnostic(&self.sess.span_diagnostic)); }.into_diagnostic(self.diagnostic()));
} }
Ok(_) => (), Ok(_) => (),
Err(err) => err.cancel(), Err(err) => err.cancel(),
@ -2309,7 +2309,7 @@ impl<'a> Parser<'a> {
if self.check_keyword(kw::Async) { if self.check_keyword(kw::Async) {
let move_async_span = self.token.span.with_lo(self.prev_token.span.data().lo); let move_async_span = self.token.span.with_lo(self.prev_token.span.data().lo);
Err(errors::AsyncMoveOrderIncorrect { span: move_async_span } Err(errors::AsyncMoveOrderIncorrect { span: move_async_span }
.into_diagnostic(&self.sess.span_diagnostic)) .into_diagnostic(self.diagnostic()))
} else { } else {
Ok(CaptureBy::Value { move_kw: move_kw_span }) Ok(CaptureBy::Value { move_kw: move_kw_span })
} }
@ -2499,7 +2499,7 @@ impl<'a> Parser<'a> {
}; };
if self.prev_token.kind == token::BinOp(token::Or) { if self.prev_token.kind == token::BinOp(token::Or) {
// This was part of a closure, the that part of the parser recover. // This was part of a closure, the that part of the parser recover.
return Err(err.into_diagnostic(&self.sess.span_diagnostic)); return Err(err.into_diagnostic(self.diagnostic()));
} else { } else {
Some(self.sess.emit_err(err)) Some(self.sess.emit_err(err))
} }
@ -3148,7 +3148,7 @@ impl<'a> Parser<'a> {
let (attrs, body) = self.parse_inner_attrs_and_block()?; let (attrs, body) = self.parse_inner_attrs_and_block()?;
if self.eat_keyword(kw::Catch) { if self.eat_keyword(kw::Catch) {
Err(errors::CatchAfterTry { span: self.prev_token.span } Err(errors::CatchAfterTry { span: self.prev_token.span }
.into_diagnostic(&self.sess.span_diagnostic)) .into_diagnostic(self.diagnostic()))
} else { } else {
let span = span_lo.to(body.span); let span = span_lo.to(body.span);
self.sess.gated_spans.gate(sym::try_blocks, span); self.sess.gated_spans.gate(sym::try_blocks, span);

View file

@ -444,11 +444,7 @@ impl<'a> Parser<'a> {
None None
}; };
if let Some(err) = err { if let Some(err) = err { Err(err.into_diagnostic(self.diagnostic())) } else { Ok(()) }
Err(err.into_diagnostic(&self.sess.span_diagnostic))
} else {
Ok(())
}
} }
fn parse_item_builtin(&mut self) -> PResult<'a, Option<ItemInfo>> { fn parse_item_builtin(&mut self) -> PResult<'a, Option<ItemInfo>> {
@ -1382,8 +1378,7 @@ impl<'a> Parser<'a> {
let span = self.prev_token.span.shrink_to_hi(); let span = self.prev_token.span.shrink_to_hi();
let err: DiagnosticBuilder<'_, ErrorGuaranteed> = let err: DiagnosticBuilder<'_, ErrorGuaranteed> =
errors::MissingConstType { span, colon, kind } errors::MissingConstType { span, colon, kind }.into_diagnostic(self.diagnostic());
.into_diagnostic(&self.sess.span_diagnostic);
err.stash(span, StashKey::ItemNoType); err.stash(span, StashKey::ItemNoType);
// The user intended that the type be inferred, // The user intended that the type be inferred,
@ -1400,7 +1395,7 @@ impl<'a> Parser<'a> {
self.bump(); self.bump();
self.sess.emit_err(err); self.sess.emit_err(err);
} else { } else {
return Err(err.into_diagnostic(&self.sess.span_diagnostic)); return Err(err.into_diagnostic(self.diagnostic()));
} }
} }
@ -1600,7 +1595,7 @@ impl<'a> Parser<'a> {
} else { } else {
let err = let err =
errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token.clone()); errors::UnexpectedTokenAfterStructName::new(self.token.span, self.token.clone());
return Err(err.into_diagnostic(&self.sess.span_diagnostic)); return Err(err.into_diagnostic(self.diagnostic()));
}; };
Ok((class_name, ItemKind::Struct(vdata, generics))) Ok((class_name, ItemKind::Struct(vdata, generics)))
@ -1796,7 +1791,7 @@ impl<'a> Parser<'a> {
let sp = previous_span.shrink_to_hi(); let sp = previous_span.shrink_to_hi();
err.missing_comma = Some(sp); err.missing_comma = Some(sp);
} }
return Err(err.into_diagnostic(&self.sess.span_diagnostic)); return Err(err.into_diagnostic(self.diagnostic()));
} }
} }
_ => { _ => {
@ -1846,7 +1841,7 @@ impl<'a> Parser<'a> {
// Make sure an error was emitted (either by recovering an angle bracket, // Make sure an error was emitted (either by recovering an angle bracket,
// or by finding an identifier as the next token), since we're // or by finding an identifier as the next token), since we're
// going to continue parsing // going to continue parsing
assert!(self.sess.span_diagnostic.has_errors().is_some()); assert!(self.diagnostic().has_errors().is_some());
} else { } else {
return Err(err); return Err(err);
} }

View file

@ -115,7 +115,7 @@ impl<'a> Parser<'a> {
Some(item) => NtItem(item), Some(item) => NtItem(item),
None => { None => {
return Err(UnexpectedNonterminal::Item(self.token.span) return Err(UnexpectedNonterminal::Item(self.token.span)
.into_diagnostic(&self.sess.span_diagnostic)); .into_diagnostic(self.diagnostic()));
} }
}, },
NonterminalKind::Block => { NonterminalKind::Block => {
@ -127,7 +127,7 @@ impl<'a> Parser<'a> {
Some(s) => NtStmt(P(s)), Some(s) => NtStmt(P(s)),
None => { None => {
return Err(UnexpectedNonterminal::Statement(self.token.span) return Err(UnexpectedNonterminal::Statement(self.token.span)
.into_diagnostic(&self.sess.span_diagnostic)); .into_diagnostic(self.diagnostic()));
} }
}, },
NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr => { NonterminalKind::PatParam { .. } | NonterminalKind::PatWithOr => {
@ -163,7 +163,7 @@ impl<'a> Parser<'a> {
span: self.token.span, span: self.token.span,
token: self.token.clone(), token: self.token.clone(),
} }
.into_diagnostic(&self.sess.span_diagnostic)); .into_diagnostic(self.diagnostic()));
} }
NonterminalKind::Path => { NonterminalKind::Path => {
NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?)) NtPath(P(self.collect_tokens_no_attrs(|this| this.parse_path(PathStyle::Type))?))
@ -181,7 +181,7 @@ impl<'a> Parser<'a> {
span: self.token.span, span: self.token.span,
token: self.token.clone(), token: self.token.clone(),
} }
.into_diagnostic(&self.sess.span_diagnostic)); .into_diagnostic(self.diagnostic()));
} }
} }
}; };

View file

@ -877,7 +877,7 @@ impl<'a> Parser<'a> {
// will direct us over to `parse_enum_variant()`. // will direct us over to `parse_enum_variant()`.
if self.token == token::OpenDelim(Delimiter::Parenthesis) { if self.token == token::OpenDelim(Delimiter::Parenthesis) {
return Err(EnumPatternInsteadOfIdentifier { span: self.prev_token.span } return Err(EnumPatternInsteadOfIdentifier { span: self.prev_token.span }
.into_diagnostic(&self.sess.span_diagnostic)); .into_diagnostic(self.diagnostic()));
} }
Ok(PatKind::Ident(binding_annotation, ident, sub)) Ok(PatKind::Ident(binding_annotation, ident, sub))
@ -991,7 +991,7 @@ impl<'a> Parser<'a> {
// check that a comma comes after every field // check that a comma comes after every field
if !ate_comma { if !ate_comma {
let mut err = ExpectedCommaAfterPatternField { span: self.token.span } let mut err = ExpectedCommaAfterPatternField { span: self.token.span }
.into_diagnostic(&self.sess.span_diagnostic); .into_diagnostic(self.diagnostic());
if let Some(mut delayed) = delayed_err { if let Some(mut delayed) = delayed_err {
delayed.emit(); delayed.emit();
} }