Skip to content

Commit d14dd9f

Browse files
committed
emit diagnostic after post-monomorphization errors
Emit a diagnostic when the monomorphized item collector encounters errors during a step of the recursive item collection. These post-monomorphization errors otherwise only show the erroneous expression without a trace, making them very obscure and hard to pinpoint whenever they happen in dependencies.
1 parent 68424e2 commit d14dd9f

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

compiler/rustc_middle/src/mir/mono.rs

+9
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,15 @@ impl<'tcx> MonoItem<'tcx> {
186186
pub fn codegen_dep_node(&self, tcx: TyCtxt<'tcx>) -> DepNode {
187187
crate::dep_graph::make_compile_mono_item(tcx, self)
188188
}
189+
190+
/// Returns the item's `CrateNum`
191+
pub fn krate(&self) -> CrateNum {
192+
match self {
193+
MonoItem::Fn(ref instance) => instance.def_id().krate,
194+
MonoItem::Static(def_id) => def_id.krate,
195+
MonoItem::GlobalAsm(..) => LOCAL_CRATE,
196+
}
197+
}
189198
}
190199

191200
impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for MonoItem<'tcx> {

compiler/rustc_mir/src/monomorphize/collector.rs

+44-2
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
184184
use rustc_data_structures::sync::{par_iter, MTLock, MTRef, ParallelIterator};
185185
use rustc_errors::{ErrorReported, FatalError};
186186
use rustc_hir as hir;
187-
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId};
187+
use rustc_hir::def_id::{DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
188188
use rustc_hir::itemlikevisit::ItemLikeVisitor;
189189
use rustc_hir::lang_items::LangItem;
190190
use rustc_index::bit_set::GrowableBitSet;
@@ -342,7 +342,8 @@ fn collect_roots(tcx: TyCtxt<'_>, mode: MonoItemCollectionMode) -> Vec<MonoItem<
342342
.collect()
343343
}
344344

345-
// Collect all monomorphized items reachable from `starting_point`
345+
/// Collect all monomorphized items reachable from `starting_point`, and emit a note diagnostic if a
346+
/// post-monorphization error is encountered during a collection step.
346347
fn collect_items_rec<'tcx>(
347348
tcx: TyCtxt<'tcx>,
348349
starting_point: Spanned<MonoItem<'tcx>>,
@@ -359,6 +360,31 @@ fn collect_items_rec<'tcx>(
359360
let mut neighbors = Vec::new();
360361
let recursion_depth_reset;
361362

363+
//
364+
// Post-monomorphization errors MVP
365+
//
366+
// We can encounter errors while monomorphizing an item, but we don't have a good way of
367+
// showing a complete stack of spans ultimately leading to collecting the erroneous one yet.
368+
// (It's also currently unclear exactly which diagnostics and information would be interesting
369+
// to report in such cases)
370+
//
371+
// This leads to suboptimal error reporting: a post-monomorphization error (PME) will be
372+
// shown with just a spanned piece of code causing the error, without information on where
373+
// it was called from. This is especially obscure if the erroneous mono item is in a
374+
// dependency. See for example issue #85155, where, before minimization, a PME happened two
375+
// crates downstream from libcore's stdarch, without a way to know which dependency was the
376+
// cause.
377+
//
378+
// If such an error occurs in the current crate, its span will be enough to locate the
379+
// source. If the cause is in another crate, the goal here is to quickly locate which mono
380+
// item in the current crate is ultimately responsible for causing the error.
381+
//
382+
// To give at least _some_ context to the user: while collecting mono items, we check the
383+
// error count. If it has changed, a PME occurred, and we trigger some diagnostics about the
384+
// current step of mono items collection.
385+
//
386+
let error_count = tcx.sess.diagnostic().err_count();
387+
362388
match starting_point.node {
363389
MonoItem::Static(def_id) => {
364390
let instance = Instance::mono(tcx, def_id);
@@ -411,6 +437,22 @@ fn collect_items_rec<'tcx>(
411437
}
412438
}
413439

440+
// Check for PMEs and emit a diagnostic if one happened. To try to show relevant edges of the
441+
// mono item graph where the PME diagnostics are currently the most problematic (e.g. ones
442+
// involving a dependency, and the lack of context is confusing) in this MVP, we focus on
443+
// diagnostics on edges crossing a crate boundary: the collected mono items which are not
444+
// defined in the local crate.
445+
if tcx.sess.diagnostic().err_count() > error_count && starting_point.node.krate() != LOCAL_CRATE
446+
{
447+
tcx.sess.span_note_without_error(
448+
starting_point.span,
449+
&format!(
450+
"the above error was encountered while instantiating `{}`",
451+
starting_point.node
452+
),
453+
);
454+
}
455+
414456
record_accesses(tcx, starting_point.node, neighbors.iter().map(|i| &i.node), inlining_map);
415457

416458
for neighbour in neighbors {

0 commit comments

Comments
 (0)