Skip to content

Commit fd9a925

Browse files
committed
Automatically taint when reporting errors from ItemCtxt
1 parent cd3d98b commit fd9a925

26 files changed

+183
-374
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use rustc_ast::Recovered;
1818
use rustc_data_structures::captures::Captures;
1919
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
2020
use rustc_data_structures::unord::UnordMap;
21-
use rustc_errors::{struct_span_code_err, Applicability, Diag, ErrorGuaranteed, StashKey, E0228};
21+
use rustc_errors::{
22+
struct_span_code_err, Applicability, Diag, DiagCtxtHandle, ErrorGuaranteed, StashKey, E0228,
23+
};
2224
use rustc_hir::def::DefKind;
2325
use rustc_hir::def_id::{DefId, LocalDefId};
2426
use rustc_hir::intravisit::{self, walk_generics, Visitor};
@@ -370,6 +372,10 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
370372
self.tcx
371373
}
372374

375+
fn dcx(&self) -> DiagCtxtHandle<'_> {
376+
self.tcx.dcx().taintable_handle(&self.tainted_by_errors)
377+
}
378+
373379
fn item_def_id(&self) -> LocalDefId {
374380
self.item_def_id
375381
}

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

+20-19
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
7676
}
7777

7878
if unbounds.len() > 1 {
79-
tcx.dcx().emit_err(errors::MultipleRelaxedDefaultBounds {
79+
self.dcx().emit_err(errors::MultipleRelaxedDefaultBounds {
8080
spans: unbounds.iter().map(|ptr| ptr.span).collect(),
8181
});
8282
}
@@ -90,7 +90,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
9090
continue;
9191
}
9292
// There was a `?Trait` bound, but it was not `?Sized`; warn.
93-
tcx.dcx().span_warn(
93+
self.dcx().span_warn(
9494
unbound.span,
9595
"relaxing a default bound only does something for `?Sized`; \
9696
all other traits are not bound by default",
@@ -310,7 +310,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
310310
duplicates
311311
.entry(assoc_item.def_id)
312312
.and_modify(|prev_span| {
313-
tcx.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
313+
self.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
314314
span: constraint.span,
315315
prev_span: *prev_span,
316316
item_name: constraint.ident,
@@ -338,7 +338,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
338338
.into(),
339339
ty::GenericParamDefKind::Type { .. } => {
340340
let guar = *emitted_bad_param_err.get_or_insert_with(|| {
341-
tcx.dcx().emit_err(
341+
self.dcx().emit_err(
342342
crate::errors::ReturnTypeNotationIllegalParam::Type {
343343
span: path_span,
344344
param_span: tcx.def_span(param.def_id),
@@ -349,7 +349,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
349349
}
350350
ty::GenericParamDefKind::Const { .. } => {
351351
let guar = *emitted_bad_param_err.get_or_insert_with(|| {
352-
tcx.dcx().emit_err(
352+
self.dcx().emit_err(
353353
crate::errors::ReturnTypeNotationIllegalParam::Const {
354354
span: path_span,
355355
param_span: tcx.def_span(param.def_id),
@@ -371,7 +371,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
371371
{
372372
alias_ty.into()
373373
} else {
374-
return Err(tcx.dcx().emit_err(crate::errors::ReturnTypeNotationOnNonRpitit {
374+
return Err(self.dcx().emit_err(crate::errors::ReturnTypeNotationOnNonRpitit {
375375
span: constraint.span,
376376
ty: tcx.liberate_late_bound_regions(assoc_item.def_id, output),
377377
fn_span: tcx.hir().span_if_local(assoc_item.def_id),
@@ -417,7 +417,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
417417
let ty = alias_term
418418
.map_bound(|alias| tcx.type_of(alias.def_id).instantiate(tcx, alias.args));
419419
let ty =
420-
check_assoc_const_binding_type(tcx, constraint.ident, ty, constraint.hir_id);
420+
check_assoc_const_binding_type(self, constraint.ident, ty, constraint.hir_id);
421421
tcx.feed_anon_const_type(anon_const.def_id, ty::EarlyBinder::bind(ty));
422422
}
423423

@@ -426,7 +426,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
426426

427427
match constraint.kind {
428428
hir::AssocItemConstraintKind::Equality { .. } if let ty::AssocKind::Fn = assoc_kind => {
429-
return Err(tcx.dcx().emit_err(crate::errors::ReturnTypeNotationEqualityBound {
429+
return Err(self.dcx().emit_err(crate::errors::ReturnTypeNotationEqualityBound {
430430
span: constraint.span,
431431
}));
432432
}
@@ -462,7 +462,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
462462
late_bound_in_term,
463463
|br_name| {
464464
struct_span_code_err!(
465-
tcx.dcx(),
465+
self.dcx(),
466466
constraint.span,
467467
E0582,
468468
"binding for associated type `{}` references {}, \
@@ -519,7 +519,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
519519
///
520520
/// [^1]: <https://github.com/rust-lang/project-const-generics/issues/28>.
521521
fn check_assoc_const_binding_type<'tcx>(
522-
tcx: TyCtxt<'tcx>,
522+
cx: &dyn HirTyLowerer<'tcx>,
523523
assoc_const: Ident,
524524
ty: ty::Binder<'tcx, Ty<'tcx>>,
525525
hir_id: hir::HirId,
@@ -536,13 +536,14 @@ fn check_assoc_const_binding_type<'tcx>(
536536
}
537537

538538
let mut collector = GenericParamAndBoundVarCollector {
539-
tcx,
539+
cx,
540540
params: Default::default(),
541541
vars: Default::default(),
542542
depth: ty::INNERMOST,
543543
};
544544
let mut guar = ty.visit_with(&mut collector).break_value();
545545

546+
let tcx = cx.tcx();
546547
let ty_note = ty
547548
.make_suggestable(tcx, false, None)
548549
.map(|ty| crate::errors::TyOfAssocConstBindingNote { assoc_const, ty });
@@ -556,7 +557,7 @@ fn check_assoc_const_binding_type<'tcx>(
556557
for index in collector.params {
557558
let param = generics.param_at(index as _, tcx);
558559
let is_self_param = param.name == rustc_span::symbol::kw::SelfUpper;
559-
guar.get_or_insert(tcx.dcx().emit_err(crate::errors::ParamInTyOfAssocConstBinding {
560+
guar.get_or_insert(cx.dcx().emit_err(crate::errors::ParamInTyOfAssocConstBinding {
560561
span: assoc_const.span,
561562
assoc_const,
562563
param_name: param.name,
@@ -574,7 +575,7 @@ fn check_assoc_const_binding_type<'tcx>(
574575
}));
575576
}
576577
for (var_def_id, var_name) in collector.vars {
577-
guar.get_or_insert(tcx.dcx().emit_err(
578+
guar.get_or_insert(cx.dcx().emit_err(
578579
crate::errors::EscapingBoundVarInTyOfAssocConstBinding {
579580
span: assoc_const.span,
580581
assoc_const,
@@ -590,14 +591,14 @@ fn check_assoc_const_binding_type<'tcx>(
590591
Ty::new_error(tcx, guar)
591592
}
592593

593-
struct GenericParamAndBoundVarCollector<'tcx> {
594-
tcx: TyCtxt<'tcx>,
594+
struct GenericParamAndBoundVarCollector<'a, 'tcx> {
595+
cx: &'a dyn HirTyLowerer<'tcx>,
595596
params: FxIndexSet<u32>,
596597
vars: FxIndexSet<(DefId, Symbol)>,
597598
depth: ty::DebruijnIndex,
598599
}
599600

600-
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'tcx> {
601+
impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'_, 'tcx> {
601602
type Result = ControlFlow<ErrorGuaranteed>;
602603

603604
fn visit_binder<T: TypeVisitable<TyCtxt<'tcx>>>(
@@ -620,7 +621,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'tcx>
620621
ty::BoundTyKind::Param(def_id, name) => (def_id, name),
621622
ty::BoundTyKind::Anon => {
622623
let reported = self
623-
.tcx
624+
.cx
624625
.dcx()
625626
.delayed_bug(format!("unexpected anon bound ty: {:?}", bt.var));
626627
return ControlFlow::Break(reported);
@@ -643,7 +644,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'tcx>
643644
ty::BrNamed(def_id, name) => (def_id, name),
644645
ty::BrAnon | ty::BrEnv => {
645646
let guar = self
646-
.tcx
647+
.cx
647648
.dcx()
648649
.delayed_bug(format!("unexpected bound region kind: {:?}", br.kind));
649650
return ControlFlow::Break(guar);
@@ -661,7 +662,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for GenericParamAndBoundVarCollector<'tcx>
661662
self.params.insert(param.index);
662663
}
663664
ty::ConstKind::Bound(db, ty::BoundVar { .. }) if db >= self.depth => {
664-
let guar = self.tcx.dcx().delayed_bug("unexpected escaping late-bound const var");
665+
let guar = self.cx.dcx().delayed_bug("unexpected escaping late-bound const var");
665666
return ControlFlow::Break(guar);
666667
}
667668
_ if ct.has_param() || ct.has_bound_vars() => return ct.super_visit_with(self),

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+21-22
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
4646
return;
4747
}
4848

49-
self.tcx().dcx().emit_err(MissingTypeParams {
49+
self.dcx().emit_err(MissingTypeParams {
5050
span,
5151
def_span: self.tcx().def_span(def_id),
5252
span_snippet: self.tcx().sess.source_map().span_to_snippet(span).ok(),
@@ -109,7 +109,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
109109

110110
if is_impl {
111111
let trait_name = self.tcx().def_path_str(trait_def_id);
112-
self.tcx().dcx().emit_err(ManualImplementation { span, trait_name });
112+
self.dcx().emit_err(ManualImplementation { span, trait_name });
113113
}
114114
}
115115

@@ -156,7 +156,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
156156

157157
if is_dummy {
158158
err.label = Some(errors::AssocItemNotFoundLabel::NotFound { span });
159-
return tcx.dcx().emit_err(err);
159+
return self.dcx().emit_err(err);
160160
}
161161

162162
let all_candidate_names: Vec<_> = all_candidates()
@@ -174,7 +174,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
174174
assoc_kind: assoc_kind_str,
175175
suggested_name,
176176
});
177-
return tcx.dcx().emit_err(err);
177+
return self.dcx().emit_err(err);
178178
}
179179

180180
// If we didn't find a good item in the supertraits (or couldn't get
@@ -239,10 +239,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
239239
assoc_kind: assoc_kind_str,
240240
suggested_name,
241241
});
242-
return tcx.dcx().emit_err(err);
242+
return self.dcx().emit_err(err);
243243
}
244244

245-
let mut err = tcx.dcx().create_err(err);
245+
let mut err = self.dcx().create_err(err);
246246
if suggest_constraining_type_param(
247247
tcx,
248248
generics,
@@ -264,7 +264,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
264264
}
265265
return err.emit();
266266
}
267-
return tcx.dcx().emit_err(err);
267+
return self.dcx().emit_err(err);
268268
}
269269
}
270270

@@ -291,7 +291,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
291291
err.label = Some(errors::AssocItemNotFoundLabel::NotFound { span: assoc_name.span });
292292
}
293293

294-
tcx.dcx().emit_err(err)
294+
self.dcx().emit_err(err)
295295
}
296296

297297
fn complain_about_assoc_kind_mismatch(
@@ -347,7 +347,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
347347
(ident.span, None, assoc_kind, assoc_item.kind)
348348
};
349349

350-
tcx.dcx().emit_err(errors::AssocKindMismatch {
350+
self.dcx().emit_err(errors::AssocKindMismatch {
351351
span,
352352
expected: super::assoc_kind_str(expected),
353353
got: super::assoc_kind_str(got),
@@ -366,8 +366,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
366366
traits: &[String],
367367
name: Symbol,
368368
) -> ErrorGuaranteed {
369-
let mut err =
370-
struct_span_code_err!(self.tcx().dcx(), span, E0223, "ambiguous associated type");
369+
let mut err = struct_span_code_err!(self.dcx(), span, E0223, "ambiguous associated type");
371370
if self
372371
.tcx()
373372
.resolutions(())
@@ -475,7 +474,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
475474
span: Span,
476475
) -> ErrorGuaranteed {
477476
let mut err = struct_span_code_err!(
478-
self.tcx().dcx(),
477+
self.dcx(),
479478
name.span,
480479
E0034,
481480
"multiple applicable items in scope"
@@ -576,7 +575,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
576575
};
577576

578577
let mut err = struct_span_code_err!(
579-
tcx.dcx(),
578+
self.dcx(),
580579
name.span,
581580
E0220,
582581
"associated type `{name}` not found for `{self_ty}` in the current scope"
@@ -662,7 +661,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
662661
bounds.sort();
663662
bounds.dedup();
664663

665-
let mut err = tcx.dcx().struct_span_err(
664+
let mut err = self.dcx().struct_span_err(
666665
name.span,
667666
format!("the associated type `{name}` exists for `{self_ty}`, but its trait bounds were not satisfied")
668667
);
@@ -829,7 +828,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
829828

830829
trait_bound_spans.sort();
831830
let mut err = struct_span_code_err!(
832-
tcx.dcx(),
831+
self.dcx(),
833832
trait_bound_spans,
834833
E0191,
835834
"the value of the associated type{} {} must be specified",
@@ -1012,7 +1011,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
10121011
.next()
10131012
{
10141013
let reported =
1015-
struct_span_code_err!(tcx.dcx(), span, E0223, "ambiguous associated type")
1014+
struct_span_code_err!(self.dcx(), span, E0223, "ambiguous associated type")
10161015
.with_span_suggestion_verbose(
10171016
ident2.span.to(ident3.span),
10181017
format!("there is an associated function with a similar name: `{name}`"),
@@ -1120,7 +1119,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11201119
let last_span = *arg_spans.last().unwrap();
11211120
let span: MultiSpan = arg_spans.into();
11221121
let mut err = struct_span_code_err!(
1123-
self.tcx().dcx(),
1122+
self.dcx(),
11241123
span,
11251124
E0109,
11261125
"{kind} arguments are not allowed on {this_type}",
@@ -1139,11 +1138,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11391138
&self,
11401139
regular_traits: &Vec<TraitAliasExpansionInfo<'_>>,
11411140
) -> ErrorGuaranteed {
1142-
let tcx = self.tcx();
11431141
let first_trait = &regular_traits[0];
11441142
let additional_trait = &regular_traits[1];
11451143
let mut err = struct_span_code_err!(
1146-
tcx.dcx(),
1144+
self.dcx(),
11471145
additional_trait.bottom().1,
11481146
E0225,
11491147
"only auto traits can be used as additional traits in a trait object"
@@ -1186,19 +1184,20 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
11861184
.find(|&trait_ref| tcx.is_trait_alias(trait_ref))
11871185
.map(|trait_ref| tcx.def_span(trait_ref));
11881186
let reported =
1189-
tcx.dcx().emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span });
1187+
self.dcx().emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span });
11901188
self.set_tainted_by_errors(reported);
11911189
reported
11921190
}
11931191
}
11941192

11951193
/// Emit an error for the given associated item constraint.
11961194
pub fn prohibit_assoc_item_constraint(
1197-
tcx: TyCtxt<'_>,
1195+
cx: &dyn HirTyLowerer<'_>,
11981196
constraint: &hir::AssocItemConstraint<'_>,
11991197
segment: Option<(DefId, &hir::PathSegment<'_>, Span)>,
12001198
) -> ErrorGuaranteed {
1201-
let mut err = tcx.dcx().create_err(AssocItemConstraintsNotAllowedHere {
1199+
let tcx = cx.tcx();
1200+
let mut err = cx.dcx().create_err(AssocItemConstraintsNotAllowedHere {
12021201
span: constraint.span,
12031202
fn_trait_expansion: if let Some((_, segment, span)) = segment
12041203
&& segment.args().parenthesized == hir::GenericArgsParentheses::ParenSugar

0 commit comments

Comments
 (0)