2012-07-10 17:33:16 -07:00
|
|
|
// Coherence phase
|
|
|
|
//
|
2014-09-12 10:54:08 -04:00
|
|
|
// The job of the coherence phase of typechecking is to ensure that
|
|
|
|
// each trait has at most one implementation for each type. This is
|
|
|
|
// done by the orphan and overlap modules. Then we build up various
|
|
|
|
// mappings. That mapping code resides here.
|
2012-07-10 17:33:16 -07:00
|
|
|
|
2023-04-10 16:04:14 +01:00
|
|
|
use crate::errors;
|
2022-11-12 23:37:52 +00:00
|
|
|
use rustc_errors::{error_code, struct_span_err};
|
2021-05-11 13:39:19 +02:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId};
|
2023-05-15 06:24:45 +02:00
|
|
|
use rustc_middle::query::Providers;
|
2023-02-22 02:18:40 +00:00
|
|
|
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
|
2020-02-11 21:19:40 +01:00
|
|
|
use rustc_trait_selection::traits;
|
2019-11-11 22:46:56 +01:00
|
|
|
|
2016-12-04 00:28:30 +02:00
|
|
|
mod builtin;
|
2017-03-20 18:35:16 -04:00
|
|
|
mod inherent_impls;
|
|
|
|
mod inherent_impls_overlap;
|
2014-09-12 10:54:08 -04:00
|
|
|
mod orphan;
|
2014-12-10 10:59:20 -05:00
|
|
|
mod unsafety;
|
2012-12-13 16:13:37 -08:00
|
|
|
|
2020-04-09 09:43:00 +01:00
|
|
|
fn check_impl(tcx: TyCtxt<'_>, impl_def_id: LocalDefId, trait_ref: ty::TraitRef<'_>) {
|
2020-02-08 00:27:07 +01:00
|
|
|
debug!(
|
|
|
|
"(checking implementation) adding impl for trait '{:?}', item '{}'",
|
|
|
|
trait_ref,
|
2023-02-16 09:25:11 +00:00
|
|
|
tcx.def_path_str(impl_def_id)
|
2020-02-08 00:27:07 +01:00
|
|
|
);
|
2017-02-03 21:13:59 -05:00
|
|
|
|
2020-02-08 00:27:07 +01:00
|
|
|
// Skip impls where one of the self type is an error type.
|
|
|
|
// This occurs with e.g., resolve failures (#30589).
|
|
|
|
if trait_ref.references_error() {
|
|
|
|
return;
|
2012-07-10 17:33:16 -07:00
|
|
|
}
|
2020-02-08 00:27:07 +01:00
|
|
|
|
|
|
|
enforce_trait_manually_implementable(tcx, impl_def_id, trait_ref.def_id);
|
|
|
|
enforce_empty_impls_for_marker_traits(tcx, impl_def_id, trait_ref.def_id);
|
2017-02-19 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
2020-04-09 09:43:00 +01:00
|
|
|
fn enforce_trait_manually_implementable(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
impl_def_id: LocalDefId,
|
|
|
|
trait_def_id: DefId,
|
|
|
|
) {
|
2022-07-04 17:23:24 +09:00
|
|
|
let impl_header_span = tcx.def_span(impl_def_id);
|
2012-07-10 17:33:16 -07:00
|
|
|
|
2022-11-12 23:37:52 +00:00
|
|
|
// Disallow *all* explicit impls of traits marked `#[rustc_deny_explicit_impl]`
|
2023-06-13 22:31:25 +00:00
|
|
|
if tcx.trait_def(trait_def_id).deny_explicit_impl {
|
2022-11-12 23:37:52 +00:00
|
|
|
let trait_name = tcx.item_name(trait_def_id);
|
|
|
|
let mut err = struct_span_err!(
|
2020-10-19 15:38:11 +02:00
|
|
|
tcx.sess,
|
2022-07-04 17:23:24 +09:00
|
|
|
impl_header_span,
|
2020-10-19 15:38:11 +02:00
|
|
|
E0322,
|
2022-11-12 23:37:52 +00:00
|
|
|
"explicit impls for the `{trait_name}` trait are not permitted"
|
|
|
|
);
|
|
|
|
err.span_label(impl_header_span, format!("impl of `{trait_name}` not allowed"));
|
2020-04-05 19:57:32 +02:00
|
|
|
|
2022-11-12 23:37:52 +00:00
|
|
|
// Maintain explicit error code for `Unsize`, since it has a useful
|
|
|
|
// explanation about using `CoerceUnsized` instead.
|
|
|
|
if Some(trait_def_id) == tcx.lang_items().unsize_trait() {
|
|
|
|
err.code(error_code!(E0328));
|
|
|
|
}
|
2012-07-10 17:33:16 -07:00
|
|
|
|
2022-11-12 23:37:52 +00:00
|
|
|
err.emit();
|
2016-09-19 09:16:25 +05:30
|
|
|
return;
|
2014-12-05 15:53:30 -08:00
|
|
|
}
|
|
|
|
|
2020-02-08 17:56:25 +00:00
|
|
|
if let ty::trait_def::TraitSpecializationKind::AlwaysApplicable =
|
|
|
|
tcx.trait_def(trait_def_id).specialization_kind
|
|
|
|
{
|
|
|
|
if !tcx.features().specialization && !tcx.features().min_specialization {
|
2023-04-10 16:04:14 +01:00
|
|
|
tcx.sess.emit_err(errors::SpecializationTrait { span: impl_header_span });
|
2020-02-08 17:56:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2014-12-05 15:53:30 -08:00
|
|
|
}
|
|
|
|
|
2018-08-25 04:33:58 -07:00
|
|
|
/// We allow impls of marker traits to overlap, so they can't override impls
|
|
|
|
/// as that could make it ambiguous which associated item to use.
|
2020-04-09 09:43:00 +01:00
|
|
|
fn enforce_empty_impls_for_marker_traits(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
impl_def_id: LocalDefId,
|
|
|
|
trait_def_id: DefId,
|
|
|
|
) {
|
2018-08-25 04:33:58 -07:00
|
|
|
if !tcx.trait_def(trait_def_id).is_marker {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if tcx.associated_item_def_ids(trait_def_id).is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-04 17:23:24 +09:00
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
|
|
|
tcx.def_span(impl_def_id),
|
|
|
|
E0715,
|
|
|
|
"impls for marker traits cannot contain items"
|
|
|
|
)
|
|
|
|
.emit();
|
2018-08-25 04:33:58 -07:00
|
|
|
}
|
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2017-03-17 16:17:45 -04:00
|
|
|
use self::builtin::coerce_unsized_info;
|
2022-03-15 16:30:30 +01:00
|
|
|
use self::inherent_impls::{crate_incoherent_impls, crate_inherent_impls, inherent_impls};
|
2017-03-20 18:35:16 -04:00
|
|
|
use self::inherent_impls_overlap::crate_inherent_impls_overlap_check;
|
2021-05-09 20:53:13 +02:00
|
|
|
use self::orphan::orphan_check_impl;
|
2017-03-17 16:17:45 -04:00
|
|
|
|
2017-02-19 14:46:29 +02:00
|
|
|
*providers = Providers {
|
|
|
|
coherent_trait,
|
2017-03-20 18:35:16 -04:00
|
|
|
crate_inherent_impls,
|
2022-03-15 16:30:30 +01:00
|
|
|
crate_incoherent_impls,
|
2017-03-20 18:35:16 -04:00
|
|
|
inherent_impls,
|
|
|
|
crate_inherent_impls_overlap_check,
|
2017-03-17 16:17:45 -04:00
|
|
|
coerce_unsized_info,
|
2021-05-09 20:53:13 +02:00
|
|
|
orphan_check_impl,
|
2017-02-19 14:46:29 +02:00
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|
2017-02-03 21:13:59 -05:00
|
|
|
|
2019-06-21 20:05:14 +02:00
|
|
|
fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) {
|
2020-02-08 00:27:07 +01:00
|
|
|
// Trigger building the specialization graph for the trait. This will detect and report any
|
|
|
|
// overlap errors.
|
2020-05-01 11:32:20 +02:00
|
|
|
tcx.ensure().specialization_graph_of(def_id);
|
2020-02-08 00:27:07 +01:00
|
|
|
|
2018-12-04 13:45:36 +01:00
|
|
|
let impls = tcx.hir().trait_impls(def_id);
|
2021-02-01 11:12:49 +01:00
|
|
|
for &impl_def_id in impls {
|
2023-07-11 22:35:29 +01:00
|
|
|
let trait_ref = tcx.impl_trait_ref(impl_def_id).unwrap().instantiate_identity();
|
2020-02-08 00:27:07 +01:00
|
|
|
|
|
|
|
check_impl(tcx, impl_def_id, trait_ref);
|
|
|
|
check_object_overlap(tcx, impl_def_id, trait_ref);
|
2017-02-19 14:46:29 +02:00
|
|
|
|
2023-02-06 08:31:19 +01:00
|
|
|
unsafety::check_item(tcx, impl_def_id);
|
|
|
|
tcx.ensure().orphan_check_impl(impl_def_id);
|
2017-02-19 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
2021-05-09 20:53:13 +02:00
|
|
|
builtin::check_trait(tcx, def_id);
|
2012-07-10 17:33:16 -07:00
|
|
|
}
|
2017-12-03 20:29:17 -02:00
|
|
|
|
2020-02-08 00:27:07 +01:00
|
|
|
/// Checks whether an impl overlaps with the automatic `impl Trait for dyn Trait`.
|
|
|
|
fn check_object_overlap<'tcx>(
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2020-04-09 09:43:00 +01:00
|
|
|
impl_def_id: LocalDefId,
|
2020-02-08 00:27:07 +01:00
|
|
|
trait_ref: ty::TraitRef<'tcx>,
|
|
|
|
) {
|
2017-12-03 20:29:17 -02:00
|
|
|
let trait_def_id = trait_ref.def_id;
|
|
|
|
|
|
|
|
if trait_ref.references_error() {
|
|
|
|
debug!("coherence: skipping impl {:?} with error {:?}", impl_def_id, trait_ref);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-02-08 00:27:07 +01:00
|
|
|
// check for overlap with the automatic `impl Trait for dyn Trait`
|
2021-09-30 19:38:50 +02:00
|
|
|
if let ty::Dynamic(data, ..) = trait_ref.self_ty().kind() {
|
2017-12-03 20:29:17 -02:00
|
|
|
// This is something like impl Trait1 for Trait2. Illegal
|
|
|
|
// if Trait1 is a supertrait of Trait2 or Trait2 is not object safe.
|
|
|
|
|
2019-01-05 16:19:34 +02:00
|
|
|
let component_def_ids = data.iter().flat_map(|predicate| {
|
|
|
|
match predicate.skip_binder() {
|
|
|
|
ty::ExistentialPredicate::Trait(tr) => Some(tr.def_id),
|
2020-06-24 23:40:33 +02:00
|
|
|
ty::ExistentialPredicate::AutoTrait(def_id) => Some(def_id),
|
2019-01-05 16:19:34 +02:00
|
|
|
// An associated type projection necessarily comes with
|
|
|
|
// an additional `Trait` requirement.
|
|
|
|
ty::ExistentialPredicate::Projection(..) => None,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for component_def_id in component_def_ids {
|
2023-01-28 15:07:21 +00:00
|
|
|
if !tcx.check_is_object_safe(component_def_id) {
|
2019-01-08 22:14:04 +01:00
|
|
|
// Without the 'object_safe_for_dispatch' feature this is an error
|
2022-11-16 20:34:16 +00:00
|
|
|
// which will be reported by wfcheck. Ignore it here.
|
2018-12-04 13:28:06 +02:00
|
|
|
// This is tested by `coherence-impl-trait-for-trait-object-safe.rs`.
|
2019-01-08 22:14:04 +01:00
|
|
|
// With the feature enabled, the trait is not implemented automatically,
|
|
|
|
// so this is valid.
|
2018-12-04 13:28:06 +02:00
|
|
|
} else {
|
2019-01-05 16:19:34 +02:00
|
|
|
let mut supertrait_def_ids = traits::supertrait_def_ids(tcx, component_def_id);
|
2018-12-04 13:28:06 +02:00
|
|
|
if supertrait_def_ids.any(|d| d == trait_def_id) {
|
2022-07-04 17:23:24 +09:00
|
|
|
let span = tcx.def_span(impl_def_id);
|
2018-12-04 13:28:06 +02:00
|
|
|
struct_span_err!(
|
|
|
|
tcx.sess,
|
2020-02-08 00:45:03 +01:00
|
|
|
span,
|
2018-12-04 13:28:06 +02:00
|
|
|
E0371,
|
|
|
|
"the object type `{}` automatically implements the trait `{}`",
|
|
|
|
trait_ref.self_ty(),
|
2018-12-19 12:31:35 +02:00
|
|
|
tcx.def_path_str(trait_def_id)
|
2019-12-22 17:42:04 -05:00
|
|
|
)
|
2018-12-04 13:28:06 +02:00
|
|
|
.span_label(
|
2020-02-08 00:45:03 +01:00
|
|
|
span,
|
2018-12-04 13:28:06 +02:00
|
|
|
format!(
|
|
|
|
"`{}` automatically implements trait `{}`",
|
|
|
|
trait_ref.self_ty(),
|
2018-12-19 12:31:35 +02:00
|
|
|
tcx.def_path_str(trait_def_id)
|
2019-12-22 17:42:04 -05:00
|
|
|
),
|
|
|
|
)
|
2018-12-04 13:28:06 +02:00
|
|
|
.emit();
|
|
|
|
}
|
2017-12-03 20:29:17 -02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|