Skip to content

Commit 47756bb

Browse files
committed
shim: adjust valid shim asserts
This commit makes valid shim asserts more specific - checking for the specific types that are valid for a given type of shim - and removes asserts for types which require substitutions. Signed-off-by: David Wood <[email protected]>
1 parent 19e8495 commit 47756bb

File tree

2 files changed

+26
-40
lines changed

2 files changed

+26
-40
lines changed

src/librustc_mir/shim.rs

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_hir::lang_items::FnMutTraitLangItem;
44
use rustc_middle::mir::*;
55
use rustc_middle::ty::query::Providers;
66
use rustc_middle::ty::subst::{InternalSubsts, Subst};
7-
use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable};
7+
use rustc_middle::ty::{self, Ty, TyCtxt};
88
use rustc_target::abi::VariantIdx;
99

1010
use rustc_index::vec::{Idx, IndexVec};
@@ -36,11 +36,6 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
3636
build_call_shim(tcx, instance, Some(Adjustment::Deref), CallKind::Direct(def_id), None)
3737
}
3838
ty::InstanceDef::FnPtrShim(def_id, ty) => {
39-
// FIXME(eddyb) support generating shims for a "shallow type",
40-
// e.g. `Foo<_>` or `[_]` instead of requiring a fully monomorphic
41-
// `Foo<Bar>` or `[String]` etc.
42-
assert!(!ty.needs_subst());
43-
4439
let trait_ = tcx.trait_of_item(def_id).unwrap();
4540
let adjustment = match tcx.fn_trait_kind_from_lang_item(trait_) {
4641
Some(ty::ClosureKind::FnOnce) => Adjustment::Identity,
@@ -83,22 +78,8 @@ fn make_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'tcx>) -> Body<'
8378
None,
8479
)
8580
}
86-
ty::InstanceDef::DropGlue(def_id, ty) => {
87-
// FIXME(eddyb) support generating shims for a "shallow type",
88-
// e.g. `Foo<_>` or `[_]` instead of requiring a fully monomorphic
89-
// `Foo<Bar>` or `[String]` etc.
90-
assert!(!ty.needs_subst());
91-
92-
build_drop_shim(tcx, def_id, ty)
93-
}
94-
ty::InstanceDef::CloneShim(def_id, ty) => {
95-
// FIXME(eddyb) support generating shims for a "shallow type",
96-
// e.g. `Foo<_>` or `[_]` instead of requiring a fully monomorphic
97-
// `Foo<Bar>` or `[String]` etc.
98-
assert!(!ty.needs_subst());
99-
100-
build_clone_shim(tcx, def_id, ty)
101-
}
81+
ty::InstanceDef::DropGlue(def_id, ty) => build_drop_shim(tcx, def_id, ty),
82+
ty::InstanceDef::CloneShim(def_id, ty) => build_clone_shim(tcx, def_id, ty),
10283
ty::InstanceDef::Virtual(..) => {
10384
bug!("InstanceDef::Virtual ({:?}) is for direct calls only", instance)
10485
}

src/librustc_ty/instance.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir::def_id::{DefId, LocalDefId};
33
use rustc_infer::infer::TyCtxtInferExt;
44
use rustc_middle::ty::subst::SubstsRef;
55
use rustc_middle::ty::{self, Instance, TyCtxt, TypeFoldable};
6-
use rustc_span::sym;
6+
use rustc_span::{sym, DUMMY_SP};
77
use rustc_target::spec::abi::Abi;
88
use rustc_trait_selection::traits;
99
use traits::{translate_substs, Reveal};
@@ -67,12 +67,19 @@ fn inner_resolve_instance<'tcx>(
6767
let ty = substs.type_at(0);
6868

6969
if ty.needs_drop(tcx, param_env) {
70-
// `DropGlue` requires a monomorphic aka concrete type.
71-
if ty.needs_subst() {
72-
return Ok(None);
70+
debug!(" => nontrivial drop glue");
71+
match ty.kind {
72+
ty::Closure(..)
73+
| ty::Generator(..)
74+
| ty::Tuple(..)
75+
| ty::Adt(..)
76+
| ty::Dynamic(..)
77+
| ty::Array(..)
78+
| ty::Slice(..) => {}
79+
// Drop shims can only be built from ADTs.
80+
_ => return Ok(None),
7381
}
7482

75-
debug!(" => nontrivial drop glue");
7683
ty::InstanceDef::DropGlue(def_id, Some(ty))
7784
} else {
7885
debug!(" => trivial drop glue");
@@ -224,17 +231,13 @@ fn resolve_associated_item<'tcx>(
224231
trait_closure_kind,
225232
))
226233
}
227-
traits::ImplSourceFnPointer(ref data) => {
228-
// `FnPtrShim` requires a monomorphic aka concrete type.
229-
if data.fn_ty.needs_subst() {
230-
return Ok(None);
231-
}
232-
233-
Some(Instance {
234+
traits::ImplSourceFnPointer(ref data) => match data.fn_ty.kind {
235+
ty::FnDef(..) | ty::FnPtr(..) => Some(Instance {
234236
def: ty::InstanceDef::FnPtrShim(trait_item.def_id, data.fn_ty),
235237
substs: rcvr_substs,
236-
})
237-
}
238+
}),
239+
_ => None,
240+
},
238241
traits::ImplSourceObject(ref data) => {
239242
let index = traits::get_vtable_index_of_object_method(tcx, data, def_id);
240243
Some(Instance { def: ty::InstanceDef::Virtual(def_id, index), substs: rcvr_substs })
@@ -246,10 +249,12 @@ fn resolve_associated_item<'tcx>(
246249
if name == sym::clone {
247250
let self_ty = trait_ref.self_ty();
248251

249-
// `CloneShim` requires a monomorphic aka concrete type.
250-
if self_ty.needs_subst() {
251-
return Ok(None);
252-
}
252+
let is_copy = self_ty.is_copy_modulo_regions(tcx.at(DUMMY_SP), param_env);
253+
match self_ty.kind {
254+
_ if is_copy => (),
255+
ty::Array(..) | ty::Closure(..) | ty::Tuple(..) => {}
256+
_ => return Ok(None),
257+
};
253258

254259
Some(Instance {
255260
def: ty::InstanceDef::CloneShim(def_id, self_ty),

0 commit comments

Comments
 (0)