Skip to content

Commit 5442fcf

Browse files
committed
Auto merge of rust-lang#127519 - matthiaskrgr:rollup-myo0fmj, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - rust-lang#127028 (Fix regression in the MIR lowering of or-patterns) - rust-lang#127091 (impl FusedIterator and a size hint for the error sources iter) - rust-lang#127358 (Automatically taint when reporting errors from ItemCtxt) - rust-lang#127382 (Use verbose style when suggesting changing `const` with `let`) - rust-lang#127397 (fix interleaved output in the default panic hook when multiple threads panic simultaneously) - rust-lang#127484 (`#[doc(alias)]`'s doc: say that ASCII spaces are allowed) - rust-lang#127496 (Update `f16`/`f128` FIXMEs that needed `(NEG_)INFINITY`) - rust-lang#127508 (small search graph refactor) r? `@ghost` `@rustbot` modify labels: rollup
2 parents f25e92b + 9a1c4cb commit 5442fcf

File tree

66 files changed

+704
-677
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+704
-677
lines changed

compiler/rustc_hir_analysis/src/collect.rs

+67-33
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};
@@ -161,7 +163,7 @@ pub struct CollectItemTypesVisitor<'tcx> {
161163
/// and suggest adding type parameters in the appropriate place, taking into consideration any and
162164
/// all already existing generic type parameters to avoid suggesting a name that is already in use.
163165
pub(crate) fn placeholder_type_error<'tcx>(
164-
tcx: TyCtxt<'tcx>,
166+
cx: &dyn HirTyLowerer<'tcx>,
165167
generics: Option<&hir::Generics<'_>>,
166168
placeholder_types: Vec<Span>,
167169
suggest: bool,
@@ -172,21 +174,21 @@ pub(crate) fn placeholder_type_error<'tcx>(
172174
return;
173175
}
174176

175-
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)
176178
.emit();
177179
}
178180

179-
pub(crate) fn placeholder_type_error_diag<'tcx>(
180-
tcx: TyCtxt<'tcx>,
181+
pub(crate) fn placeholder_type_error_diag<'cx, 'tcx>(
182+
cx: &'cx dyn HirTyLowerer<'tcx>,
181183
generics: Option<&hir::Generics<'_>>,
182184
placeholder_types: Vec<Span>,
183185
additional_spans: Vec<Span>,
184186
suggest: bool,
185187
hir_ty: Option<&hir::Ty<'_>>,
186188
kind: &'static str,
187-
) -> Diag<'tcx> {
189+
) -> Diag<'cx> {
188190
if placeholder_types.is_empty() {
189-
return bad_placeholder(tcx, additional_spans, kind);
191+
return bad_placeholder(cx, additional_spans, kind);
190192
}
191193

192194
let params = generics.map(|g| g.params).unwrap_or_default();
@@ -210,7 +212,7 @@ pub(crate) fn placeholder_type_error_diag<'tcx>(
210212
}
211213

212214
let mut err =
213-
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);
214216

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

225227
// Check if parent is const or static
226228
is_const_or_static = matches!(
227-
tcx.parent_hir_node(hir_ty.hir_id),
229+
cx.tcx().parent_hir_node(hir_ty.hir_id),
228230
Node::Item(&hir::Item {
229231
kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
230232
..
@@ -267,7 +269,16 @@ fn reject_placeholder_type_signatures_in_item<'tcx>(
267269
let mut visitor = HirPlaceholderCollector::default();
268270
visitor.visit_item(item);
269271

270-
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+
);
271282
}
272283

273284
impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
@@ -329,15 +340,15 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
329340
///////////////////////////////////////////////////////////////////////////
330341
// Utility types and common code for the above passes.
331342

332-
fn bad_placeholder<'tcx>(
333-
tcx: TyCtxt<'tcx>,
343+
fn bad_placeholder<'cx, 'tcx>(
344+
cx: &'cx dyn HirTyLowerer<'tcx>,
334345
mut spans: Vec<Span>,
335346
kind: &'static str,
336-
) -> Diag<'tcx> {
347+
) -> Diag<'cx> {
337348
let kind = if kind.ends_with('s') { format!("{kind}es") } else { format!("{kind}s") };
338349

339350
spans.sort();
340-
tcx.dcx().create_err(errors::PlaceholderNotAllowedItemSignatures { spans, kind })
351+
cx.dcx().create_err(errors::PlaceholderNotAllowedItemSignatures { spans, kind })
341352
}
342353

343354
impl<'tcx> ItemCtxt<'tcx> {
@@ -370,21 +381,24 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
370381
self.tcx
371382
}
372383

384+
fn dcx(&self) -> DiagCtxtHandle<'_> {
385+
self.tcx.dcx().taintable_handle(&self.tainted_by_errors)
386+
}
387+
373388
fn item_def_id(&self) -> LocalDefId {
374389
self.item_def_id
375390
}
376391

377392
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
378393
if let RegionInferReason::BorrowedObjectLifetimeDefault = reason {
379394
let e = struct_span_code_err!(
380-
self.tcx().dcx(),
395+
self.dcx(),
381396
span,
382397
E0228,
383398
"the lifetime bound for this object type cannot be deduced \
384399
from context; please supply an explicit bound"
385400
)
386401
.emit();
387-
self.set_tainted_by_errors(e);
388402
ty::Region::new_error(self.tcx(), e)
389403
} else {
390404
// This indicates an illegal lifetime in a non-assoc-trait position
@@ -509,10 +523,6 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
509523
None
510524
}
511525

512-
fn set_tainted_by_errors(&self, err: ErrorGuaranteed) {
513-
self.tainted_by_errors.set(Some(err));
514-
}
515-
516526
fn lower_fn_sig(
517527
&self,
518528
decl: &hir::FnDecl<'tcx>,
@@ -570,7 +580,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
570580
// `ident_span` to not emit an error twice when we have `fn foo(_: fn() -> _)`.
571581

572582
let mut diag = crate::collect::placeholder_type_error_diag(
573-
tcx,
583+
self,
574584
generics,
575585
visitor.0,
576586
infer_replacements.iter().map(|(s, _)| *s).collect(),
@@ -590,7 +600,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
590600
);
591601
}
592602

593-
self.set_tainted_by_errors(diag.emit());
603+
diag.emit();
594604
}
595605

596606
(input_tys, output_ty)
@@ -639,6 +649,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
639649
let it = tcx.hir().item(item_id);
640650
debug!(item = %it.ident, id = %it.hir_id());
641651
let def_id = item_id.owner_id.def_id;
652+
let icx = ItemCtxt::new(tcx, def_id);
642653

643654
match &it.kind {
644655
// These don't define types.
@@ -663,7 +674,7 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
663674
let mut visitor = HirPlaceholderCollector::default();
664675
visitor.visit_foreign_item(item);
665676
placeholder_type_error(
666-
tcx,
677+
icx.lowerer(),
667678
None,
668679
visitor.0,
669680
false,
@@ -742,7 +753,14 @@ fn lower_item(tcx: TyCtxt<'_>, item_id: hir::ItemId) {
742753
if !ty.is_suggestable_infer_ty() {
743754
let mut visitor = HirPlaceholderCollector::default();
744755
visitor.visit_item(it);
745-
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+
);
746764
}
747765
}
748766

@@ -760,6 +778,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
760778
let trait_item = tcx.hir().trait_item(trait_item_id);
761779
let def_id = trait_item_id.owner_id;
762780
tcx.ensure().generics_of(def_id);
781+
let icx = ItemCtxt::new(tcx, def_id.def_id);
763782

764783
match trait_item.kind {
765784
hir::TraitItemKind::Fn(..) => {
@@ -776,7 +795,14 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
776795
// Account for `const C: _;`.
777796
let mut visitor = HirPlaceholderCollector::default();
778797
visitor.visit_trait_item(trait_item);
779-
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+
);
780806
}
781807
}
782808

@@ -787,7 +813,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
787813
// Account for `type T = _;`.
788814
let mut visitor = HirPlaceholderCollector::default();
789815
visitor.visit_trait_item(trait_item);
790-
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
816+
placeholder_type_error(icx.lowerer(), None, visitor.0, false, None, "associated type");
791817
}
792818

793819
hir::TraitItemKind::Type(_, None) => {
@@ -798,7 +824,7 @@ fn lower_trait_item(tcx: TyCtxt<'_>, trait_item_id: hir::TraitItemId) {
798824
let mut visitor = HirPlaceholderCollector::default();
799825
visitor.visit_trait_item(trait_item);
800826

801-
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
827+
placeholder_type_error(icx.lowerer(), None, visitor.0, false, None, "associated type");
802828
}
803829
};
804830

@@ -811,6 +837,7 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
811837
tcx.ensure().type_of(def_id);
812838
tcx.ensure().predicates_of(def_id);
813839
let impl_item = tcx.hir().impl_item(impl_item_id);
840+
let icx = ItemCtxt::new(tcx, def_id.def_id);
814841
match impl_item.kind {
815842
hir::ImplItemKind::Fn(..) => {
816843
tcx.ensure().codegen_fn_attrs(def_id);
@@ -821,14 +848,21 @@ fn lower_impl_item(tcx: TyCtxt<'_>, impl_item_id: hir::ImplItemId) {
821848
let mut visitor = HirPlaceholderCollector::default();
822849
visitor.visit_impl_item(impl_item);
823850

824-
placeholder_type_error(tcx, None, visitor.0, false, None, "associated type");
851+
placeholder_type_error(icx.lowerer(), None, visitor.0, false, None, "associated type");
825852
}
826853
hir::ImplItemKind::Const(ty, _) => {
827854
// Account for `const T: _ = ..;`
828855
if !ty.is_suggestable_infer_ty() {
829856
let mut visitor = HirPlaceholderCollector::default();
830857
visitor.visit_impl_item(impl_item);
831-
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+
);
832866
}
833867
}
834868
}
@@ -1385,7 +1419,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
13851419
..
13861420
})
13871421
| Item(hir::Item { kind: ItemKind::Fn(sig, generics, _), .. }) => {
1388-
infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
1422+
infer_return_ty_for_fn_sig(sig, generics, def_id, &icx)
13891423
}
13901424

13911425
ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => {
@@ -1402,7 +1436,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
14021436
None,
14031437
)
14041438
} else {
1405-
infer_return_ty_for_fn_sig(tcx, sig, generics, def_id, &icx)
1439+
infer_return_ty_for_fn_sig(sig, generics, def_id, &icx)
14061440
}
14071441
}
14081442

@@ -1455,12 +1489,12 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFn
14551489
}
14561490

14571491
fn infer_return_ty_for_fn_sig<'tcx>(
1458-
tcx: TyCtxt<'tcx>,
14591492
sig: &hir::FnSig<'tcx>,
14601493
generics: &hir::Generics<'_>,
14611494
def_id: LocalDefId,
14621495
icx: &ItemCtxt<'tcx>,
14631496
) -> ty::PolyFnSig<'tcx> {
1497+
let tcx = icx.tcx;
14641498
let hir_id = tcx.local_def_id_to_hir_id(def_id);
14651499

14661500
match sig.decl.output.get_infer_ret_ty() {
@@ -1492,7 +1526,7 @@ fn infer_return_ty_for_fn_sig<'tcx>(
14921526
let mut visitor = HirPlaceholderCollector::default();
14931527
visitor.visit_ty(ty);
14941528

1495-
let mut diag = bad_placeholder(tcx, visitor.0, "return type");
1529+
let mut diag = bad_placeholder(icx.lowerer(), visitor.0, "return type");
14961530
let ret_ty = fn_sig.output();
14971531
// Don't leak types into signatures unless they're nameable!
14981532
// 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)