Skip to content

Commit

Permalink
Rollup merge of rust-lang#97292 - compiler-errors:tcxify-rustc, r=dav…
Browse files Browse the repository at this point in the history
…idtwco

Lifetime variance fixes for rustc

rust-lang#97287 migrates rustc to a `Ty` type that is invariant over its lifetime `'tcx`, so I need to fix a bunch of places that assume that `Ty<'a>` and `Ty<'b>` can be unified by shortening both to some common lifetime.

This is doable, since many lifetimes are already `'tcx`, so all this PR does is be a bit more explicit that elided lifetimes are actually `'tcx`.

Split out from rust-lang#97287 so the compiler team can review independently.
  • Loading branch information
Dylan-DPC authored May 23, 2022
2 parents 215722b + 1784634 commit f4bf64c
Show file tree
Hide file tree
Showing 22 changed files with 92 additions and 86 deletions.
7 changes: 4 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use either::Either;
use rustc_const_eval::util::CallKind;
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashSet;
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed, MultiSpan};
use rustc_hir as hir;
Expand Down Expand Up @@ -1622,10 +1623,10 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
location: Location,
mpi: MovePathIndex,
) -> (Vec<MoveSite>, Vec<Location>) {
fn predecessor_locations<'a>(
body: &'a mir::Body<'_>,
fn predecessor_locations<'tcx, 'a>(
body: &'a mir::Body<'tcx>,
location: Location,
) -> impl Iterator<Item = Location> + 'a {
) -> impl Iterator<Item = Location> + Captures<'tcx> + 'a {
if location.statement_index == 0 {
let predecessors = body.predecessors()[location.block].to_vec();
Either::Left(predecessors.into_iter().map(move |bb| body.terminator_loc(bb)))
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_borrowck/src/member_constraints.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rustc_data_structures::captures::Captures;
use rustc_data_structures::fx::FxHashMap;
use rustc_index::vec::IndexVec;
use rustc_middle::infer::MemberConstraint;
Expand Down Expand Up @@ -140,11 +141,13 @@ where
}
}

impl<R> MemberConstraintSet<'_, R>
impl<'tcx, R> MemberConstraintSet<'tcx, R>
where
R: Copy + Hash + Eq,
{
pub(crate) fn all_indices(&self) -> impl Iterator<Item = NllMemberConstraintIndex> + '_ {
pub(crate) fn all_indices(
&self,
) -> impl Iterator<Item = NllMemberConstraintIndex> + Captures<'tcx> + '_ {
self.constraints.indices()
}

Expand All @@ -154,7 +157,7 @@ where
pub(crate) fn indices(
&self,
member_region_vid: R,
) -> impl Iterator<Item = NllMemberConstraintIndex> + '_ {
) -> impl Iterator<Item = NllMemberConstraintIndex> + Captures<'tcx> + '_ {
let mut next = self.first_constraints.get(&member_region_vid).cloned();
std::iter::from_fn(move || -> Option<NllMemberConstraintIndex> {
if let Some(current) = next {
Expand Down
16 changes: 8 additions & 8 deletions compiler/rustc_borrowck/src/type_check/liveness/polonius.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ use super::TypeChecker;
type VarPointRelation = Vec<(Local, LocationIndex)>;
type PathPointRelation = Vec<(MovePathIndex, LocationIndex)>;

struct UseFactsExtractor<'me> {
struct UseFactsExtractor<'me, 'tcx> {
var_defined_at: &'me mut VarPointRelation,
var_used_at: &'me mut VarPointRelation,
location_table: &'me LocationTable,
var_dropped_at: &'me mut VarPointRelation,
move_data: &'me MoveData<'me>,
move_data: &'me MoveData<'tcx>,
path_accessed_at_base: &'me mut PathPointRelation,
}

// A Visitor to walk through the MIR and extract point-wise facts
impl UseFactsExtractor<'_> {
impl UseFactsExtractor<'_, '_> {
fn location_to_index(&self, location: Location) -> LocationIndex {
self.location_table.mid_index(location)
}
Expand Down Expand Up @@ -53,7 +53,7 @@ impl UseFactsExtractor<'_> {
}
}

impl Visitor<'_> for UseFactsExtractor<'_> {
impl<'a, 'tcx> Visitor<'tcx> for UseFactsExtractor<'a, 'tcx> {
fn visit_local(&mut self, &local: &Local, context: PlaceContext, location: Location) {
match def_use::categorize(context) {
Some(DefUse::Def) => self.insert_def(local, location),
Expand All @@ -63,7 +63,7 @@ impl Visitor<'_> for UseFactsExtractor<'_> {
}
}

fn visit_place(&mut self, place: &Place<'_>, context: PlaceContext, location: Location) {
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, location: Location) {
self.super_place(place, context, location);
match context {
PlaceContext::NonMutatingUse(_) => {
Expand All @@ -82,11 +82,11 @@ impl Visitor<'_> for UseFactsExtractor<'_> {
}
}

pub(super) fn populate_access_facts<'tcx>(
typeck: &mut TypeChecker<'_, 'tcx>,
pub(super) fn populate_access_facts<'a, 'tcx>(
typeck: &mut TypeChecker<'a, 'tcx>,
body: &Body<'tcx>,
location_table: &LocationTable,
move_data: &MoveData<'_>,
move_data: &MoveData<'tcx>,
dropped_at: &mut Vec<(Local, Location)>,
) {
debug!("populate_access_facts()");
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_const_eval/src/interpret/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
size: Size,
align: Align,
kind: MemoryKind<M::MemoryKind>,
) -> InterpResult<'static, Pointer<M::PointerTag>> {
) -> InterpResult<'tcx, Pointer<M::PointerTag>> {
let alloc = Allocation::uninit(size, align, M::PANIC_ON_ALLOC_FAIL)?;
Ok(self.allocate_raw_ptr(alloc, kind))
}
Expand Down Expand Up @@ -402,7 +402,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
msg: CheckInAllocMsg,
alloc_size: impl FnOnce(AllocId, Size, M::TagExtra) -> InterpResult<'tcx, (Size, Align, T)>,
) -> InterpResult<'tcx, Option<T>> {
fn check_offset_align(offset: u64, align: Align) -> InterpResult<'static> {
fn check_offset_align<'tcx>(offset: u64, align: Align) -> InterpResult<'tcx> {
if offset % align.bytes() == 0 {
Ok(())
} else {
Expand Down Expand Up @@ -654,7 +654,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
&self,
id: AllocId,
liveness: AllocCheck,
) -> InterpResult<'static, (Size, Align)> {
) -> InterpResult<'tcx, (Size, Align)> {
// # Regular allocations
// Don't use `self.get_raw` here as that will
// a) cause cycles in case `id` refers to a static
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ where
&mut self,
layout: TyAndLayout<'tcx>,
kind: MemoryKind<M::MemoryKind>,
) -> InterpResult<'static, MPlaceTy<'tcx, M::PointerTag>> {
) -> InterpResult<'tcx, MPlaceTy<'tcx, M::PointerTag>> {
let ptr = self.allocate_ptr(layout.size, layout.align.abi, kind)?;
Ok(MPlaceTy::from_aligned_ptr(ptr.into(), layout))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
&self,
anon_region: Region<'tcx>,
replace_region: Region<'tcx>,
) -> Option<AnonymousParamInfo<'_>> {
) -> Option<AnonymousParamInfo<'tcx>> {
find_param_with_region(self.tcx(), anon_region, replace_region)
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/interpret/allocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ impl<Tag> Allocation<Tag> {

/// Try to create an Allocation of `size` bytes, failing if there is not enough memory
/// available to the compiler to do so.
pub fn uninit(size: Size, align: Align, panic_on_fail: bool) -> InterpResult<'static, Self> {
pub fn uninit<'tcx>(size: Size, align: Align, panic_on_fail: bool) -> InterpResult<'tcx, Self> {
let bytes = Box::<[u8]>::try_new_zeroed_slice(size.bytes_usize()).map_err(|_| {
// This results in an error that can happen non-deterministically, since the memory
// available to the compiler can change between runs. Normally queries are always
Expand Down
34 changes: 17 additions & 17 deletions compiler/rustc_middle/src/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,90 +397,90 @@ impl<'tcx, Tag: Provenance> Scalar<Tag> {
/// Converts the scalar to produce an unsigned integer of the given size.
/// Fails if the scalar is a pointer.
#[inline]
pub fn to_uint(self, size: Size) -> InterpResult<'static, u128> {
pub fn to_uint(self, size: Size) -> InterpResult<'tcx, u128> {
self.to_bits(size)
}

/// Converts the scalar to produce a `u8`. Fails if the scalar is a pointer.
pub fn to_u8(self) -> InterpResult<'static, u8> {
pub fn to_u8(self) -> InterpResult<'tcx, u8> {
self.to_uint(Size::from_bits(8)).map(|v| u8::try_from(v).unwrap())
}

/// Converts the scalar to produce a `u16`. Fails if the scalar is a pointer.
pub fn to_u16(self) -> InterpResult<'static, u16> {
pub fn to_u16(self) -> InterpResult<'tcx, u16> {
self.to_uint(Size::from_bits(16)).map(|v| u16::try_from(v).unwrap())
}

/// Converts the scalar to produce a `u32`. Fails if the scalar is a pointer.
pub fn to_u32(self) -> InterpResult<'static, u32> {
pub fn to_u32(self) -> InterpResult<'tcx, u32> {
self.to_uint(Size::from_bits(32)).map(|v| u32::try_from(v).unwrap())
}

/// Converts the scalar to produce a `u64`. Fails if the scalar is a pointer.
pub fn to_u64(self) -> InterpResult<'static, u64> {
pub fn to_u64(self) -> InterpResult<'tcx, u64> {
self.to_uint(Size::from_bits(64)).map(|v| u64::try_from(v).unwrap())
}

/// Converts the scalar to produce a `u128`. Fails if the scalar is a pointer.
pub fn to_u128(self) -> InterpResult<'static, u128> {
pub fn to_u128(self) -> InterpResult<'tcx, u128> {
self.to_uint(Size::from_bits(128))
}

/// Converts the scalar to produce a machine-pointer-sized unsigned integer.
/// Fails if the scalar is a pointer.
pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'static, u64> {
pub fn to_machine_usize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, u64> {
let b = self.to_uint(cx.data_layout().pointer_size)?;
Ok(u64::try_from(b).unwrap())
}

/// Converts the scalar to produce a signed integer of the given size.
/// Fails if the scalar is a pointer.
#[inline]
pub fn to_int(self, size: Size) -> InterpResult<'static, i128> {
pub fn to_int(self, size: Size) -> InterpResult<'tcx, i128> {
let b = self.to_bits(size)?;
Ok(size.sign_extend(b) as i128)
}

/// Converts the scalar to produce an `i8`. Fails if the scalar is a pointer.
pub fn to_i8(self) -> InterpResult<'static, i8> {
pub fn to_i8(self) -> InterpResult<'tcx, i8> {
self.to_int(Size::from_bits(8)).map(|v| i8::try_from(v).unwrap())
}

/// Converts the scalar to produce an `i16`. Fails if the scalar is a pointer.
pub fn to_i16(self) -> InterpResult<'static, i16> {
pub fn to_i16(self) -> InterpResult<'tcx, i16> {
self.to_int(Size::from_bits(16)).map(|v| i16::try_from(v).unwrap())
}

/// Converts the scalar to produce an `i32`. Fails if the scalar is a pointer.
pub fn to_i32(self) -> InterpResult<'static, i32> {
pub fn to_i32(self) -> InterpResult<'tcx, i32> {
self.to_int(Size::from_bits(32)).map(|v| i32::try_from(v).unwrap())
}

/// Converts the scalar to produce an `i64`. Fails if the scalar is a pointer.
pub fn to_i64(self) -> InterpResult<'static, i64> {
pub fn to_i64(self) -> InterpResult<'tcx, i64> {
self.to_int(Size::from_bits(64)).map(|v| i64::try_from(v).unwrap())
}

/// Converts the scalar to produce an `i128`. Fails if the scalar is a pointer.
pub fn to_i128(self) -> InterpResult<'static, i128> {
pub fn to_i128(self) -> InterpResult<'tcx, i128> {
self.to_int(Size::from_bits(128))
}

/// Converts the scalar to produce a machine-pointer-sized signed integer.
/// Fails if the scalar is a pointer.
pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'static, i64> {
pub fn to_machine_isize(self, cx: &impl HasDataLayout) -> InterpResult<'tcx, i64> {
let b = self.to_int(cx.data_layout().pointer_size)?;
Ok(i64::try_from(b).unwrap())
}

#[inline]
pub fn to_f32(self) -> InterpResult<'static, Single> {
pub fn to_f32(self) -> InterpResult<'tcx, Single> {
// Going through `u32` to check size and truncation.
Ok(Single::from_bits(self.to_u32()?.into()))
}

#[inline]
pub fn to_f64(self) -> InterpResult<'static, Double> {
pub fn to_f64(self) -> InterpResult<'tcx, Double> {
// Going through `u64` to check size and truncation.
Ok(Double::from_bits(self.to_u64()?.into()))
}
Expand Down Expand Up @@ -534,7 +534,7 @@ impl<Tag> ScalarMaybeUninit<Tag> {
}

#[inline]
pub fn check_init(self) -> InterpResult<'static, Scalar<Tag>> {
pub fn check_init<'tcx>(self) -> InterpResult<'tcx, Scalar<Tag>> {
match self {
ScalarMaybeUninit::Scalar(scalar) => Ok(scalar),
ScalarMaybeUninit::Uninit => throw_ub!(InvalidUninitBytes(None)),
Expand Down
7 changes: 5 additions & 2 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use crate::ty::subst::{GenericArg, InternalSubsts, Subst, SubstsRef};
use crate::ty::{self, List, Ty, TyCtxt};
use crate::ty::{AdtDef, InstanceDef, Region, ScalarInt, UserTypeAnnotationIndex};

use rustc_data_structures::captures::Captures;
use rustc_errors::ErrorGuaranteed;
use rustc_hir::def::{CtorKind, Namespace};
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID};
Expand Down Expand Up @@ -484,7 +485,7 @@ impl<'tcx> Body<'tcx> {

/// Returns an iterator over all user-declared mutable locals.
#[inline]
pub fn mut_vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
pub fn mut_vars_iter<'a>(&'a self) -> impl Iterator<Item = Local> + Captures<'tcx> + 'a {
(self.arg_count + 1..self.local_decls.len()).filter_map(move |index| {
let local = Local::new(index);
let decl = &self.local_decls[local];
Expand All @@ -498,7 +499,9 @@ impl<'tcx> Body<'tcx> {

/// Returns an iterator over all user-declared mutable arguments and locals.
#[inline]
pub fn mut_vars_and_args_iter<'a>(&'a self) -> impl Iterator<Item = Local> + 'a {
pub fn mut_vars_and_args_iter<'a>(
&'a self,
) -> impl Iterator<Item = Local> + Captures<'tcx> + 'a {
(1..self.local_decls.len()).filter_map(move |index| {
let local = Local::new(index);
let decl = &self.local_decls[local];
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<'tcx> MirPatch<'tcx> {
}
}

pub fn source_info_for_location(&self, body: &Body<'_>, loc: Location) -> SourceInfo {
pub fn source_info_for_location(&self, body: &Body<'tcx>, loc: Location) -> SourceInfo {
let data = match loc.block.index().checked_sub(body.basic_blocks().len()) {
Some(new) => &self.new_blocks[new],
None => &body[loc.block],
Expand Down
12 changes: 7 additions & 5 deletions compiler/rustc_middle/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ impl Hash for ObligationCause<'_> {
}
}

const MISC_OBLIGATION_CAUSE_CODE: ObligationCauseCode<'static> = MiscObligation;

impl<'tcx> ObligationCause<'tcx> {
#[inline]
pub fn new(
Expand Down Expand Up @@ -201,7 +199,7 @@ pub struct UnifyReceiverContext<'tcx> {

#[derive(Clone, Debug, PartialEq, Eq, Hash, Lift, Default)]
pub struct InternedObligationCauseCode<'tcx> {
/// `None` for `MISC_OBLIGATION_CAUSE_CODE` (a common case, occurs ~60% of
/// `None` for `ObligationCauseCode::MiscObligation` (a common case, occurs ~60% of
/// the time). `Some` otherwise.
code: Option<Lrc<ObligationCauseCode<'tcx>>>,
}
Expand All @@ -210,7 +208,11 @@ impl<'tcx> ObligationCauseCode<'tcx> {
#[inline(always)]
fn into(self) -> InternedObligationCauseCode<'tcx> {
InternedObligationCauseCode {
code: if let MISC_OBLIGATION_CAUSE_CODE = self { None } else { Some(Lrc::new(self)) },
code: if let ObligationCauseCode::MiscObligation = self {
None
} else {
Some(Lrc::new(self))
},
}
}
}
Expand All @@ -219,7 +221,7 @@ impl<'tcx> std::ops::Deref for InternedObligationCauseCode<'tcx> {
type Target = ObligationCauseCode<'tcx>;

fn deref(&self) -> &Self::Target {
self.code.as_deref().unwrap_or(&MISC_OBLIGATION_CAUSE_CODE)
self.code.as_deref().unwrap_or(&ObligationCauseCode::MiscObligation)
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2163,7 +2163,7 @@ impl<'tcx> SizeSkeleton<'tcx> {
}
}

pub fn same_size(self, other: SizeSkeleton<'_>) -> bool {
pub fn same_size(self, other: SizeSkeleton<'tcx>) -> bool {
match (self, other) {
(SizeSkeleton::Known(a), SizeSkeleton::Known(b)) => a == b,
(SizeSkeleton::Pointer { tail: a, .. }, SizeSkeleton::Pointer { tail: b, .. }) => {
Expand Down
Loading

0 comments on commit f4bf64c

Please sign in to comment.