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::{CrateNum, DefId, LOCAL_CRATE};
19 use rustc::ty::{TyCtxt, TypeFoldable};
20 use rustc::ty::maps::Providers;
26 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);
49 let trait_def = tcx.trait_def(trait_ref.def_id);
50 trait_def.record_local_impl(tcx, impl_def_id, trait_ref);
54 fn enforce_trait_manually_implementable(tcx: TyCtxt, impl_def_id: DefId, trait_def_id: DefId) {
55 let did = Some(trait_def_id);
56 let li = &tcx.lang_items;
58 // Disallow *all* explicit impls of `Sized` and `Unsize` for now.
59 if did == li.sized_trait() {
60 let span = tcx.span_of_impl(impl_def_id).unwrap();
61 struct_span_err!(tcx.sess,
64 "explicit impls for the `Sized` trait are not permitted")
65 .span_label(span, &format!("impl of 'Sized' not allowed"))
70 if did == li.unsize_trait() {
71 let span = tcx.span_of_impl(impl_def_id).unwrap();
75 "explicit impls for the `Unsize` trait are not permitted");
79 if tcx.sess.features.borrow().unboxed_closures {
80 // the feature gate allows all Fn traits
84 let trait_name = if did == li.fn_trait() {
86 } else if did == li.fn_mut_trait() {
88 } else if did == li.fn_once_trait() {
91 return; // everything OK
93 let mut err = struct_span_err!(tcx.sess,
94 tcx.span_of_impl(impl_def_id).unwrap(),
96 "manual implementations of `{}` are experimental",
99 "add `#![feature(unboxed_closures)]` to the crate attributes to enable");
103 pub fn provide(providers: &mut Providers) {
104 use self::builtin::coerce_unsized_info;
105 use self::inherent_impls::{crate_inherent_impls, inherent_impls};
106 use self::inherent_impls_overlap::crate_inherent_impls_overlap_check;
108 *providers = Providers {
110 crate_inherent_impls,
112 crate_inherent_impls_overlap_check,
118 fn coherent_trait<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
119 (_, def_id): (CrateNum, DefId)) {
120 tcx.populate_implementations_for_trait_if_necessary(def_id);
122 let impls = tcx.hir.trait_impls(def_id);
123 for &impl_id in impls {
124 check_impl(tcx, impl_id);
126 for &impl_id in impls {
127 overlap::check_impl(tcx, impl_id);
129 builtin::check_trait(tcx, def_id);
132 pub fn check_coherence<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
133 for &trait_def_id in tcx.hir.krate().trait_impls.keys() {
134 tcx.coherent_trait((LOCAL_CRATE, trait_def_id));
137 unsafety::check(tcx);
139 overlap::check_default_impls(tcx);
141 // these queries are executed for side-effects (error reporting):
142 tcx.crate_inherent_impls(LOCAL_CRATE);
143 tcx.crate_inherent_impls_overlap_check(LOCAL_CRATE);