Skip to content

Commit a841909

Browse files
committed
InstanceDef::Item
1 parent 178c650 commit a841909

File tree

14 files changed

+80
-58
lines changed

14 files changed

+80
-58
lines changed

src/librustc_codegen_ssa/back/symbol_export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,9 @@ fn exported_symbols_provider_local(
249249
}
250250

251251
match *mono_item {
252-
MonoItem::Fn(Instance { def: InstanceDef::Item(def_id), substs }) => {
252+
MonoItem::Fn(Instance { def: InstanceDef::Item(def), substs }) => {
253253
if substs.non_erasable_generics().next().is_some() {
254-
let symbol = ExportedSymbol::Generic(def_id, substs);
254+
let symbol = ExportedSymbol::Generic(def.did, substs);
255255
symbols.push((symbol, SymbolExportLevel::Rust));
256256
}
257257
}

src/librustc_middle/mir/mono.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,8 @@ impl<'tcx> CodegenUnit<'tcx> {
346346
// instances into account. The others don't matter for
347347
// the codegen tests and can even make item order
348348
// unstable.
349-
InstanceDef::Item(def_id) => {
350-
def_id.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id))
349+
InstanceDef::Item(def) => {
350+
def.did.as_local().map(|def_id| tcx.hir().as_local_hir_id(def_id))
351351
}
352352
InstanceDef::VtableShim(..)
353353
| InstanceDef::ReifyShim(..)

src/librustc_middle/ty/instance.rs

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub enum InstanceDef<'tcx> {
2929
/// - `fn` items
3030
/// - closures
3131
/// - generators
32-
Item(DefId),
32+
Item(ty::WithOptParam<DefId>),
3333

3434
/// An intrinsic `fn` item (with `"rust-intrinsic"` or `"platform-intrinsic"` ABI).
3535
///
@@ -160,8 +160,8 @@ impl<'tcx> Instance<'tcx> {
160160
self.substs.non_erasable_generics().next()?;
161161

162162
match self.def {
163-
InstanceDef::Item(def_id) => tcx
164-
.upstream_monomorphizations_for(def_id)
163+
InstanceDef::Item(def) => tcx
164+
.upstream_monomorphizations_for(def.did)
165165
.and_then(|monos| monos.get(&self.substs).cloned()),
166166
InstanceDef::DropGlue(_, Some(_)) => tcx.upstream_drop_glue_for(self.substs),
167167
_ => None,
@@ -171,10 +171,10 @@ impl<'tcx> Instance<'tcx> {
171171

172172
impl<'tcx> InstanceDef<'tcx> {
173173
#[inline]
174-
pub fn def_id(&self) -> DefId {
175-
match *self {
176-
InstanceDef::Item(def_id)
177-
| InstanceDef::VtableShim(def_id)
174+
pub fn def_id(self) -> DefId {
175+
match self {
176+
InstanceDef::Item(def) => def.did,
177+
InstanceDef::VtableShim(def_id)
178178
| InstanceDef::ReifyShim(def_id)
179179
| InstanceDef::FnPtrShim(def_id, _)
180180
| InstanceDef::Virtual(def_id, _)
@@ -185,6 +185,21 @@ impl<'tcx> InstanceDef<'tcx> {
185185
}
186186
}
187187

188+
#[inline]
189+
pub fn with_opt_param(self) -> ty::WithOptParam<DefId> {
190+
match self {
191+
InstanceDef::Item(def) => def,
192+
InstanceDef::VtableShim(def_id)
193+
| InstanceDef::ReifyShim(def_id)
194+
| InstanceDef::FnPtrShim(def_id, _)
195+
| InstanceDef::Virtual(def_id, _)
196+
| InstanceDef::Intrinsic(def_id)
197+
| InstanceDef::ClosureOnceShim { call_once: def_id }
198+
| InstanceDef::DropGlue(def_id, _)
199+
| InstanceDef::CloneShim(def_id, _) => ty::WithOptParam::dummy(def_id),
200+
}
201+
}
202+
188203
#[inline]
189204
pub fn attrs(&self, tcx: TyCtxt<'tcx>) -> ty::Attributes<'tcx> {
190205
tcx.get_attrs(self.def_id())
@@ -198,7 +213,7 @@ impl<'tcx> InstanceDef<'tcx> {
198213
pub fn requires_inline(&self, tcx: TyCtxt<'tcx>) -> bool {
199214
use rustc_hir::definitions::DefPathData;
200215
let def_id = match *self {
201-
ty::InstanceDef::Item(def_id) => def_id,
216+
ty::InstanceDef::Item(def) => def.did,
202217
ty::InstanceDef::DropGlue(_, Some(_)) => return false,
203218
_ => return true,
204219
};
@@ -244,8 +259,8 @@ impl<'tcx> InstanceDef<'tcx> {
244259

245260
pub fn requires_caller_location(&self, tcx: TyCtxt<'_>) -> bool {
246261
match *self {
247-
InstanceDef::Item(def_id) => {
248-
tcx.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
262+
InstanceDef::Item(def) => {
263+
tcx.codegen_fn_attrs(def.did).flags.contains(CodegenFnAttrFlags::TRACK_CALLER)
249264
}
250265
_ => false,
251266
}
@@ -283,7 +298,7 @@ impl<'tcx> Instance<'tcx> {
283298
def_id,
284299
substs
285300
);
286-
Instance { def: InstanceDef::Item(def_id), substs }
301+
Instance { def: InstanceDef::Item(ty::WithOptParam::dummy(def_id)), substs }
287302
}
288303

289304
pub fn mono(tcx: TyCtxt<'tcx>, def_id: DefId) -> Instance<'tcx> {
@@ -356,9 +371,9 @@ impl<'tcx> Instance<'tcx> {
356371
debug!("resolve(def_id={:?}, substs={:?})", def_id, substs);
357372
Instance::resolve(tcx, param_env, def_id, substs).ok().flatten().map(|mut resolved| {
358373
match resolved.def {
359-
InstanceDef::Item(def_id) if resolved.def.requires_caller_location(tcx) => {
374+
InstanceDef::Item(def) if resolved.def.requires_caller_location(tcx) => {
360375
debug!(" => fn pointer created for function with #[track_caller]");
361-
resolved.def = InstanceDef::ReifyShim(def_id);
376+
resolved.def = InstanceDef::ReifyShim(def.did);
362377
}
363378
InstanceDef::Virtual(def_id, _) => {
364379
debug!(" => fn pointer created for virtual call");

src/librustc_middle/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2880,7 +2880,7 @@ impl<'tcx> TyCtxt<'tcx> {
28802880
/// Returns the possibly-auto-generated MIR of a `(DefId, Subst)` pair.
28812881
pub fn instance_mir(self, instance: ty::InstanceDef<'tcx>) -> &'tcx Body<'tcx> {
28822882
match instance {
2883-
ty::InstanceDef::Item(did) => self.optimized_mir(did),
2883+
ty::InstanceDef::Item(def) => self.optimized_mir(def.did),
28842884
ty::InstanceDef::VtableShim(..)
28852885
| ty::InstanceDef::ReifyShim(..)
28862886
| ty::InstanceDef::Intrinsic(..)

src/librustc_middle/ty/structural_impls.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
838838
Self {
839839
substs: self.substs.fold_with(folder),
840840
def: match self.def {
841-
Item(did) => Item(did.fold_with(folder)),
841+
Item(def) => Item(def.fold_with(folder)),
842842
VtableShim(did) => VtableShim(did.fold_with(folder)),
843843
ReifyShim(did) => ReifyShim(did.fold_with(folder)),
844844
Intrinsic(did) => Intrinsic(did.fold_with(folder)),
@@ -857,7 +857,8 @@ impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
857857
use crate::ty::InstanceDef::*;
858858
self.substs.visit_with(visitor)
859859
|| match self.def {
860-
Item(did) | VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
860+
Item(def) => def.visit_with(visitor),
861+
VtableShim(did) | ReifyShim(did) | Intrinsic(did) | Virtual(did, _) => {
861862
did.visit_with(visitor)
862863
}
863864
FnPtrShim(did, ty) | CloneShim(did, ty) => {

src/librustc_mir/const_eval/eval_queries.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -288,21 +288,21 @@ pub fn const_eval_raw_provider<'tcx>(
288288
}
289289

290290
let cid = key.value;
291-
let def_id = cid.instance.def.def_id();
291+
let def = cid.instance.def.with_opt_param();
292292

293-
if let Some(def_id) = def_id.as_local() {
294-
if tcx.has_typeck_tables(def_id) {
295-
if let Some(error_reported) = tcx.typeck_tables_of(def_id).tainted_by_errors {
293+
if let Some(def) = def.as_local() {
294+
if tcx.has_typeck_tables(def.did) {
295+
if let Some(error_reported) = tcx.typeck_tables_of_const_arg(def).tainted_by_errors {
296296
return Err(ErrorHandled::Reported(error_reported));
297297
}
298298
}
299299
}
300300

301-
let is_static = tcx.is_static(def_id);
301+
let is_static = tcx.is_static(def.did);
302302

303303
let mut ecx = InterpCx::new(
304304
tcx,
305-
tcx.def_span(cid.instance.def_id()),
305+
tcx.def_span(def.did),
306306
key.param_env,
307307
CompileTimeInterpreter::new(tcx.sess.const_eval_limit()),
308308
MemoryExtra { can_access_statics: is_static },
@@ -334,9 +334,9 @@ pub fn const_eval_raw_provider<'tcx>(
334334
}
335335

336336
v
337-
} else if let Some(def_id) = def_id.as_local() {
337+
} else if let Some(def) = def.as_local() {
338338
// constant defined in this crate, we can figure out a lint level!
339-
match tcx.def_kind(def_id.to_def_id()) {
339+
match tcx.def_kind(def.did.to_def_id()) {
340340
// constants never produce a hard error at the definition site. Anything else is
341341
// a backwards compatibility hazard (and will break old versions of winapi for
342342
// sure)
@@ -346,9 +346,9 @@ pub fn const_eval_raw_provider<'tcx>(
346346
// validation thus preventing such a hard error from being a backwards
347347
// compatibility hazard
348348
DefKind::Const | DefKind::AssocConst => {
349-
let hir_id = tcx.hir().as_local_hir_id(def_id);
349+
let hir_id = tcx.hir().as_local_hir_id(def.did);
350350
err.report_as_lint(
351-
tcx.at(tcx.def_span(def_id)),
351+
tcx.at(tcx.def_span(def.did)),
352352
"any use of this value will cause an error",
353353
hir_id,
354354
Some(err.span),
@@ -359,7 +359,7 @@ pub fn const_eval_raw_provider<'tcx>(
359359
// deny-by-default lint
360360
_ => {
361361
if let Some(p) = cid.promoted {
362-
let span = tcx.promoted_mir(def_id)[p].span;
362+
let span = tcx.promoted_mir(def.did)[p].span;
363363
if let err_inval!(ReferencedConstant) = err.error {
364364
err.report_as_error(
365365
tcx.at(span),
@@ -369,7 +369,7 @@ pub fn const_eval_raw_provider<'tcx>(
369369
err.report_as_lint(
370370
tcx.at(span),
371371
"reaching this expression at runtime will panic or abort",
372-
tcx.hir().as_local_hir_id(def_id),
372+
tcx.hir().as_local_hir_id(def.did),
373373
Some(err.span),
374374
)
375375
}

src/librustc_mir/const_eval/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,11 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
191191
debug!("find_mir_or_eval_fn: {:?}", instance);
192192

193193
// Only check non-glue functions
194-
if let ty::InstanceDef::Item(def_id) = instance.def {
194+
if let ty::InstanceDef::Item(def) = instance.def {
195195
// Execution might have wandered off into other crates, so we cannot do a stability-
196196
// sensitive check here. But we can at least rule out functions that are not const
197197
// at all.
198-
if ecx.tcx.is_const_fn_raw(def_id) {
198+
if ecx.tcx.is_const_fn_raw(def.did) {
199199
// If this function is a `const fn` then under certain circumstances we
200200
// can evaluate call via the query system, thus memoizing all future calls.
201201
if ecx.try_eval_const_fn_call(instance, ret, args)? {

src/librustc_mir/interpret/eval_context.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -394,24 +394,26 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
394394
promoted: Option<mir::Promoted>,
395395
) -> InterpResult<'tcx, &'tcx mir::Body<'tcx>> {
396396
// do not continue if typeck errors occurred (can only occur in local crate)
397-
let did = instance.def_id();
398-
if let Some(did) = did.as_local() {
399-
if self.tcx.has_typeck_tables(did) {
400-
if let Some(error_reported) = self.tcx.typeck_tables_of(did).tainted_by_errors {
397+
let def = instance.with_opt_param();
398+
if let Some(def) = def.as_local() {
399+
if self.tcx.has_typeck_tables(def.did) {
400+
if let Some(error_reported) =
401+
self.tcx.typeck_tables_of_const_arg(def).tainted_by_errors
402+
{
401403
throw_inval!(TypeckError(error_reported))
402404
}
403405
}
404406
}
405407
trace!("load mir(instance={:?}, promoted={:?})", instance, promoted);
406408
if let Some(promoted) = promoted {
407-
return Ok(&self.tcx.promoted_mir(did)[promoted]);
409+
return Ok(&self.tcx.promoted_mir(def.did)[promoted]);
408410
}
409411
match instance {
410-
ty::InstanceDef::Item(def_id) => {
411-
if self.tcx.is_mir_available(did) {
412-
Ok(self.tcx.optimized_mir(did))
412+
ty::InstanceDef::Item(def) => {
413+
if self.tcx.is_mir_available(def.did) {
414+
Ok(self.tcx.optimized_mir(def.did))
413415
} else {
414-
throw_unsup!(NoMirFor(def_id))
416+
throw_unsup!(NoMirFor(def.did))
415417
}
416418
}
417419
_ => Ok(self.tcx.instance_mir(instance)),

src/librustc_mir/monomorphize/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -768,8 +768,8 @@ fn visit_instance_use<'tcx>(
768768
// need a mono item.
769769
fn should_monomorphize_locally<'tcx>(tcx: TyCtxt<'tcx>, instance: &Instance<'tcx>) -> bool {
770770
let def_id = match instance.def {
771-
ty::InstanceDef::Item(def_id) | ty::InstanceDef::DropGlue(def_id, Some(_)) => def_id,
772-
771+
ty::InstanceDef::Item(def) => def.did,
772+
ty::InstanceDef::DropGlue(def_id, Some(_)) => def_id,
773773
ty::InstanceDef::VtableShim(..)
774774
| ty::InstanceDef::ReifyShim(..)
775775
| ty::InstanceDef::ClosureOnceShim { .. }

src/librustc_mir/monomorphize/partitioning.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ fn mono_item_visibility(
314314
};
315315

316316
let def_id = match instance.def {
317-
InstanceDef::Item(def_id) | InstanceDef::DropGlue(def_id, Some(_)) => def_id,
317+
InstanceDef::Item(def) => def.did,
318+
InstanceDef::DropGlue(def_id, Some(_)) => def_id,
318319

319320
// These are all compiler glue and such, never exported, always hidden.
320321
InstanceDef::VtableShim(..)
@@ -704,7 +705,7 @@ fn characteristic_def_id_of_mono_item<'tcx>(
704705
match mono_item {
705706
MonoItem::Fn(instance) => {
706707
let def_id = match instance.def {
707-
ty::InstanceDef::Item(def_id) => def_id,
708+
ty::InstanceDef::Item(def) => def.did,
708709
ty::InstanceDef::VtableShim(..)
709710
| ty::InstanceDef::ReifyShim(..)
710711
| ty::InstanceDef::FnPtrShim(..)

0 commit comments

Comments
 (0)