]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/variance/mod.rs
A few cleanups and minor improvements to typeck
[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 parameters. See the [rustc guide]
12 //! chapter for more info.
13 //!
14 //! [rustc guide]: https://rust-lang-nursery.github.io/rustc-guide/variance.html
15
16 use arena;
17 use rustc::hir;
18 use hir::Node;
19 use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
20 use rustc::ty::{self, CrateVariancesMap, TyCtxt};
21 use rustc::ty::query::Providers;
22 use rustc_data_structures::sync::Lrc;
23
24 /// Defines the `TermsContext` basically houses an arena where we can
25 /// allocate terms.
26 mod terms;
27
28 /// Code to gather up constraints.
29 mod constraints;
30
31 /// Code to solve constraints and write out the results.
32 mod solve;
33
34 /// Code to write unit tests of variance.
35 pub mod test;
36
37 /// Code for transforming variances.
38 mod xform;
39
40 pub fn provide(providers: &mut Providers) {
41     *providers = Providers {
42         variances_of,
43         crate_variances,
44         ..*providers
45     };
46 }
47
48 fn crate_variances<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, crate_num: CrateNum)
49                              -> Lrc<CrateVariancesMap> {
50     assert_eq!(crate_num, LOCAL_CRATE);
51     let mut arena = arena::TypedArena::new();
52     let terms_cx = terms::determine_parameters_to_be_inferred(tcx, &mut arena);
53     let constraints_cx = constraints::add_constraints_from_crate(terms_cx);
54     Lrc::new(solve::solve_constraints(constraints_cx))
55 }
56
57 fn variances_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, item_def_id: DefId)
58                           -> Lrc<Vec<ty::Variance>> {
59     let id = tcx.hir.as_local_node_id(item_def_id).expect("expected local def-id");
60     let unsupported = || {
61         // Variance not relevant.
62         span_bug!(tcx.hir.span(id), "asked to compute variance for wrong kind of item")
63     };
64     match tcx.hir.get(id) {
65         Node::Item(item) => match item.node {
66             hir::ItemKind::Enum(..) |
67             hir::ItemKind::Struct(..) |
68             hir::ItemKind::Union(..) |
69             hir::ItemKind::Fn(..) => {}
70
71             _ => unsupported()
72         },
73
74         Node::TraitItem(item) => match item.node {
75             hir::TraitItemKind::Method(..) => {}
76
77             _ => unsupported()
78         },
79
80         Node::ImplItem(item) => match item.node {
81             hir::ImplItemKind::Method(..) => {}
82
83             _ => unsupported()
84         },
85
86         Node::ForeignItem(item) => match item.node {
87             hir::ForeignItemKind::Fn(..) => {}
88
89             _ => unsupported()
90         },
91
92         Node::Variant(_) | Node::StructCtor(_) => {}
93
94         _ => unsupported()
95     }
96
97     // Everything else must be inferred.
98
99     let crate_map = tcx.crate_variances(LOCAL_CRATE);
100     crate_map.variances.get(&item_def_id)
101                        .unwrap_or(&crate_map.empty_variance)
102                        .clone()
103 }
104