1 use rustc_data_structures::svh::Svh;
3 use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
4 use rustc_middle::hir::map as hir_map;
5 use rustc_middle::ty::subst::Subst;
6 use rustc_middle::ty::{self, ToPredicate, Ty, TyCtxt, WithConstness};
7 use rustc_session::CrateDisambiguator;
8 use rustc_span::symbol::Symbol;
10 use rustc_trait_selection::traits;
12 fn sized_constraint_for_ty<'tcx>(
19 let result = match ty.kind {
20 Bool | Char | Int(..) | Uint(..) | Float(..) | RawPtr(..) | Ref(..) | FnDef(..)
21 | FnPtr(_) | Array(..) | Closure(..) | Generator(..) | Never => vec![],
23 Str | Dynamic(..) | Slice(_) | Foreign(..) | Error | GeneratorWitness(..) => {
24 // these are never sized - return the target type
28 Tuple(ref tys) => match tys.last() {
30 Some(ty) => sized_constraint_for_ty(tcx, adtdef, ty.expect_ty()),
35 let adt_tys = adt.sized_constraint(tcx);
36 debug!("sized_constraint_for_ty({:?}) intermediate = {:?}", ty, adt_tys);
39 .map(|ty| ty.subst(tcx, substs))
40 .flat_map(|ty| sized_constraint_for_ty(tcx, adtdef, ty))
44 Projection(..) | Opaque(..) => {
45 // must calculate explicitly.
46 // FIXME: consider special-casing always-Sized projections
51 // perf hack: if there is a `T: Sized` bound, then
52 // we know that `T` is Sized and do not need to check
55 let sized_trait = match tcx.lang_items().sized_trait() {
59 let sized_predicate = ty::Binder::dummy(ty::TraitRef {
61 substs: tcx.mk_substs_trait(ty, &[]),
65 let predicates = tcx.predicates_of(adtdef.did).predicates;
66 if predicates.iter().any(|(p, _)| *p == sized_predicate) { vec![] } else { vec![ty] }
69 Placeholder(..) | Bound(..) | Infer(..) => {
70 bug!("unexpected type `{:?}` in sized_constraint_for_ty", ty)
73 debug!("sized_constraint_for_ty({:?}) = {:?}", ty, result);
77 fn associated_item_from_trait_item_ref(
79 parent_def_id: LocalDefId,
80 parent_vis: &hir::Visibility<'_>,
81 trait_item_ref: &hir::TraitItemRef,
83 let def_id = tcx.hir().local_def_id(trait_item_ref.id.hir_id);
84 let (kind, has_self) = match trait_item_ref.kind {
85 hir::AssocItemKind::Const => (ty::AssocKind::Const, false),
86 hir::AssocItemKind::Fn { has_self } => (ty::AssocKind::Fn, has_self),
87 hir::AssocItemKind::Type => (ty::AssocKind::Type, false),
88 hir::AssocItemKind::OpaqueTy => bug!("only impls can have opaque types"),
92 ident: trait_item_ref.ident,
94 // Visibility of trait items is inherited from their traits.
95 vis: ty::Visibility::from_hir(parent_vis, trait_item_ref.id.hir_id, tcx),
96 defaultness: trait_item_ref.defaultness,
97 def_id: def_id.to_def_id(),
98 container: ty::TraitContainer(parent_def_id.to_def_id()),
99 fn_has_self_parameter: has_self,
103 fn associated_item_from_impl_item_ref(
105 parent_def_id: LocalDefId,
106 impl_item_ref: &hir::ImplItemRef<'_>,
108 let def_id = tcx.hir().local_def_id(impl_item_ref.id.hir_id);
109 let (kind, has_self) = match impl_item_ref.kind {
110 hir::AssocItemKind::Const => (ty::AssocKind::Const, false),
111 hir::AssocItemKind::Fn { has_self } => (ty::AssocKind::Fn, has_self),
112 hir::AssocItemKind::Type => (ty::AssocKind::Type, false),
113 hir::AssocItemKind::OpaqueTy => (ty::AssocKind::OpaqueTy, false),
117 ident: impl_item_ref.ident,
119 // Visibility of trait impl items doesn't matter.
120 vis: ty::Visibility::from_hir(&impl_item_ref.vis, impl_item_ref.id.hir_id, tcx),
121 defaultness: impl_item_ref.defaultness,
122 def_id: def_id.to_def_id(),
123 container: ty::ImplContainer(parent_def_id.to_def_id()),
124 fn_has_self_parameter: has_self,
128 fn associated_item(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssocItem {
129 let id = tcx.hir().as_local_hir_id(def_id.expect_local());
130 let parent_id = tcx.hir().get_parent_item(id);
131 let parent_def_id = tcx.hir().local_def_id(parent_id);
132 let parent_item = tcx.hir().expect_item(parent_id);
133 match parent_item.kind {
134 hir::ItemKind::Impl { ref items, .. } => {
135 if let Some(impl_item_ref) = items.iter().find(|i| i.id.hir_id == id) {
137 associated_item_from_impl_item_ref(tcx, parent_def_id, impl_item_ref);
138 debug_assert_eq!(assoc_item.def_id, def_id);
143 hir::ItemKind::Trait(.., ref trait_item_refs) => {
144 if let Some(trait_item_ref) = trait_item_refs.iter().find(|i| i.id.hir_id == id) {
145 let assoc_item = associated_item_from_trait_item_ref(
151 debug_assert_eq!(assoc_item.def_id, def_id);
161 "unexpected parent of trait or impl item or item not found: {:?}",
166 fn impl_defaultness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::Defaultness {
167 let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local());
168 let item = tcx.hir().expect_item(hir_id);
169 if let hir::ItemKind::Impl { defaultness, .. } = item.kind {
172 bug!("`impl_defaultness` called on {:?}", item);
176 /// Calculates the `Sized` constraint.
178 /// In fact, there are only a few options for the types in the constraint:
179 /// - an obviously-unsized type
180 /// - a type parameter or projection whose Sizedness can't be known
181 /// - a tuple of type parameters or projections, if there are multiple
183 /// - a Error, if a type contained itself. The representability
184 /// check should catch this case.
185 fn adt_sized_constraint(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AdtSizedConstraint<'_> {
186 let def = tcx.adt_def(def_id);
188 let result = tcx.mk_type_list(
191 .flat_map(|v| v.fields.last())
192 .flat_map(|f| sized_constraint_for_ty(tcx, def, tcx.type_of(f.did))),
195 debug!("adt_sized_constraint: {:?} => {:?}", def, result);
197 ty::AdtSizedConstraint(result)
200 fn associated_item_def_ids(tcx: TyCtxt<'_>, def_id: DefId) -> &[DefId] {
201 let id = tcx.hir().as_local_hir_id(def_id.expect_local());
202 let item = tcx.hir().expect_item(id);
204 hir::ItemKind::Trait(.., ref trait_item_refs) => tcx.arena.alloc_from_iter(
207 .map(|trait_item_ref| trait_item_ref.id)
208 .map(|id| tcx.hir().local_def_id(id.hir_id).to_def_id()),
210 hir::ItemKind::Impl { ref items, .. } => tcx.arena.alloc_from_iter(
213 .map(|impl_item_ref| impl_item_ref.id)
214 .map(|id| tcx.hir().local_def_id(id.hir_id).to_def_id()),
216 hir::ItemKind::TraitAlias(..) => &[],
217 _ => span_bug!(item.span, "associated_item_def_ids: not impl or trait"),
221 fn associated_items(tcx: TyCtxt<'_>, def_id: DefId) -> ty::AssociatedItems<'_> {
222 let items = tcx.associated_item_def_ids(def_id).iter().map(|did| tcx.associated_item(*did));
223 ty::AssociatedItems::new(items)
226 fn def_span(tcx: TyCtxt<'_>, def_id: DefId) -> Span {
227 tcx.hir().span_if_local(def_id).unwrap()
230 /// If the given `DefId` describes an item belonging to a trait,
231 /// returns the `DefId` of the trait that the trait item belongs to;
232 /// otherwise, returns `None`.
233 fn trait_of_item(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
234 tcx.opt_associated_item(def_id).and_then(|associated_item| match associated_item.container {
235 ty::TraitContainer(def_id) => Some(def_id),
236 ty::ImplContainer(_) => None,
240 /// See `ParamEnv` struct definition for details.
241 fn param_env(tcx: TyCtxt<'_>, def_id: DefId) -> ty::ParamEnv<'_> {
242 // The param_env of an impl Trait type is its defining function's param_env
243 if let Some(parent) = ty::is_impl_trait_defn(tcx, def_id) {
244 return param_env(tcx, parent);
246 // Compute the bounds on Self and the type parameters.
248 let ty::InstantiatedPredicates { predicates, .. } =
249 tcx.predicates_of(def_id).instantiate_identity(tcx);
251 // Finally, we have to normalize the bounds in the environment, in
252 // case they contain any associated type projections. This process
253 // can yield errors if the put in illegal associated types, like
254 // `<i32 as Foo>::Bar` where `i32` does not implement `Foo`. We
255 // report these errors right here; this doesn't actually feel
256 // right to me, because constructing the environment feels like a
257 // kind of a "idempotent" action, but I'm not sure where would be
258 // a better place. In practice, we construct environments for
259 // every fn once during type checking, and we'll abort if there
260 // are any errors at that point, so after type checking you can be
261 // sure that this will succeed without errors anyway.
263 let unnormalized_env = ty::ParamEnv::new(
264 tcx.intern_predicates(&predicates),
265 traits::Reveal::UserFacing,
266 tcx.sess.opts.debugging_opts.chalk.then_some(def_id),
271 .map(|def_id| tcx.hir().as_local_hir_id(def_id))
272 .map_or(hir::CRATE_HIR_ID, |id| {
273 tcx.hir().maybe_body_owned_by(id).map_or(id, |body| body.hir_id)
275 let cause = traits::ObligationCause::misc(tcx.def_span(def_id), body_id);
276 traits::normalize_param_env_or_error(tcx, def_id, unnormalized_env, cause)
279 fn crate_disambiguator(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CrateDisambiguator {
280 assert_eq!(crate_num, LOCAL_CRATE);
281 tcx.sess.local_crate_disambiguator()
284 fn original_crate_name(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Symbol {
285 assert_eq!(crate_num, LOCAL_CRATE);
289 fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {
290 tcx.index_hir(crate_num).crate_hash
293 fn instance_def_size_estimate<'tcx>(
295 instance_def: ty::InstanceDef<'tcx>,
300 InstanceDef::Item(..) | InstanceDef::DropGlue(..) => {
301 let mir = tcx.instance_mir(instance_def);
302 mir.basic_blocks().iter().map(|bb| bb.statements.len()).sum()
304 // Estimate the size of other compiler-generated shims to be 1.
309 /// If `def_id` is an issue 33140 hack impl, returns its self type; otherwise, returns `None`.
311 /// See [`ty::ImplOverlapKind::Issue33140`] for more details.
312 fn issue33140_self_ty(tcx: TyCtxt<'_>, def_id: DefId) -> Option<Ty<'_>> {
313 debug!("issue33140_self_ty({:?})", def_id);
316 .impl_trait_ref(def_id)
317 .unwrap_or_else(|| bug!("issue33140_self_ty called on inherent impl {:?}", def_id));
319 debug!("issue33140_self_ty({:?}), trait-ref={:?}", def_id, trait_ref);
321 let is_marker_like = tcx.impl_polarity(def_id) == ty::ImplPolarity::Positive
322 && tcx.associated_item_def_ids(trait_ref.def_id).is_empty();
324 // Check whether these impls would be ok for a marker trait.
326 debug!("issue33140_self_ty - not marker-like!");
330 // impl must be `impl Trait for dyn Marker1 + Marker2 + ...`
331 if trait_ref.substs.len() != 1 {
332 debug!("issue33140_self_ty - impl has substs!");
336 let predicates = tcx.predicates_of(def_id);
337 if predicates.parent.is_some() || !predicates.predicates.is_empty() {
338 debug!("issue33140_self_ty - impl has predicates {:?}!", predicates);
342 let self_ty = trait_ref.self_ty();
343 let self_ty_matches = match self_ty.kind {
344 ty::Dynamic(ref data, ty::ReStatic) => data.principal().is_none(),
349 debug!("issue33140_self_ty - MATCHES!");
352 debug!("issue33140_self_ty - non-matching self type");
357 /// Check if a function is async.
358 fn asyncness(tcx: TyCtxt<'_>, def_id: DefId) -> hir::IsAsync {
359 let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local());
361 let node = tcx.hir().get(hir_id);
363 let fn_like = hir_map::blocks::FnLikeNode::from_node(node).unwrap_or_else(|| {
364 bug!("asyncness: expected fn-like node but got `{:?}`", def_id);
370 pub fn provide(providers: &mut ty::query::Providers<'_>) {
371 *providers = ty::query::Providers {
374 associated_item_def_ids,
376 adt_sized_constraint,
383 instance_def_size_estimate,