Skip to content

Commit d3a742b

Browse files
Miscellaneous renaming
1 parent bcf38b5 commit d3a742b

File tree

5 files changed

+71
-68
lines changed

5 files changed

+71
-68
lines changed

compiler/rustc_const_eval/src/interpret/terminator.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -883,13 +883,12 @@ impl<'tcx, M: Machine<'tcx>> InterpCx<'tcx, M> {
883883
ty::ExistentialTraitRef::erase_self_ty(tcx, virtual_trait_ref);
884884
let concrete_trait_ref = existential_trait_ref.with_self_ty(tcx, dyn_ty);
885885

886-
let concrete_method = Instance::resolve_for_vtable(
886+
let concrete_method = Instance::expect_resolve_for_vtable(
887887
tcx,
888888
self.param_env,
889889
def_id,
890890
instance.args.rebase_onto(tcx, trait_def_id, concrete_trait_ref.args),
891-
)
892-
.unwrap();
891+
);
893892
assert_eq!(fn_inst, concrete_method);
894893
}
895894

compiler/rustc_middle/src/query/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2197,8 +2197,8 @@ rustc_queries! {
21972197
/// * `Err(ErrorGuaranteed)` when the `Instance` resolution process
21982198
/// couldn't complete due to errors elsewhere - this is distinct
21992199
/// from `Ok(None)` to avoid misleading diagnostics when an error
2200-
/// has already been/will be emitted, for the original cause
2201-
query resolve_instance(
2200+
/// has already been/will be emitted, for the original cause.
2201+
query resolve_instance_raw(
22022202
key: ty::ParamEnvAnd<'tcx, (DefId, GenericArgsRef<'tcx>)>
22032203
) -> Result<Option<ty::Instance<'tcx>>, ErrorGuaranteed> {
22042204
desc { "resolving instance `{}`", ty::Instance::new(key.value.0, key.value.1) }

compiler/rustc_middle/src/ty/instance.rs

Lines changed: 62 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ impl<'tcx> Instance<'tcx> {
505505
// below is more likely to ignore the bounds in scope (e.g. if the only
506506
// generic parameters mentioned by `args` were lifetime ones).
507507
let args = tcx.erase_regions(args);
508-
tcx.resolve_instance(tcx.erase_regions(param_env.and((def_id, args))))
508+
tcx.resolve_instance_raw(tcx.erase_regions(param_env.and((def_id, args))))
509509
}
510510

511511
pub fn expect_resolve(
@@ -571,77 +571,81 @@ impl<'tcx> Instance<'tcx> {
571571
})
572572
}
573573

574-
pub fn resolve_for_vtable(
574+
pub fn expect_resolve_for_vtable(
575575
tcx: TyCtxt<'tcx>,
576576
param_env: ty::ParamEnv<'tcx>,
577577
def_id: DefId,
578578
args: GenericArgsRef<'tcx>,
579-
) -> Option<Instance<'tcx>> {
579+
) -> Instance<'tcx> {
580580
debug!("resolve_for_vtable(def_id={:?}, args={:?})", def_id, args);
581581
let fn_sig = tcx.fn_sig(def_id).instantiate_identity();
582582
let is_vtable_shim = !fn_sig.inputs().skip_binder().is_empty()
583583
&& fn_sig.input(0).skip_binder().is_param(0)
584584
&& tcx.generics_of(def_id).has_self;
585+
585586
if is_vtable_shim {
586587
debug!(" => associated item with unsizeable self: Self");
587-
Some(Instance { def: InstanceKind::VTableShim(def_id), args })
588-
} else {
589-
let reason = tcx.sess.is_sanitizer_kcfi_enabled().then_some(ReifyReason::Vtable);
590-
Instance::resolve(tcx, param_env, def_id, args).ok().flatten().map(|mut resolved| {
591-
match resolved.def {
592-
InstanceKind::Item(def) => {
593-
// We need to generate a shim when we cannot guarantee that
594-
// the caller of a trait object method will be aware of
595-
// `#[track_caller]` - this ensures that the caller
596-
// and callee ABI will always match.
597-
//
598-
// The shim is generated when all of these conditions are met:
599-
//
600-
// 1) The underlying method expects a caller location parameter
601-
// in the ABI
602-
if resolved.def.requires_caller_location(tcx)
603-
// 2) The caller location parameter comes from having `#[track_caller]`
604-
// on the implementation, and *not* on the trait method.
605-
&& !tcx.should_inherit_track_caller(def)
606-
// If the method implementation comes from the trait definition itself
607-
// (e.g. `trait Foo { #[track_caller] my_fn() { /* impl */ } }`),
608-
// then we don't need to generate a shim. This check is needed because
609-
// `should_inherit_track_caller` returns `false` if our method
610-
// implementation comes from the trait block, and not an impl block
611-
&& !matches!(
612-
tcx.opt_associated_item(def),
613-
Some(ty::AssocItem {
614-
container: ty::AssocItemContainer::TraitContainer,
615-
..
616-
})
617-
)
618-
{
619-
if tcx.is_closure_like(def) {
620-
debug!(" => vtable fn pointer created for closure with #[track_caller]: {:?} for method {:?} {:?}",
621-
def, def_id, args);
622-
623-
// Create a shim for the `FnOnce/FnMut/Fn` method we are calling
624-
// - unlike functions, invoking a closure always goes through a
625-
// trait.
626-
resolved = Instance { def: InstanceKind::ReifyShim(def_id, reason), args };
627-
} else {
628-
debug!(
629-
" => vtable fn pointer created for function with #[track_caller]: {:?}", def
630-
);
631-
resolved.def = InstanceKind::ReifyShim(def, reason);
632-
}
633-
}
634-
}
635-
InstanceKind::Virtual(def_id, _) => {
636-
debug!(" => vtable fn pointer created for virtual call");
637-
resolved.def = InstanceKind::ReifyShim(def_id, reason)
588+
return Instance { def: InstanceKind::VTableShim(def_id), args };
589+
}
590+
591+
let mut resolved = Instance::expect_resolve(tcx, param_env, def_id, args);
592+
593+
let reason = tcx.sess.is_sanitizer_kcfi_enabled().then_some(ReifyReason::Vtable);
594+
match resolved.def {
595+
InstanceKind::Item(def) => {
596+
// We need to generate a shim when we cannot guarantee that
597+
// the caller of a trait object method will be aware of
598+
// `#[track_caller]` - this ensures that the caller
599+
// and callee ABI will always match.
600+
//
601+
// The shim is generated when all of these conditions are met:
602+
//
603+
// 1) The underlying method expects a caller location parameter
604+
// in the ABI
605+
if resolved.def.requires_caller_location(tcx)
606+
// 2) The caller location parameter comes from having `#[track_caller]`
607+
// on the implementation, and *not* on the trait method.
608+
&& !tcx.should_inherit_track_caller(def)
609+
// If the method implementation comes from the trait definition itself
610+
// (e.g. `trait Foo { #[track_caller] my_fn() { /* impl */ } }`),
611+
// then we don't need to generate a shim. This check is needed because
612+
// `should_inherit_track_caller` returns `false` if our method
613+
// implementation comes from the trait block, and not an impl block
614+
&& !matches!(
615+
tcx.opt_associated_item(def),
616+
Some(ty::AssocItem {
617+
container: ty::AssocItemContainer::TraitContainer,
618+
..
619+
})
620+
)
621+
{
622+
if tcx.is_closure_like(def) {
623+
debug!(
624+
" => vtable fn pointer created for closure with #[track_caller]: {:?} for method {:?} {:?}",
625+
def, def_id, args
626+
);
627+
628+
// Create a shim for the `FnOnce/FnMut/Fn` method we are calling
629+
// - unlike functions, invoking a closure always goes through a
630+
// trait.
631+
resolved = Instance { def: InstanceKind::ReifyShim(def_id, reason), args };
632+
} else {
633+
debug!(
634+
" => vtable fn pointer created for function with #[track_caller]: {:?}",
635+
def
636+
);
637+
resolved.def = InstanceKind::ReifyShim(def, reason);
638638
}
639-
_ => {}
640639
}
641-
642-
resolved
643-
})
640+
}
641+
InstanceKind::Virtual(def_id, _) => {
642+
debug!(" => vtable fn pointer created for virtual call");
643+
resolved.def = InstanceKind::ReifyShim(def_id, reason)
644+
}
645+
_ => {}
644646
}
647+
648+
resolved
645649
}
646650

647651
pub fn resolve_closure(

compiler/rustc_trait_selection/src/traits/vtable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,13 +285,13 @@ fn vtable_entries<'tcx>(
285285
return VtblEntry::Vacant;
286286
}
287287

288-
let instance = ty::Instance::resolve_for_vtable(
288+
let instance = ty::Instance::expect_resolve_for_vtable(
289289
tcx,
290290
ty::ParamEnv::reveal_all(),
291291
def_id,
292292
args,
293-
)
294-
.expect("resolution failed during building vtable representation");
293+
);
294+
295295
VtblEntry::Method(instance)
296296
});
297297

compiler/rustc_ty_utils/src/instance.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use traits::{translate_args, Reveal};
1616

1717
use crate::errors::UnexpectedFnPtrAssociatedItem;
1818

19-
fn resolve_instance<'tcx>(
19+
fn resolve_instance_raw<'tcx>(
2020
tcx: TyCtxt<'tcx>,
2121
key: ty::ParamEnvAnd<'tcx, (DefId, GenericArgsRef<'tcx>)>,
2222
) -> Result<Option<Instance<'tcx>>, ErrorGuaranteed> {
@@ -364,5 +364,5 @@ fn resolve_associated_item<'tcx>(
364364
}
365365

366366
pub(crate) fn provide(providers: &mut Providers) {
367-
*providers = Providers { resolve_instance, ..*providers };
367+
*providers = Providers { resolve_instance_raw, ..*providers };
368368
}

0 commit comments

Comments
 (0)