Skip to content

Commit 2d14db3

Browse files
committed
Auto merge of #108006 - cjgillot:def-impl, r=oli-obk
Avoid accessing HIR when it can be avoided Experiment to see if it helps some incremental cases. Will be rebased once #107942 gets merged. r? `@ghost`
2 parents 999ac5f + 065f0b2 commit 2d14db3

File tree

43 files changed

+312
-773
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+312
-773
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -1182,13 +1182,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11821182
);
11831183
}
11841184
let parent_did = tcx.parent(method_did);
1185-
let parent_self_ty = (tcx.def_kind(parent_did)
1186-
== rustc_hir::def::DefKind::Impl)
1187-
.then_some(parent_did)
1188-
.and_then(|did| match tcx.type_of(did).kind() {
1189-
ty::Adt(def, ..) => Some(def.did()),
1190-
_ => None,
1191-
});
1185+
let parent_self_ty =
1186+
matches!(tcx.def_kind(parent_did), rustc_hir::def::DefKind::Impl { .. })
1187+
.then_some(parent_did)
1188+
.and_then(|did| match tcx.type_of(did).kind() {
1189+
ty::Adt(def, ..) => Some(def.did()),
1190+
_ => None,
1191+
});
11921192
let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
11931193
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
11941194
});

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -852,9 +852,9 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
852852

853853
let tcx = self.infcx.tcx;
854854
let region_parent = tcx.parent(region.def_id);
855-
if tcx.def_kind(region_parent) != DefKind::Impl {
855+
let DefKind::Impl { .. } = tcx.def_kind(region_parent) else {
856856
return None;
857-
}
857+
};
858858

859859
let found = tcx
860860
.any_free_region_meets(&tcx.type_of(region_parent), |r| *r == ty::ReEarlyBound(region));

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

+34-33
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ use std::collections::hash_map::Entry::*;
22

33
use rustc_ast::expand::allocator::ALLOCATOR_METHODS;
44
use rustc_data_structures::fx::FxHashMap;
5-
use rustc_hir as hir;
5+
use rustc_hir::def::DefKind;
66
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId, LOCAL_CRATE};
7-
use rustc_hir::Node;
87
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
98
use rustc_middle::middle::exported_symbols::{
109
metadata_symbol_name, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
1110
};
1211
use rustc_middle::ty::query::{ExternProviders, Providers};
1312
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
1413
use rustc_middle::ty::Instance;
15-
use rustc_middle::ty::{self, SymbolName, TyCtxt};
14+
use rustc_middle::ty::{self, DefIdTree, SymbolName, TyCtxt};
1615
use rustc_session::config::{CrateType, OomStrategy};
1716
use rustc_target::spec::SanitizerSet;
1817

@@ -74,32 +73,34 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
7473
//
7574
// As a result, if this id is an FFI item (foreign item) then we only
7675
// let it through if it's included statically.
77-
match tcx.hir().get_by_def_id(def_id) {
78-
Node::ForeignItem(..) => {
79-
tcx.native_library(def_id).map_or(false, |library| library.kind.is_statically_included()).then_some(def_id)
80-
}
76+
if let Some(parent_id) = tcx.opt_local_parent(def_id)
77+
&& let DefKind::ForeignMod = tcx.def_kind(parent_id)
78+
{
79+
let library = tcx.native_library(def_id)?;
80+
return library.kind.is_statically_included().then_some(def_id);
81+
}
8182

82-
// Only consider nodes that actually have exported symbols.
83-
Node::Item(&hir::Item {
84-
kind: hir::ItemKind::Static(..) | hir::ItemKind::Fn(..),
85-
..
86-
})
87-
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => {
88-
let generics = tcx.generics_of(def_id);
89-
if !generics.requires_monomorphization(tcx)
90-
// Functions marked with #[inline] are codegened with "internal"
91-
// linkage and are not exported unless marked with an extern
92-
// indicator
93-
&& (!Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
94-
|| tcx.codegen_fn_attrs(def_id.to_def_id()).contains_extern_indicator())
95-
{
96-
Some(def_id)
97-
} else {
98-
None
99-
}
100-
}
83+
// Only consider nodes that actually have exported symbols.
84+
match tcx.def_kind(def_id) {
85+
DefKind::Fn | DefKind::Static(_) => {}
86+
DefKind::AssocFn if tcx.impl_of_method(def_id.to_def_id()).is_some() => {}
87+
_ => return None,
88+
};
10189

102-
_ => None,
90+
let generics = tcx.generics_of(def_id);
91+
if generics.requires_monomorphization(tcx) {
92+
return None;
93+
}
94+
95+
// Functions marked with #[inline] are codegened with "internal"
96+
// linkage and are not exported unless marked with an extern
97+
// indicator
98+
if !Instance::mono(tcx, def_id.to_def_id()).def.generates_cgu_internal_copy(tcx)
99+
|| tcx.codegen_fn_attrs(def_id.to_def_id()).contains_extern_indicator()
100+
{
101+
Some(def_id)
102+
} else {
103+
None
103104
}
104105
})
105106
.map(|def_id| {
@@ -118,7 +119,7 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
118119
tcx.symbol_name(Instance::mono(tcx, def_id.to_def_id())),
119120
export_level
120121
);
121-
(def_id.to_def_id(), SymbolExportInfo {
122+
let info = SymbolExportInfo {
122123
level: export_level,
123124
kind: if tcx.is_static(def_id.to_def_id()) {
124125
if codegen_attrs.flags.contains(CodegenFnAttrFlags::THREAD_LOCAL) {
@@ -130,8 +131,10 @@ fn reachable_non_generics_provider(tcx: TyCtxt<'_>, cnum: CrateNum) -> DefIdMap<
130131
SymbolExportKind::Text
131132
},
132133
used: codegen_attrs.flags.contains(CodegenFnAttrFlags::USED)
133-
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER) || used,
134-
})
134+
|| codegen_attrs.flags.contains(CodegenFnAttrFlags::USED_LINKER)
135+
|| used,
136+
};
137+
(def_id.to_def_id(), info)
135138
})
136139
.collect();
137140

@@ -457,9 +460,7 @@ fn symbol_export_level(tcx: TyCtxt<'_>, sym_def_id: DefId) -> SymbolExportLevel
457460
let target = &tcx.sess.target.llvm_target;
458461
// WebAssembly cannot export data symbols, so reduce their export level
459462
if target.contains("emscripten") {
460-
if let Some(Node::Item(&hir::Item { kind: hir::ItemKind::Static(..), .. })) =
461-
tcx.hir().get_if_local(sym_def_id)
462-
{
463+
if let DefKind::Static(_) = tcx.def_kind(sym_def_id) {
463464
return SymbolExportLevel::Rust;
464465
}
465466
}

compiler/rustc_codegen_ssa/src/target_features.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ use rustc_attr::InstructionSetAttr;
33
use rustc_data_structures::fx::FxHashMap;
44
use rustc_data_structures::fx::FxHashSet;
55
use rustc_errors::Applicability;
6-
use rustc_hir as hir;
6+
use rustc_hir::def::DefKind;
77
use rustc_hir::def_id::DefId;
88
use rustc_hir::def_id::LocalDefId;
99
use rustc_hir::def_id::LOCAL_CRATE;
1010
use rustc_middle::ty::query::Providers;
11-
use rustc_middle::ty::TyCtxt;
11+
use rustc_middle::ty::{DefIdTree, TyCtxt};
1212
use rustc_session::parse::feature_err;
1313
use rustc_session::Session;
1414
use rustc_span::symbol::sym;
@@ -440,12 +440,9 @@ fn asm_target_features(tcx: TyCtxt<'_>, did: DefId) -> &FxHashSet<Symbol> {
440440
/// Checks the function annotated with `#[target_feature]` is not a safe
441441
/// trait method implementation, reporting an error if it is.
442442
pub fn check_target_feature_trait_unsafe(tcx: TyCtxt<'_>, id: LocalDefId, attr_span: Span) {
443-
let hir_id = tcx.hir().local_def_id_to_hir_id(id);
444-
let node = tcx.hir().get(hir_id);
445-
if let hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) = node {
446-
let parent_id = tcx.hir().get_parent_item(hir_id);
447-
let parent_item = tcx.hir().expect_item(parent_id.def_id);
448-
if let hir::ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) = parent_item.kind {
443+
if let DefKind::AssocFn = tcx.def_kind(id) {
444+
let parent_id = tcx.local_parent(id);
445+
if let DefKind::Impl { of_trait: true } = tcx.def_kind(parent_id) {
449446
tcx.sess
450447
.struct_span_err(
451448
attr_span,

compiler/rustc_const_eval/src/const_eval/fn_queries.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ pub fn is_unstable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Symbol> {
1717

1818
pub fn is_parent_const_impl_raw(tcx: TyCtxt<'_>, def_id: LocalDefId) -> bool {
1919
let parent_id = tcx.local_parent(def_id);
20-
tcx.def_kind(parent_id) == DefKind::Impl && tcx.constness(parent_id) == hir::Constness::Const
20+
matches!(tcx.def_kind(parent_id), DefKind::Impl { .. })
21+
&& tcx.constness(parent_id) == hir::Constness::Const
2122
}
2223

2324
/// Checks whether an item is considered to be `const`. If it is a constructor, it is const. If

compiler/rustc_hir/src/def.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ pub enum DefKind {
116116
LifetimeParam,
117117
/// A use of `global_asm!`.
118118
GlobalAsm,
119-
Impl,
119+
Impl {
120+
of_trait: bool,
121+
},
120122
Closure,
121123
Generator,
122124
}
@@ -155,7 +157,7 @@ impl DefKind {
155157
DefKind::AnonConst => "constant expression",
156158
DefKind::InlineConst => "inline constant",
157159
DefKind::Field => "field",
158-
DefKind::Impl => "implementation",
160+
DefKind::Impl { .. } => "implementation",
159161
DefKind::Closure => "closure",
160162
DefKind::Generator => "generator",
161163
DefKind::ExternCrate => "extern crate",
@@ -171,7 +173,7 @@ impl DefKind {
171173
| DefKind::AssocFn
172174
| DefKind::Enum
173175
| DefKind::OpaqueTy
174-
| DefKind::Impl
176+
| DefKind::Impl { .. }
175177
| DefKind::Use
176178
| DefKind::InlineConst
177179
| DefKind::ExternCrate => "an",
@@ -216,7 +218,7 @@ impl DefKind {
216218
| DefKind::Use
217219
| DefKind::ForeignMod
218220
| DefKind::GlobalAsm
219-
| DefKind::Impl
221+
| DefKind::Impl { .. }
220222
| DefKind::ImplTraitPlaceholder => None,
221223
}
222224
}
@@ -255,7 +257,7 @@ impl DefKind {
255257
| DefKind::ForeignMod
256258
| DefKind::OpaqueTy
257259
| DefKind::ImplTraitPlaceholder
258-
| DefKind::Impl
260+
| DefKind::Impl { .. }
259261
| DefKind::Field
260262
| DefKind::TyParam
261263
| DefKind::ConstParam

compiler/rustc_hir/src/target.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl Target {
116116
DefKind::Union => Target::Union,
117117
DefKind::Trait => Target::Trait,
118118
DefKind::TraitAlias => Target::TraitAlias,
119-
DefKind::Impl => Target::Impl,
119+
DefKind::Impl { .. } => Target::Impl,
120120
_ => panic!("impossible case reached"),
121121
}
122122
}

0 commit comments

Comments
 (0)