Skip to content

Commit aece064

Browse files
committed
Remove HirTyLowerer::set_tainted_by_errors, since it is now redundant
1 parent fd9a925 commit aece064

File tree

6 files changed

+97
-96
lines changed

6 files changed

+97
-96
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+60-32
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub struct CollectItemTypesVisitor<'tcx> {
163163
/// and suggest adding type parameters in the appropriate place, taking into consideration any and
164164
/// all already existing generic type parameters to avoid suggesting a name that is already in use.
165165
pub(crate) fn placeholder_type_error<'tcx>(
166-
tcx: TyCtxt<'tcx>,
166+
cx: &dyn HirTyLowerer<'tcx>,
167167
generics: Option<&hir::Generics<'_>>,
168168
placeholder_types: Vec<Span>,
169169
suggest: bool,
@@ -174,21 +174,21 @@ pub(crate) fn placeholder_type_error<'tcx>(
174174
return;
175175
}
176176

177-
placeholder_type_error_diag(tcx, generics, placeholder_types, vec![], suggest, hir_ty, kind)
177+
placeholder_type_error_diag(cx, generics, placeholder_types, vec![], suggest, hir_ty, kind)
178178
.emit();
179179
}
180180

181-
pub(crate) fn placeholder_type_error_diag<'tcx>(
182-
tcx: TyCtxt<'tcx>,
181+
pub(crate) fn placeholder_type_error_diag<'cx, 'tcx>(
182+
cx: &'cx dyn HirTyLowerer<'tcx>,
183183
generics: Option<&hir::Generics<'_>>,
184184
placeholder_types: Vec<Span>,
185185
additional_spans: Vec<Span>,
186186
suggest: bool,
187187
hir_ty: Option<&hir::Ty<'_>>,
188188
kind: &'static str,
189-
) -> Diag<'tcx> {
189+
) -> Diag<'cx> {
190190
if placeholder_types.is_empty() {
191-
return bad_placeholder(tcx, additional_spans, kind);
191+
return bad_placeholder(cx, additional_spans, kind);
192192
}
193193

194194
let params = generics.map(|g| g.params).unwrap_or_default();
@@ -212,7 +212,7 @@ pub(crate) fn placeholder_type_error_diag<'tcx>(
212212
}
213213

214214
let mut err =
215-
bad_placeholder(tcx, placeholder_types.into_iter().chain(additional_spans).collect(), kind);
215+
bad_placeholder(cx, placeholder_types.into_iter().chain(additional_spans).collect(), kind);
216216

217217
// Suggest, but only if it is not a function in const or static
218218
if suggest {
@@ -226,7 +226,7 @@ pub(crate) fn placeholder_type_error_diag<'tcx>(
226226

227227
// Check if parent is const or static
228228
is_const_or_static = matches!(
229-
tcx.parent_hir_node(hir_ty.hir_id),
229+
cx.tcx().parent_hir_node(hir_ty.hir_id),
230230
Node::Item(&hir::Item {
231231
kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
232232
..
@@ -269,7 +269,16 @@ fn reject_placeholder_type_signatures_in_item<'tcx>(
269269
let mut visitor = HirPlaceholderCollector::default();
270270
visitor.visit_item(item);
271271

272-
placeholder_type_error(tcx, Some(generics), visitor.0, suggest, None, item.kind.descr());
272+
let icx = ItemCtxt::new(tcx, item.owner_id.def_id);
273+
274+
placeholder_type_error(
275+
icx.lowerer(),
276+
Some(generics),
277+
visitor.0,
278+
suggest,
279+
None,
280+
item.kind.descr(),
281+
);
273282
}
274283

275284
impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
@@ -331,15 +340,15 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
331340
///////////////////////////////////////////////////////////////////////////
332341
// Utility types and common code for the above passes.
333342

334-
fn bad_placeholder<'tcx>(
335-
tcx: TyCtxt<'tcx>,
343+
fn bad_placeholder<'cx, 'tcx>(
344+
cx: &'cx dyn HirTyLowerer<'tcx>,
336345
mut spans: Vec<Span>,
337346
kind: &'static str,
338-
) -> Diag<'tcx> {
347+
) -> Diag<'cx> {
339348
let kind = if kind.ends_with('s') { format!("{kind}es") } else { format!("{kind}s") };
340349

341350
spans.sort();
342-
tcx.dcx().create_err(errors::PlaceholderNotAllowedItemSignatures { spans, kind })
351+
cx.dcx().create_err(errors::PlaceholderNotAllowedItemSignatures { spans, kind })
343352
}
344353

345354
impl<'tcx> ItemCtxt<'tcx> {
@@ -383,14 +392,13 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
383392
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
384393
if let RegionInferReason::BorrowedObjectLifetimeDefault = reason {
385394
let e = struct_span_code_err!(
386-
self.tcx().dcx(),
395+
self.dcx(),
387396
span,
388397
E0228,
389398
"the lifetime bound for this object type cannot be deduced \
390399
from context; please supply an explicit bound"
391400
)
392401
.emit();
393-
self.set_tainted_by_errors(e);
394402
ty::Region::new_error(self.tcx(), e)
395403
} else {
396404
// This indicates an illegal lifetime in a non-assoc-trait position
@@ -515,10 +523,6 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
515523
None
516524
}
517525

518-
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
519-
self.tainted_by_errors.set(Some(err));
520-
}
521-
522526
fn lower_fn_sig(
523527
&self,
524528
decl: &hir::FnDecl<'tcx>,
@@ -576,7 +580,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
576580
// `ident_span` to not emit an error twice when we have `fn foo(_: fn() -> _)`.
577581

578582
let mut diag = crate::collect::placeholder_type_error_diag(
579-
tcx,
583+
self,
580584
generics,
581585
visitor.0,
582586
infer_replacements.iter().map(|(s, _)| *s).collect(),
@@ -596,7 +600,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
596600
);
597601
}
598602

599-
self.set_tainted_by_errors(diag.emit());
603+
diag.emit();
600604
}
601605

602606
(input_tys, output_ty)
@@ -645,6 +649,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
645649
let it = tcx.hir().item(item_id);
646650
debug!(item = %it.ident, id = %it.hir_id());
647651
let def_id = item_id.owner_id.def_id;
652+
let icx = ItemCtxt::new(tcx, def_id);
648653

649654
match &it.kind {
650655
// These don't define types.
@@ -669,7 +674,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
669674
let mut visitor = HirPlaceholderCollector::default();
670675
visitor.visit_foreign_item(item);
671676
placeholder_type_error(
672-
tcx,
677+
icx.lowerer(),
673678
None,
674679
visitor.0,
675680
false,
@@ -748,7 +753,14 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
748753
if !ty.is_suggestable_infer_ty() {
749754
let mut visitor = HirPlaceholderCollector::default();
750755
visitor.visit_item(it);
751-
placeholder_type_error(tcx, None, visitor.0, false, None, it.kind.descr());
756+
placeholder_type_error(
757+
icx.lowerer(),
758+
None,
759+
visitor.0,
760+
false,
761+
None,
762+
it.kind.descr(),
763+
);
752764
}
753765
}
754766

@@ -766,6 +778,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
766778
let trait_item = tcx.hir().trait_item(trait_item_id);
767779
let def_id = trait_item_id.owner_id;
768780
tcx.ensure().generics_of(def_id);
781+
let icx = ItemCtxt::new(tcx, def_id.def_id);
769782

770783
match trait_item.kind {
771784
hir::TraitItemKind::Fn(..) => {
@@ -782,7 +795,14 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
782795
// Account for `const C: _;`.
783796
let mut visitor = HirPlaceholderCollector::default();
784797
visitor.visit_trait_item(trait_item);
785-
placeholder_type_error(tcx, None, visitor.0, false, None, "associated constant");
798+
placeholder_type_error(
799+
icx.lowerer(),
800+
None,
801+
visitor.0,
802+
false,
803+
None,
804+
"associated constant",
805+
);
786806
}
787807
}
788808

@@ -793,7 +813,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
793813
// Account for `type T = _;`.
794814
let mut visitor = HirPlaceholderCollector::default();
795815
visitor.visit_trait_item(trait_item);
796-
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
816+
placeholder_type_error(icx.lowerer(), None, visitor.0, false, None, "associated type");
797817
}
798818

799819
hir::TraitItemKind::Type(_, None) => {
@@ -804,7 +824,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
804824
let mut visitor = HirPlaceholderCollector::default();
805825
visitor.visit_trait_item(trait_item);
806826

807-
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
827+
placeholder_type_error(icx.lowerer(), None, visitor.0, false, None, "associated type");
808828
}
809829
};
810830

@@ -817,6 +837,7 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
817837
tcx.ensure().type_of(def_id);
818838
tcx.ensure().predicates_of(def_id);
819839
let impl_item = tcx.hir().impl_item(impl_item_id);
840+
let icx = ItemCtxt::new(tcx, def_id.def_id);
820841
match impl_item.kind {
821842
hir::ImplItemKind::Fn(..) => {
822843
tcx.ensure().codegen_fn_attrs(def_id);
@@ -827,14 +848,21 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
827848
let mut visitor = HirPlaceholderCollector::default();
828849
visitor.visit_impl_item(impl_item);
829850

830-
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
851+
placeholder_type_error(icx.lowerer(), None, visitor.0, false, None, "associated type");
831852
}
832853
hir::ImplItemKind::Const(ty, _) => {
833854
// Account for `const T: _ = ..;`
834855
if !ty.is_suggestable_infer_ty() {
835856
let mut visitor = HirPlaceholderCollector::default();
836857
visitor.visit_impl_item(impl_item);
837-
placeholder_type_error(tcx, None, visitor.0, false, None, "associated constant");
858+
placeholder_type_error(
859+
icx.lowerer(),
860+
None,
861+
visitor.0,
862+
false,
863+
None,
864+
"associated constant",
865+
);
838866
}
839867
}
840868
}
@@ -1385,7 +1413,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
13851413
..
13861414
})
13871415
| Item(hir::Item { kind: ItemKind::Fn(sig, generics, _), .. }) => {
1388-
infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
1416+
infer_return_ty_for_fn_sig(sig, generics, def_id, &icx)
13891417
}
13901418

13911419
ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => {
@@ -1402,7 +1430,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
14021430
None,
14031431
)
14041432
} else {
1405-
infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
1433+
infer_return_ty_for_fn_sig(sig, generics, def_id, &icx)
14061434
}
14071435
}
14081436

@@ -1455,12 +1483,12 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
14551483
}
14561484

14571485
fn infer_return_ty_for_fn_sig<'tcx>(
1458-
tcx: TyCtxt<'tcx>,
14591486
sig: &hir::FnSig<'tcx>,
14601487
generics: &hir::Generics<'_>,
14611488
def_id: LocalDefId,
14621489
icx: &ItemCtxt<'tcx>,
14631490
) -> ty::PolyFnSig<'tcx> {
1491+
let tcx = icx.tcx;
14641492
let hir_id = tcx.local_def_id_to_hir_id(def_id);
14651493

14661494
match sig.decl.output.get_infer_ret_ty() {
@@ -1492,7 +1520,7 @@ fn infer_return_ty_for_fn_sig<'tcx>(
14921520
let mut visitor = HirPlaceholderCollector::default();
14931521
visitor.visit_ty(ty);
14941522

1495-
let mut diag = bad_placeholder(tcx, visitor.0, "return type");
1523+
let mut diag = bad_placeholder(icx.lowerer(), visitor.0, "return type");
14961524
let ret_ty = fn_sig.output();
14971525
// Don't leak types into signatures unless they're nameable!
14981526
// For example, if a function returns itself, we don't want that

compiler/rustc_hir_analysis/src/collect/type_of.rs

+18-9
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_span::symbol::Ident;
1212
use rustc_span::{Span, DUMMY_SP};
1313

1414
use crate::errors::TypeofReservedKeywordUsed;
15+
use crate::hir_ty_lowering::HirTyLowerer;
1516

1617
use super::bad_placeholder;
1718
use super::ItemCtxt;
@@ -357,7 +358,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
357358
.and_then(|body_id| {
358359
ty.is_suggestable_infer_ty().then(|| {
359360
infer_placeholder_type(
360-
tcx,
361+
icx.lowerer(),
361362
def_id,
362363
body_id,
363364
ty.span,
@@ -381,7 +382,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
381382
ImplItemKind::Const(ty, body_id) => {
382383
if ty.is_suggestable_infer_ty() {
383384
infer_placeholder_type(
384-
tcx,
385+
icx.lowerer(),
385386
def_id,
386387
body_id,
387388
ty.span,
@@ -405,7 +406,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
405406
ItemKind::Static(ty, .., body_id) => {
406407
if ty.is_suggestable_infer_ty() {
407408
infer_placeholder_type(
408-
tcx,
409+
icx.lowerer(),
409410
def_id,
410411
body_id,
411412
ty.span,
@@ -418,7 +419,14 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
418419
}
419420
ItemKind::Const(ty, _, body_id) => {
420421
if ty.is_suggestable_infer_ty() {
421-
infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident, "constant")
422+
infer_placeholder_type(
423+
icx.lowerer(),
424+
def_id,
425+
body_id,
426+
ty.span,
427+
item.ident,
428+
"constant",
429+
)
422430
} else {
423431
icx.lower_ty(ty)
424432
}
@@ -559,21 +567,22 @@ pub(super) fn type_of_opaque(
559567
}
560568
}
561569

562-
fn infer_placeholder_type<'a>(
563-
tcx: TyCtxt<'a>,
570+
fn infer_placeholder_type<'tcx>(
571+
cx: &dyn HirTyLowerer<'tcx>,
564572
def_id: LocalDefId,
565573
body_id: hir::BodyId,
566574
span: Span,
567575
item_ident: Ident,
568576
kind: &'static str,
569-
) -> Ty<'a> {
577+
) -> Ty<'tcx> {
578+
let tcx = cx.tcx();
570579
let ty = tcx.diagnostic_only_typeck(def_id).node_type(body_id.hir_id);
571580

572581
// If this came from a free `const` or `static mut?` item,
573582
// then the user may have written e.g. `const A = 42;`.
574583
// In this case, the parser has stashed a diagnostic for
575584
// us to improve in typeck so we do that now.
576-
let guar = tcx
585+
let guar = cx
577586
.dcx()
578587
.try_steal_modify_and_emit_err(span, StashKey::ItemNoType, |err| {
579588
if !ty.references_error() {
@@ -602,7 +611,7 @@ fn infer_placeholder_type<'a>(
602611
}
603612
})
604613
.unwrap_or_else(|| {
605-
let mut diag = bad_placeholder(tcx, vec![span], kind);
614+
let mut diag = bad_placeholder(cx, vec![span], kind);
606615

607616
if !ty.references_error() {
608617
if let Some(ty) = ty.make_suggestable(tcx, false, None) {

0 commit comments

Comments
 (0)