Skip to content

Commit fa52eda

Browse files
authored
Rollup merge of #119869 - oli-obk:track_errors2, r=matthewjasper
replace `track_errors` usages with bubbling up `ErrorGuaranteed` more of the same as #117449 (removing `track_errors`)
2 parents c0da80f + 18e6643 commit fa52eda

File tree

33 files changed

+345
-195
lines changed

33 files changed

+345
-195
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
14461446
}
14471447

14481448
let candidates: Vec<_> = tcx
1449-
.inherent_impls(adt_did)
1449+
.inherent_impls(adt_did)?
14501450
.iter()
14511451
.filter_map(|&impl_| Some((impl_, self.lookup_assoc_ty_unchecked(name, block, impl_)?)))
14521452
.collect();

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

+48-31
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,41 @@ use rustc_hir::def_id::{DefId, LocalDefId};
1313
use rustc_middle::ty::fast_reject::{simplify_type, SimplifiedType, TreatParams};
1414
use rustc_middle::ty::{self, CrateInherentImpls, Ty, TyCtxt};
1515
use rustc_span::symbol::sym;
16+
use rustc_span::ErrorGuaranteed;
1617

1718
use crate::errors;
1819

1920
/// On-demand query: yields a map containing all types mapped to their inherent impls.
20-
pub fn crate_inherent_impls(tcx: TyCtxt<'_>, (): ()) -> CrateInherentImpls {
21+
pub fn crate_inherent_impls(
22+
tcx: TyCtxt<'_>,
23+
(): (),
24+
) -> Result<&'_ CrateInherentImpls, ErrorGuaranteed> {
2125
let mut collect = InherentCollect { tcx, impls_map: Default::default() };
26+
let mut res = Ok(());
2227
for id in tcx.hir().items() {
23-
collect.check_item(id);
28+
res = res.and(collect.check_item(id));
2429
}
25-
collect.impls_map
30+
res?;
31+
Ok(tcx.arena.alloc(collect.impls_map))
2632
}
2733

28-
pub fn crate_incoherent_impls(tcx: TyCtxt<'_>, simp: SimplifiedType) -> &[DefId] {
29-
let crate_map = tcx.crate_inherent_impls(());
30-
tcx.arena.alloc_from_iter(
34+
pub fn crate_incoherent_impls(
35+
tcx: TyCtxt<'_>,
36+
simp: SimplifiedType,
37+
) -> Result<&[DefId], ErrorGuaranteed> {
38+
let crate_map = tcx.crate_inherent_impls(())?;
39+
Ok(tcx.arena.alloc_from_iter(
3140
crate_map.incoherent_impls.get(&simp).unwrap_or(&Vec::new()).iter().map(|d| d.to_def_id()),
32-
)
41+
))
3342
}
3443

3544
/// On-demand query: yields a vector of the inherent impls for a specific type.
36-
pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: LocalDefId) -> &[DefId] {
37-
let crate_map = tcx.crate_inherent_impls(());
38-
match crate_map.inherent_impls.get(&ty_def_id) {
45+
pub fn inherent_impls(tcx: TyCtxt<'_>, ty_def_id: LocalDefId) -> Result<&[DefId], ErrorGuaranteed> {
46+
let crate_map = tcx.crate_inherent_impls(())?;
47+
Ok(match crate_map.inherent_impls.get(&ty_def_id) {
3948
Some(v) => &v[..],
4049
None => &[],
41-
}
50+
})
4251
}
4352

4453
struct InherentCollect<'tcx> {
@@ -47,33 +56,36 @@ struct InherentCollect<'tcx> {
4756
}
4857

4958
impl<'tcx> InherentCollect<'tcx> {
50-
fn check_def_id(&mut self, impl_def_id: LocalDefId, self_ty: Ty<'tcx>, ty_def_id: DefId) {
59+
fn check_def_id(
60+
&mut self,
61+
impl_def_id: LocalDefId,
62+
self_ty: Ty<'tcx>,
63+
ty_def_id: DefId,
64+
) -> Result<(), ErrorGuaranteed> {
5165
if let Some(ty_def_id) = ty_def_id.as_local() {
5266
// Add the implementation to the mapping from implementation to base
5367
// type def ID, if there is a base type for this implementation and
5468
// the implementation does not have any associated traits.
5569
let vec = self.impls_map.inherent_impls.entry(ty_def_id).or_default();
5670
vec.push(impl_def_id.to_def_id());
57-
return;
71+
return Ok(());
5872
}
5973

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

6377
if !self.tcx.has_attr(ty_def_id, sym::rustc_has_incoherent_inherent_impls) {
6478
let impl_span = self.tcx.def_span(impl_def_id);
65-
self.tcx.dcx().emit_err(errors::InherentTyOutside { span: impl_span });
66-
return;
79+
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutside { span: impl_span }));
6780
}
6881

6982
for &impl_item in items {
7083
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
7184
let impl_span = self.tcx.def_span(impl_def_id);
72-
self.tcx.dcx().emit_err(errors::InherentTyOutsideRelevant {
85+
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutsideRelevant {
7386
span: impl_span,
7487
help_span: self.tcx.def_span(impl_item),
75-
});
76-
return;
88+
}));
7789
}
7890
}
7991

@@ -82,24 +94,28 @@ impl<'tcx> InherentCollect<'tcx> {
8294
} else {
8395
bug!("unexpected self type: {:?}", self_ty);
8496
}
97+
Ok(())
8598
} else {
8699
let impl_span = self.tcx.def_span(impl_def_id);
87-
self.tcx.dcx().emit_err(errors::InherentTyOutsideNew { span: impl_span });
100+
Err(self.tcx.dcx().emit_err(errors::InherentTyOutsideNew { span: impl_span }))
88101
}
89102
}
90103

91-
fn check_primitive_impl(&mut self, impl_def_id: LocalDefId, ty: Ty<'tcx>) {
104+
fn check_primitive_impl(
105+
&mut self,
106+
impl_def_id: LocalDefId,
107+
ty: Ty<'tcx>,
108+
) -> Result<(), ErrorGuaranteed> {
92109
let items = self.tcx.associated_item_def_ids(impl_def_id);
93110
if !self.tcx.hir().rustc_coherence_is_core() {
94111
if self.tcx.features().rustc_attrs {
95112
for &impl_item in items {
96113
if !self.tcx.has_attr(impl_item, sym::rustc_allow_incoherent_impl) {
97114
let span = self.tcx.def_span(impl_def_id);
98-
self.tcx.dcx().emit_err(errors::InherentTyOutsidePrimitive {
115+
return Err(self.tcx.dcx().emit_err(errors::InherentTyOutsidePrimitive {
99116
span,
100117
help_span: self.tcx.def_span(impl_item),
101-
});
102-
return;
118+
}));
103119
}
104120
}
105121
} else {
@@ -108,8 +124,7 @@ impl<'tcx> InherentCollect<'tcx> {
108124
if let ty::Ref(_, subty, _) = ty.kind() {
109125
note = Some(errors::InherentPrimitiveTyNote { subty: *subty });
110126
}
111-
self.tcx.dcx().emit_err(errors::InherentPrimitiveTy { span, note });
112-
return;
127+
return Err(self.tcx.dcx().emit_err(errors::InherentPrimitiveTy { span, note }));
113128
}
114129
}
115130

@@ -118,11 +133,12 @@ impl<'tcx> InherentCollect<'tcx> {
118133
} else {
119134
bug!("unexpected primitive type: {:?}", ty);
120135
}
136+
Ok(())
121137
}
122138

123-
fn check_item(&mut self, id: hir::ItemId) {
139+
fn check_item(&mut self, id: hir::ItemId) -> Result<(), ErrorGuaranteed> {
124140
if !matches!(self.tcx.def_kind(id.owner_id), DefKind::Impl { of_trait: false }) {
125-
return;
141+
return Ok(());
126142
}
127143

128144
let id = id.owner_id.def_id;
@@ -132,10 +148,10 @@ impl<'tcx> InherentCollect<'tcx> {
132148
ty::Adt(def, _) => self.check_def_id(id, self_ty, def.did()),
133149
ty::Foreign(did) => self.check_def_id(id, self_ty, did),
134150
ty::Dynamic(data, ..) if data.principal_def_id().is_some() => {
135-
self.check_def_id(id, self_ty, data.principal_def_id().unwrap());
151+
self.check_def_id(id, self_ty, data.principal_def_id().unwrap())
136152
}
137153
ty::Dynamic(..) => {
138-
self.tcx.dcx().emit_err(errors::InherentDyn { span: item_span });
154+
Err(self.tcx.dcx().emit_err(errors::InherentDyn { span: item_span }))
139155
}
140156
ty::Bool
141157
| ty::Char
@@ -151,7 +167,7 @@ impl<'tcx> InherentCollect<'tcx> {
151167
| ty::FnPtr(_)
152168
| ty::Tuple(..) => self.check_primitive_impl(id, self_ty),
153169
ty::Alias(..) | ty::Param(_) => {
154-
self.tcx.dcx().emit_err(errors::InherentNominal { span: item_span });
170+
Err(self.tcx.dcx().emit_err(errors::InherentNominal { span: item_span }))
155171
}
156172
ty::FnDef(..)
157173
| ty::Closure(..)
@@ -162,7 +178,8 @@ impl<'tcx> InherentCollect<'tcx> {
162178
| ty::Infer(_) => {
163179
bug!("unexpected impl self type of impl: {:?} {:?}", id, self_ty);
164180
}
165-
ty::Error(_) => {}
181+
// We could bail out here, but that will silence other useful errors.
182+
ty::Error(_) => Ok(()),
166183
}
167184
}
168185
}

compiler/rustc_hir_analysis/src/coherence/inherent_impls_overlap.rs

+29-19
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@ use rustc_hir::def_id::DefId;
66
use rustc_index::IndexVec;
77
use rustc_middle::traits::specialization_graph::OverlapMode;
88
use rustc_middle::ty::{self, TyCtxt};
9-
use rustc_span::Symbol;
9+
use rustc_span::{ErrorGuaranteed, Symbol};
1010
use rustc_trait_selection::traits::{self, SkipLeakCheck};
1111
use smallvec::SmallVec;
1212
use std::collections::hash_map::Entry;
1313

14-
pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, (): ()) {
14+
pub fn crate_inherent_impls_overlap_check(tcx: TyCtxt<'_>, (): ()) -> Result<(), ErrorGuaranteed> {
1515
let mut inherent_overlap_checker = InherentOverlapChecker { tcx };
16+
let mut res = Ok(());
1617
for id in tcx.hir().items() {
17-
inherent_overlap_checker.check_item(id);
18+
res = res.and(inherent_overlap_checker.check_item(id));
1819
}
20+
res
1921
}
2022

2123
struct InherentOverlapChecker<'tcx> {
@@ -58,10 +60,11 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
5860
== item2.ident(self.tcx).normalize_to_macros_2_0()
5961
}
6062

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

6466
let mut seen_items = FxHashMap::default();
67+
let mut res = Ok(());
6568
for impl_item in impl_items.in_definition_order() {
6669
let span = self.tcx.def_span(impl_item.def_id);
6770
let ident = impl_item.ident(self.tcx);
@@ -70,7 +73,7 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
7073
match seen_items.entry(norm_ident) {
7174
Entry::Occupied(entry) => {
7275
let former = entry.get();
73-
struct_span_code_err!(
76+
res = Err(struct_span_code_err!(
7477
self.tcx.dcx(),
7578
span,
7679
E0592,
@@ -79,24 +82,26 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
7982
)
8083
.with_span_label(span, format!("duplicate definitions for `{ident}`"))
8184
.with_span_label(*former, format!("other definition for `{ident}`"))
82-
.emit();
85+
.emit());
8386
}
8487
Entry::Vacant(entry) => {
8588
entry.insert(span);
8689
}
8790
}
8891
}
92+
res
8993
}
9094

9195
fn check_for_common_items_in_impls(
9296
&self,
9397
impl1: DefId,
9498
impl2: DefId,
9599
overlap: traits::OverlapResult<'_>,
96-
) {
100+
) -> Result<(), ErrorGuaranteed> {
97101
let impl_items1 = self.tcx.associated_items(impl1);
98102
let impl_items2 = self.tcx.associated_items(impl2);
99103

104+
let mut res = Ok(());
100105
for &item1 in impl_items1.in_definition_order() {
101106
let collision = impl_items2
102107
.filter_by_name_unhygienic(item1.name)
@@ -128,17 +133,18 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
128133
traits::add_placeholder_note(&mut err);
129134
}
130135

131-
err.emit();
136+
res = Err(err.emit());
132137
}
133138
}
139+
res
134140
}
135141

136142
fn check_for_overlapping_inherent_impls(
137143
&self,
138144
overlap_mode: OverlapMode,
139145
impl1_def_id: DefId,
140146
impl2_def_id: DefId,
141-
) {
147+
) -> Result<(), ErrorGuaranteed> {
142148
let maybe_overlap = traits::overlapping_impls(
143149
self.tcx,
144150
impl1_def_id,
@@ -150,17 +156,19 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
150156
);
151157

152158
if let Some(overlap) = maybe_overlap {
153-
self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap);
159+
self.check_for_common_items_in_impls(impl1_def_id, impl2_def_id, overlap)
160+
} else {
161+
Ok(())
154162
}
155163
}
156164

157-
fn check_item(&mut self, id: hir::ItemId) {
165+
fn check_item(&mut self, id: hir::ItemId) -> Result<(), ErrorGuaranteed> {
158166
let def_kind = self.tcx.def_kind(id.owner_id);
159167
if !matches!(def_kind, DefKind::Enum | DefKind::Struct | DefKind::Trait | DefKind::Union) {
160-
return;
168+
return Ok(());
161169
}
162170

163-
let impls = self.tcx.inherent_impls(id.owner_id);
171+
let impls = self.tcx.inherent_impls(id.owner_id)?;
164172

165173
let overlap_mode = OverlapMode::get(self.tcx, id.owner_id.to_def_id());
166174

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

180189
for &(&impl2_def_id, impl_items2) in &impls_items[(i + 1)..] {
181190
if self.impls_have_common_items(impl_items1, impl_items2) {
182-
self.check_for_overlapping_inherent_impls(
191+
res = res.and(self.check_for_overlapping_inherent_impls(
183192
overlap_mode,
184193
impl1_def_id,
185194
impl2_def_id,
186-
);
195+
));
187196
}
188197
}
189198
}
@@ -315,20 +324,21 @@ impl<'tcx> InherentOverlapChecker<'tcx> {
315324
impl_blocks.sort_unstable();
316325
for (i, &impl1_items_idx) in impl_blocks.iter().enumerate() {
317326
let &(&impl1_def_id, impl_items1) = &impls_items[impl1_items_idx];
318-
self.check_for_duplicate_items_in_impl(impl1_def_id);
327+
res = res.and(self.check_for_duplicate_items_in_impl(impl1_def_id));
319328

320329
for &impl2_items_idx in impl_blocks[(i + 1)..].iter() {
321330
let &(&impl2_def_id, impl_items2) = &impls_items[impl2_items_idx];
322331
if self.impls_have_common_items(impl_items1, impl_items2) {
323-
self.check_for_overlapping_inherent_impls(
332+
res = res.and(self.check_for_overlapping_inherent_impls(
324333
overlap_mode,
325334
impl1_def_id,
326335
impl2_def_id,
327-
);
336+
));
328337
}
329338
}
330339
}
331340
}
332341
}
342+
res
333343
}
334344
}

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ use rustc_hir::intravisit::{self, Visitor};
55
use rustc_hir::{self as hir, def, Expr, ImplItem, Item, Node, TraitItem};
66
use rustc_middle::hir::nested_filter;
77
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
8-
use rustc_span::{sym, DUMMY_SP};
8+
use rustc_span::{sym, ErrorGuaranteed, DUMMY_SP};
99

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

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

18-
tcx.dcx().emit_err(TypeOf { span: tcx.def_span(id.owner_id), type_of });
19+
res = Err(tcx.dcx().emit_err(TypeOf { span: tcx.def_span(id.owner_id), type_of }));
1920
}
2021
}
2122
}
23+
res
2224
}
2325

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

0 commit comments

Comments
 (0)