Auto merge of #108006 - cjgillot:def-impl, r=oli-obk
Avoid accessing HIR when it can be avoided Experiment to see if it helps some incremental cases. Will be rebased once https://github.com/rust-lang/rust/pull/107942 gets merged. r? `@ghost`
This commit is contained in:
commit
2d14db321b
43 changed files with 316 additions and 777 deletions
|
@ -21,7 +21,9 @@ use rustc_middle::middle::stability::EvalResult;
|
|||
use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
|
||||
use rustc_middle::ty::subst::GenericArgKind;
|
||||
use rustc_middle::ty::util::{Discr, IntTypeExt};
|
||||
use rustc_middle::ty::{self, AdtDef, ParamEnv, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable};
|
||||
use rustc_middle::ty::{
|
||||
self, AdtDef, DefIdTree, ParamEnv, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
|
||||
};
|
||||
use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVENTIONS};
|
||||
use rustc_span::symbol::sym;
|
||||
use rustc_span::{self, Span};
|
||||
|
@ -174,16 +176,8 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
|
|||
Ok(l) => l,
|
||||
// Foreign statics that overflow their allowed size should emit an error
|
||||
Err(LayoutError::SizeOverflow(_))
|
||||
if {
|
||||
let node = tcx.hir().get_by_def_id(def_id);
|
||||
matches!(
|
||||
node,
|
||||
hir::Node::ForeignItem(hir::ForeignItem {
|
||||
kind: hir::ForeignItemKind::Static(..),
|
||||
..
|
||||
})
|
||||
)
|
||||
} =>
|
||||
if matches!(tcx.def_kind(def_id), DefKind::Static(_)
|
||||
if tcx.def_kind(tcx.local_parent(def_id)) == DefKind::ForeignMod) =>
|
||||
{
|
||||
tcx.sess
|
||||
.struct_span_err(span, "extern static is too large for the current architecture")
|
||||
|
@ -215,7 +209,7 @@ fn check_static_inhabited(tcx: TyCtxt<'_>, def_id: LocalDefId) {
|
|||
fn check_opaque(tcx: TyCtxt<'_>, id: hir::ItemId) {
|
||||
let item = tcx.hir().item(id);
|
||||
let hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) = item.kind else {
|
||||
tcx.sess.delay_span_bug(tcx.hir().span(id.hir_id()), "expected opaque item");
|
||||
tcx.sess.delay_span_bug(item.span, "expected opaque item");
|
||||
return;
|
||||
};
|
||||
|
||||
|
@ -529,45 +523,34 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
|
|||
check_enum(tcx, id.owner_id.def_id);
|
||||
}
|
||||
DefKind::Fn => {} // entirely within check_item_body
|
||||
DefKind::Impl => {
|
||||
let it = tcx.hir().item(id);
|
||||
let hir::ItemKind::Impl(impl_) = it.kind else { return };
|
||||
debug!("ItemKind::Impl {} with id {:?}", it.ident, it.owner_id);
|
||||
if let Some(impl_trait_ref) = tcx.impl_trait_ref(it.owner_id) {
|
||||
DefKind::Impl { of_trait } => {
|
||||
if of_trait && let Some(impl_trait_ref) = tcx.impl_trait_ref(id.owner_id) {
|
||||
check_impl_items_against_trait(
|
||||
tcx,
|
||||
it.span,
|
||||
it.owner_id.def_id,
|
||||
id.owner_id.def_id,
|
||||
impl_trait_ref.subst_identity(),
|
||||
&impl_.items,
|
||||
);
|
||||
check_on_unimplemented(tcx, it);
|
||||
check_on_unimplemented(tcx, id);
|
||||
}
|
||||
}
|
||||
DefKind::Trait => {
|
||||
let it = tcx.hir().item(id);
|
||||
let hir::ItemKind::Trait(_, _, _, _, items) = it.kind else {
|
||||
return;
|
||||
};
|
||||
check_on_unimplemented(tcx, it);
|
||||
let assoc_items = tcx.associated_items(id.owner_id);
|
||||
check_on_unimplemented(tcx, id);
|
||||
|
||||
for item in items.iter() {
|
||||
let item = tcx.hir().trait_item(item.id);
|
||||
match &item.kind {
|
||||
hir::TraitItemKind::Fn(sig, _) => {
|
||||
let abi = sig.header.abi;
|
||||
fn_maybe_err(tcx, item.ident.span, abi);
|
||||
for assoc_item in assoc_items.in_definition_order() {
|
||||
match assoc_item.kind {
|
||||
ty::AssocKind::Fn => {
|
||||
let abi = tcx.fn_sig(assoc_item.def_id).skip_binder().abi();
|
||||
fn_maybe_err(tcx, assoc_item.ident(tcx).span, abi);
|
||||
}
|
||||
hir::TraitItemKind::Type(.., Some(default)) => {
|
||||
let assoc_item = tcx.associated_item(item.owner_id);
|
||||
ty::AssocKind::Type if assoc_item.defaultness(tcx).has_value() => {
|
||||
let trait_substs =
|
||||
InternalSubsts::identity_for_item(tcx, it.owner_id.to_def_id());
|
||||
InternalSubsts::identity_for_item(tcx, id.owner_id.to_def_id());
|
||||
let _: Result<_, rustc_errors::ErrorGuaranteed> = check_type_bounds(
|
||||
tcx,
|
||||
assoc_item,
|
||||
assoc_item,
|
||||
default.span,
|
||||
tcx.mk_trait_ref(it.owner_id.to_def_id(), trait_substs),
|
||||
tcx.mk_trait_ref(id.owner_id.to_def_id(), trait_substs),
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
|
@ -679,7 +662,7 @@ fn check_item_type(tcx: TyCtxt<'_>, id: hir::ItemId) {
|
|||
}
|
||||
}
|
||||
|
||||
pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {
|
||||
pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, item: hir::ItemId) {
|
||||
// an error would be reported if this fails.
|
||||
let _ = OnUnimplementedDirective::of_item(tcx, item.owner_id.to_def_id());
|
||||
}
|
||||
|
@ -689,7 +672,7 @@ pub(super) fn check_specialization_validity<'tcx>(
|
|||
trait_def: &ty::TraitDef,
|
||||
trait_item: &ty::AssocItem,
|
||||
impl_id: DefId,
|
||||
impl_item: &hir::ImplItemRef,
|
||||
impl_item: DefId,
|
||||
) {
|
||||
let Ok(ancestors) = trait_def.ancestors(tcx, impl_id) else { return };
|
||||
let mut ancestor_impls = ancestors.skip(1).filter_map(|parent| {
|
||||
|
@ -735,10 +718,8 @@ pub(super) fn check_specialization_validity<'tcx>(
|
|||
|
||||
fn check_impl_items_against_trait<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
full_impl_span: Span,
|
||||
impl_id: LocalDefId,
|
||||
impl_trait_ref: ty::TraitRef<'tcx>,
|
||||
impl_item_refs: &[hir::ImplItemRef],
|
||||
) {
|
||||
// If the trait reference itself is erroneous (so the compilation is going
|
||||
// to fail), skip checking the items here -- the `impl_item` table in `tcx`
|
||||
|
@ -747,12 +728,14 @@ fn check_impl_items_against_trait<'tcx>(
|
|||
return;
|
||||
}
|
||||
|
||||
let impl_item_refs = tcx.associated_item_def_ids(impl_id);
|
||||
|
||||
// Negative impls are not expected to have any items
|
||||
match tcx.impl_polarity(impl_id) {
|
||||
ty::ImplPolarity::Reservation | ty::ImplPolarity::Positive => {}
|
||||
ty::ImplPolarity::Negative => {
|
||||
if let [first_item_ref, ..] = impl_item_refs {
|
||||
let first_item_span = tcx.hir().impl_item(first_item_ref.id).span;
|
||||
let first_item_span = tcx.def_span(first_item_ref);
|
||||
struct_span_err!(
|
||||
tcx.sess,
|
||||
first_item_span,
|
||||
|
@ -767,43 +750,27 @@ fn check_impl_items_against_trait<'tcx>(
|
|||
|
||||
let trait_def = tcx.trait_def(impl_trait_ref.def_id);
|
||||
|
||||
for impl_item in impl_item_refs {
|
||||
let ty_impl_item = tcx.associated_item(impl_item.id.owner_id);
|
||||
for &impl_item in impl_item_refs {
|
||||
let ty_impl_item = tcx.associated_item(impl_item);
|
||||
let ty_trait_item = if let Some(trait_item_id) = ty_impl_item.trait_item_def_id {
|
||||
tcx.associated_item(trait_item_id)
|
||||
} else {
|
||||
// Checked in `associated_item`.
|
||||
tcx.sess.delay_span_bug(impl_item.span, "missing associated item in trait");
|
||||
tcx.sess.delay_span_bug(tcx.def_span(impl_item), "missing associated item in trait");
|
||||
continue;
|
||||
};
|
||||
let impl_item_full = tcx.hir().impl_item(impl_item.id);
|
||||
match impl_item_full.kind {
|
||||
hir::ImplItemKind::Const(..) => {
|
||||
match ty_impl_item.kind {
|
||||
ty::AssocKind::Const => {
|
||||
let _ = tcx.compare_impl_const((
|
||||
impl_item.id.owner_id.def_id,
|
||||
impl_item.expect_local(),
|
||||
ty_impl_item.trait_item_def_id.unwrap(),
|
||||
));
|
||||
}
|
||||
hir::ImplItemKind::Fn(..) => {
|
||||
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
|
||||
compare_impl_method(
|
||||
tcx,
|
||||
&ty_impl_item,
|
||||
&ty_trait_item,
|
||||
impl_trait_ref,
|
||||
opt_trait_span,
|
||||
);
|
||||
ty::AssocKind::Fn => {
|
||||
compare_impl_method(tcx, &ty_impl_item, &ty_trait_item, impl_trait_ref);
|
||||
}
|
||||
hir::ImplItemKind::Type(impl_ty) => {
|
||||
let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
|
||||
compare_impl_ty(
|
||||
tcx,
|
||||
&ty_impl_item,
|
||||
impl_ty.span,
|
||||
&ty_trait_item,
|
||||
impl_trait_ref,
|
||||
opt_trait_span,
|
||||
);
|
||||
ty::AssocKind::Type => {
|
||||
compare_impl_ty(tcx, &ty_impl_item, &ty_trait_item, impl_trait_ref);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -838,6 +805,8 @@ fn check_impl_items_against_trait<'tcx>(
|
|||
.map_or(false, |node_item| !node_item.defining_node.is_from_trait());
|
||||
|
||||
if !is_implemented_here {
|
||||
let full_impl_span =
|
||||
tcx.hir().span_with_body(tcx.hir().local_def_id_to_hir_id(impl_id));
|
||||
match tcx.eval_default_body_stability(trait_item_id, full_impl_span) {
|
||||
EvalResult::Deny { feature, reason, issue, .. } => default_body_is_unstable(
|
||||
tcx,
|
||||
|
@ -864,6 +833,8 @@ fn check_impl_items_against_trait<'tcx>(
|
|||
}
|
||||
|
||||
if !missing_items.is_empty() {
|
||||
let full_impl_span =
|
||||
tcx.hir().span_with_body(tcx.hir().local_def_id_to_hir_id(impl_id));
|
||||
missing_items_err(tcx, tcx.def_span(impl_id), &missing_items, full_impl_span);
|
||||
}
|
||||
|
||||
|
|
|
@ -33,7 +33,6 @@ use std::iter;
|
|||
/// # Parameters
|
||||
///
|
||||
/// - `impl_m`: type of the method we are checking
|
||||
/// - `impl_m_span`: span to use for reporting errors
|
||||
/// - `trait_m`: the method in the trait
|
||||
/// - `impl_trait_ref`: the TraitRef corresponding to the trait implementation
|
||||
pub(super) fn compare_impl_method<'tcx>(
|
||||
|
@ -41,23 +40,19 @@ pub(super) fn compare_impl_method<'tcx>(
|
|||
impl_m: &ty::AssocItem,
|
||||
trait_m: &ty::AssocItem,
|
||||
impl_trait_ref: ty::TraitRef<'tcx>,
|
||||
trait_item_span: Option<Span>,
|
||||
) {
|
||||
debug!("compare_impl_method(impl_trait_ref={:?})", impl_trait_ref);
|
||||
|
||||
let impl_m_span = tcx.def_span(impl_m.def_id);
|
||||
|
||||
let _: Result<_, ErrorGuaranteed> = try {
|
||||
compare_self_type(tcx, impl_m, impl_m_span, trait_m, impl_trait_ref)?;
|
||||
compare_number_of_generics(tcx, impl_m, trait_m, trait_item_span, false)?;
|
||||
compare_self_type(tcx, impl_m, trait_m, impl_trait_ref)?;
|
||||
compare_number_of_generics(tcx, impl_m, trait_m, false)?;
|
||||
compare_generic_param_kinds(tcx, impl_m, trait_m, false)?;
|
||||
compare_number_of_method_arguments(tcx, impl_m, impl_m_span, trait_m, trait_item_span)?;
|
||||
compare_number_of_method_arguments(tcx, impl_m, trait_m)?;
|
||||
compare_synthetic_generics(tcx, impl_m, trait_m)?;
|
||||
compare_asyncness(tcx, impl_m, impl_m_span, trait_m, trait_item_span)?;
|
||||
compare_asyncness(tcx, impl_m, trait_m)?;
|
||||
compare_method_predicate_entailment(
|
||||
tcx,
|
||||
impl_m,
|
||||
impl_m_span,
|
||||
trait_m,
|
||||
impl_trait_ref,
|
||||
CheckImpliedWfMode::Check,
|
||||
|
@ -131,11 +126,10 @@ pub(super) fn compare_impl_method<'tcx>(
|
|||
///
|
||||
/// Finally we register each of these predicates as an obligation and check that
|
||||
/// they hold.
|
||||
#[instrument(level = "debug", skip(tcx, impl_m_span, impl_trait_ref))]
|
||||
#[instrument(level = "debug", skip(tcx, impl_trait_ref))]
|
||||
fn compare_method_predicate_entailment<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
impl_m: &ty::AssocItem,
|
||||
impl_m_span: Span,
|
||||
trait_m: &ty::AssocItem,
|
||||
impl_trait_ref: ty::TraitRef<'tcx>,
|
||||
check_implied_wf: CheckImpliedWfMode,
|
||||
|
@ -148,6 +142,7 @@ fn compare_method_predicate_entailment<'tcx>(
|
|||
// FIXME(@lcnr): remove that after removing `cause.body_id` from
|
||||
// obligations.
|
||||
let impl_m_def_id = impl_m.def_id.expect_local();
|
||||
let impl_m_span = tcx.def_span(impl_m_def_id);
|
||||
let cause = ObligationCause::new(
|
||||
impl_m_span,
|
||||
impl_m_def_id,
|
||||
|
@ -315,7 +310,6 @@ fn compare_method_predicate_entailment<'tcx>(
|
|||
return compare_method_predicate_entailment(
|
||||
tcx,
|
||||
impl_m,
|
||||
impl_m_span,
|
||||
trait_m,
|
||||
impl_trait_ref,
|
||||
CheckImpliedWfMode::Skip,
|
||||
|
@ -353,7 +347,6 @@ fn compare_method_predicate_entailment<'tcx>(
|
|||
return compare_method_predicate_entailment(
|
||||
tcx,
|
||||
impl_m,
|
||||
impl_m_span,
|
||||
trait_m,
|
||||
impl_trait_ref,
|
||||
CheckImpliedWfMode::Skip,
|
||||
|
@ -535,9 +528,7 @@ enum CheckImpliedWfMode {
|
|||
fn compare_asyncness<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
impl_m: &ty::AssocItem,
|
||||
impl_m_span: Span,
|
||||
trait_m: &ty::AssocItem,
|
||||
trait_item_span: Option<Span>,
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
if tcx.asyncness(trait_m.def_id) == hir::IsAsync::Async {
|
||||
match tcx.fn_sig(impl_m.def_id).skip_binder().skip_binder().output().kind() {
|
||||
|
@ -549,9 +540,9 @@ fn compare_asyncness<'tcx>(
|
|||
}
|
||||
_ => {
|
||||
return Err(tcx.sess.emit_err(crate::errors::AsyncTraitImplShouldBeAsync {
|
||||
span: impl_m_span,
|
||||
span: tcx.def_span(impl_m.def_id),
|
||||
method_name: trait_m.name,
|
||||
trait_item_span,
|
||||
trait_item_span: tcx.hir().span_if_local(trait_m.def_id),
|
||||
}));
|
||||
}
|
||||
};
|
||||
|
@ -606,7 +597,7 @@ pub(super) fn collect_return_position_impl_trait_in_trait_tys<'tcx>(
|
|||
|
||||
// First, check a few of the same things as `compare_impl_method`,
|
||||
// just so we don't ICE during substitution later.
|
||||
compare_number_of_generics(tcx, impl_m, trait_m, tcx.hir().span_if_local(impl_m.def_id), true)?;
|
||||
compare_number_of_generics(tcx, impl_m, trait_m, true)?;
|
||||
compare_generic_param_kinds(tcx, impl_m, trait_m, true)?;
|
||||
check_region_bounds_on_impl_item(tcx, impl_m, trait_m, true)?;
|
||||
|
||||
|
@ -1094,7 +1085,6 @@ fn extract_spans_for_error_reporting<'tcx>(
|
|||
fn compare_self_type<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
impl_m: &ty::AssocItem,
|
||||
impl_m_span: Span,
|
||||
trait_m: &ty::AssocItem,
|
||||
impl_trait_ref: ty::TraitRef<'tcx>,
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
|
@ -1130,6 +1120,7 @@ fn compare_self_type<'tcx>(
|
|||
|
||||
(false, true) => {
|
||||
let self_descr = self_string(impl_m);
|
||||
let impl_m_span = tcx.def_span(impl_m.def_id);
|
||||
let mut err = struct_span_err!(
|
||||
tcx.sess,
|
||||
impl_m_span,
|
||||
|
@ -1149,6 +1140,7 @@ fn compare_self_type<'tcx>(
|
|||
|
||||
(true, false) => {
|
||||
let self_descr = self_string(trait_m);
|
||||
let impl_m_span = tcx.def_span(impl_m.def_id);
|
||||
let mut err = struct_span_err!(
|
||||
tcx.sess,
|
||||
impl_m_span,
|
||||
|
@ -1196,7 +1188,6 @@ fn compare_number_of_generics<'tcx>(
|
|||
tcx: TyCtxt<'tcx>,
|
||||
impl_: &ty::AssocItem,
|
||||
trait_: &ty::AssocItem,
|
||||
trait_span: Option<Span>,
|
||||
delay: bool,
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
let trait_own_counts = tcx.generics_of(trait_.def_id).own_counts();
|
||||
|
@ -1256,6 +1247,7 @@ fn compare_number_of_generics<'tcx>(
|
|||
.collect();
|
||||
(Some(arg_spans), impl_trait_spans)
|
||||
} else {
|
||||
let trait_span = tcx.hir().span_if_local(trait_.def_id);
|
||||
(trait_span.map(|s| vec![s]), vec![])
|
||||
};
|
||||
|
||||
|
@ -1338,9 +1330,7 @@ fn compare_number_of_generics<'tcx>(
|
|||
fn compare_number_of_method_arguments<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
impl_m: &ty::AssocItem,
|
||||
impl_m_span: Span,
|
||||
trait_m: &ty::AssocItem,
|
||||
trait_item_span: Option<Span>,
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
let impl_m_fty = tcx.fn_sig(impl_m.def_id);
|
||||
let trait_m_fty = tcx.fn_sig(trait_m.def_id);
|
||||
|
@ -1362,7 +1352,7 @@ fn compare_number_of_method_arguments<'tcx>(
|
|||
}
|
||||
})
|
||||
})
|
||||
.or(trait_item_span);
|
||||
.or_else(|| tcx.hir().span_if_local(trait_m.def_id));
|
||||
|
||||
let (impl_m_sig, _) = &tcx.hir().expect_impl_item(impl_m.def_id.expect_local()).expect_fn();
|
||||
let pos = impl_number_args.saturating_sub(1);
|
||||
|
@ -1377,7 +1367,7 @@ fn compare_number_of_method_arguments<'tcx>(
|
|||
arg.span.with_lo(impl_m_sig.decl.inputs[0].span.lo())
|
||||
}
|
||||
})
|
||||
.unwrap_or(impl_m_span);
|
||||
.unwrap_or_else(|| tcx.def_span(impl_m.def_id));
|
||||
|
||||
let mut err = struct_span_err!(
|
||||
tcx.sess,
|
||||
|
@ -1747,22 +1737,16 @@ pub(super) fn compare_impl_const_raw(
|
|||
pub(super) fn compare_impl_ty<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
impl_ty: &ty::AssocItem,
|
||||
impl_ty_span: Span,
|
||||
trait_ty: &ty::AssocItem,
|
||||
impl_trait_ref: ty::TraitRef<'tcx>,
|
||||
trait_item_span: Option<Span>,
|
||||
) {
|
||||
debug!("compare_impl_type(impl_trait_ref={:?})", impl_trait_ref);
|
||||
|
||||
let _: Result<(), ErrorGuaranteed> = try {
|
||||
compare_number_of_generics(tcx, impl_ty, trait_ty, trait_item_span, false)?;
|
||||
|
||||
compare_number_of_generics(tcx, impl_ty, trait_ty, false)?;
|
||||
compare_generic_param_kinds(tcx, impl_ty, trait_ty, false)?;
|
||||
|
||||
let sp = tcx.def_span(impl_ty.def_id);
|
||||
compare_type_predicate_entailment(tcx, impl_ty, sp, trait_ty, impl_trait_ref)?;
|
||||
|
||||
check_type_bounds(tcx, trait_ty, impl_ty, impl_ty_span, impl_trait_ref)?;
|
||||
compare_type_predicate_entailment(tcx, impl_ty, trait_ty, impl_trait_ref)?;
|
||||
check_type_bounds(tcx, trait_ty, impl_ty, impl_trait_ref)?;
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -1771,7 +1755,6 @@ pub(super) fn compare_impl_ty<'tcx>(
|
|||
fn compare_type_predicate_entailment<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
impl_ty: &ty::AssocItem,
|
||||
impl_ty_span: Span,
|
||||
trait_ty: &ty::AssocItem,
|
||||
impl_trait_ref: ty::TraitRef<'tcx>,
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
|
@ -1808,6 +1791,7 @@ fn compare_type_predicate_entailment<'tcx>(
|
|||
|
||||
debug!("compare_type_predicate_entailment: bounds={:?}", hybrid_preds);
|
||||
|
||||
let impl_ty_span = tcx.def_span(impl_ty_def_id);
|
||||
let normalize_cause = traits::ObligationCause::misc(impl_ty_span, impl_ty_def_id);
|
||||
let param_env = ty::ParamEnv::new(
|
||||
tcx.intern_predicates(&hybrid_preds.predicates),
|
||||
|
@ -1873,7 +1857,6 @@ pub(super) fn check_type_bounds<'tcx>(
|
|||
tcx: TyCtxt<'tcx>,
|
||||
trait_ty: &ty::AssocItem,
|
||||
impl_ty: &ty::AssocItem,
|
||||
impl_ty_span: Span,
|
||||
impl_trait_ref: ty::TraitRef<'tcx>,
|
||||
) -> Result<(), ErrorGuaranteed> {
|
||||
// Given
|
||||
|
@ -2009,8 +1992,15 @@ pub(super) fn check_type_bounds<'tcx>(
|
|||
let infcx = tcx.infer_ctxt().build();
|
||||
let ocx = ObligationCtxt::new(&infcx);
|
||||
|
||||
let assumed_wf_types =
|
||||
ocx.assumed_wf_types(param_env, impl_ty_span, impl_ty.def_id.expect_local());
|
||||
let impl_ty_span = match tcx.hir().get_by_def_id(impl_ty_def_id) {
|
||||
hir::Node::TraitItem(hir::TraitItem {
|
||||
kind: hir::TraitItemKind::Type(_, Some(ty)),
|
||||
..
|
||||
}) => ty.span,
|
||||
hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Type(ty), .. }) => ty.span,
|
||||
_ => bug!(),
|
||||
};
|
||||
let assumed_wf_types = ocx.assumed_wf_types(param_env, impl_ty_span, impl_ty_def_id);
|
||||
|
||||
let normalize_cause = ObligationCause::new(
|
||||
impl_ty_span,
|
||||
|
|
|
@ -414,7 +414,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
|
|||
// Check that sym actually points to a function. Later passes
|
||||
// depend on this.
|
||||
hir::InlineAsmOperand::SymFn { anon_const } => {
|
||||
let ty = self.tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
|
||||
let ty = self.tcx.type_of(anon_const.def_id);
|
||||
match ty.kind() {
|
||||
ty::Never | ty::Error(_) => {}
|
||||
ty::FnDef(..) => {}
|
||||
|
@ -422,7 +422,7 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
|
|||
let mut err =
|
||||
self.tcx.sess.struct_span_err(*op_sp, "invalid `sym` operand");
|
||||
err.span_label(
|
||||
self.tcx.hir().span(anon_const.body.hir_id),
|
||||
self.tcx.def_span(anon_const.def_id),
|
||||
&format!("is {} `{}`", ty.kind().article(), ty),
|
||||
);
|
||||
err.help("`sym` operands must refer to either a function or a static");
|
||||
|
|
|
@ -75,7 +75,6 @@ pub use check::check_abi;
|
|||
use check::check_mod_item_types;
|
||||
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
||||
use rustc_errors::{pluralize, struct_span_err, Applicability, Diagnostic, DiagnosticBuilder};
|
||||
use rustc_hir as hir;
|
||||
use rustc_hir::def_id::{DefId, LocalDefId};
|
||||
use rustc_hir::intravisit::Visitor;
|
||||
use rustc_index::bit_set::BitSet;
|
||||
|
@ -169,27 +168,24 @@ fn maybe_check_static_with_link_section(tcx: TyCtxt<'_>, id: LocalDefId) {
|
|||
}
|
||||
}
|
||||
|
||||
fn report_forbidden_specialization(
|
||||
tcx: TyCtxt<'_>,
|
||||
impl_item: &hir::ImplItemRef,
|
||||
parent_impl: DefId,
|
||||
) {
|
||||
fn report_forbidden_specialization(tcx: TyCtxt<'_>, impl_item: DefId, parent_impl: DefId) {
|
||||
let span = tcx.def_span(impl_item);
|
||||
let ident = tcx.item_name(impl_item);
|
||||
let mut err = struct_span_err!(
|
||||
tcx.sess,
|
||||
impl_item.span,
|
||||
span,
|
||||
E0520,
|
||||
"`{}` specializes an item from a parent `impl`, but \
|
||||
that item is not marked `default`",
|
||||
impl_item.ident
|
||||
"`{}` specializes an item from a parent `impl`, but that item is not marked `default`",
|
||||
ident,
|
||||
);
|
||||
err.span_label(impl_item.span, format!("cannot specialize default item `{}`", impl_item.ident));
|
||||
err.span_label(span, format!("cannot specialize default item `{}`", ident));
|
||||
|
||||
match tcx.span_of_impl(parent_impl) {
|
||||
Ok(span) => {
|
||||
err.span_label(span, "parent `impl` is here");
|
||||
err.note(&format!(
|
||||
"to specialize, `{}` in the parent `impl` must be marked `default`",
|
||||
impl_item.ident
|
||||
ident
|
||||
));
|
||||
}
|
||||
Err(cname) => {
|
||||
|
|
|
@ -202,8 +202,7 @@ fn visit_implementation_of_coerce_unsized(tcx: TyCtxt<'_>, impl_did: LocalDefId)
|
|||
fn visit_implementation_of_dispatch_from_dyn(tcx: TyCtxt<'_>, impl_did: LocalDefId) {
|
||||
debug!("visit_implementation_of_dispatch_from_dyn: impl_did={:?}", impl_did);
|
||||
|
||||
let impl_hir_id = tcx.hir().local_def_id_to_hir_id(impl_did);
|
||||
let span = tcx.hir().span(impl_hir_id);
|
||||
let span = tcx.def_span(impl_did);
|
||||
|
||||
let dispatch_from_dyn_trait = tcx.require_lang_item(LangItem::DispatchFromDyn, Some(span));
|
||||
|
||||
|
|
|
@ -14,7 +14,6 @@ use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
|
|||
use rustc_middle::ty::fast_reject::{simplify_type, SimplifiedType, TreatParams};
|
||||
use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt};
|
||||
use rustc_span::symbol::sym;
|
||||
use rustc_span::Span;
|
||||
|
||||
/// On-demand query: yields a map containing all types mapped to their inherent impls.
|
||||
pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls {
|
||||
|
@ -57,86 +56,76 @@ const ADD_ATTR: &str =
|
|||
"alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items";
|
||||
|
||||
impl<'tcx> InherentCollect<'tcx> {
|
||||
fn check_def_id(&mut self, item: &hir::Item<'_>, self_ty: Ty<'tcx>, def_id: DefId, span: Span) {
|
||||
let impl_def_id = item.owner_id;
|
||||
if let Some(def_id) = def_id.as_local() {
|
||||
fn check_def_id(&mut self, impl_def_id: LocalDefId, self_ty: Ty<'tcx>, ty_def_id: DefId) {
|
||||
if let Some(ty_def_id) = ty_def_id.as_local() {
|
||||
// Add the implementation to the mapping from implementation to base
|
||||
// type def ID, if there is a base type for this implementation and
|
||||
// the implementation does not have any associated traits.
|
||||
let vec = self.impls_map.inherent_impls.entry(def_id).or_default();
|
||||
let vec = self.impls_map.inherent_impls.entry(ty_def_id).or_default();
|
||||
vec.push(impl_def_id.to_def_id());
|
||||
return;
|
||||
}
|
||||
|
||||
if self.tcx.features().rustc_attrs {
|
||||
let hir::ItemKind::Impl(&hir::Impl { items, .. }) = item.kind else {
|
||||
bug!("expected `impl` item: {:?}", item);
|
||||
};
|
||||
let items = self.tcx.associated_item_def_ids(impl_def_id);
|
||||
|
||||
if !self.tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls) {
|
||||
if !self.tcx.has_attr(ty_def_id, sym::rustc_has_incoherent_inherent_impls) {
|
||||
let impl_span = self.tcx.def_span(impl_def_id);
|
||||
struct_span_err!(
|
||||
self.tcx.sess,
|
||||
span,
|
||||
impl_span,
|
||||
E0390,
|
||||
"cannot define inherent `impl` for a type outside of the crate where the type is defined",
|
||||
)
|
||||
.help(INTO_DEFINING_CRATE)
|
||||
.span_help(span, ADD_ATTR_TO_TY)
|
||||
.span_help(impl_span, ADD_ATTR_TO_TY)
|
||||
.emit();
|
||||
return;
|
||||
}
|
||||
|
||||
for impl_item in items {
|
||||
if !self
|
||||
.tcx
|
||||
.has_attr(impl_item.id.owner_id.to_def_id(), sym::rustc_allow_incoherent_impl)
|
||||
{
|
||||
for &impl_item in items {
|
||||
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
|
||||
let impl_span = self.tcx.def_span(impl_def_id);
|
||||
struct_span_err!(
|
||||
self.tcx.sess,
|
||||
span,
|
||||
impl_span,
|
||||
E0390,
|
||||
"cannot define inherent `impl` for a type outside of the crate where the type is defined",
|
||||
)
|
||||
.help(INTO_DEFINING_CRATE)
|
||||
.span_help(self.tcx.hir().span(impl_item.id.hir_id()), ADD_ATTR)
|
||||
.span_help(self.tcx.def_span(impl_item), ADD_ATTR)
|
||||
.emit();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsInfer) {
|
||||
self.impls_map.incoherent_impls.entry(simp).or_default().push(impl_def_id.def_id);
|
||||
self.impls_map.incoherent_impls.entry(simp).or_default().push(impl_def_id);
|
||||
} else {
|
||||
bug!("unexpected self type: {:?}", self_ty);
|
||||
}
|
||||
} else {
|
||||
let impl_span = self.tcx.def_span(impl_def_id);
|
||||
struct_span_err!(
|
||||
self.tcx.sess,
|
||||
span,
|
||||
impl_span,
|
||||
E0116,
|
||||
"cannot define inherent `impl` for a type outside of the crate \
|
||||
where the type is defined"
|
||||
)
|
||||
.span_label(span, "impl for type defined outside of crate.")
|
||||
.span_label(impl_span, "impl for type defined outside of crate.")
|
||||
.note("define and implement a trait or new type instead")
|
||||
.emit();
|
||||
}
|
||||
}
|
||||
|
||||
fn check_primitive_impl(
|
||||
&mut self,
|
||||
impl_def_id: LocalDefId,
|
||||
ty: Ty<'tcx>,
|
||||
items: &[hir::ImplItemRef],
|
||||
span: Span,
|
||||
) {
|
||||
fn check_primitive_impl(&mut self, impl_def_id: LocalDefId, ty: Ty<'tcx>) {
|
||||
let items = self.tcx.associated_item_def_ids(impl_def_id);
|
||||
if !self.tcx.hir().rustc_coherence_is_core() {
|
||||
if self.tcx.features().rustc_attrs {
|
||||
for item in items {
|
||||
if !self
|
||||
.tcx
|
||||
.has_attr(item.id.owner_id.to_def_id(), sym::rustc_allow_incoherent_impl)
|
||||
{
|
||||
for &impl_item in items {
|
||||
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
|
||||
let span = self.tcx.def_span(impl_def_id);
|
||||
struct_span_err!(
|
||||
self.tcx.sess,
|
||||
span,
|
||||
|
@ -144,12 +133,13 @@ impl<'tcx> InherentCollect<'tcx> {
|
|||
"cannot define inherent `impl` for primitive types outside of `core`",
|
||||
)
|
||||
.help(INTO_CORE)
|
||||
.span_help(item.span, ADD_ATTR)
|
||||
.span_help(self.tcx.def_span(impl_item), ADD_ATTR)
|
||||
.emit();
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let span = self.tcx.def_span(impl_def_id);
|
||||
let mut err = struct_span_err!(
|
||||
self.tcx.sess,
|
||||
span,
|
||||
|
@ -177,35 +167,27 @@ impl<'tcx> InherentCollect<'tcx> {
|
|||
}
|
||||
|
||||
fn check_item(&mut self, id: hir::ItemId) {
|
||||
if !matches!(self.tcx.def_kind(id.owner_id), DefKind::Impl) {
|
||||
if !matches!(self.tcx.def_kind(id.owner_id), DefKind::Impl { of_trait: false }) {
|
||||
return;
|
||||
}
|
||||
|
||||
let item = self.tcx.hir().item(id);
|
||||
let impl_span = self.tcx.hir().span(id.hir_id());
|
||||
let hir::ItemKind::Impl(hir::Impl { of_trait: None, items, .. }) = item.kind else {
|
||||
return;
|
||||
};
|
||||
|
||||
let self_ty = self.tcx.type_of(item.owner_id);
|
||||
let id = id.owner_id.def_id;
|
||||
let item_span = self.tcx.def_span(id);
|
||||
let self_ty = self.tcx.type_of(id);
|
||||
match *self_ty.kind() {
|
||||
ty::Adt(def, _) => {
|
||||
self.check_def_id(item, self_ty, def.did(), impl_span);
|
||||
}
|
||||
ty::Foreign(did) => {
|
||||
self.check_def_id(item, self_ty, did, impl_span);
|
||||
}
|
||||
ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()),
|
||||
ty::Foreign(did) => self.check_def_id(id, self_ty, did),
|
||||
ty::Dynamic(data, ..) if data.principal_def_id().is_some() => {
|
||||
self.check_def_id(item, self_ty, data.principal_def_id().unwrap(), impl_span);
|
||||
self.check_def_id(id, self_ty, data.principal_def_id().unwrap());
|
||||
}
|
||||
ty::Dynamic(..) => {
|
||||
struct_span_err!(
|
||||
self.tcx.sess,
|
||||
impl_span,
|
||||
item_span,
|
||||
E0785,
|
||||
"cannot define inherent `impl` for a dyn auto trait"
|
||||
)
|
||||
.span_label(impl_span, "impl requires at least one non-auto trait")
|
||||
.span_label(item_span, "impl requires at least one non-auto trait")
|
||||
.note("define and implement a new trait or type instead")
|
||||
.emit();
|
||||
}
|
||||
|
@ -221,18 +203,16 @@ impl<'tcx> InherentCollect<'tcx> {
|
|||
| ty::Ref(..)
|
||||
| ty::Never
|
||||
| ty::FnPtr(_)
|
||||
| ty::Tuple(..) => {
|
||||
self.check_primitive_impl(item.owner_id.def_id, self_ty, items, impl_span)
|
||||
}
|
||||
| ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
|
||||
ty::Alias(..) | ty::Param(_) => {
|
||||
let mut err = struct_span_err!(
|
||||
self.tcx.sess,
|
||||
impl_span,
|
||||
item_span,
|
||||
E0118,
|
||||
"no nominal type found for inherent implementation"
|
||||
);
|
||||
|
||||
err.span_label(impl_span, "impl requires a nominal type")
|
||||
err.span_label(item_span, "impl requires a nominal type")
|
||||
.note("either implement a trait on it or create a newtype to wrap it instead");
|
||||
|
||||
err.emit();
|
||||
|
@ -245,7 +225,7 @@ impl<'tcx> InherentCollect<'tcx> {
|
|||
| ty::Bound(..)
|
||||
| ty::Placeholder(_)
|
||||
| ty::Infer(_) => {
|
||||
bug!("unexpected impl self type of impl: {:?} {:?}", item.owner_id, self_ty);
|
||||
bug!("unexpected impl self type of impl: {:?} {:?}", id, self_ty);
|
||||
}
|
||||
ty::Error(_) => {}
|
||||
}
|
||||
|
|
|
@ -39,25 +39,27 @@ fn do_orphan_check_impl<'tcx>(
|
|||
) -> Result<(), ErrorGuaranteed> {
|
||||
let trait_def_id = trait_ref.def_id;
|
||||
|
||||
let item = tcx.hir().expect_item(def_id);
|
||||
let hir::ItemKind::Impl(impl_) = item.kind else {
|
||||
bug!("{:?} is not an impl: {:?}", def_id, item);
|
||||
};
|
||||
let sp = tcx.def_span(def_id);
|
||||
let tr = impl_.of_trait.as_ref().unwrap();
|
||||
|
||||
match traits::orphan_check(tcx, item.owner_id.to_def_id()) {
|
||||
match traits::orphan_check(tcx, def_id.to_def_id()) {
|
||||
Ok(()) => {}
|
||||
Err(err) => emit_orphan_check_error(
|
||||
tcx,
|
||||
sp,
|
||||
item.span,
|
||||
tr.path.span,
|
||||
trait_ref,
|
||||
impl_.self_ty.span,
|
||||
&impl_.generics,
|
||||
err,
|
||||
)?,
|
||||
Err(err) => {
|
||||
let item = tcx.hir().expect_item(def_id);
|
||||
let hir::ItemKind::Impl(impl_) = item.kind else {
|
||||
bug!("{:?} is not an impl: {:?}", def_id, item);
|
||||
};
|
||||
let tr = impl_.of_trait.as_ref().unwrap();
|
||||
let sp = tcx.def_span(def_id);
|
||||
|
||||
emit_orphan_check_error(
|
||||
tcx,
|
||||
sp,
|
||||
item.span,
|
||||
tr.path.span,
|
||||
trait_ref,
|
||||
impl_.self_ty.span,
|
||||
&impl_.generics,
|
||||
err,
|
||||
)?
|
||||
}
|
||||
}
|
||||
|
||||
// In addition to the above rules, we restrict impls of auto traits
|
||||
|
@ -235,7 +237,10 @@ fn do_orphan_check_impl<'tcx>(
|
|||
| ty::GeneratorWitnessMIR(..)
|
||||
| ty::Bound(..)
|
||||
| ty::Placeholder(..)
|
||||
| ty::Infer(..) => span_bug!(sp, "weird self type for autotrait impl"),
|
||||
| ty::Infer(..) => {
|
||||
let sp = tcx.def_span(def_id);
|
||||
span_bug!(sp, "weird self type for autotrait impl")
|
||||
}
|
||||
|
||||
ty::Error(..) => (LocalImpl::Allow, NonlocalImpl::Allow),
|
||||
};
|
||||
|
@ -254,6 +259,7 @@ fn do_orphan_check_impl<'tcx>(
|
|||
is one of the trait object's trait bounds",
|
||||
trait = tcx.def_path_str(trait_def_id),
|
||||
);
|
||||
let sp = tcx.def_span(def_id);
|
||||
let reported =
|
||||
struct_span_err!(tcx.sess, sp, E0321, "{}", msg).note(label).emit();
|
||||
return Err(reported);
|
||||
|
@ -282,6 +288,7 @@ fn do_orphan_check_impl<'tcx>(
|
|||
non-struct/enum type",
|
||||
)),
|
||||
} {
|
||||
let sp = tcx.def_span(def_id);
|
||||
let reported =
|
||||
struct_span_err!(tcx.sess, sp, E0321, "{}", msg).span_label(sp, label).emit();
|
||||
return Err(reported);
|
||||
|
|
|
@ -1563,7 +1563,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
|
|||
// See issue #83753. If someone writes an associated type on a non-trait, just treat it as
|
||||
// there being no supertrait HRTBs.
|
||||
match tcx.def_kind(def_id) {
|
||||
DefKind::Trait | DefKind::TraitAlias | DefKind::Impl => {}
|
||||
DefKind::Trait | DefKind::TraitAlias | DefKind::Impl { .. } => {}
|
||||
_ => break None,
|
||||
}
|
||||
|
||||
|
|
|
@ -55,7 +55,7 @@ fn check_mod_impl_wf(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
|
|||
let min_specialization = tcx.features().min_specialization;
|
||||
let module = tcx.hir_module_items(module_def_id);
|
||||
for id in module.items() {
|
||||
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl) {
|
||||
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) {
|
||||
enforce_impl_params_are_constrained(tcx, id.owner_id.def_id);
|
||||
if min_specialization {
|
||||
check_min_specialization(tcx, id.owner_id.def_id);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue