Make crate_inherent_impls fallible and stop using track_errors
for it
This commit is contained in:
parent
49347ee12d
commit
557b111870
19 changed files with 115 additions and 73 deletions
|
@ -1446,7 +1446,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
|
||||||
}
|
}
|
||||||
|
|
||||||
let candidates: Vec<_> = tcx
|
let candidates: Vec<_> = tcx
|
||||||
.inherent_impls(adt_did)
|
.inherent_impls(adt_did)?
|
||||||
.iter()
|
.iter()
|
||||||
.filter_map(|&impl_| Some((impl_, self.lookup_assoc_ty_unchecked(name, block, impl_)?)))
|
.filter_map(|&impl_| Some((impl_, self.lookup_assoc_ty_unchecked(name, block, impl_)?)))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
@ -13,32 +13,41 @@ use rustc_hir::def_id::{DefId, LocalDefId};
|
||||||
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::ErrorGuaranteed;
|
||||||
|
|
||||||
use crate::errors;
|
use crate::errors;
|
||||||
|
|
||||||
/// 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<'_>,
|
||||||
|
(): (),
|
||||||
|
) -> Result<&'_ CrateInherentImpls, ErrorGuaranteed> {
|
||||||
let mut collect = InherentCollect { tcx, impls_map: Default::default() };
|
let mut collect = InherentCollect { tcx, impls_map: Default::default() };
|
||||||
|
let mut res = Ok(());
|
||||||
for id in tcx.hir().items() {
|
for id in tcx.hir().items() {
|
||||||
collect.check_item(id);
|
res = res.and(collect.check_item(id));
|
||||||
}
|
}
|
||||||
collect.impls_map
|
res?;
|
||||||
|
Ok(tcx.arena.alloc(collect.impls_map))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn crate_incoherent_impls(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
|
pub fn crate_incoherent_impls(
|
||||||
let crate_map = tcx.crate_inherent_impls(());
|
tcx: TyCtxt<'_>,
|
||||||
tcx.arena.alloc_from_iter(
|
simp: SimplifiedType,
|
||||||
|
) -> Result<&[DefId], ErrorGuaranteed> {
|
||||||
|
let crate_map = tcx.crate_inherent_impls(())?;
|
||||||
|
Ok(tcx.arena.alloc_from_iter(
|
||||||
crate_map.incoherent_impls.get(&simp).unwrap_or(&Vec::new()).iter().map(|d| d.to_def_id()),
|
crate_map.incoherent_impls.get(&simp).unwrap_or(&Vec::new()).iter().map(|d| d.to_def_id()),
|
||||||
)
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// On-demand query: yields a vector of the inherent impls for a specific type.
|
/// On-demand query: yields a vector of the inherent impls for a specific type.
|
||||||
pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: LocalDefId) -> &[DefId] {
|
pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: LocalDefId) -> Result<&[DefId], ErrorGuaranteed> {
|
||||||
let crate_map = tcx.crate_inherent_impls(());
|
let crate_map = tcx.crate_inherent_impls(())?;
|
||||||
match crate_map.inherent_impls.get(&ty_def_id) {
|
Ok(match crate_map.inherent_impls.get(&ty_def_id) {
|
||||||
Some(v) => &v[..],
|
Some(v) => &v[..],
|
||||||
None => &[],
|
None => &[],
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
struct InherentCollect<'tcx> {
|
struct InherentCollect<'tcx> {
|
||||||
|
@ -47,14 +56,19 @@ struct InherentCollect<'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'tcx> InherentCollect<'tcx> {
|
impl<'tcx> InherentCollect<'tcx> {
|
||||||
fn check_def_id(&mut self, impl_def_id: LocalDefId, self_ty: Ty<'tcx>, ty_def_id: DefId) {
|
fn check_def_id(
|
||||||
|
&mut self,
|
||||||
|
impl_def_id: LocalDefId,
|
||||||
|
self_ty: Ty<'tcx>,
|
||||||
|
ty_def_id: DefId,
|
||||||
|
) -> Result<(), ErrorGuaranteed> {
|
||||||
if let Some(ty_def_id) = ty_def_id.as_local() {
|
if let Some(ty_def_id) = ty_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(ty_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 Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.tcx.features().rustc_attrs {
|
if self.tcx.features().rustc_attrs {
|
||||||
|
@ -62,18 +76,16 @@ impl<'tcx> InherentCollect<'tcx> {
|
||||||
|
|
||||||
if !self.tcx.has_attr(ty_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);
|
let impl_span = self.tcx.def_span(impl_def_id);
|
||||||
self.tcx.dcx().emit_err(errors::InherentTyOutside { span: impl_span });
|
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutside { span: impl_span }));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for &impl_item in items {
|
for &impl_item in items {
|
||||||
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
|
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
|
||||||
let impl_span = self.tcx.def_span(impl_def_id);
|
let impl_span = self.tcx.def_span(impl_def_id);
|
||||||
self.tcx.dcx().emit_err(errors::InherentTyOutsideRelevant {
|
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutsideRelevant {
|
||||||
span: impl_span,
|
span: impl_span,
|
||||||
help_span: self.tcx.def_span(impl_item),
|
help_span: self.tcx.def_span(impl_item),
|
||||||
});
|
}));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -82,24 +94,28 @@ impl<'tcx> InherentCollect<'tcx> {
|
||||||
} else {
|
} else {
|
||||||
bug!("unexpected self type: {:?}", self_ty);
|
bug!("unexpected self type: {:?}", self_ty);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
let impl_span = self.tcx.def_span(impl_def_id);
|
let impl_span = self.tcx.def_span(impl_def_id);
|
||||||
self.tcx.dcx().emit_err(errors::InherentTyOutsideNew { span: impl_span });
|
Err(self.tcx.dcx().emit_err(errors::InherentTyOutsideNew { span: impl_span }))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_primitive_impl(&mut self, impl_def_id: LocalDefId, ty: Ty<'tcx>) {
|
fn check_primitive_impl(
|
||||||
|
&mut self,
|
||||||
|
impl_def_id: LocalDefId,
|
||||||
|
ty: Ty<'tcx>,
|
||||||
|
) -> Result<(), ErrorGuaranteed> {
|
||||||
let items = self.tcx.associated_item_def_ids(impl_def_id);
|
let items = self.tcx.associated_item_def_ids(impl_def_id);
|
||||||
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 &impl_item in items {
|
for &impl_item in items {
|
||||||
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
|
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
|
||||||
let span = self.tcx.def_span(impl_def_id);
|
let span = self.tcx.def_span(impl_def_id);
|
||||||
self.tcx.dcx().emit_err(errors::InherentTyOutsidePrimitive {
|
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutsidePrimitive {
|
||||||
span,
|
span,
|
||||||
help_span: self.tcx.def_span(impl_item),
|
help_span: self.tcx.def_span(impl_item),
|
||||||
});
|
}));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -108,8 +124,7 @@ impl<'tcx> InherentCollect<'tcx> {
|
||||||
if let ty::Ref(_, subty, _) = ty.kind() {
|
if let ty::Ref(_, subty, _) = ty.kind() {
|
||||||
note = Some(errors::InherentPrimitiveTyNote { subty: *subty });
|
note = Some(errors::InherentPrimitiveTyNote { subty: *subty });
|
||||||
}
|
}
|
||||||
self.tcx.dcx().emit_err(errors::InherentPrimitiveTy { span, note });
|
return Err(self.tcx.dcx().emit_err(errors::InherentPrimitiveTy { span, note }));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,11 +133,12 @@ impl<'tcx> InherentCollect<'tcx> {
|
||||||
} else {
|
} else {
|
||||||
bug!("unexpected primitive type: {:?}", ty);
|
bug!("unexpected primitive type: {:?}", ty);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_item(&mut self, id: hir::ItemId) {
|
fn check_item(&mut self, id: hir::ItemId) -> Result<(), ErrorGuaranteed> {
|
||||||
if !matches!(self.tcx.def_kind(id.owner_id), DefKind::Impl { of_trait: false }) {
|
if !matches!(self.tcx.def_kind(id.owner_id), DefKind::Impl { of_trait: false }) {
|
||||||
return;
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = id.owner_id.def_id;
|
let id = id.owner_id.def_id;
|
||||||
|
@ -132,10 +148,10 @@ impl<'tcx> InherentCollect<'tcx> {
|
||||||
ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()),
|
ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()),
|
||||||
ty::Foreign(did) => self.check_def_id(id, self_ty, did),
|
ty::Foreign(did) => self.check_def_id(id, self_ty, did),
|
||||||
ty::Dynamic(data, ..) if data.principal_def_id().is_some() => {
|
ty::Dynamic(data, ..) if data.principal_def_id().is_some() => {
|
||||||
self.check_def_id(id, self_ty, data.principal_def_id().unwrap());
|
self.check_def_id(id, self_ty, data.principal_def_id().unwrap())
|
||||||
}
|
}
|
||||||
ty::Dynamic(..) => {
|
ty::Dynamic(..) => {
|
||||||
self.tcx.dcx().emit_err(errors::InherentDyn { span: item_span });
|
Err(self.tcx.dcx().emit_err(errors::InherentDyn { span: item_span }))
|
||||||
}
|
}
|
||||||
ty::Bool
|
ty::Bool
|
||||||
| ty::Char
|
| ty::Char
|
||||||
|
@ -151,7 +167,7 @@ impl<'tcx> InherentCollect<'tcx> {
|
||||||
| ty::FnPtr(_)
|
| ty::FnPtr(_)
|
||||||
| ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
|
| ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
|
||||||
ty::Alias(..) | ty::Param(_) => {
|
ty::Alias(..) | ty::Param(_) => {
|
||||||
self.tcx.dcx().emit_err(errors::InherentNominal { span: item_span });
|
Err(self.tcx.dcx().emit_err(errors::InherentNominal { span: item_span }))
|
||||||
}
|
}
|
||||||
ty::FnDef(..)
|
ty::FnDef(..)
|
||||||
| ty::Closure(..)
|
| ty::Closure(..)
|
||||||
|
@ -162,7 +178,8 @@ impl<'tcx> InherentCollect<'tcx> {
|
||||||
| ty::Infer(_) => {
|
| ty::Infer(_) => {
|
||||||
bug!("unexpected impl self type of impl: {:?} {:?}", id, self_ty);
|
bug!("unexpected impl self type of impl: {:?} {:?}", id, self_ty);
|
||||||
}
|
}
|
||||||
ty::Error(_) => {}
|
// We could bail out here, but that will silence other useful errors.
|
||||||
|
ty::Error(_) => Ok(()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -168,7 +168,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let impls = self.tcx.inherent_impls(id.owner_id);
|
let impls = self.tcx.inherent_impls(id.owner_id)?;
|
||||||
|
|
||||||
let overlap_mode = OverlapMode::get(self.tcx, id.owner_id.to_def_id());
|
let overlap_mode = OverlapMode::get(self.tcx, id.owner_id.to_def_id());
|
||||||
|
|
||||||
|
|
|
@ -181,10 +181,9 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
|
||||||
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
|
for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
|
||||||
tcx.ensure().coherent_trait(trait_def_id);
|
tcx.ensure().coherent_trait(trait_def_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
// these queries are executed for side-effects (error reporting):
|
|
||||||
tcx.ensure().crate_inherent_impls(());
|
|
||||||
}))
|
}))
|
||||||
|
// these queries are executed for side-effects (error reporting):
|
||||||
|
.and(tcx.ensure().crate_inherent_impls(()))
|
||||||
.and(tcx.ensure().crate_inherent_impls_overlap_check(()))
|
.and(tcx.ensure().crate_inherent_impls_overlap_check(()))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
|
|
@ -2104,7 +2104,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
let mut items = self
|
let mut items = self
|
||||||
.tcx
|
.tcx
|
||||||
.inherent_impls(def_id)
|
.inherent_impls(def_id)
|
||||||
.iter()
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
.flat_map(|i| self.tcx.associated_items(i).in_definition_order())
|
.flat_map(|i| self.tcx.associated_items(i).in_definition_order())
|
||||||
// Only assoc fn with no receivers.
|
// Only assoc fn with no receivers.
|
||||||
.filter(|item| {
|
.filter(|item| {
|
||||||
|
|
|
@ -711,14 +711,14 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
|
||||||
let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsCandidateKey) else {
|
let Some(simp) = simplify_type(self.tcx, self_ty, TreatParams::AsCandidateKey) else {
|
||||||
bug!("unexpected incoherent type: {:?}", self_ty)
|
bug!("unexpected incoherent type: {:?}", self_ty)
|
||||||
};
|
};
|
||||||
for &impl_def_id in self.tcx.incoherent_impls(simp) {
|
for &impl_def_id in self.tcx.incoherent_impls(simp).into_iter().flatten() {
|
||||||
self.assemble_inherent_impl_probe(impl_def_id);
|
self.assemble_inherent_impl_probe(impl_def_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assemble_inherent_impl_candidates_for_type(&mut self, def_id: DefId) {
|
fn assemble_inherent_impl_candidates_for_type(&mut self, def_id: DefId) {
|
||||||
let impl_def_ids = self.tcx.at(self.span).inherent_impls(def_id);
|
let impl_def_ids = self.tcx.at(self.span).inherent_impls(def_id).into_iter().flatten();
|
||||||
for &impl_def_id in impl_def_ids.iter() {
|
for &impl_def_id in impl_def_ids {
|
||||||
self.assemble_inherent_impl_probe(impl_def_id);
|
self.assemble_inherent_impl_probe(impl_def_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -359,7 +359,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
if let ty::Adt(adt_def, _) = ty.kind() {
|
if let ty::Adt(adt_def, _) = ty.kind() {
|
||||||
self.tcx
|
self.tcx
|
||||||
.inherent_impls(adt_def.did())
|
.inherent_impls(adt_def.did())
|
||||||
.iter()
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
.any(|def_id| self.associated_value(*def_id, item_name).is_some())
|
.any(|def_id| self.associated_value(*def_id, item_name).is_some())
|
||||||
} else {
|
} else {
|
||||||
false
|
false
|
||||||
|
@ -1048,7 +1049,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
let mut inherent_impls_candidate = self
|
let mut inherent_impls_candidate = self
|
||||||
.tcx
|
.tcx
|
||||||
.inherent_impls(adt.did())
|
.inherent_impls(adt.did())
|
||||||
.iter()
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
.copied()
|
.copied()
|
||||||
.filter(|def_id| {
|
.filter(|def_id| {
|
||||||
if let Some(assoc) = self.associated_value(*def_id, item_name) {
|
if let Some(assoc) = self.associated_value(*def_id, item_name) {
|
||||||
|
@ -1103,7 +1105,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
"the {item_kind} was found for\n{type_candidates}{additional_types}"
|
"the {item_kind} was found for\n{type_candidates}{additional_types}"
|
||||||
));
|
));
|
||||||
} else {
|
} else {
|
||||||
'outer: for inherent_impl_did in self.tcx.inherent_impls(adt.did()) {
|
'outer: for inherent_impl_did in
|
||||||
|
self.tcx.inherent_impls(adt.did()).into_iter().flatten()
|
||||||
|
{
|
||||||
for inherent_method in
|
for inherent_method in
|
||||||
self.tcx.associated_items(inherent_impl_did).in_definition_order()
|
self.tcx.associated_items(inherent_impl_did).in_definition_order()
|
||||||
{
|
{
|
||||||
|
@ -1457,9 +1461,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
let ty::Adt(adt_def, _) = rcvr_ty.kind() else {
|
let ty::Adt(adt_def, _) = rcvr_ty.kind() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
let mut items = self
|
// FIXME(oli-obk): try out bubbling this error up one level and cancelling the other error in that case.
|
||||||
.tcx
|
let Ok(impls) = self.tcx.inherent_impls(adt_def.did()) else { return };
|
||||||
.inherent_impls(adt_def.did())
|
let mut items = impls
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|i| self.tcx.associated_items(i).in_definition_order())
|
.flat_map(|i| self.tcx.associated_items(i).in_definition_order())
|
||||||
// Only assoc fn with no receivers.
|
// Only assoc fn with no receivers.
|
||||||
|
@ -1823,7 +1827,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
||||||
simplify_type(tcx, ty, TreatParams::AsCandidateKey)
|
simplify_type(tcx, ty, TreatParams::AsCandidateKey)
|
||||||
.and_then(|simp| {
|
.and_then(|simp| {
|
||||||
tcx.incoherent_impls(simp)
|
tcx.incoherent_impls(simp)
|
||||||
.iter()
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
.find_map(|&id| self.associated_value(id, item_name))
|
.find_map(|&id| self.associated_value(id, item_name))
|
||||||
})
|
})
|
||||||
.is_some()
|
.is_some()
|
||||||
|
|
|
@ -283,7 +283,7 @@ provide! { tcx, def_id, other, cdata,
|
||||||
tcx.arena.alloc_from_iter(cdata.get_associated_item_or_field_def_ids(def_id.index))
|
tcx.arena.alloc_from_iter(cdata.get_associated_item_or_field_def_ids(def_id.index))
|
||||||
}
|
}
|
||||||
associated_item => { cdata.get_associated_item(def_id.index, tcx.sess) }
|
associated_item => { cdata.get_associated_item(def_id.index, tcx.sess) }
|
||||||
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
|
inherent_impls => { Ok(cdata.get_inherent_implementations_for_type(tcx, def_id.index)) }
|
||||||
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
|
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
|
||||||
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
|
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
|
||||||
is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }
|
is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }
|
||||||
|
@ -328,7 +328,7 @@ provide! { tcx, def_id, other, cdata,
|
||||||
traits => { tcx.arena.alloc_from_iter(cdata.get_traits()) }
|
traits => { tcx.arena.alloc_from_iter(cdata.get_traits()) }
|
||||||
trait_impls_in_crate => { tcx.arena.alloc_from_iter(cdata.get_trait_impls()) }
|
trait_impls_in_crate => { tcx.arena.alloc_from_iter(cdata.get_trait_impls()) }
|
||||||
implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) }
|
implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) }
|
||||||
crate_incoherent_impls => { cdata.get_incoherent_impls(tcx, other) }
|
crate_incoherent_impls => { Ok(cdata.get_incoherent_impls(tcx, other)) }
|
||||||
|
|
||||||
dep_kind => { cdata.dep_kind }
|
dep_kind => { cdata.dep_kind }
|
||||||
module_children => {
|
module_children => {
|
||||||
|
|
|
@ -1485,7 +1485,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
let inherent_impls = tcx.with_stable_hashing_context(|hcx| {
|
let inherent_impls = tcx.with_stable_hashing_context(|hcx| {
|
||||||
tcx.crate_inherent_impls(()).inherent_impls.to_sorted(&hcx, true)
|
tcx.crate_inherent_impls(()).unwrap().inherent_impls.to_sorted(&hcx, true)
|
||||||
});
|
});
|
||||||
for (def_id, impls) in inherent_impls {
|
for (def_id, impls) in inherent_impls {
|
||||||
record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| {
|
record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| {
|
||||||
|
@ -2028,7 +2028,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
|
||||||
empty_proc_macro!(self);
|
empty_proc_macro!(self);
|
||||||
let tcx = self.tcx;
|
let tcx = self.tcx;
|
||||||
let all_impls = tcx.with_stable_hashing_context(|hcx| {
|
let all_impls = tcx.with_stable_hashing_context(|hcx| {
|
||||||
tcx.crate_inherent_impls(()).incoherent_impls.to_sorted(&hcx, true)
|
tcx.crate_inherent_impls(()).unwrap().incoherent_impls.to_sorted(&hcx, true)
|
||||||
});
|
});
|
||||||
|
|
||||||
let all_impls: Vec<_> = all_impls
|
let all_impls: Vec<_> = all_impls
|
||||||
|
|
|
@ -114,6 +114,7 @@ macro_rules! arena_types {
|
||||||
[] mod_child: rustc_middle::metadata::ModChild,
|
[] mod_child: rustc_middle::metadata::ModChild,
|
||||||
[] features: rustc_feature::Features,
|
[] features: rustc_feature::Features,
|
||||||
[decode] specialization_graph: rustc_middle::traits::specialization_graph::Graph,
|
[decode] specialization_graph: rustc_middle::traits::specialization_graph::Graph,
|
||||||
|
[] crate_inherent_impls: rustc_middle::ty::CrateInherentImpls,
|
||||||
]);
|
]);
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -78,6 +78,10 @@ impl<T> EraseType for Result<&'_ T, rustc_errors::ErrorGuaranteed> {
|
||||||
type Result = [u8; size_of::<Result<&'static (), rustc_errors::ErrorGuaranteed>>()];
|
type Result = [u8; size_of::<Result<&'static (), rustc_errors::ErrorGuaranteed>>()];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> EraseType for Result<&'_ [T], rustc_errors::ErrorGuaranteed> {
|
||||||
|
type Result = [u8; size_of::<Result<&'static [()], rustc_errors::ErrorGuaranteed>>()];
|
||||||
|
}
|
||||||
|
|
||||||
impl<T> EraseType for Result<&'_ T, traits::CodegenObligationError> {
|
impl<T> EraseType for Result<&'_ T, traits::CodegenObligationError> {
|
||||||
type Result = [u8; size_of::<Result<&'static (), traits::CodegenObligationError>>()];
|
type Result = [u8; size_of::<Result<&'static (), traits::CodegenObligationError>>()];
|
||||||
}
|
}
|
||||||
|
|
|
@ -859,13 +859,13 @@ rustc_queries! {
|
||||||
/// Maps a `DefId` of a type to a list of its inherent impls.
|
/// Maps a `DefId` of a type to a list of its inherent impls.
|
||||||
/// Contains implementations of methods that are inherent to a type.
|
/// Contains implementations of methods that are inherent to a type.
|
||||||
/// Methods in these implementations don't need to be exported.
|
/// Methods in these implementations don't need to be exported.
|
||||||
query inherent_impls(key: DefId) -> &'tcx [DefId] {
|
query inherent_impls(key: DefId) -> Result<&'tcx [DefId], ErrorGuaranteed> {
|
||||||
desc { |tcx| "collecting inherent impls for `{}`", tcx.def_path_str(key) }
|
desc { |tcx| "collecting inherent impls for `{}`", tcx.def_path_str(key) }
|
||||||
cache_on_disk_if { key.is_local() }
|
cache_on_disk_if { key.is_local() }
|
||||||
separate_provide_extern
|
separate_provide_extern
|
||||||
}
|
}
|
||||||
|
|
||||||
query incoherent_impls(key: SimplifiedType) -> &'tcx [DefId] {
|
query incoherent_impls(key: SimplifiedType) -> Result<&'tcx [DefId], ErrorGuaranteed> {
|
||||||
desc { |tcx| "collecting all inherent impls for `{:?}`", key }
|
desc { |tcx| "collecting all inherent impls for `{:?}`", key }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1012,9 +1012,9 @@ rustc_queries! {
|
||||||
|
|
||||||
/// Gets a complete map from all types to their inherent impls.
|
/// Gets a complete map from all types to their inherent impls.
|
||||||
/// Not meant to be used directly outside of coherence.
|
/// Not meant to be used directly outside of coherence.
|
||||||
query crate_inherent_impls(k: ()) -> &'tcx CrateInherentImpls {
|
query crate_inherent_impls(k: ()) -> Result<&'tcx CrateInherentImpls, ErrorGuaranteed> {
|
||||||
arena_cache
|
|
||||||
desc { "finding all inherent impls defined in crate" }
|
desc { "finding all inherent impls defined in crate" }
|
||||||
|
ensure_forwards_result_if_red
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks all types in the crate for overlap in their inherent impls. Reports errors.
|
/// Checks all types in the crate for overlap in their inherent impls. Reports errors.
|
||||||
|
@ -1647,7 +1647,7 @@ rustc_queries! {
|
||||||
///
|
///
|
||||||
/// Do not call this directly, but instead use the `incoherent_impls` query.
|
/// Do not call this directly, but instead use the `incoherent_impls` query.
|
||||||
/// This query is only used to get the data necessary for that query.
|
/// This query is only used to get the data necessary for that query.
|
||||||
query crate_incoherent_impls(key: (CrateNum, SimplifiedType)) -> &'tcx [DefId] {
|
query crate_incoherent_impls(key: (CrateNum, SimplifiedType)) -> Result<&'tcx [DefId], ErrorGuaranteed> {
|
||||||
desc { |tcx| "collecting all impls for a type in a crate" }
|
desc { |tcx| "collecting all impls for a type in a crate" }
|
||||||
separate_provide_extern
|
separate_provide_extern
|
||||||
}
|
}
|
||||||
|
|
|
@ -174,7 +174,7 @@ pub fn query_ensure<'tcx, Cache>(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn query_ensure_error_guaranteed<'tcx, Cache>(
|
pub fn query_ensure_error_guaranteed<'tcx, Cache, T>(
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'tcx>,
|
||||||
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
|
execute_query: fn(TyCtxt<'tcx>, Span, Cache::Key, QueryMode) -> Option<Cache::Value>,
|
||||||
query_cache: &Cache,
|
query_cache: &Cache,
|
||||||
|
@ -182,14 +182,16 @@ pub fn query_ensure_error_guaranteed<'tcx, Cache>(
|
||||||
check_cache: bool,
|
check_cache: bool,
|
||||||
) -> Result<(), ErrorGuaranteed>
|
) -> Result<(), ErrorGuaranteed>
|
||||||
where
|
where
|
||||||
Cache: QueryCache<Value = super::erase::Erase<Result<(), ErrorGuaranteed>>>,
|
Cache: QueryCache<Value = super::erase::Erase<Result<T, ErrorGuaranteed>>>,
|
||||||
|
Result<T, ErrorGuaranteed>: EraseType,
|
||||||
{
|
{
|
||||||
let key = key.into_query_param();
|
let key = key.into_query_param();
|
||||||
if let Some(res) = try_get_cached(tcx, query_cache, &key) {
|
if let Some(res) = try_get_cached(tcx, query_cache, &key) {
|
||||||
super::erase::restore(res)
|
super::erase::restore(res).map(drop)
|
||||||
} else {
|
} else {
|
||||||
execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { check_cache })
|
execute_query(tcx, DUMMY_SP, key, QueryMode::Ensure { check_cache })
|
||||||
.map(super::erase::restore)
|
.map(super::erase::restore)
|
||||||
|
.map(|res| res.map(drop))
|
||||||
// Either we actually executed the query, which means we got a full `Result`,
|
// Either we actually executed the query, which means we got a full `Result`,
|
||||||
// or we can just assume the query succeeded, because it was green in the
|
// or we can just assume the query succeeded, because it was green in the
|
||||||
// incremental cache. If it is green, that means that the previous compilation
|
// incremental cache. If it is green, that means that the previous compilation
|
||||||
|
@ -205,7 +207,7 @@ macro_rules! query_ensure {
|
||||||
query_ensure($($args)*)
|
query_ensure($($args)*)
|
||||||
};
|
};
|
||||||
([(ensure_forwards_result_if_red) $($rest:tt)*]$($args:tt)*) => {
|
([(ensure_forwards_result_if_red) $($rest:tt)*]$($args:tt)*) => {
|
||||||
query_ensure_error_guaranteed($($args)*)
|
query_ensure_error_guaranteed($($args)*).map(|_| ())
|
||||||
};
|
};
|
||||||
([$other:tt $($modifiers:tt)*]$($args:tt)*) => {
|
([$other:tt $($modifiers:tt)*]$($args:tt)*) => {
|
||||||
query_ensure!([$($modifiers)*]$($args)*)
|
query_ensure!([$($modifiers)*]$($args)*)
|
||||||
|
@ -667,5 +669,7 @@ mod sealed {
|
||||||
|
|
||||||
pub use sealed::IntoQueryParam;
|
pub use sealed::IntoQueryParam;
|
||||||
|
|
||||||
|
use super::erase::EraseType;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, HashStable)]
|
#[derive(Copy, Clone, Debug, HashStable)]
|
||||||
pub struct CyclePlaceholder(pub ErrorGuaranteed);
|
pub struct CyclePlaceholder(pub ErrorGuaranteed);
|
||||||
|
|
|
@ -254,16 +254,28 @@ pub(super) fn trait_impls_of_provider(tcx: TyCtxt<'_>, trait_id: DefId) -> Trait
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Query provider for `incoherent_impls`.
|
/// Query provider for `incoherent_impls`.
|
||||||
pub(super) fn incoherent_impls_provider(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
|
pub(super) fn incoherent_impls_provider(
|
||||||
|
tcx: TyCtxt<'_>,
|
||||||
|
simp: SimplifiedType,
|
||||||
|
) -> Result<&[DefId], ErrorGuaranteed> {
|
||||||
let mut impls = Vec::new();
|
let mut impls = Vec::new();
|
||||||
|
|
||||||
|
let mut res = Ok(());
|
||||||
for cnum in iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) {
|
for cnum in iter::once(LOCAL_CRATE).chain(tcx.crates(()).iter().copied()) {
|
||||||
for &impl_def_id in tcx.crate_incoherent_impls((cnum, simp)) {
|
let incoherent_impls = match tcx.crate_incoherent_impls((cnum, simp)) {
|
||||||
|
Ok(impls) => impls,
|
||||||
|
Err(e) => {
|
||||||
|
res = Err(e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
for &impl_def_id in incoherent_impls {
|
||||||
impls.push(impl_def_id)
|
impls.push(impl_def_id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!(?impls);
|
debug!(?impls);
|
||||||
|
res?;
|
||||||
|
|
||||||
tcx.arena.alloc_slice(&impls)
|
Ok(tcx.arena.alloc_slice(&impls))
|
||||||
}
|
}
|
||||||
|
|
|
@ -1433,7 +1433,7 @@ fn collect_alloc<'tcx>(tcx: TyCtxt<'tcx>, alloc_id: AllocId, output: &mut MonoIt
|
||||||
}
|
}
|
||||||
|
|
||||||
fn assoc_fn_of_type<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, fn_ident: Ident) -> Option<DefId> {
|
fn assoc_fn_of_type<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, fn_ident: Ident) -> Option<DefId> {
|
||||||
for impl_def_id in tcx.inherent_impls(def_id) {
|
for impl_def_id in tcx.inherent_impls(def_id).ok()? {
|
||||||
if let Some(new) = tcx.associated_items(impl_def_id).find_by_name_and_kind(
|
if let Some(new) = tcx.associated_items(impl_def_id).find_by_name_and_kind(
|
||||||
tcx,
|
tcx,
|
||||||
fn_ident,
|
fn_ident,
|
||||||
|
|
|
@ -1745,12 +1745,10 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
|
||||||
// Doing analysis on local `DefId`s would cause infinite recursion.
|
// Doing analysis on local `DefId`s would cause infinite recursion.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
let Ok(impls) = self.r.tcx.inherent_impls(def_id) else { return };
|
||||||
// Look at all the associated functions without receivers in the type's
|
// Look at all the associated functions without receivers in the type's
|
||||||
// inherent impls to look for builders that return `Self`
|
// inherent impls to look for builders that return `Self`
|
||||||
let mut items = self
|
let mut items = impls
|
||||||
.r
|
|
||||||
.tcx
|
|
||||||
.inherent_impls(def_id)
|
|
||||||
.iter()
|
.iter()
|
||||||
.flat_map(|i| self.r.tcx.associated_items(i).in_definition_order())
|
.flat_map(|i| self.r.tcx.associated_items(i).in_definition_order())
|
||||||
// Only assoc fn with no receivers.
|
// Only assoc fn with no receivers.
|
||||||
|
|
|
@ -312,7 +312,7 @@ pub(crate) fn build_impls(
|
||||||
let tcx = cx.tcx;
|
let tcx = cx.tcx;
|
||||||
|
|
||||||
// for each implementation of an item represented by `did`, build the clean::Item for that impl
|
// for each implementation of an item represented by `did`, build the clean::Item for that impl
|
||||||
for &did in tcx.inherent_impls(did).iter() {
|
for &did in tcx.inherent_impls(did).into_iter().flatten() {
|
||||||
build_impl(cx, did, attrs, ret);
|
build_impl(cx, did, attrs, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -325,7 +325,7 @@ pub(crate) fn build_impls(
|
||||||
if tcx.has_attr(did, sym::rustc_has_incoherent_inherent_impls) {
|
if tcx.has_attr(did, sym::rustc_has_incoherent_inherent_impls) {
|
||||||
let type_ =
|
let type_ =
|
||||||
if tcx.is_trait(did) { SimplifiedType::Trait(did) } else { SimplifiedType::Adt(did) };
|
if tcx.is_trait(did) { SimplifiedType::Trait(did) } else { SimplifiedType::Adt(did) };
|
||||||
for &did in tcx.incoherent_impls(type_) {
|
for &did in tcx.incoherent_impls(type_).into_iter().flatten() {
|
||||||
build_impl(cx, did, attrs, ret);
|
build_impl(cx, did, attrs, ret);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1862,7 +1862,7 @@ impl PrimitiveType {
|
||||||
.get(self)
|
.get(self)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flatten()
|
.flatten()
|
||||||
.flat_map(move |&simp| tcx.incoherent_impls(simp))
|
.flat_map(move |&simp| tcx.incoherent_impls(simp).into_iter().flatten())
|
||||||
.copied()
|
.copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1870,7 +1870,7 @@ impl PrimitiveType {
|
||||||
Self::simplified_types()
|
Self::simplified_types()
|
||||||
.values()
|
.values()
|
||||||
.flatten()
|
.flatten()
|
||||||
.flat_map(move |&simp| tcx.incoherent_impls(simp))
|
.flat_map(move |&simp| tcx.incoherent_impls(simp).into_iter().flatten())
|
||||||
.copied()
|
.copied()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -624,7 +624,8 @@ impl<'a, 'tcx> LinkCollector<'a, 'tcx> {
|
||||||
// Checks if item_name belongs to `impl SomeItem`
|
// Checks if item_name belongs to `impl SomeItem`
|
||||||
let mut assoc_items: Vec<_> = tcx
|
let mut assoc_items: Vec<_> = tcx
|
||||||
.inherent_impls(did)
|
.inherent_impls(did)
|
||||||
.iter()
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
.flat_map(|&imp| {
|
.flat_map(|&imp| {
|
||||||
filter_assoc_items_by_name_and_namespace(
|
filter_assoc_items_by_name_and_namespace(
|
||||||
tcx,
|
tcx,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue