Skip to content

Commit d3901cd

Browse files
RalfJungbjorn3
authored andcommitted
simplify and explain MiriBeRustCompilerCalls logic for crate types
1 parent 9964970 commit d3901cd

File tree

1 file changed

+65
-63
lines changed

1 file changed

+65
-63
lines changed

src/tools/miri/src/bin/miri.rs

Lines changed: 65 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -274,15 +274,20 @@ struct MiriBeRustCompilerCalls {
274274
impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
275275
#[allow(rustc::potential_query_instability)] // rustc_codegen_ssa (where this code is copied from) also allows this lint
276276
fn config(&mut self, config: &mut Config) {
277-
if self.target_crate {
278-
config.make_codegen_backend = Some(Box::new(make_miri_codegen_backend));
277+
if !self.target_crate {
278+
// For a host crate, we fully behave like rustc.
279+
return;
279280
}
281+
// For a target crate, we emit an rlib that Miri can later consume.
282+
config.make_codegen_backend = Some(Box::new(make_miri_codegen_backend));
280283

281-
if config.opts.prints.is_empty() && self.target_crate {
284+
// Avoid warnings about unsupported crate types. However, only do that we we are *not* being
285+
// queried by cargo about the supported crate types so that cargo still receives the
286+
// warnings it expects.
287+
if config.opts.prints.is_empty() {
282288
#[allow(rustc::bad_opt_access)] // tcx does not exist yet
283289
{
284290
let any_crate_types = !config.opts.crate_types.is_empty();
285-
// Avoid warnings about unsupported crate types.
286291
config
287292
.opts
288293
.crate_types
@@ -293,66 +298,63 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
293298
assert!(!config.opts.crate_types.is_empty());
294299
}
295300
}
296-
297-
// Queries overridden here affect the data stored in `rmeta` files of dependencies,
298-
// which will be used later in non-`MIRI_BE_RUSTC` mode.
299-
config.override_queries = Some(|_, local_providers| {
300-
// We need to add #[used] symbols to exported_symbols for `lookup_link_section`.
301-
// FIXME handle this somehow in rustc itself to avoid this hack.
302-
local_providers.exported_non_generic_symbols = |tcx, LocalCrate| {
303-
let reachable_set = tcx.with_stable_hashing_context(|hcx| {
304-
tcx.reachable_set(()).to_sorted(&hcx, true)
305-
});
306-
tcx.arena.alloc_from_iter(
307-
// This is based on:
308-
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L62-L63
309-
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L174
310-
reachable_set.into_iter().filter_map(|&local_def_id| {
311-
// Do the same filtering that rustc does:
312-
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L84-L102
313-
// Otherwise it may cause unexpected behaviours and ICEs
314-
// (https://github.com/rust-lang/rust/issues/86261).
315-
let is_reachable_non_generic = matches!(
316-
tcx.hir_node_by_def_id(local_def_id),
317-
Node::Item(&hir::Item {
318-
kind: hir::ItemKind::Static(..) | hir::ItemKind::Fn{ .. },
319-
..
320-
}) | Node::ImplItem(&hir::ImplItem {
321-
kind: hir::ImplItemKind::Fn(..),
322-
..
323-
})
324-
if !tcx.generics_of(local_def_id).requires_monomorphization(tcx)
325-
);
326-
if !is_reachable_non_generic {
327-
return None;
328-
}
329-
let codegen_fn_attrs = tcx.codegen_fn_attrs(local_def_id);
330-
if codegen_fn_attrs.contains_extern_indicator()
331-
|| codegen_fn_attrs
332-
.flags
333-
.contains(CodegenFnAttrFlags::USED_COMPILER)
334-
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
335-
{
336-
Some((
337-
ExportedSymbol::NonGeneric(local_def_id.to_def_id()),
338-
// Some dummy `SymbolExportInfo` here. We only use
339-
// `exported_symbols` in shims/foreign_items.rs and the export info
340-
// is ignored.
341-
SymbolExportInfo {
342-
level: SymbolExportLevel::C,
343-
kind: SymbolExportKind::Text,
344-
used: false,
345-
rustc_std_internal_symbol: false,
346-
},
347-
))
348-
} else {
349-
None
350-
}
351-
}),
352-
)
353-
}
354-
});
355301
}
302+
303+
// Queries overridden here affect the data stored in `rmeta` files of dependencies,
304+
// which will be used later in non-`MIRI_BE_RUSTC` mode.
305+
config.override_queries = Some(|_, local_providers| {
306+
// We need to add #[used] symbols to exported_symbols for `lookup_link_section`.
307+
// FIXME handle this somehow in rustc itself to avoid this hack.
308+
local_providers.exported_non_generic_symbols = |tcx, LocalCrate| {
309+
let reachable_set = tcx
310+
.with_stable_hashing_context(|hcx| tcx.reachable_set(()).to_sorted(&hcx, true));
311+
tcx.arena.alloc_from_iter(
312+
// This is based on:
313+
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L62-L63
314+
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L174
315+
reachable_set.into_iter().filter_map(|&local_def_id| {
316+
// Do the same filtering that rustc does:
317+
// https://github.com/rust-lang/rust/blob/2962e7c0089d5c136f4e9600b7abccfbbde4973d/compiler/rustc_codegen_ssa/src/back/symbol_export.rs#L84-L102
318+
// Otherwise it may cause unexpected behaviours and ICEs
319+
// (https://github.com/rust-lang/rust/issues/86261).
320+
let is_reachable_non_generic = matches!(
321+
tcx.hir_node_by_def_id(local_def_id),
322+
Node::Item(&hir::Item {
323+
kind: hir::ItemKind::Static(..) | hir::ItemKind::Fn{ .. },
324+
..
325+
}) | Node::ImplItem(&hir::ImplItem {
326+
kind: hir::ImplItemKind::Fn(..),
327+
..
328+
})
329+
if !tcx.generics_of(local_def_id).requires_monomorphization(tcx)
330+
);
331+
if !is_reachable_non_generic {
332+
return None;
333+
}
334+
let codegen_fn_attrs = tcx.codegen_fn_attrs(local_def_id);
335+
if codegen_fn_attrs.contains_extern_indicator()
336+
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::USED_COMPILER)
337+
|| codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
338+
{
339+
Some((
340+
ExportedSymbol::NonGeneric(local_def_id.to_def_id()),
341+
// Some dummy `SymbolExportInfo` here. We only use
342+
// `exported_symbols` in shims/foreign_items.rs and the export info
343+
// is ignored.
344+
SymbolExportInfo {
345+
level: SymbolExportLevel::C,
346+
kind: SymbolExportKind::Text,
347+
used: false,
348+
rustc_std_internal_symbol: false,
349+
},
350+
))
351+
} else {
352+
None
353+
}
354+
}),
355+
)
356+
}
357+
});
356358
}
357359

358360
fn after_analysis<'tcx>(

0 commit comments

Comments
 (0)