Skip to content

Commit 6330c27

Browse files
committed
Auto merge of #103962 - matthiaskrgr:rollup-9av8i6k, r=matthiaskrgr
Rollup of 7 pull requests Successful merges: - #103680 (CStr: add some doc links) - #103780 (Fix late-bound lifetime closure ICEs in HIR typeck and MIR borrowck) - #103845 (Add track_caller to some Lock methods) - #103935 (Remove rustdoc clean::Visibility type) - #103941 (Fixed typos) - #103950 (Fix ICE when negative impl is collected during eager mono) - #103953 (Remove unused argument from `throw_unresolved_import_error`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 47c008e + 1edd63f commit 6330c27

File tree

22 files changed

+334
-213
lines changed

22 files changed

+334
-213
lines changed

compiler/rustc_borrowck/src/region_infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2314,7 +2314,7 @@ impl<'tcx> ClosureRegionRequirementsExt<'tcx> for ClosureRegionRequirements<'tcx
23142314
tcx,
23152315
closure_substs,
23162316
self.num_external_vids,
2317-
tcx.typeck_root_def_id(closure_def_id),
2317+
closure_def_id.expect_local(),
23182318
);
23192319
debug!("apply_requirements: closure_mapping={:?}", closure_mapping);
23202320

compiler/rustc_borrowck/src/universal_regions.rs

+93-34
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ use rustc_hir::{BodyOwnerKind, HirId};
2222
use rustc_index::vec::{Idx, IndexVec};
2323
use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin};
2424
use rustc_middle::ty::fold::TypeFoldable;
25-
use rustc_middle::ty::{self, InlineConstSubsts, InlineConstSubstsParts, RegionVid, Ty, TyCtxt};
25+
use rustc_middle::ty::{
26+
self, DefIdTree, InlineConstSubsts, InlineConstSubstsParts, RegionVid, Ty, TyCtxt,
27+
};
2628
use rustc_middle::ty::{InternalSubsts, SubstsRef};
2729
use std::iter;
2830

@@ -241,15 +243,15 @@ impl<'tcx> UniversalRegions<'tcx> {
241243
tcx: TyCtxt<'tcx>,
242244
closure_substs: SubstsRef<'tcx>,
243245
expected_num_vars: usize,
244-
typeck_root_def_id: DefId,
246+
closure_def_id: LocalDefId,
245247
) -> IndexVec<RegionVid, ty::Region<'tcx>> {
246248
let mut region_mapping = IndexVec::with_capacity(expected_num_vars);
247249
region_mapping.push(tcx.lifetimes.re_static);
248250
tcx.for_each_free_region(&closure_substs, |fr| {
249251
region_mapping.push(fr);
250252
});
251253

252-
for_each_late_bound_region_defined_on(tcx, typeck_root_def_id, |r| {
254+
for_each_late_bound_region_in_recursive_scope(tcx, tcx.local_parent(closure_def_id), |r| {
253255
region_mapping.push(r);
254256
});
255257

@@ -339,9 +341,8 @@ impl<'tcx> UniversalRegions<'tcx> {
339341
// tests, and the resulting print-outs include def-ids
340342
// and other things that are not stable across tests!
341343
// So we just include the region-vid. Annoying.
342-
let typeck_root_def_id = tcx.typeck_root_def_id(def_id);
343-
for_each_late_bound_region_defined_on(tcx, typeck_root_def_id, |r| {
344-
err.note(&format!("late-bound region is {:?}", self.to_region_vid(r),));
344+
for_each_late_bound_region_in_recursive_scope(tcx, def_id.expect_local(), |r| {
345+
err.note(&format!("late-bound region is {:?}", self.to_region_vid(r)));
345346
});
346347
}
347348
DefiningTy::Generator(def_id, substs, _) => {
@@ -354,9 +355,8 @@ impl<'tcx> UniversalRegions<'tcx> {
354355
// FIXME: As above, we'd like to print out the region
355356
// `r` but doing so is not stable across architectures
356357
// and so forth.
357-
let typeck_root_def_id = tcx.typeck_root_def_id(def_id);
358-
for_each_late_bound_region_defined_on(tcx, typeck_root_def_id, |r| {
359-
err.note(&format!("late-bound region is {:?}", self.to_region_vid(r),));
358+
for_each_late_bound_region_in_recursive_scope(tcx, def_id.expect_local(), |r| {
359+
err.note(&format!("late-bound region is {:?}", self.to_region_vid(r)));
360360
});
361361
}
362362
DefiningTy::FnDef(def_id, substs) => {
@@ -421,13 +421,24 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
421421
first_extern_index
422422
} else {
423423
// If this is a closure, generator, or inline-const, then the late-bound regions from the enclosing
424-
// function are actually external regions to us. For example, here, 'a is not local
424+
// function/closures are actually external regions to us. For example, here, 'a is not local
425425
// to the closure c (although it is local to the fn foo):
426426
// fn foo<'a>() {
427427
// let c = || { let x: &'a u32 = ...; }
428428
// }
429-
self.infcx
430-
.replace_late_bound_regions_with_nll_infer_vars(self.mir_def.did, &mut indices);
429+
for_each_late_bound_region_in_recursive_scope(
430+
self.infcx.tcx,
431+
self.infcx.tcx.local_parent(self.mir_def.did),
432+
|r| {
433+
debug!(?r);
434+
if !indices.indices.contains_key(&r) {
435+
let region_vid = self.infcx.next_nll_region_var(FR);
436+
debug!(?region_vid);
437+
indices.insert_late_bound_region(r, region_vid.to_region_vid());
438+
}
439+
},
440+
);
441+
431442
// Any regions created during the execution of `defining_ty` or during the above
432443
// late-bound region replacement are all considered 'extern' regions
433444
self.infcx.num_region_vars()
@@ -444,12 +455,16 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> {
444455
bound_inputs_and_output,
445456
&mut indices,
446457
);
447-
// Converse of above, if this is a function then the late-bound regions declared on its
448-
// signature are local to the fn.
449-
if self.mir_def.did.to_def_id() == typeck_root_def_id {
450-
self.infcx
451-
.replace_late_bound_regions_with_nll_infer_vars(self.mir_def.did, &mut indices);
452-
}
458+
// Converse of above, if this is a function/closure then the late-bound regions declared on its
459+
// signature are local.
460+
for_each_late_bound_region_in_item(self.infcx.tcx, self.mir_def.did, |r| {
461+
debug!(?r);
462+
if !indices.indices.contains_key(&r) {
463+
let region_vid = self.infcx.next_nll_region_var(FR);
464+
debug!(?region_vid);
465+
indices.insert_late_bound_region(r, region_vid.to_region_vid());
466+
}
467+
});
453468

454469
let (unnormalized_output_ty, mut unnormalized_input_tys) =
455470
inputs_and_output.split_last().unwrap();
@@ -692,7 +707,13 @@ trait InferCtxtExt<'tcx> {
692707
where
693708
T: TypeFoldable<'tcx>;
694709

695-
fn replace_late_bound_regions_with_nll_infer_vars(
710+
fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope(
711+
&self,
712+
mir_def_id: LocalDefId,
713+
indices: &mut UniversalRegionIndices<'tcx>,
714+
);
715+
716+
fn replace_late_bound_regions_with_nll_infer_vars_in_item(
696717
&self,
697718
mir_def_id: LocalDefId,
698719
indices: &mut UniversalRegionIndices<'tcx>,
@@ -746,13 +767,28 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> {
746767
/// set of late-bound regions and checks for any that we have not yet seen, adding them to the
747768
/// inputs vector.
748769
#[instrument(skip(self, indices))]
749-
fn replace_late_bound_regions_with_nll_infer_vars(
770+
fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope(
771+
&self,
772+
mir_def_id: LocalDefId,
773+
indices: &mut UniversalRegionIndices<'tcx>,
774+
) {
775+
for_each_late_bound_region_in_recursive_scope(self.tcx, mir_def_id, |r| {
776+
debug!(?r);
777+
if !indices.indices.contains_key(&r) {
778+
let region_vid = self.next_nll_region_var(FR);
779+
debug!(?region_vid);
780+
indices.insert_late_bound_region(r, region_vid.to_region_vid());
781+
}
782+
});
783+
}
784+
785+
#[instrument(skip(self, indices))]
786+
fn replace_late_bound_regions_with_nll_infer_vars_in_item(
750787
&self,
751788
mir_def_id: LocalDefId,
752789
indices: &mut UniversalRegionIndices<'tcx>,
753790
) {
754-
let typeck_root_def_id = self.tcx.typeck_root_def_id(mir_def_id.to_def_id());
755-
for_each_late_bound_region_defined_on(self.tcx, typeck_root_def_id, |r| {
791+
for_each_late_bound_region_in_item(self.tcx, mir_def_id, |r| {
756792
debug!(?r);
757793
if !indices.indices.contains_key(&r) {
758794
let region_vid = self.next_nll_region_var(FR);
@@ -803,21 +839,44 @@ impl<'tcx> UniversalRegionIndices<'tcx> {
803839
}
804840
}
805841

806-
/// Iterates over the late-bound regions defined on fn_def_id and
807-
/// invokes `f` with the liberated form of each one.
808-
fn for_each_late_bound_region_defined_on<'tcx>(
842+
/// Iterates over the late-bound regions defined on `mir_def_id` and all of its
843+
/// parents, up to the typeck root, and invokes `f` with the liberated form
844+
/// of each one.
845+
fn for_each_late_bound_region_in_recursive_scope<'tcx>(
809846
tcx: TyCtxt<'tcx>,
810-
fn_def_id: DefId,
847+
mut mir_def_id: LocalDefId,
811848
mut f: impl FnMut(ty::Region<'tcx>),
812849
) {
813-
if let Some(late_bounds) = tcx.is_late_bound_map(fn_def_id.expect_local()) {
814-
for &region_def_id in late_bounds.iter() {
815-
let name = tcx.item_name(region_def_id.to_def_id());
816-
let liberated_region = tcx.mk_region(ty::ReFree(ty::FreeRegion {
817-
scope: fn_def_id,
818-
bound_region: ty::BoundRegionKind::BrNamed(region_def_id.to_def_id(), name),
819-
}));
820-
f(liberated_region);
850+
let typeck_root_def_id = tcx.typeck_root_def_id(mir_def_id.to_def_id());
851+
852+
// Walk up the tree, collecting late-bound regions until we hit the typeck root
853+
loop {
854+
for_each_late_bound_region_in_item(tcx, mir_def_id, &mut f);
855+
856+
if mir_def_id.to_def_id() == typeck_root_def_id {
857+
break;
858+
} else {
859+
mir_def_id = tcx.local_parent(mir_def_id);
821860
}
822861
}
823862
}
863+
864+
/// Iterates over the late-bound regions defined on `mir_def_id` and all of its
865+
/// parents, up to the typeck root, and invokes `f` with the liberated form
866+
/// of each one.
867+
fn for_each_late_bound_region_in_item<'tcx>(
868+
tcx: TyCtxt<'tcx>,
869+
mir_def_id: LocalDefId,
870+
mut f: impl FnMut(ty::Region<'tcx>),
871+
) {
872+
if !tcx.def_kind(mir_def_id).is_fn_like() {
873+
return;
874+
}
875+
876+
for bound_var in tcx.late_bound_vars(tcx.hir().local_def_id_to_hir_id(mir_def_id)) {
877+
let ty::BoundVariableKind::Region(bound_region) = bound_var else { continue; };
878+
let liberated_region = tcx
879+
.mk_region(ty::ReFree(ty::FreeRegion { scope: mir_def_id.to_def_id(), bound_region }));
880+
f(liberated_region);
881+
}
882+
}

compiler/rustc_data_structures/src/sync.rs

+11
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ impl<T> Lock<T> {
410410

411411
#[cfg(parallel_compiler)]
412412
#[inline(always)]
413+
#[track_caller]
413414
pub fn lock(&self) -> LockGuard<'_, T> {
414415
if ERROR_CHECKING {
415416
self.0.try_lock().expect("lock was already held")
@@ -420,21 +421,25 @@ impl<T> Lock<T> {
420421

421422
#[cfg(not(parallel_compiler))]
422423
#[inline(always)]
424+
#[track_caller]
423425
pub fn lock(&self) -> LockGuard<'_, T> {
424426
self.0.borrow_mut()
425427
}
426428

427429
#[inline(always)]
430+
#[track_caller]
428431
pub fn with_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
429432
f(&mut *self.lock())
430433
}
431434

432435
#[inline(always)]
436+
#[track_caller]
433437
pub fn borrow(&self) -> LockGuard<'_, T> {
434438
self.lock()
435439
}
436440

437441
#[inline(always)]
442+
#[track_caller]
438443
pub fn borrow_mut(&self) -> LockGuard<'_, T> {
439444
self.lock()
440445
}
@@ -476,6 +481,7 @@ impl<T> RwLock<T> {
476481

477482
#[cfg(not(parallel_compiler))]
478483
#[inline(always)]
484+
#[track_caller]
479485
pub fn read(&self) -> ReadGuard<'_, T> {
480486
self.0.borrow()
481487
}
@@ -491,6 +497,7 @@ impl<T> RwLock<T> {
491497
}
492498

493499
#[inline(always)]
500+
#[track_caller]
494501
pub fn with_read_lock<F: FnOnce(&T) -> R, R>(&self, f: F) -> R {
495502
f(&*self.read())
496503
}
@@ -509,6 +516,7 @@ impl<T> RwLock<T> {
509516

510517
#[cfg(not(parallel_compiler))]
511518
#[inline(always)]
519+
#[track_caller]
512520
pub fn write(&self) -> WriteGuard<'_, T> {
513521
self.0.borrow_mut()
514522
}
@@ -524,16 +532,19 @@ impl<T> RwLock<T> {
524532
}
525533

526534
#[inline(always)]
535+
#[track_caller]
527536
pub fn with_write_lock<F: FnOnce(&mut T) -> R, R>(&self, f: F) -> R {
528537
f(&mut *self.write())
529538
}
530539

531540
#[inline(always)]
541+
#[track_caller]
532542
pub fn borrow(&self) -> ReadGuard<'_, T> {
533543
self.read()
534544
}
535545

536546
#[inline(always)]
547+
#[track_caller]
537548
pub fn borrow_mut(&self) -> WriteGuard<'_, T> {
538549
self.write()
539550
}

compiler/rustc_hir_analysis/src/collect/lifetimes.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1377,11 +1377,12 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
13771377
} else if let Some(body_id) = outermost_body {
13781378
let fn_id = self.tcx.hir().body_owner(body_id);
13791379
match self.tcx.hir().get(fn_id) {
1380-
Node::Item(&hir::Item { kind: hir::ItemKind::Fn(..), .. })
1381-
| Node::TraitItem(&hir::TraitItem {
1380+
Node::Item(hir::Item { kind: hir::ItemKind::Fn(..), .. })
1381+
| Node::TraitItem(hir::TraitItem {
13821382
kind: hir::TraitItemKind::Fn(..), ..
13831383
})
1384-
| Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }) => {
1384+
| Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. })
1385+
| Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(..), .. }) => {
13851386
let scope = self.tcx.hir().local_def_id(fn_id);
13861387
def = Region::Free(scope.to_def_id(), def.id().unwrap());
13871388
}

compiler/rustc_monomorphize/src/collector.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,10 @@ fn create_mono_items_for_default_impls<'tcx>(
13361336
) {
13371337
match item.kind {
13381338
hir::ItemKind::Impl(ref impl_) => {
1339+
if matches!(impl_.polarity, hir::ImplPolarity::Negative(_)) {
1340+
return;
1341+
}
1342+
13391343
for param in impl_.generics.params {
13401344
match param.kind {
13411345
hir::GenericParamKind::Lifetime { .. } => {}

compiler/rustc_resolve/src/imports.rs

+10-18
Original file line numberDiff line numberDiff line change
@@ -473,7 +473,7 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
473473
{
474474
// In the case of a new import line, throw a diagnostic message
475475
// for the previous line.
476-
self.throw_unresolved_import_error(errors, None);
476+
self.throw_unresolved_import_error(errors);
477477
errors = vec![];
478478
}
479479
if seen_spans.insert(err.span) {
@@ -505,29 +505,21 @@ impl<'a, 'b> ImportResolver<'a, 'b> {
505505
}
506506

507507
if !errors.is_empty() {
508-
self.throw_unresolved_import_error(errors, None);
508+
self.throw_unresolved_import_error(errors);
509509
}
510510
}
511511

512-
fn throw_unresolved_import_error(
513-
&self,
514-
errors: Vec<(String, UnresolvedImportError)>,
515-
span: Option<MultiSpan>,
516-
) {
512+
fn throw_unresolved_import_error(&self, errors: Vec<(String, UnresolvedImportError)>) {
513+
if errors.is_empty() {
514+
return;
515+
}
516+
517517
/// Upper limit on the number of `span_label` messages.
518518
const MAX_LABEL_COUNT: usize = 10;
519519

520-
let (span, msg) = if errors.is_empty() {
521-
(span.unwrap(), "unresolved import".to_string())
522-
} else {
523-
let span = MultiSpan::from_spans(errors.iter().map(|(_, err)| err.span).collect());
524-
525-
let paths = errors.iter().map(|(path, _)| format!("`{}`", path)).collect::<Vec<_>>();
526-
527-
let msg = format!("unresolved import{} {}", pluralize!(paths.len()), paths.join(", "),);
528-
529-
(span, msg)
530-
};
520+
let span = MultiSpan::from_spans(errors.iter().map(|(_, err)| err.span).collect());
521+
let paths = errors.iter().map(|(path, _)| format!("`{}`", path)).collect::<Vec<_>>();
522+
let msg = format!("unresolved import{} {}", pluralize!(paths.len()), paths.join(", "),);
531523

532524
let mut diag = struct_span_err!(self.r.session, span, E0432, "{}", &msg);
533525

library/alloc/src/vec/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2588,7 +2588,7 @@ impl<T: Clone, A: Allocator> ExtendFromWithinSpec for Vec<T, A> {
25882588
let (this, spare, len) = unsafe { self.split_at_spare_mut_with_len() };
25892589

25902590
// SAFETY:
2591-
// - caller guaratees that src is a valid index
2591+
// - caller guarantees that src is a valid index
25922592
let to_clone = unsafe { this.get_unchecked(src) };
25932593

25942594
iter::zip(to_clone, spare)
@@ -2607,7 +2607,7 @@ impl<T: Copy, A: Allocator> ExtendFromWithinSpec for Vec<T, A> {
26072607
let (init, spare) = self.split_at_spare_mut();
26082608

26092609
// SAFETY:
2610-
// - caller guaratees that `src` is a valid index
2610+
// - caller guarantees that `src` is a valid index
26112611
let source = unsafe { init.get_unchecked(src) };
26122612

26132613
// SAFETY:

0 commit comments

Comments
 (0)