Skip to content

Commit

Permalink
Rollup merge of rust-lang#119869 - oli-obk:track_errors2, r=matthewja…
Browse files Browse the repository at this point in the history
…sper

replace `track_errors` usages with bubbling up `ErrorGuaranteed`

more of the same as rust-lang#117449 (removing `track_errors`)
  • Loading branch information
matthiaskrgr committed Jan 18, 2024
2 parents c0da80f + 18e6643 commit fa52eda
Show file tree
Hide file tree
Showing 33 changed files with 345 additions and 195 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1446,7 +1446,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}

let candidates: Vec<_> = tcx
.inherent_impls(adt_did)
.inherent_impls(adt_did)?
.iter()
.filter_map(|&impl_| Some((impl_, self.lookup_assoc_ty_unchecked(name, block, impl_)?)))
.collect();
Expand Down
79 changes: 48 additions & 31 deletions compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{self, CrateInherentImpls, Ty, TyCtxt};
use rustc_span::symbol::sym;
use rustc_span::ErrorGuaranteed;

use crate::errors;

/// 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 res = Ok(());
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] {
let crate_map = tcx.crate_inherent_impls(());
tcx.arena.alloc_from_iter(
pub fn crate_incoherent_impls(
tcx: TyCtxt<'_>,
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()),
)
))
}

/// 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] {
let crate_map = tcx.crate_inherent_impls(());
match crate_map.inherent_impls.get(&ty_def_id) {
pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: LocalDefId) -> Result<&[DefId], ErrorGuaranteed> {
let crate_map = tcx.crate_inherent_impls(())?;
Ok(match crate_map.inherent_impls.get(&ty_def_id) {
Some(v) => &v[..],
None => &[],
}
})
}

struct InherentCollect<'tcx> {
Expand All @@ -47,33 +56,36 @@ struct 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() {
// 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(ty_def_id).or_default();
vec.push(impl_def_id.to_def_id());
return;
return Ok(());
}

if self.tcx.features().rustc_attrs {
let items = self.tcx.associated_item_def_ids(impl_def_id);

if !self.tcx.has_attr(ty_def_id, sym::rustc_has_incoherent_inherent_impls) {
let impl_span = self.tcx.def_span(impl_def_id);
self.tcx.dcx().emit_err(errors::InherentTyOutside { span: impl_span });
return;
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutside { span: impl_span }));
}

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);
self.tcx.dcx().emit_err(errors::InherentTyOutsideRelevant {
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutsideRelevant {
span: impl_span,
help_span: self.tcx.def_span(impl_item),
});
return;
}));
}
}

Expand All @@ -82,24 +94,28 @@ impl<'tcx> InherentCollect<'tcx> {
} else {
bug!("unexpected self type: {:?}", self_ty);
}
Ok(())
} else {
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);
if !self.tcx.hir().rustc_coherence_is_core() {
if self.tcx.features().rustc_attrs {
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);
self.tcx.dcx().emit_err(errors::InherentTyOutsidePrimitive {
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutsidePrimitive {
span,
help_span: self.tcx.def_span(impl_item),
});
return;
}));
}
}
} else {
Expand All @@ -108,8 +124,7 @@ impl<'tcx> InherentCollect<'tcx> {
if let ty::Ref(_, subty, _) = ty.kind() {
note = Some(errors::InherentPrimitiveTyNote { subty: *subty });
}
self.tcx.dcx().emit_err(errors::InherentPrimitiveTy { span, note });
return;
return Err(self.tcx.dcx().emit_err(errors::InherentPrimitiveTy { span, note }));
}
}

Expand All @@ -118,11 +133,12 @@ impl<'tcx> InherentCollect<'tcx> {
} else {
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 }) {
return;
return Ok(());
}

let id = id.owner_id.def_id;
Expand All @@ -132,10 +148,10 @@ impl<'tcx> InherentCollect<'tcx> {
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(id, self_ty, data.principal_def_id().unwrap());
self.check_def_id(id, self_ty, data.principal_def_id().unwrap())
}
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::Char
Expand All @@ -151,7 +167,7 @@ impl<'tcx> InherentCollect<'tcx> {
| ty::FnPtr(_)
| ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
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::Closure(..)
Expand All @@ -162,7 +178,8 @@ impl<'tcx> InherentCollect<'tcx> {
| ty::Infer(_) => {
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(()),
}
}
}
48 changes: 29 additions & 19 deletions compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@ use rustc_hir::def_id::DefId;
use rustc_index::IndexVec;
use rustc_middle::traits::specialization_graph::OverlapMode;
use rustc_middle::ty::{self, TyCtxt};
use rustc_span::Symbol;
use rustc_span::{ErrorGuaranteed, Symbol};
use rustc_trait_selection::traits::{self, SkipLeakCheck};
use smallvec::SmallVec;
use std::collections::hash_map::Entry;

pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, (): ()) {
pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, (): ()) -> Result<(), ErrorGuaranteed> {
let mut inherent_overlap_checker = InherentOverlapChecker { tcx };
let mut res = Ok(());
for id in tcx.hir().items() {
inherent_overlap_checker.check_item(id);
res = res.and(inherent_overlap_checker.check_item(id));
}
res
}

struct InherentOverlapChecker<'tcx> {
Expand Down Expand Up @@ -58,10 +60,11 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
== item2.ident(self.tcx).normalize_to_macros_2_0()
}

fn check_for_duplicate_items_in_impl(&self, impl_: DefId) {
fn check_for_duplicate_items_in_impl(&self, impl_: DefId) -> Result<(), ErrorGuaranteed> {
let impl_items = self.tcx.associated_items(impl_);

let mut seen_items = FxHashMap::default();
let mut res = Ok(());
for impl_item in impl_items.in_definition_order() {
let span = self.tcx.def_span(impl_item.def_id);
let ident = impl_item.ident(self.tcx);
Expand All @@ -70,7 +73,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
match seen_items.entry(norm_ident) {
Entry::Occupied(entry) => {
let former = entry.get();
struct_span_code_err!(
res = Err(struct_span_code_err!(
self.tcx.dcx(),
span,
E0592,
Expand All @@ -79,24 +82,26 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
)
.with_span_label(span, format!("duplicate definitions for `{ident}`"))
.with_span_label(*former, format!("other definition for `{ident}`"))
.emit();
.emit());
}
Entry::Vacant(entry) => {
entry.insert(span);
}
}
}
res
}

fn check_for_common_items_in_impls(
&self,
impl1: DefId,
impl2: DefId,
overlap: traits::OverlapResult<'_>,
) {
) -> Result<(), ErrorGuaranteed> {
let impl_items1 = self.tcx.associated_items(impl1);
let impl_items2 = self.tcx.associated_items(impl2);

let mut res = Ok(());
for &item1 in impl_items1.in_definition_order() {
let collision = impl_items2
.filter_by_name_unhygienic(item1.name)
Expand Down Expand Up @@ -128,17 +133,18 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
traits::add_placeholder_note(&mut err);
}

err.emit();
res = Err(err.emit());
}
}
res
}

fn check_for_overlapping_inherent_impls(
&self,
overlap_mode: OverlapMode,
impl1_def_id: DefId,
impl2_def_id: DefId,
) {
) -> Result<(), ErrorGuaranteed> {
let maybe_overlap = traits::overlapping_impls(
self.tcx,
impl1_def_id,
Expand All @@ -150,17 +156,19 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
);

if let Some(overlap) = maybe_overlap {
self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap);
self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap)
} else {
Ok(())
}
}

fn check_item(&mut self, id: hir::ItemId) {
fn check_item(&mut self, id: hir::ItemId) -> Result<(), ErrorGuaranteed> {
let def_kind = self.tcx.def_kind(id.owner_id);
if !matches!(def_kind, DefKind::Enum | DefKind::Struct | DefKind::Trait | DefKind::Union) {
return;
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());

Expand All @@ -173,17 +181,18 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
// otherwise switch to an allocating algorithm with
// faster asymptotic runtime.
const ALLOCATING_ALGO_THRESHOLD: usize = 500;
let mut res = Ok(());
if impls.len() < ALLOCATING_ALGO_THRESHOLD {
for (i, &(&impl1_def_id, impl_items1)) in impls_items.iter().enumerate() {
self.check_for_duplicate_items_in_impl(impl1_def_id);
res = res.and(self.check_for_duplicate_items_in_impl(impl1_def_id));

for &(&impl2_def_id, impl_items2) in &impls_items[(i + 1)..] {
if self.impls_have_common_items(impl_items1, impl_items2) {
self.check_for_overlapping_inherent_impls(
res = res.and(self.check_for_overlapping_inherent_impls(
overlap_mode,
impl1_def_id,
impl2_def_id,
);
));
}
}
}
Expand Down Expand Up @@ -315,20 +324,21 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
impl_blocks.sort_unstable();
for (i, &impl1_items_idx) in impl_blocks.iter().enumerate() {
let &(&impl1_def_id, impl_items1) = &impls_items[impl1_items_idx];
self.check_for_duplicate_items_in_impl(impl1_def_id);
res = res.and(self.check_for_duplicate_items_in_impl(impl1_def_id));

for &impl2_items_idx in impl_blocks[(i + 1)..].iter() {
let &(&impl2_def_id, impl_items2) = &impls_items[impl2_items_idx];
if self.impls_have_common_items(impl_items1, impl_items2) {
self.check_for_overlapping_inherent_impls(
res = res.and(self.check_for_overlapping_inherent_impls(
overlap_mode,
impl1_def_id,
impl2_def_id,
);
));
}
}
}
}
}
res
}
}
8 changes: 5 additions & 3 deletions compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{self as hir, def, Expr, ImplItem, Item, Node, TraitItem};
use rustc_middle::hir::nested_filter;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
use rustc_span::{sym, DUMMY_SP};
use rustc_span::{sym, ErrorGuaranteed, DUMMY_SP};

use crate::errors::{TaitForwardCompat, TypeOf, UnconstrainedOpaqueType};

pub fn test_opaque_hidden_types(tcx: TyCtxt<'_>) {
pub fn test_opaque_hidden_types(tcx: TyCtxt<'_>) -> Result<(), ErrorGuaranteed> {
let mut res = Ok(());
if tcx.has_attr(CRATE_DEF_ID, sym::rustc_hidden_type_of_opaques) {
for id in tcx.hir().items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::OpaqueTy) {
let type_of = tcx.type_of(id.owner_id).instantiate_identity();

tcx.dcx().emit_err(TypeOf { span: tcx.def_span(id.owner_id), type_of });
res = Err(tcx.dcx().emit_err(TypeOf { span: tcx.def_span(id.owner_id), type_of }));
}
}
}
res
}

/// Checks "defining uses" of opaque `impl Trait` types to ensure that they meet the restrictions
Expand Down
Loading

0 comments on commit fa52eda

Please sign in to comment.