Skip to content

Commit 7ff17c1

Browse files
authored
Rollup merge of #76910 - lcnr:foreign-item-like, r=oli-obk
transmute: use diagnostic item closes #66075, we now have no remaining uses of `match_def_path` in the compiler while some uses still remain in `clippy`. cc @RalfJung
2 parents 9567b5a + bfa2030 commit 7ff17c1

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

compiler/rustc_lint/src/builtin.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -2350,13 +2350,6 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
23502350

23512351
/// Determine if this expression is a "dangerous initialization".
23522352
fn is_dangerous_init(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option<InitKind> {
2353-
// `transmute` is inside an anonymous module (the `extern` block?);
2354-
// `Invalid` represents the empty string and matches that.
2355-
// FIXME(#66075): use diagnostic items. Somehow, that does not seem to work
2356-
// on intrinsics right now.
2357-
const TRANSMUTE_PATH: &[Symbol] =
2358-
&[sym::core, sym::intrinsics, kw::Invalid, sym::transmute];
2359-
23602353
if let hir::ExprKind::Call(ref path_expr, ref args) = expr.kind {
23612354
// Find calls to `mem::{uninitialized,zeroed}` methods.
23622355
if let hir::ExprKind::Path(ref qpath) = path_expr.kind {
@@ -2366,7 +2359,7 @@ impl<'tcx> LateLintPass<'tcx> for InvalidValue {
23662359
return Some(InitKind::Zeroed);
23672360
} else if cx.tcx.is_diagnostic_item(sym::mem_uninitialized, def_id) {
23682361
return Some(InitKind::Uninit);
2369-
} else if cx.match_def_path(def_id, TRANSMUTE_PATH) {
2362+
} else if cx.tcx.is_diagnostic_item(sym::transmute, def_id) {
23702363
if is_zero(&args[0]) {
23712364
return Some(InitKind::Zeroed);
23722365
}

compiler/rustc_lint/src/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,10 @@ impl<'tcx> LateContext<'tcx> {
720720
/// Anonymous scopes such as `extern` imports are matched with `kw::Invalid`;
721721
/// inherent `impl` blocks are matched with the name of the type.
722722
///
723+
/// Instead of using this method, it is often preferable to instead use
724+
/// `rustc_diagnostic_item` or a `lang_item`. This is less prone to errors
725+
/// as paths get invalidated if the target definition moves.
726+
///
723727
/// # Examples
724728
///
725729
/// ```rust,ignore (no context or def id available)

compiler/rustc_passes/src/diagnostic_items.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
use rustc_ast as ast;
1313
use rustc_data_structures::fx::FxHashMap;
1414
use rustc_hir as hir;
15-
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1615
use rustc_hir::itemlikevisit::ItemLikeVisitor;
1716
use rustc_middle::ty::query::Providers;
1817
use rustc_middle::ty::TyCtxt;
1918
use rustc_session::Session;
19+
use rustc_span::def_id::{DefId, LOCAL_CRATE};
2020
use rustc_span::symbol::{sym, Symbol};
2121

2222
struct DiagnosticItemCollector<'tcx> {
@@ -100,6 +100,18 @@ fn collect<'tcx>(tcx: TyCtxt<'tcx>) -> FxHashMap<Symbol, DefId> {
100100

101101
// Collect diagnostic items in this crate.
102102
tcx.hir().krate().visit_all_item_likes(&mut collector);
103+
// FIXME(visit_all_item_likes): Foreign items are not visited
104+
// here, so we have to manually look at them for now.
105+
for foreign_module in tcx.foreign_modules(LOCAL_CRATE) {
106+
for &foreign_item in foreign_module.foreign_items.iter() {
107+
match tcx.hir().get(tcx.hir().local_def_id_to_hir_id(foreign_item.expect_local())) {
108+
hir::Node::ForeignItem(item) => {
109+
collector.observe_item(item.attrs, item.hir_id);
110+
}
111+
item => bug!("unexpected foreign item {:?}", item),
112+
}
113+
}
114+
}
103115

104116
collector.items
105117
}

library/core/src/intrinsics.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,7 @@ extern "rust-intrinsic" {
10711071
// NOTE: While this makes the intrinsic const stable, we have some custom code in const fn
10721072
// checks that prevent its use within `const fn`.
10731073
#[rustc_const_stable(feature = "const_transmute", since = "1.46.0")]
1074+
#[cfg_attr(not(bootstrap), rustc_diagnostic_item = "transmute")]
10741075
pub fn transmute<T, U>(e: T) -> U;
10751076

10761077
/// Returns `true` if the actual type given as `T` requires drop

0 commit comments

Comments
 (0)