]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ty_utils/src/common_traits.rs
Rollup merge of #101740 - andrewpollack:add-ignore-fuchsia-ui-tests, r=tmandry
[rust.git] / compiler / rustc_ty_utils / src / common_traits.rs
1 //! Queries for checking whether a type implements one of a few common traits.
2
3 use rustc_hir::lang_items::LangItem;
4 use rustc_infer::infer::TyCtxtInferExt;
5 use rustc_middle::ty::{self, Ty, TyCtxt};
6 use rustc_span::DUMMY_SP;
7 use rustc_trait_selection::traits;
8
9 fn is_copy_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
10     is_item_raw(tcx, query, LangItem::Copy)
11 }
12
13 fn is_sized_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
14     is_item_raw(tcx, query, LangItem::Sized)
15 }
16
17 fn is_freeze_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
18     is_item_raw(tcx, query, LangItem::Freeze)
19 }
20
21 fn is_unpin_raw<'tcx>(tcx: TyCtxt<'tcx>, query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>) -> bool {
22     is_item_raw(tcx, query, LangItem::Unpin)
23 }
24
25 fn is_item_raw<'tcx>(
26     tcx: TyCtxt<'tcx>,
27     query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>,
28     item: LangItem,
29 ) -> bool {
30     let (param_env, ty) = query.into_parts();
31     let trait_def_id = tcx.require_lang_item(item, None);
32     tcx.infer_ctxt().enter(|infcx| {
33         traits::type_known_to_meet_bound_modulo_regions(
34             &infcx,
35             param_env,
36             ty,
37             trait_def_id,
38             DUMMY_SP,
39         )
40     })
41 }
42
43 pub(crate) fn provide(providers: &mut ty::query::Providers) {
44     *providers = ty::query::Providers {
45         is_copy_raw,
46         is_sized_raw,
47         is_freeze_raw,
48         is_unpin_raw,
49         ..*providers
50     };
51 }