]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ty_utils/src/common_traits.rs
Rollup merge of #107700 - jyn514:tools-builder, r=Mark-Simulacrum
[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     let infcx = tcx.infer_ctxt().build();
33     traits::type_known_to_meet_bound_modulo_regions(&infcx, param_env, ty, trait_def_id, DUMMY_SP)
34 }
35
36 pub(crate) fn provide(providers: &mut ty::query::Providers) {
37     *providers = ty::query::Providers {
38         is_copy_raw,
39         is_sized_raw,
40         is_freeze_raw,
41         is_unpin_raw,
42         ..*providers
43     };
44 }