Auto merge of #120919 - oli-obk:impl_polarity, r=compiler-errors
Merge `impl_polarity` and `impl_trait_ref` queries Hopefully this is perf neutral. I want to finish https://github.com/rust-lang/rust/pull/120835 and stop using the HIR in `coherent_trait`, which should then give us a perf improvement.
This commit is contained in:
commit
d26b417112
22 changed files with 156 additions and 131 deletions
|
@ -1673,9 +1673,9 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
.is_accessible_from(self.item_def_id(), tcx)
|
||||
&& tcx.all_impls(*trait_def_id)
|
||||
.any(|impl_def_id| {
|
||||
let trait_ref = tcx.impl_trait_ref(impl_def_id);
|
||||
trait_ref.is_some_and(|trait_ref| {
|
||||
let impl_ = trait_ref.instantiate(
|
||||
let impl_header = tcx.impl_trait_header(impl_def_id);
|
||||
impl_header.is_some_and(|header| {
|
||||
let header = header.instantiate(
|
||||
tcx,
|
||||
infcx.fresh_args_for_item(DUMMY_SP, impl_def_id),
|
||||
);
|
||||
|
@ -1687,11 +1687,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
infcx
|
||||
.can_eq(
|
||||
ty::ParamEnv::empty(),
|
||||
impl_.self_ty(),
|
||||
header.trait_ref.self_ty(),
|
||||
value,
|
||||
)
|
||||
) && header.polarity != ty::ImplPolarity::Negative
|
||||
})
|
||||
&& tcx.impl_polarity(impl_def_id) != ty::ImplPolarity::Negative
|
||||
})
|
||||
})
|
||||
.map(|trait_def_id| tcx.def_path_str(trait_def_id))
|
||||
|
@ -1737,13 +1736,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
|||
} else {
|
||||
// Find all the types that have an `impl` for the trait.
|
||||
tcx.all_impls(trait_def_id)
|
||||
.filter(|impl_def_id| {
|
||||
.filter_map(|impl_def_id| tcx.impl_trait_header(impl_def_id))
|
||||
.filter(|header| {
|
||||
// Consider only accessible traits
|
||||
tcx.visibility(trait_def_id).is_accessible_from(self.item_def_id(), tcx)
|
||||
&& tcx.impl_polarity(impl_def_id) != ty::ImplPolarity::Negative
|
||||
&& header.skip_binder().polarity != ty::ImplPolarity::Negative
|
||||
})
|
||||
.filter_map(|impl_def_id| tcx.impl_trait_ref(impl_def_id))
|
||||
.map(|impl_| impl_.instantiate_identity().self_ty())
|
||||
.map(|header| header.instantiate_identity().trait_ref.self_ty())
|
||||
// We don't care about blanket impls.
|
||||
.filter(|self_ty| !self_ty.has_non_region_param())
|
||||
.map(|self_ty| tcx.erase_regions(self_ty).to_string())
|
||||
|
|
|
@ -527,8 +527,12 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
|
|||
}
|
||||
DefKind::Fn => {} // entirely within check_item_body
|
||||
DefKind::Impl { of_trait } => {
|
||||
if of_trait && let Some(impl_trait_ref) = tcx.impl_trait_ref(def_id) {
|
||||
check_impl_items_against_trait(tcx, def_id, impl_trait_ref.instantiate_identity());
|
||||
if of_trait && let Some(impl_trait_header) = tcx.impl_trait_header(def_id) {
|
||||
check_impl_items_against_trait(
|
||||
tcx,
|
||||
def_id,
|
||||
impl_trait_header.instantiate_identity(),
|
||||
);
|
||||
check_on_unimplemented(tcx, def_id);
|
||||
}
|
||||
}
|
||||
|
@ -719,19 +723,19 @@ pub(super) fn check_specialization_validity<'tcx>(
|
|||
fn check_impl_items_against_trait<'tcx>(
|
||||
tcx: TyCtxt<'tcx>,
|
||||
impl_id: LocalDefId,
|
||||
impl_trait_ref: ty::TraitRef<'tcx>,
|
||||
impl_trait_header: ty::ImplTraitHeader<'tcx>,
|
||||
) {
|
||||
// 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`
|
||||
// isn't populated for such impls.
|
||||
if impl_trait_ref.references_error() {
|
||||
if impl_trait_header.references_error() {
|
||||
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) {
|
||||
match impl_trait_header.polarity {
|
||||
ty::ImplPolarity::Reservation | ty::ImplPolarity::Positive => {}
|
||||
ty::ImplPolarity::Negative => {
|
||||
if let [first_item_ref, ..] = impl_item_refs {
|
||||
|
@ -748,7 +752,7 @@ fn check_impl_items_against_trait<'tcx>(
|
|||
}
|
||||
}
|
||||
|
||||
let trait_def = tcx.trait_def(impl_trait_ref.def_id);
|
||||
let trait_def = tcx.trait_def(impl_trait_header.trait_ref.def_id);
|
||||
|
||||
for &impl_item in impl_item_refs {
|
||||
let ty_impl_item = tcx.associated_item(impl_item);
|
||||
|
@ -767,10 +771,10 @@ fn check_impl_items_against_trait<'tcx>(
|
|||
));
|
||||
}
|
||||
ty::AssocKind::Fn => {
|
||||
compare_impl_method(tcx, ty_impl_item, ty_trait_item, impl_trait_ref);
|
||||
compare_impl_method(tcx, ty_impl_item, ty_trait_item, impl_trait_header.trait_ref);
|
||||
}
|
||||
ty::AssocKind::Type => {
|
||||
compare_impl_ty(tcx, ty_impl_item, ty_trait_item, impl_trait_ref);
|
||||
compare_impl_ty(tcx, ty_impl_item, ty_trait_item, impl_trait_header.trait_ref);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -790,7 +794,7 @@ fn check_impl_items_against_trait<'tcx>(
|
|||
let mut must_implement_one_of: Option<&[Ident]> =
|
||||
trait_def.must_implement_one_of.as_deref();
|
||||
|
||||
for &trait_item_id in tcx.associated_item_def_ids(impl_trait_ref.def_id) {
|
||||
for &trait_item_id in tcx.associated_item_def_ids(impl_trait_header.trait_ref.def_id) {
|
||||
let leaf_def = ancestors.leaf_def(tcx, trait_item_id);
|
||||
|
||||
let is_implemented = leaf_def
|
||||
|
@ -868,7 +872,7 @@ fn check_impl_items_against_trait<'tcx>(
|
|||
|
||||
if let Some(missing_items) = must_implement_one_of {
|
||||
let attr_span = tcx
|
||||
.get_attr(impl_trait_ref.def_id, sym::rustc_must_implement_one_of)
|
||||
.get_attr(impl_trait_header.trait_ref.def_id, sym::rustc_must_implement_one_of)
|
||||
.map(|attr| attr.span);
|
||||
|
||||
missing_items_must_implement_one_of_err(
|
||||
|
|
|
@ -245,9 +245,9 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
|
|||
// won't be allowed unless there's an *explicit* implementation of `Send`
|
||||
// for `T`
|
||||
hir::ItemKind::Impl(impl_) => {
|
||||
let is_auto = tcx
|
||||
.impl_trait_ref(def_id)
|
||||
.is_some_and(|trait_ref| tcx.trait_is_auto(trait_ref.skip_binder().def_id));
|
||||
let header = tcx.impl_trait_header(def_id);
|
||||
let is_auto = header
|
||||
.is_some_and(|header| tcx.trait_is_auto(header.skip_binder().trait_ref.def_id));
|
||||
let mut res = Ok(());
|
||||
if let (hir::Defaultness::Default { .. }, true) = (impl_.defaultness, is_auto) {
|
||||
let sp = impl_.of_trait.as_ref().map_or(item.span, |t| t.path.span);
|
||||
|
@ -259,11 +259,12 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
|
|||
.emit());
|
||||
}
|
||||
// We match on both `ty::ImplPolarity` and `ast::ImplPolarity` just to get the `!` span.
|
||||
match tcx.impl_polarity(def_id) {
|
||||
ty::ImplPolarity::Positive => {
|
||||
match header.map(|h| h.skip_binder().polarity) {
|
||||
// `None` means this is an inherent impl
|
||||
Some(ty::ImplPolarity::Positive) | None => {
|
||||
res = res.and(check_impl(tcx, item, impl_.self_ty, &impl_.of_trait));
|
||||
}
|
||||
ty::ImplPolarity::Negative => {
|
||||
Some(ty::ImplPolarity::Negative) => {
|
||||
let ast::ImplPolarity::Negative(span) = impl_.polarity else {
|
||||
bug!("impl_polarity query disagrees with impl's polarity in AST");
|
||||
};
|
||||
|
@ -280,7 +281,7 @@ fn check_item<'tcx>(tcx: TyCtxt<'tcx>, item: &'tcx hir::Item<'tcx>) -> Result<()
|
|||
.emit());
|
||||
}
|
||||
}
|
||||
ty::ImplPolarity::Reservation => {
|
||||
Some(ty::ImplPolarity::Reservation) => {
|
||||
// FIXME: what amount of WF checking do we need for reservation impls?
|
||||
}
|
||||
}
|
||||
|
|
|
@ -79,8 +79,7 @@ pub fn provide(providers: &mut Providers) {
|
|||
trait_def,
|
||||
adt_def,
|
||||
fn_sig,
|
||||
impl_trait_ref,
|
||||
impl_polarity,
|
||||
impl_trait_header,
|
||||
coroutine_kind,
|
||||
coroutine_for_closure,
|
||||
collect_mod_item_types,
|
||||
|
@ -600,7 +599,7 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
|
|||
hir::ItemKind::Impl { .. } => {
|
||||
tcx.ensure().generics_of(def_id);
|
||||
tcx.ensure().type_of(def_id);
|
||||
tcx.ensure().impl_trait_ref(def_id);
|
||||
tcx.ensure().impl_trait_header(def_id);
|
||||
tcx.ensure().predicates_of(def_id);
|
||||
}
|
||||
hir::ItemKind::Trait(..) => {
|
||||
|
@ -1499,19 +1498,20 @@ fn suggest_impl_trait<'tcx>(
|
|||
None
|
||||
}
|
||||
|
||||
fn impl_trait_ref(
|
||||
fn impl_trait_header(
|
||||
tcx: TyCtxt<'_>,
|
||||
def_id: LocalDefId,
|
||||
) -> Option<ty::EarlyBinder<ty::TraitRef<'_>>> {
|
||||
) -> Option<ty::EarlyBinder<ty::ImplTraitHeader<'_>>> {
|
||||
let icx = ItemCtxt::new(tcx, def_id);
|
||||
let impl_ = tcx.hir().expect_item(def_id).expect_impl();
|
||||
let item = tcx.hir().expect_item(def_id);
|
||||
let impl_ = item.expect_impl();
|
||||
impl_
|
||||
.of_trait
|
||||
.as_ref()
|
||||
.map(|ast_trait_ref| {
|
||||
let selfty = tcx.type_of(def_id).instantiate_identity();
|
||||
|
||||
if let Some(ErrorGuaranteed { .. }) = check_impl_constness(
|
||||
let trait_ref = if let Some(ErrorGuaranteed { .. }) = check_impl_constness(
|
||||
tcx,
|
||||
tcx.is_const_trait_impl_raw(def_id.to_def_id()),
|
||||
ast_trait_ref,
|
||||
|
@ -1536,9 +1536,12 @@ fn impl_trait_ref(
|
|||
icx.astconv().instantiate_mono_trait_ref(trait_ref, selfty)
|
||||
} else {
|
||||
icx.astconv().instantiate_mono_trait_ref(ast_trait_ref, selfty)
|
||||
}
|
||||
};
|
||||
ty::EarlyBinder::bind(ty::ImplTraitHeader {
|
||||
trait_ref,
|
||||
polarity: polarity_of_impl(tcx, def_id, impl_, item.span)
|
||||
})
|
||||
})
|
||||
.map(ty::EarlyBinder::bind)
|
||||
}
|
||||
|
||||
fn check_impl_constness(
|
||||
|
@ -1566,43 +1569,34 @@ fn check_impl_constness(
|
|||
}))
|
||||
}
|
||||
|
||||
fn impl_polarity(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplPolarity {
|
||||
fn polarity_of_impl(
|
||||
tcx: TyCtxt<'_>,
|
||||
def_id: LocalDefId,
|
||||
impl_: &hir::Impl<'_>,
|
||||
span: Span,
|
||||
) -> ty::ImplPolarity {
|
||||
let is_rustc_reservation = tcx.has_attr(def_id, sym::rustc_reservation_impl);
|
||||
let item = tcx.hir().expect_item(def_id);
|
||||
match &item.kind {
|
||||
hir::ItemKind::Impl(hir::Impl {
|
||||
polarity: hir::ImplPolarity::Negative(span),
|
||||
of_trait,
|
||||
..
|
||||
}) => {
|
||||
match &impl_ {
|
||||
hir::Impl { polarity: hir::ImplPolarity::Negative(span), of_trait, .. } => {
|
||||
if is_rustc_reservation {
|
||||
let span = span.to(of_trait.as_ref().map_or(*span, |t| t.path.span));
|
||||
tcx.dcx().span_err(span, "reservation impls can't be negative");
|
||||
}
|
||||
ty::ImplPolarity::Negative
|
||||
}
|
||||
hir::ItemKind::Impl(hir::Impl {
|
||||
polarity: hir::ImplPolarity::Positive,
|
||||
of_trait: None,
|
||||
..
|
||||
}) => {
|
||||
hir::Impl { polarity: hir::ImplPolarity::Positive, of_trait: None, .. } => {
|
||||
if is_rustc_reservation {
|
||||
tcx.dcx().span_err(item.span, "reservation impls can't be inherent");
|
||||
tcx.dcx().span_err(span, "reservation impls can't be inherent");
|
||||
}
|
||||
ty::ImplPolarity::Positive
|
||||
}
|
||||
hir::ItemKind::Impl(hir::Impl {
|
||||
polarity: hir::ImplPolarity::Positive,
|
||||
of_trait: Some(_),
|
||||
..
|
||||
}) => {
|
||||
hir::Impl { polarity: hir::ImplPolarity::Positive, of_trait: Some(_), .. } => {
|
||||
if is_rustc_reservation {
|
||||
ty::ImplPolarity::Reservation
|
||||
} else {
|
||||
ty::ImplPolarity::Positive
|
||||
}
|
||||
}
|
||||
item => bug!("impl_polarity: {:?} not an impl", item),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue