Do not fetch HIR for inherent impls.

This commit is contained in:
Camille GILLOT 2023-02-12 18:31:04 +00:00
parent 03dff82d59
commit 2a51e73ac9

View File

@ -14,7 +14,6 @@
use rustc_middle::ty::fast_reject::{simplify_type, SimplifiedType, TreatParams}; use rustc_middle::ty::fast_reject::{simplify_type, SimplifiedType, TreatParams};
use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt}; use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt};
use rustc_span::symbol::sym; use rustc_span::symbol::sym;
use rustc_span::Span;
/// On-demand query: yields a map containing all types mapped to their inherent impls. /// On-demand query: yields a map containing all types mapped to their inherent impls.
pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls { pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls {
@ -57,86 +56,76 @@ struct InherentCollect<'tcx> {
"alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items"; "alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items";
impl<'tcx> InherentCollect<'tcx> { impl<'tcx> InherentCollect<'tcx> {
fn check_def_id(&mut self, item: &hir::Item<'_>, self_ty: Ty<'tcx>, def_id: DefId, span: Span) { fn check_def_id(&mut self, impl_def_id: LocalDefId, self_ty: Ty<'tcx>, ty_def_id: DefId) {
let impl_def_id = item.owner_id; if let Some(ty_def_id) = ty_def_id.as_local() {
if let Some(def_id) = def_id.as_local() {
// Add the implementation to the mapping from implementation to base // Add the implementation to the mapping from implementation to base
// type def ID, if there is a base type for this implementation and // type def ID, if there is a base type for this implementation and
// the implementation does not have any associated traits. // 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()); vec.push(impl_def_id.to_def_id());
return; return;
} }
if self.tcx.features().rustc_attrs { if self.tcx.features().rustc_attrs {
let hir::ItemKind::Impl(&hir::Impl { items, .. }) = item.kind else { let items = self.tcx.associated_item_def_ids(impl_def_id);
bug!("expected `impl` item: {:?}", item);
};
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!( struct_span_err!(
self.tcx.sess, self.tcx.sess,
span, impl_span,
E0390, E0390,
"cannot define inherent `impl` for a type outside of the crate where the type is defined", "cannot define inherent `impl` for a type outside of the crate where the type is defined",
) )
.help(INTO_DEFINING_CRATE) .help(INTO_DEFINING_CRATE)
.span_help(span, ADD_ATTR_TO_TY) .span_help(impl_span, ADD_ATTR_TO_TY)
.emit(); .emit();
return; return;
} }
for impl_item in items { for &impl_item in items {
if !self if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
.tcx let impl_span = self.tcx.def_span(impl_def_id);
.has_attr(impl_item.id.owner_id.to_def_id(), sym::rustc_allow_incoherent_impl)
{
struct_span_err!( struct_span_err!(
self.tcx.sess, self.tcx.sess,
span, impl_span,
E0390, E0390,
"cannot define inherent `impl` for a type outside of the crate where the type is defined", "cannot define inherent `impl` for a type outside of the crate where the type is defined",
) )
.help(INTO_DEFINING_CRATE) .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(); .emit();
return; return;
} }
} }
if let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsInfer) { 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 { } else {
bug!("unexpected self type: {:?}", self_ty); bug!("unexpected self type: {:?}", self_ty);
} }
} else { } else {
let impl_span = self.tcx.def_span(impl_def_id);
struct_span_err!( struct_span_err!(
self.tcx.sess, self.tcx.sess,
span, impl_span,
E0116, E0116,
"cannot define inherent `impl` for a type outside of the crate \ "cannot define inherent `impl` for a type outside of the crate \
where the type is defined" 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") .note("define and implement a trait or new type instead")
.emit(); .emit();
} }
} }
fn check_primitive_impl( fn check_primitive_impl(&mut self, impl_def_id: LocalDefId, ty: Ty<'tcx>) {
&mut self, let items = self.tcx.associated_item_def_ids(impl_def_id);
impl_def_id: LocalDefId,
ty: Ty<'tcx>,
items: &[hir::ImplItemRef],
span: Span,
) {
if !self.tcx.hir().rustc_coherence_is_core() { if !self.tcx.hir().rustc_coherence_is_core() {
if self.tcx.features().rustc_attrs { if self.tcx.features().rustc_attrs {
for item in items { for &impl_item in items {
if !self if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
.tcx let span = self.tcx.def_span(impl_def_id);
.has_attr(item.id.owner_id.to_def_id(), sym::rustc_allow_incoherent_impl)
{
struct_span_err!( struct_span_err!(
self.tcx.sess, self.tcx.sess,
span, span,
@ -144,12 +133,13 @@ fn check_primitive_impl(
"cannot define inherent `impl` for primitive types outside of `core`", "cannot define inherent `impl` for primitive types outside of `core`",
) )
.help(INTO_CORE) .help(INTO_CORE)
.span_help(item.span, ADD_ATTR) .span_help(self.tcx.def_span(impl_item), ADD_ATTR)
.emit(); .emit();
return; return;
} }
} }
} else { } else {
let span = self.tcx.def_span(impl_def_id);
let mut err = struct_span_err!( let mut err = struct_span_err!(
self.tcx.sess, self.tcx.sess,
span, span,
@ -181,31 +171,23 @@ fn check_item(&mut self, id: hir::ItemId) {
return; return;
} }
let item = self.tcx.hir().item(id); let id = id.owner_id.def_id;
let impl_span = self.tcx.hir().span(id.hir_id()); let item_span = self.tcx.def_span(id);
let hir::ItemKind::Impl(hir::Impl { of_trait: None, items, .. }) = item.kind else { let self_ty = self.tcx.type_of(id);
return;
};
let self_ty = self.tcx.type_of(item.owner_id);
match *self_ty.kind() { match *self_ty.kind() {
ty::Adt(def, _) => { ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()),
self.check_def_id(item, self_ty, def.did(), impl_span); ty::Foreign(did) => self.check_def_id(id, self_ty, did),
}
ty::Foreign(did) => {
self.check_def_id(item, self_ty, did, impl_span);
}
ty::Dynamic(data, ..) if data.principal_def_id().is_some() => { 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(..) => { ty::Dynamic(..) => {
struct_span_err!( struct_span_err!(
self.tcx.sess, self.tcx.sess,
impl_span, item_span,
E0785, E0785,
"cannot define inherent `impl` for a dyn auto trait" "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") .note("define and implement a new trait or type instead")
.emit(); .emit();
} }
@ -221,18 +203,16 @@ fn check_item(&mut self, id: hir::ItemId) {
| ty::Ref(..) | ty::Ref(..)
| ty::Never | ty::Never
| ty::FnPtr(_) | ty::FnPtr(_)
| ty::Tuple(..) => { | ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
self.check_primitive_impl(item.owner_id.def_id, self_ty, items, impl_span)
}
ty::Alias(..) | ty::Param(_) => { ty::Alias(..) | ty::Param(_) => {
let mut err = struct_span_err!( let mut err = struct_span_err!(
self.tcx.sess, self.tcx.sess,
impl_span, item_span,
E0118, E0118,
"no nominal type found for inherent implementation" "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"); .note("either implement a trait on it or create a newtype to wrap it instead");
err.emit(); err.emit();
@ -245,7 +225,7 @@ fn check_item(&mut self, id: hir::ItemId) {
| ty::Bound(..) | ty::Bound(..)
| ty::Placeholder(_) | ty::Placeholder(_)
| ty::Infer(_) => { | 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(_) => {} ty::Error(_) => {}
} }