]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/variance/mod.rs
Replace Rc with Lrc for shared data
[rust.git] / src / librustc_typeck / variance / mod.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Module for inferring the variance of type and lifetime
12 //! parameters. See README.md for details.
13
14 use arena;
15 use rustc::hir;
16 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
17 use rustc::ty::{self, CrateVariancesMap, TyCtxt};
18 use rustc::ty::maps::Providers;
19 use rustc_data_structures::sync::Lrc;
20
21 /// Defines the `TermsContext` basically houses an arena where we can
22 /// allocate terms.
23 mod terms;
24
25 /// Code to gather up constraints.
26 mod constraints;
27
28 /// Code to solve constraints and write out the results.
29 mod solve;
30
31 /// Code to write unit tests of variance.
32 pub mod test;
33
34 /// Code for transforming variances.
35 mod xform;
36
37 pub fn provide(providers: &mut Providers) {
38     *providers = Providers {
39         variances_of,
40         crate_variances,
41         ..*providers
42     };
43 }
44
45 fn crate_variances<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum)
46                              -> Lrc<CrateVariancesMap> {
47     assert_eq!(crate_num, LOCAL_CRATE);
48     let mut arena = arena::TypedArena::new();
49     let terms_cx = terms::determine_parameters_to_be_inferred(tcx, &mut arena);
50     let constraints_cx = constraints::add_constraints_from_crate(terms_cx);
51     Lrc::new(solve::solve_constraints(constraints_cx))
52 }
53
54 fn variances_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
55                             -> Lrc<Vec<ty::Variance>> {
56     let id = tcx.hir.as_local_node_id(item_def_id).expect("expected local def-id");
57     let unsupported = || {
58         // Variance not relevant.
59         span_bug!(tcx.hir.span(id), "asked to compute variance for wrong kind of item")
60     };
61     match tcx.hir.get(id) {
62         hir::map::NodeItem(item) => match item.node {
63             hir::ItemEnum(..) |
64             hir::ItemStruct(..) |
65             hir::ItemUnion(..) |
66             hir::ItemFn(..) => {}
67
68             _ => unsupported()
69         },
70
71         hir::map::NodeTraitItem(item) => match item.node {
72             hir::TraitItemKind::Method(..) => {}
73
74             _ => unsupported()
75         },
76
77         hir::map::NodeImplItem(item) => match item.node {
78             hir::ImplItemKind::Method(..) => {}
79
80             _ => unsupported()
81         },
82
83         hir::map::NodeForeignItem(item) => match item.node {
84             hir::ForeignItemFn(..) => {}
85
86             _ => unsupported()
87         },
88
89         hir::map::NodeVariant(_) | hir::map::NodeStructCtor(_) => {}
90
91         _ => unsupported()
92     }
93
94     // Everything else must be inferred.
95
96     let crate_map = tcx.crate_variances(LOCAL_CRATE);
97     crate_map.variances.get(&item_def_id)
98                        .unwrap_or(&crate_map.empty_variance)
99                        .clone()
100 }
101