]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/coherence/mod.rs
Auto merge of #40737 - nagisa:safe-slicing-strs, r=BurntSushi
[rust.git] / src / librustc_typeck / coherence / mod.rs
1 // Copyright 2014 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 // Coherence phase
12 //
13 // The job of the coherence phase of typechecking is to ensure that
14 // each trait has at most one implementation for each type. This is
15 // done by the orphan and overlap modules. Then we build up various
16 // mappings. That mapping code resides here.
17
18 use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
19 use rustc::ty::{self, TyCtxt, TypeFoldable};
20 use rustc::ty::maps::Providers;
21 use rustc::dep_graph::DepNode;
22
23 use syntax::ast;
24 use syntax_pos::DUMMY_SP;
25
26 mod builtin;
27 mod inherent_impls;
28 mod inherent_impls_overlap;
29 mod orphan;
30 mod overlap;
31 mod unsafety;
32
33 fn check_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, node_id: ast::NodeId) {
34     let impl_def_id = tcx.hir.local_def_id(node_id);
35
36     // If there are no traits, then this implementation must have a
37     // base type.
38
39     if let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) {
40         debug!("(checking implementation) adding impl for trait '{:?}', item '{}'",
41                 trait_ref,
42                 tcx.item_path_str(impl_def_id));
43
44         // Skip impls where one of the self type is an error type.
45         // This occurs with e.g. resolve failures (#30589).
46         if trait_ref.references_error() {
47             return;
48         }
49
50         enforce_trait_manually_implementable(tcx, impl_def_id, trait_ref.def_id);
51         let trait_def = tcx.lookup_trait_def(trait_ref.def_id);
52         trait_def.record_local_impl(tcx, impl_def_id, trait_ref);
53     }
54 }
55
56 fn enforce_trait_manually_implementable(tcx: TyCtxt, impl_def_id: DefId, trait_def_id: DefId) {
57     let did = Some(trait_def_id);
58     let li = &tcx.lang_items;
59
60     // Disallow *all* explicit impls of `Sized` and `Unsize` for now.
61     if did == li.sized_trait() {
62         let span = tcx.span_of_impl(impl_def_id).unwrap();
63         struct_span_err!(tcx.sess,
64                          span,
65                          E0322,
66                          "explicit impls for the `Sized` trait are not permitted")
67             .span_label(span, &format!("impl of 'Sized' not allowed"))
68             .emit();
69         return;
70     }
71
72     if did == li.unsize_trait() {
73         let span = tcx.span_of_impl(impl_def_id).unwrap();
74         span_err!(tcx.sess,
75                   span,
76                   E0328,
77                   "explicit impls for the `Unsize` trait are not permitted");
78         return;
79     }
80
81     if tcx.sess.features.borrow().unboxed_closures {
82         // the feature gate allows all Fn traits
83         return;
84     }
85
86     let trait_name = if did == li.fn_trait() {
87         "Fn"
88     } else if did == li.fn_mut_trait() {
89         "FnMut"
90     } else if did == li.fn_once_trait() {
91         "FnOnce"
92     } else {
93         return; // everything OK
94     };
95     let mut err = struct_span_err!(tcx.sess,
96                                    tcx.span_of_impl(impl_def_id).unwrap(),
97                                    E0183,
98                                    "manual implementations of `{}` are experimental",
99                                    trait_name);
100     help!(&mut err,
101           "add `#![feature(unboxed_closures)]` to the crate attributes to enable");
102     err.emit();
103 }
104
105 pub fn provide(providers: &mut Providers) {
106     use self::builtin::coerce_unsized_info;
107     use self::inherent_impls::{crate_inherent_impls, inherent_impls};
108     use self::inherent_impls_overlap::crate_inherent_impls_overlap_check;
109
110     *providers = Providers {
111         coherent_trait,
112         crate_inherent_impls,
113         inherent_impls,
114         crate_inherent_impls_overlap_check,
115         coerce_unsized_info,
116         ..*providers
117     };
118 }
119
120 fn coherent_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
121                             (_, def_id): (CrateNum, DefId)) {
122     tcx.populate_implementations_for_trait_if_necessary(def_id);
123
124     let impls = tcx.hir.trait_impls(def_id);
125     for &impl_id in impls {
126         check_impl(tcx, impl_id);
127     }
128     for &impl_id in impls {
129         overlap::check_impl(tcx, impl_id);
130     }
131     builtin::check_trait(tcx, def_id);
132 }
133
134 pub fn check_coherence<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
135     let _task = tcx.dep_graph.in_task(DepNode::Coherence);
136     for &trait_def_id in tcx.hir.krate().trait_impls.keys() {
137         ty::queries::coherent_trait::get(tcx, DUMMY_SP, (LOCAL_CRATE, trait_def_id));
138     }
139
140     unsafety::check(tcx);
141     orphan::check(tcx);
142     overlap::check_default_impls(tcx);
143
144     // these queries are executed for side-effects (error reporting):
145     ty::queries::crate_inherent_impls::get(tcx, DUMMY_SP, LOCAL_CRATE);
146     ty::queries::crate_inherent_impls_overlap_check::get(tcx, DUMMY_SP, LOCAL_CRATE);
147 }