]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/variance/mod.rs
Rollup merge of #61056 - euclio:custom-discriminant-error, r=estebank
[rust.git] / src / librustc_typeck / variance / mod.rs
1 //! Module for inferring the variance of type and lifetime parameters. See the [rustc guide]
2 //! chapter for more info.
3 //!
4 //! [rustc guide]: https://rust-lang.github.io/rustc-guide/variance.html
5
6 use arena;
7 use rustc::hir;
8 use hir::Node;
9 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
10 use rustc::ty::{self, CrateVariancesMap, TyCtxt};
11 use rustc::ty::query::Providers;
12
13 /// Defines the `TermsContext` basically houses an arena where we can
14 /// allocate terms.
15 mod terms;
16
17 /// Code to gather up constraints.
18 mod constraints;
19
20 /// Code to solve constraints and write out the results.
21 mod solve;
22
23 /// Code to write unit tests of variance.
24 pub mod test;
25
26 /// Code for transforming variances.
27 mod xform;
28
29 pub fn provide(providers: &mut Providers<'_>) {
30     *providers = Providers {
31         variances_of,
32         crate_variances,
33         ..*providers
34     };
35 }
36
37 fn crate_variances<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum)
38                              -> &'tcx CrateVariancesMap<'tcx> {
39     assert_eq!(crate_num, LOCAL_CRATE);
40     let mut arena = arena::TypedArena::default();
41     let terms_cx = terms::determine_parameters_to_be_inferred(tcx, &mut arena);
42     let constraints_cx = constraints::add_constraints_from_crate(terms_cx);
43     tcx.arena.alloc(solve::solve_constraints(constraints_cx))
44 }
45
46 fn variances_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
47                           -> &'tcx [ty::Variance] {
48     let id = tcx.hir().as_local_hir_id(item_def_id).expect("expected local def-id");
49     let unsupported = || {
50         // Variance not relevant.
51         span_bug!(tcx.hir().span_by_hir_id(id), "asked to compute variance for wrong kind of item")
52     };
53     match tcx.hir().get_by_hir_id(id) {
54         Node::Item(item) => match item.node {
55             hir::ItemKind::Enum(..) |
56             hir::ItemKind::Struct(..) |
57             hir::ItemKind::Union(..) |
58             hir::ItemKind::Fn(..) => {}
59
60             _ => unsupported()
61         },
62
63         Node::TraitItem(item) => match item.node {
64             hir::TraitItemKind::Method(..) => {}
65
66             _ => unsupported()
67         },
68
69         Node::ImplItem(item) => match item.node {
70             hir::ImplItemKind::Method(..) => {}
71
72             _ => unsupported()
73         },
74
75         Node::ForeignItem(item) => match item.node {
76             hir::ForeignItemKind::Fn(..) => {}
77
78             _ => unsupported()
79         },
80
81         Node::Variant(_) | Node::Ctor(..) => {}
82
83         _ => unsupported()
84     }
85
86     // Everything else must be inferred.
87
88     let crate_map = tcx.crate_variances(LOCAL_CRATE);
89     crate_map.variances.get(&item_def_id)
90                        .map(|p| *p)
91                        .unwrap_or(&[])
92 }