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 rustc_errors::codes::*;
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
use rustc_errors::struct_span_code_err;
|
2024-06-14 14:46:32 -04:00
|
|
|
use rustc_hir::LangItem;
|
2024-09-22 19:05:04 -04: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};
|
2024-02-23 12:11:11 +00:00
|
|
|
use rustc_session::parse::feature_err;
|
2024-09-22 19:05:04 -04:00
|
|
|
use rustc_span::{ErrorGuaranteed, sym};
|
2024-08-30 09:02:58 +10:00
|
|
|
use tracing::debug;
|
2019-11-11 22:46:56 +01:00
|
|
|
|
2024-02-10 13:54:28 +11:00
|
|
|
use crate::errors;
|
2024-07-29 08:13:50 +10: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
|
|
|
|
2024-01-23 15:23:22 +00:00
|
|
|
fn check_impl(
|
|
|
|
tcx: TyCtxt<'_>,
|
|
|
|
impl_def_id: LocalDefId,
|
|
|
|
trait_ref: ty::TraitRef<'_>,
|
2024-02-13 09:29:31 +00:00
|
|
|
trait_def: &ty::TraitDef,
|
2024-01-23 15:23:22 +00:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
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() {
|
2024-01-23 15:23:22 +00:00
|
|
|
return Ok(());
|
2012-07-10 17:33:16 -07:00
|
|
|
}
|
2020-02-08 00:27:07 +01:00
|
|
|
|
2024-02-13 09:29:31 +00:00
|
|
|
enforce_trait_manually_implementable(tcx, impl_def_id, trait_ref.def_id, trait_def)
|
|
|
|
.and(enforce_empty_impls_for_marker_traits(tcx, impl_def_id, trait_ref.def_id, trait_def))
|
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,
|
2024-02-13 09:29:31 +00:00
|
|
|
trait_def: &ty::TraitDef,
|
2024-01-23 15:23:22 +00:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
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
|
|
|
|
2024-09-11 13:32:53 -04:00
|
|
|
if tcx.is_lang_item(trait_def_id, LangItem::Freeze) && !tcx.features().freeze_impls {
|
|
|
|
feature_err(
|
|
|
|
&tcx.sess,
|
|
|
|
sym::freeze_impls,
|
|
|
|
impl_header_span,
|
|
|
|
"explicit impls for the `Freeze` trait are not permitted",
|
|
|
|
)
|
|
|
|
.with_span_label(impl_header_span, format!("impl of `Freeze` not allowed"))
|
|
|
|
.emit();
|
2024-02-23 12:11:11 +00:00
|
|
|
}
|
|
|
|
|
2022-11-12 23:37:52 +00:00
|
|
|
// Disallow *all* explicit impls of traits marked `#[rustc_deny_explicit_impl]`
|
2024-02-13 09:29:31 +00:00
|
|
|
if trait_def.deny_explicit_impl {
|
2022-11-12 23:37:52 +00:00
|
|
|
let trait_name = tcx.item_name(trait_def_id);
|
2024-01-04 09:08:36 +11:00
|
|
|
let mut err = struct_span_code_err!(
|
2023-12-18 22:21:37 +11:00
|
|
|
tcx.dcx(),
|
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.
|
2024-06-14 14:46:32 -04:00
|
|
|
if tcx.is_lang_item(trait_def_id, LangItem::Unsize) {
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
err.code(E0328);
|
2022-11-12 23:37:52 +00:00
|
|
|
}
|
2012-07-10 17:33:16 -07:00
|
|
|
|
2024-01-23 15:23:22 +00:00
|
|
|
return Err(err.emit());
|
2014-12-05 15:53:30 -08:00
|
|
|
}
|
|
|
|
|
2024-02-13 09:29:31 +00:00
|
|
|
if let ty::trait_def::TraitSpecializationKind::AlwaysApplicable = trait_def.specialization_kind
|
2020-02-08 17:56:25 +00:00
|
|
|
{
|
2024-02-10 13:54:28 +11:00
|
|
|
if !tcx.features().specialization
|
|
|
|
&& !tcx.features().min_specialization
|
|
|
|
&& !impl_header_span.allows_unstable(sym::specialization)
|
|
|
|
&& !impl_header_span.allows_unstable(sym::min_specialization)
|
|
|
|
{
|
2024-01-23 15:23:22 +00:00
|
|
|
return Err(tcx.dcx().emit_err(errors::SpecializationTrait { span: impl_header_span }));
|
2020-02-08 17:56:25 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-23 15:23:22 +00:00
|
|
|
Ok(())
|
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,
|
2024-02-13 09:29:31 +00:00
|
|
|
trait_def: &ty::TraitDef,
|
2024-01-23 15:23:22 +00:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
2024-02-13 09:29:31 +00:00
|
|
|
if !trait_def.is_marker {
|
2024-01-23 15:23:22 +00:00
|
|
|
return Ok(());
|
2018-08-25 04:33:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if tcx.associated_item_def_ids(trait_def_id).is_empty() {
|
2024-01-23 15:23:22 +00:00
|
|
|
return Ok(());
|
2018-08-25 04:33:58 -07:00
|
|
|
}
|
|
|
|
|
2024-01-23 15:23:22 +00:00
|
|
|
Err(struct_span_code_err!(
|
2023-12-18 22:21:37 +11:00
|
|
|
tcx.dcx(),
|
2022-07-04 17:23:24 +09:00
|
|
|
tcx.def_span(impl_def_id),
|
|
|
|
E0715,
|
|
|
|
"impls for marker traits cannot contain items"
|
|
|
|
)
|
2024-01-23 15:23:22 +00:00
|
|
|
.emit())
|
2018-08-25 04:33:58 -07:00
|
|
|
}
|
|
|
|
|
2024-08-27 13:14:50 +10:00
|
|
|
pub(crate) 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
|
|
|
|
2024-01-23 15:23:22 +00:00
|
|
|
fn coherent_trait(tcx: TyCtxt<'_>, def_id: DefId) -> Result<(), ErrorGuaranteed> {
|
2024-02-09 09:24:39 +00:00
|
|
|
// If there are no impls for the trait, then "all impls" are trivially coherent and we won't check anything
|
|
|
|
// anyway. Thus we bail out even before the specialization graph, avoiding the dep_graph edge.
|
|
|
|
let Some(impls) = tcx.all_local_trait_impls(()).get(&def_id) else { return Ok(()) };
|
2020-02-08 00:27:07 +01:00
|
|
|
// Trigger building the specialization graph for the trait. This will detect and report any
|
|
|
|
// overlap errors.
|
2024-01-23 15:23:22 +00:00
|
|
|
let mut res = tcx.ensure().specialization_graph_of(def_id);
|
2020-02-08 00:27:07 +01:00
|
|
|
|
2021-02-01 11:12:49 +01:00
|
|
|
for &impl_def_id in impls {
|
2024-03-05 20:19:05 +01:00
|
|
|
let trait_header = tcx.impl_trait_header(impl_def_id).unwrap();
|
|
|
|
let trait_ref = trait_header.trait_ref.instantiate_identity();
|
|
|
|
let trait_def = tcx.trait_def(trait_ref.def_id);
|
2020-02-08 00:27:07 +01:00
|
|
|
|
2024-03-05 20:19:05 +01:00
|
|
|
res = res.and(check_impl(tcx, impl_def_id, trait_ref, trait_def));
|
|
|
|
res = res.and(check_object_overlap(tcx, impl_def_id, trait_ref));
|
2017-02-19 14:46:29 +02:00
|
|
|
|
2024-02-13 09:29:31 +00:00
|
|
|
res = res.and(unsafety::check_item(tcx, impl_def_id, trait_header, trait_def));
|
2024-01-23 15:23:22 +00:00
|
|
|
res = res.and(tcx.ensure().orphan_check_impl(impl_def_id));
|
2024-02-13 09:38:13 +00:00
|
|
|
res = res.and(builtin::check_trait(tcx, def_id, impl_def_id, trait_header));
|
2017-02-19 14:46:29 +02:00
|
|
|
}
|
|
|
|
|
2024-02-09 09:38:22 +00:00
|
|
|
res
|
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>,
|
2024-01-23 15:23:22 +00:00
|
|
|
) -> Result<(), ErrorGuaranteed> {
|
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);
|
2024-01-23 15:23:22 +00:00
|
|
|
return Ok(());
|
2017-12-03 20:29:17 -02:00
|
|
|
}
|
|
|
|
|
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 {
|
2024-05-21 14:39:30 -04:00
|
|
|
if !tcx.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 {
|
2024-04-27 13:58:37 -04:00
|
|
|
let mut supertrait_def_ids = tcx.supertrait_def_ids(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);
|
2024-01-23 15:23:22 +00:00
|
|
|
return Err(struct_span_code_err!(
|
2023-12-18 22:21:37 +11:00
|
|
|
tcx.dcx(),
|
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
|
|
|
)
|
2024-01-09 09:08:49 +11:00
|
|
|
.with_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
|
|
|
),
|
|
|
|
)
|
2024-01-23 15:23:22 +00:00
|
|
|
.emit());
|
2018-12-04 13:28:06 +02:00
|
|
|
}
|
2017-12-03 20:29:17 -02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-01-23 15:23:22 +00:00
|
|
|
Ok(())
|
2017-12-03 20:29:17 -02:00
|
|
|
}
|