]> git.lizzy.rs Git - rust.git/blob - src/librustc_infer/traits/misc.rs
Move librustc/{traits,infer} to librustc_infer.
[rust.git] / src / librustc_infer / traits / misc.rs
1 //! Miscellaneous type-system utilities that are too small to deserve their own modules.
2
3 use crate::traits::{self, ObligationCause};
4 use crate::ty::{self, Ty, TyCtxt, TypeFoldable};
5
6 use rustc_hir as hir;
7
8 #[derive(Clone)]
9 pub enum CopyImplementationError<'tcx> {
10     InfrigingFields(Vec<&'tcx ty::FieldDef>),
11     NotAnAdt,
12     HasDestructor,
13 }
14
15 pub fn can_type_implement_copy(
16     tcx: TyCtxt<'tcx>,
17     param_env: ty::ParamEnv<'tcx>,
18     self_type: Ty<'tcx>,
19 ) -> Result<(), CopyImplementationError<'tcx>> {
20     // FIXME: (@jroesch) float this code up
21     tcx.infer_ctxt().enter(|infcx| {
22         let (adt, substs) = match self_type.kind {
23             // These types used to have a builtin impl.
24             // Now libcore provides that impl.
25             ty::Uint(_)
26             | ty::Int(_)
27             | ty::Bool
28             | ty::Float(_)
29             | ty::Char
30             | ty::RawPtr(..)
31             | ty::Never
32             | ty::Ref(_, _, hir::Mutability::Not) => return Ok(()),
33
34             ty::Adt(adt, substs) => (adt, substs),
35
36             _ => return Err(CopyImplementationError::NotAnAdt),
37         };
38
39         let mut infringing = Vec::new();
40         for variant in &adt.variants {
41             for field in &variant.fields {
42                 let ty = field.ty(tcx, substs);
43                 if ty.references_error() {
44                     continue;
45                 }
46                 let span = tcx.def_span(field.did);
47                 let cause = ObligationCause { span, ..ObligationCause::dummy() };
48                 let ctx = traits::FulfillmentContext::new();
49                 match traits::fully_normalize(&infcx, ctx, cause, param_env, &ty) {
50                     Ok(ty) => {
51                         if !infcx.type_is_copy_modulo_regions(param_env, ty, span) {
52                             infringing.push(field);
53                         }
54                     }
55                     Err(errors) => {
56                         infcx.report_fulfillment_errors(&errors, None, false);
57                     }
58                 };
59             }
60         }
61         if !infringing.is_empty() {
62             return Err(CopyImplementationError::InfrigingFields(infringing));
63         }
64         if adt.has_dtor(tcx) {
65             return Err(CopyImplementationError::HasDestructor);
66         }
67
68         Ok(())
69     })
70 }