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.
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.
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.
18 use hir::def_id::{DefId, LOCAL_CRATE};
20 use rustc::ty::{self, TyCtxt, TypeFoldable};
21 use rustc::ty::maps::Providers;
27 mod inherent_impls_overlap;
31 fn check_impl<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, node_id: ast::NodeId) {
32 let impl_def_id = tcx.hir.local_def_id(node_id);
34 // If there are no traits, then this implementation must have a
37 if let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) {
38 debug!("(checking implementation) adding impl for trait '{:?}', item '{}'",
40 tcx.item_path_str(impl_def_id));
42 // Skip impls where one of the self type is an error type.
43 // This occurs with e.g. resolve failures (#30589).
44 if trait_ref.references_error() {
48 enforce_trait_manually_implementable(tcx, impl_def_id, trait_ref.def_id);
52 fn enforce_trait_manually_implementable(tcx: TyCtxt, impl_def_id: DefId, trait_def_id: DefId) {
53 let did = Some(trait_def_id);
54 let li = tcx.lang_items();
56 // Disallow *all* explicit impls of `Sized` and `Unsize` for now.
57 if did == li.sized_trait() {
58 let span = tcx.span_of_impl(impl_def_id).unwrap();
59 struct_span_err!(tcx.sess,
62 "explicit impls for the `Sized` trait are not permitted")
63 .span_label(span, "impl of 'Sized' not allowed")
68 if did == li.unsize_trait() {
69 let span = tcx.span_of_impl(impl_def_id).unwrap();
73 "explicit impls for the `Unsize` trait are not permitted");
77 if tcx.features().unboxed_closures {
78 // the feature gate allows all Fn traits
82 let trait_name = if did == li.fn_trait() {
84 } else if did == li.fn_mut_trait() {
86 } else if did == li.fn_once_trait() {
89 return; // everything OK
91 let mut err = struct_span_err!(tcx.sess,
92 tcx.span_of_impl(impl_def_id).unwrap(),
94 "manual implementations of `{}` are experimental",
97 "add `#![feature(unboxed_closures)]` to the crate attributes to enable");
101 pub fn provide(providers: &mut Providers) {
102 use self::builtin::coerce_unsized_info;
103 use self::inherent_impls::{crate_inherent_impls, inherent_impls};
104 use self::inherent_impls_overlap::crate_inherent_impls_overlap_check;
106 *providers = Providers {
108 crate_inherent_impls,
110 crate_inherent_impls_overlap_check,
116 fn coherent_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, def_id: DefId) {
117 let impls = tcx.hir.trait_impls(def_id);
118 for &impl_id in impls {
119 check_impl(tcx, impl_id);
121 for &impl_id in impls {
122 check_impl_overlap(tcx, impl_id);
124 builtin::check_trait(tcx, def_id);
127 pub fn check_coherence<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
128 for &trait_def_id in tcx.hir.krate().trait_impls.keys() {
129 ty::maps::queries::coherent_trait::ensure(tcx, trait_def_id);
132 unsafety::check(tcx);
135 // these queries are executed for side-effects (error reporting):
136 ty::maps::queries::crate_inherent_impls::ensure(tcx, LOCAL_CRATE);
137 ty::maps::queries::crate_inherent_impls_overlap_check::ensure(tcx, LOCAL_CRATE);
140 /// Overlap: No two impls for the same trait are implemented for the
141 /// same type. Likewise, no two inherent impls for a given type
142 /// constructor provide a method with the same name.
143 fn check_impl_overlap<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, node_id: ast::NodeId) {
144 let impl_def_id = tcx.hir.local_def_id(node_id);
145 let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
146 let trait_def_id = trait_ref.def_id;
148 if trait_ref.references_error() {
149 debug!("coherence: skipping impl {:?} with error {:?}",
150 impl_def_id, trait_ref);
154 // Trigger building the specialization graph for the trait of this impl.
155 // This will detect any overlap errors.
156 tcx.specialization_graph_of(trait_def_id);
158 // check for overlap with the automatic `impl Trait for Trait`
159 if let ty::TyDynamic(ref data, ..) = trait_ref.self_ty().sty {
160 // This is something like impl Trait1 for Trait2. Illegal
161 // if Trait1 is a supertrait of Trait2 or Trait2 is not object safe.
163 if data.principal().map_or(true, |p| !tcx.is_object_safe(p.def_id())) {
164 // This is an error, but it will be reported by wfcheck. Ignore it here.
165 // This is tested by `coherence-impl-trait-for-trait-object-safe.rs`.
167 let mut supertrait_def_ids =
168 traits::supertrait_def_ids(tcx,
169 data.principal().unwrap().def_id());
170 if supertrait_def_ids.any(|d| d == trait_def_id) {
172 tcx.span_of_impl(impl_def_id).unwrap(),
174 "the object type `{}` automatically \
175 implements the trait `{}`",
177 tcx.item_path_str(trait_def_id));