Skip to content

Commit 35b3ef9

Browse files
authored
Unrolled build for rust-lang#121723
Rollup merge of rust-lang#121723 - nnethercote:two-diagnostic-things, r=oli-obk Two diagnostic things Two minor improvements to diagnostics-related things. r? ``@RalfJung``
2 parents d3d145e + 199be46 commit 35b3ef9

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

compiler/rustc_errors/src/diagnostic.rs

+19
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,25 @@ impl EmissionGuarantee for rustc_span::fatal_error::FatalError {
108108

109109
/// Trait implemented by error types. This is rarely implemented manually. Instead, use
110110
/// `#[derive(Diagnostic)]` -- see [rustc_macros::Diagnostic].
111+
///
112+
/// When implemented manually, it should be generic over the emission
113+
/// guarantee, i.e.:
114+
/// ```ignore (fragment)
115+
/// impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for Foo { ... }
116+
/// ```
117+
/// rather than being specific:
118+
/// ```ignore (fragment)
119+
/// impl<'a> IntoDiagnostic<'a> for Bar { ... } // the default type param is `ErrorGuaranteed`
120+
/// impl<'a> IntoDiagnostic<'a, ()> for Baz { ... }
121+
/// ```
122+
/// There are two reasons for this.
123+
/// - A diagnostic like `Foo` *could* be emitted at any level -- `level` is
124+
/// passed in to `into_diagnostic` from outside. Even if in practice it is
125+
/// always emitted at a single level, we let the diagnostic creation/emission
126+
/// site determine the level (by using `create_err`, `emit_warn`, etc.)
127+
/// rather than the `IntoDiagnostic` impl.
128+
/// - Derived impls are always generic, and it's good for the hand-written
129+
/// impls to be consistent with them.
111130
#[rustc_diagnostic_item = "IntoDiagnostic"]
112131
pub trait IntoDiagnostic<'a, G: EmissionGuarantee = ErrorGuaranteed> {
113132
/// Write out as a diagnostic out of `DiagCtxt`.

compiler/rustc_errors/src/lib.rs

+21-21
Original file line numberDiff line numberDiff line change
@@ -1498,14 +1498,26 @@ impl DiagCtxtInner {
14981498
let bugs: Vec<_> =
14991499
std::mem::take(&mut self.delayed_bugs).into_iter().map(|(b, _)| b).collect();
15001500

1501-
// If backtraces are enabled, also print the query stack
15021501
let backtrace = std::env::var_os("RUST_BACKTRACE").map_or(true, |x| &x != "0");
1503-
for (i, bug) in bugs.into_iter().enumerate() {
1504-
if let Some(file) = self.ice_file.as_ref()
1505-
&& let Ok(mut out) = std::fs::File::options().create(true).append(true).open(file)
1506-
{
1507-
let _ = write!(
1508-
&mut out,
1502+
let decorate = backtrace || self.ice_file.is_none();
1503+
let mut out = self
1504+
.ice_file
1505+
.as_ref()
1506+
.and_then(|file| std::fs::File::options().create(true).append(true).open(file).ok());
1507+
1508+
// Put the overall explanation before the `DelayedBug`s, to frame them
1509+
// better (e.g. separate warnings from them). Also, use notes, which
1510+
// don't count as errors, to avoid possibly triggering
1511+
// `-Ztreat-err-as-bug`, which we don't want.
1512+
let note1 = "no errors encountered even though delayed bugs were created";
1513+
let note2 = "those delayed bugs will now be shown as internal compiler errors";
1514+
self.emit_diagnostic(DiagInner::new(Note, note1));
1515+
self.emit_diagnostic(DiagInner::new(Note, note2));
1516+
1517+
for bug in bugs {
1518+
if let Some(out) = &mut out {
1519+
_ = write!(
1520+
out,
15091521
"delayed bug: {}\n{}\n",
15101522
bug.inner
15111523
.messages
@@ -1516,21 +1528,9 @@ impl DiagCtxtInner {
15161528
);
15171529
}
15181530

1519-
if i == 0 {
1520-
// Put the overall explanation before the `DelayedBug`s, to
1521-
// frame them better (e.g. separate warnings from them). Also,
1522-
// make it a note so it doesn't count as an error, because that
1523-
// could trigger `-Ztreat-err-as-bug`, which we don't want.
1524-
let note1 = "no errors encountered even though delayed bugs were created";
1525-
let note2 = "those delayed bugs will now be shown as internal compiler errors";
1526-
self.emit_diagnostic(DiagInner::new(Note, note1));
1527-
self.emit_diagnostic(DiagInner::new(Note, note2));
1528-
}
1529-
1530-
let mut bug =
1531-
if backtrace || self.ice_file.is_none() { bug.decorate(self) } else { bug.inner };
1531+
let mut bug = if decorate { bug.decorate(self) } else { bug.inner };
15321532

1533-
// "Undelay" the delayed bugs (into plain `Bug`s).
1533+
// "Undelay" the delayed bugs into plain bugs.
15341534
if bug.level != DelayedBug {
15351535
// NOTE(eddyb) not panicking here because we're already producing
15361536
// an ICE, and the more information the merrier.

0 commit comments

Comments
 (0)