3 // The job of the coherence phase of typechecking is to ensure that
4 // each trait has at most one implementation for each type. This is
5 // done by the orphan and overlap modules. Then we build up various
6 // mappings. That mapping code resides here.
9 use rustc::ty::query::Providers;
10 use rustc::ty::{self, TyCtxt, TypeFoldable};
11 use rustc_error_codes::*;
12 use rustc_errors::struct_span_err;
13 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
18 mod inherent_impls_overlap;
22 fn check_impl(tcx: TyCtxt<'_>, hir_id: HirId) {
23 let impl_def_id = tcx.hir().local_def_id(hir_id);
25 // If there are no traits, then this implementation must have a
28 if let Some(trait_ref) = tcx.impl_trait_ref(impl_def_id) {
30 "(checking implementation) adding impl for trait '{:?}', item '{}'",
32 tcx.def_path_str(impl_def_id)
35 // Skip impls where one of the self type is an error type.
36 // This occurs with e.g., resolve failures (#30589).
37 if trait_ref.references_error() {
41 enforce_trait_manually_implementable(tcx, impl_def_id, trait_ref.def_id);
42 enforce_empty_impls_for_marker_traits(tcx, impl_def_id, trait_ref.def_id);
46 fn enforce_trait_manually_implementable(tcx: TyCtxt<'_>, impl_def_id: DefId, trait_def_id: DefId) {
47 let did = Some(trait_def_id);
48 let li = tcx.lang_items();
49 let span = tcx.sess.source_map().def_span(tcx.span_of_impl(impl_def_id).unwrap());
51 // Disallow *all* explicit impls of `Sized` and `Unsize` for now.
52 if did == li.sized_trait() {
57 "explicit impls for the `Sized` trait are not permitted"
59 .span_label(span, "impl of 'Sized' not allowed")
64 if did == li.unsize_trait() {
69 "explicit impls for the `Unsize` trait are not permitted"
71 .span_label(span, "impl of `Unsize` not allowed")
76 if tcx.features().unboxed_closures {
77 // the feature gate allows all Fn traits
81 let trait_name = if did == li.fn_trait() {
83 } else if did == li.fn_mut_trait() {
85 } else if did == li.fn_once_trait() {
88 return; // everything OK
94 "manual implementations of `{}` are experimental",
97 .span_label(span, format!("manual implementations of `{}` are experimental", trait_name))
98 .help("add `#![feature(unboxed_closures)]` to the crate attributes to enable")
102 /// We allow impls of marker traits to overlap, so they can't override impls
103 /// as that could make it ambiguous which associated item to use.
104 fn enforce_empty_impls_for_marker_traits(tcx: TyCtxt<'_>, impl_def_id: DefId, trait_def_id: DefId) {
105 if !tcx.trait_def(trait_def_id).is_marker {
109 if tcx.associated_item_def_ids(trait_def_id).is_empty() {
113 let span = tcx.sess.source_map().def_span(tcx.span_of_impl(impl_def_id).unwrap());
114 struct_span_err!(tcx.sess, span, E0715, "impls for marker traits cannot contain items").emit();
117 pub fn provide(providers: &mut Providers<'_>) {
118 use self::builtin::coerce_unsized_info;
119 use self::inherent_impls::{crate_inherent_impls, inherent_impls};
120 use self::inherent_impls_overlap::crate_inherent_impls_overlap_check;
122 *providers = Providers {
124 crate_inherent_impls,
126 crate_inherent_impls_overlap_check,
132 fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) {
133 let impls = tcx.hir().trait_impls(def_id);
134 for &impl_id in impls {
135 check_impl(tcx, impl_id);
137 for &impl_id in impls {
138 check_impl_overlap(tcx, impl_id);
140 builtin::check_trait(tcx, def_id);
143 pub fn check_coherence(tcx: TyCtxt<'_>) {
144 for &trait_def_id in tcx.hir().krate().trait_impls.keys() {
145 tcx.ensure().coherent_trait(trait_def_id);
148 tcx.sess.time("unsafety_checking", || unsafety::check(tcx));
149 tcx.sess.time("orphan_checking", || orphan::check(tcx));
151 // these queries are executed for side-effects (error reporting):
152 tcx.ensure().crate_inherent_impls(LOCAL_CRATE);
153 tcx.ensure().crate_inherent_impls_overlap_check(LOCAL_CRATE);
156 /// Overlap: no two impls for the same trait are implemented for the
157 /// same type. Likewise, no two inherent impls for a given type
158 /// constructor provide a method with the same name.
159 fn check_impl_overlap<'tcx>(tcx: TyCtxt<'tcx>, hir_id: HirId) {
160 let impl_def_id = tcx.hir().local_def_id(hir_id);
161 let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap();
162 let trait_def_id = trait_ref.def_id;
164 if trait_ref.references_error() {
165 debug!("coherence: skipping impl {:?} with error {:?}", impl_def_id, trait_ref);
169 // Trigger building the specialization graph for the trait of this impl.
170 // This will detect any overlap errors.
171 tcx.specialization_graph_of(trait_def_id);
173 // check for overlap with the automatic `impl Trait for Trait`
174 if let ty::Dynamic(ref data, ..) = trait_ref.self_ty().kind {
175 // This is something like impl Trait1 for Trait2. Illegal
176 // if Trait1 is a supertrait of Trait2 or Trait2 is not object safe.
178 let component_def_ids = data.iter().flat_map(|predicate| {
179 match predicate.skip_binder() {
180 ty::ExistentialPredicate::Trait(tr) => Some(tr.def_id),
181 ty::ExistentialPredicate::AutoTrait(def_id) => Some(*def_id),
182 // An associated type projection necessarily comes with
183 // an additional `Trait` requirement.
184 ty::ExistentialPredicate::Projection(..) => None,
188 for component_def_id in component_def_ids {
189 if !tcx.is_object_safe(component_def_id) {
190 // Without the 'object_safe_for_dispatch' feature this is an error
191 // which will be reported by wfcheck. Ignore it here.
192 // This is tested by `coherence-impl-trait-for-trait-object-safe.rs`.
193 // With the feature enabled, the trait is not implemented automatically,
196 let mut supertrait_def_ids = traits::supertrait_def_ids(tcx, component_def_id);
197 if supertrait_def_ids.any(|d| d == trait_def_id) {
198 let sp = tcx.sess.source_map().def_span(tcx.span_of_impl(impl_def_id).unwrap());
203 "the object type `{}` automatically implements the trait `{}`",
205 tcx.def_path_str(trait_def_id)
210 "`{}` automatically implements trait `{}`",
212 tcx.def_path_str(trait_def_id)