Skip to content

Commit bba8710

Browse files
committed
Auto merge of #83596 - jyn514:session-dead-code, r=oli-obk
Remove dead or useless code from Session This is a more principled follow-up to #83185 (comment). - Rename `Parser::span_fatal_err` -> `Parser::span_err` - Remove some unnecessary uses of `struct_span_fatal` - Make `Diagnostic::span_fatal` unconditionally raise an error - Add `impl Deref<Target = Handler>` for Session and remove all functions that are exactly the same as their Handler counterparts - Note why `Handler::fatal` is different from `Sesssion::fatal` - Remove unused `opt_span_warn` function r? `@oli-obk` or `@estebank`
2 parents 19dae7b + f25aa57 commit bba8710

File tree

9 files changed

+38
-69
lines changed

9 files changed

+38
-69
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1236,9 +1236,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12361236
(Some(..), Some(..), HalfOpen) => hir::LangItem::Range,
12371237
(None, Some(..), Closed) => hir::LangItem::RangeToInclusive,
12381238
(Some(..), Some(..), Closed) => unreachable!(),
1239-
(_, None, Closed) => {
1240-
self.diagnostic().span_fatal(span, "inclusive range with no end").raise()
1241-
}
1239+
(_, None, Closed) => self.diagnostic().span_fatal(span, "inclusive range with no end"),
12421240
};
12431241

12441242
let fields = self.arena.alloc_from_iter(

compiler/rustc_errors/src/lib.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -634,19 +634,19 @@ impl Handler {
634634
DiagnosticBuilder::new(self, Level::Note, msg)
635635
}
636636

637-
pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: &str) -> FatalError {
637+
pub fn span_fatal(&self, span: impl Into<MultiSpan>, msg: &str) -> ! {
638638
self.emit_diag_at_span(Diagnostic::new(Fatal, msg), span);
639-
FatalError
639+
FatalError.raise()
640640
}
641641

642642
pub fn span_fatal_with_code(
643643
&self,
644644
span: impl Into<MultiSpan>,
645645
msg: &str,
646646
code: DiagnosticId,
647-
) -> FatalError {
647+
) -> ! {
648648
self.emit_diag_at_span(Diagnostic::new_with_code(Fatal, Some(code), msg), span);
649-
FatalError
649+
FatalError.raise()
650650
}
651651

652652
pub fn span_err(&self, span: impl Into<MultiSpan>, msg: &str) {
@@ -692,6 +692,7 @@ impl Handler {
692692
db
693693
}
694694

695+
// NOTE: intentionally doesn't raise an error so rustc_codegen_ssa only reports fatal errors in the main thread
695696
pub fn fatal(&self, msg: &str) -> FatalError {
696697
self.inner.borrow_mut().fatal(msg)
697698
}

compiler/rustc_parse/src/lexer/mod.rs

+25-45
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,11 @@ impl<'a> StringReader<'a> {
148148
None => "unterminated block comment",
149149
};
150150
let last_bpos = self.pos;
151-
self.sess
152-
.span_diagnostic
153-
.struct_span_fatal_with_code(
154-
self.mk_sp(start, last_bpos),
155-
msg,
156-
error_code!(E0758),
157-
)
158-
.emit();
159-
FatalError.raise();
151+
self.sess.span_diagnostic.span_fatal_with_code(
152+
self.mk_sp(start, last_bpos),
153+
msg,
154+
error_code!(E0758),
155+
);
160156
}
161157

162158
// Skip non-doc comments
@@ -315,57 +311,41 @@ impl<'a> StringReader<'a> {
315311
let (lit_kind, mode, prefix_len, postfix_len) = match kind {
316312
rustc_lexer::LiteralKind::Char { terminated } => {
317313
if !terminated {
318-
self.sess
319-
.span_diagnostic
320-
.struct_span_fatal_with_code(
321-
self.mk_sp(start, suffix_start),
322-
"unterminated character literal",
323-
error_code!(E0762),
324-
)
325-
.emit();
326-
FatalError.raise();
314+
self.sess.span_diagnostic.span_fatal_with_code(
315+
self.mk_sp(start, suffix_start),
316+
"unterminated character literal",
317+
error_code!(E0762),
318+
)
327319
}
328320
(token::Char, Mode::Char, 1, 1) // ' '
329321
}
330322
rustc_lexer::LiteralKind::Byte { terminated } => {
331323
if !terminated {
332-
self.sess
333-
.span_diagnostic
334-
.struct_span_fatal_with_code(
335-
self.mk_sp(start + BytePos(1), suffix_start),
336-
"unterminated byte constant",
337-
error_code!(E0763),
338-
)
339-
.emit();
340-
FatalError.raise();
324+
self.sess.span_diagnostic.span_fatal_with_code(
325+
self.mk_sp(start + BytePos(1), suffix_start),
326+
"unterminated byte constant",
327+
error_code!(E0763),
328+
)
341329
}
342330
(token::Byte, Mode::Byte, 2, 1) // b' '
343331
}
344332
rustc_lexer::LiteralKind::Str { terminated } => {
345333
if !terminated {
346-
self.sess
347-
.span_diagnostic
348-
.struct_span_fatal_with_code(
349-
self.mk_sp(start, suffix_start),
350-
"unterminated double quote string",
351-
error_code!(E0765),
352-
)
353-
.emit();
354-
FatalError.raise();
334+
self.sess.span_diagnostic.span_fatal_with_code(
335+
self.mk_sp(start, suffix_start),
336+
"unterminated double quote string",
337+
error_code!(E0765),
338+
)
355339
}
356340
(token::Str, Mode::Str, 1, 1) // " "
357341
}
358342
rustc_lexer::LiteralKind::ByteStr { terminated } => {
359343
if !terminated {
360-
self.sess
361-
.span_diagnostic
362-
.struct_span_fatal_with_code(
363-
self.mk_sp(start + BytePos(1), suffix_start),
364-
"unterminated double quote byte string",
365-
error_code!(E0766),
366-
)
367-
.emit();
368-
FatalError.raise();
344+
self.sess.span_diagnostic.span_fatal_with_code(
345+
self.mk_sp(start + BytePos(1), suffix_start),
346+
"unterminated double quote byte string",
347+
error_code!(E0766),
348+
)
369349
}
370350
(token::ByteStr, Mode::ByteStr, 2, 1) // b" "
371351
}

compiler/rustc_parse/src/parser/diagnostics.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,7 @@ impl AttemptLocalParseRecovery {
144144
}
145145

146146
impl<'a> Parser<'a> {
147-
pub(super) fn span_fatal_err<S: Into<MultiSpan>>(
148-
&self,
149-
sp: S,
150-
err: Error,
151-
) -> DiagnosticBuilder<'a> {
147+
pub(super) fn span_err<S: Into<MultiSpan>>(&self, sp: S, err: Error) -> DiagnosticBuilder<'a> {
152148
err.span_err(sp, self.diagnostic())
153149
}
154150

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,7 @@ impl<'a> Parser<'a> {
13261326
token::CloseDelim(token::Brace) => {}
13271327
token::DocComment(..) => {
13281328
let previous_span = self.prev_token.span;
1329-
let mut err = self.span_fatal_err(self.token.span, Error::UselessDocComment);
1329+
let mut err = self.span_err(self.token.span, Error::UselessDocComment);
13301330
self.bump(); // consume the doc comment
13311331
let comma_after_doc_seen = self.eat(&token::Comma);
13321332
// `seen_comma` is always false, because we are inside doc block

compiler/rustc_parse/src/parser/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ impl<'a> Parser<'a> {
525525
fn ident_or_err(&mut self) -> PResult<'a, (Ident, /* is_raw */ bool)> {
526526
self.token.ident().ok_or_else(|| match self.prev_token.kind {
527527
TokenKind::DocComment(..) => {
528-
self.span_fatal_err(self.prev_token.span, Error::UselessDocComment)
528+
self.span_err(self.prev_token.span, Error::UselessDocComment)
529529
}
530530
_ => self.expected_ident_found(),
531531
})

compiler/rustc_parse/src/parser/stmt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl<'a> Parser<'a> {
168168
fn error_outer_attrs(&self, attrs: &[Attribute]) {
169169
if let [.., last] = attrs {
170170
if last.is_doc_comment() {
171-
self.span_fatal_err(last.span, Error::UselessDocComment).emit();
171+
self.span_err(last.span, Error::UselessDocComment).emit();
172172
} else if attrs.iter().any(|a| a.style == AttrStyle::Outer) {
173173
self.struct_span_err(last.span, "expected statement after outer attribute").emit();
174174
}

compiler/rustc_session/src/cgu_reuse_tracker.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl CguReuseTracker {
112112
not recorded",
113113
cgu_user_name, cgu_name
114114
);
115-
diag.span_fatal(error_span.0, &msg).raise();
115+
diag.span_fatal(error_span.0, &msg)
116116
}
117117
}
118118
}

compiler/rustc_session/src/session.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,15 @@ impl Session {
421421
}
422422

423423
pub fn span_fatal<S: Into<MultiSpan>>(&self, sp: S, msg: &str) -> ! {
424-
self.diagnostic().span_fatal(sp, msg).raise()
424+
self.diagnostic().span_fatal(sp, msg)
425425
}
426426
pub fn span_fatal_with_code<S: Into<MultiSpan>>(
427427
&self,
428428
sp: S,
429429
msg: &str,
430430
code: DiagnosticId,
431431
) -> ! {
432-
self.diagnostic().span_fatal_with_code(sp, msg, code).raise()
432+
self.diagnostic().span_fatal_with_code(sp, msg, code)
433433
}
434434
pub fn fatal(&self, msg: &str) -> ! {
435435
self.diagnostic().fatal(msg).raise()
@@ -492,12 +492,6 @@ impl Session {
492492
pub fn warn(&self, msg: &str) {
493493
self.diagnostic().warn(msg)
494494
}
495-
pub fn opt_span_warn<S: Into<MultiSpan>>(&self, opt_sp: Option<S>, msg: &str) {
496-
match opt_sp {
497-
Some(sp) => self.span_warn(sp, msg),
498-
None => self.warn(msg),
499-
}
500-
}
501495
/// Delay a span_bug() call until abort_if_errors()
502496
#[track_caller]
503497
pub fn delay_span_bug<S: Into<MultiSpan>>(&self, sp: S, msg: &str) {

0 commit comments

Comments
 (0)