Skip to content

Commit

Permalink
Auto merge of rust-lang#108861 - b-naber:eager-nll-type-relating, r=lcnr
Browse files Browse the repository at this point in the history
Make NLL Type Relating Eager

We previously instantiated bound regions in nll type relating lazily. Making this eager is more consistent with how we handle type relating in [`higher_ranked_sub`](https://github.com/rust-lang/rust/blob/0a3b557d528dd7c8a88ceca6f7dc0699b89a3ef4/compiler/rustc_infer/src/infer/higher_ranked/mod.rs#L28) and should allow us to short circuit in case there's structural equality.
  • Loading branch information
bors committed Mar 23, 2023
2 parents 8859fde + 8f4cf2e commit 1c771fe
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 199 deletions.
18 changes: 4 additions & 14 deletions compiler/rustc_borrowck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -510,16 +510,11 @@ impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
.as_var()
.unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region));

if cfg!(debug_assertions) {
if cfg!(debug_assertions) && !self.inside_canonicalization_ctxt() {
debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);
let ctxt = get_ctxt_fn();
let mut var_to_origin = self.reg_var_to_origin.borrow_mut();
let prev = var_to_origin.insert(vid, ctxt);

// This only makes sense if not called in a canonicalization context. If this
// ever changes we either want to get rid of `BorrowckInferContext::reg_var_to_origin`
// or modify how we track nll region vars for that map.
assert!(matches!(prev, None));
var_to_origin.insert(vid, ctxt);
}

next_region
Expand All @@ -539,16 +534,11 @@ impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> {
.as_var()
.unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region));

if cfg!(debug_assertions) {
if cfg!(debug_assertions) && !self.inside_canonicalization_ctxt() {
debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin);
let ctxt = get_ctxt_fn();
let mut var_to_origin = self.reg_var_to_origin.borrow_mut();
let prev = var_to_origin.insert(vid, ctxt);

// This only makes sense if not called in a canonicalization context. If this
// ever changes we either want to get rid of `BorrowckInferContext::reg_var_to_origin`
// or modify how we track nll region vars for that map.
assert!(matches!(prev, None));
var_to_origin.insert(vid, ctxt);
}

next_region
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_borrowck/src/region_infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,12 @@ fn sccs_info<'cx, 'tcx>(

let mut var_to_origin_sorted = var_to_origin.clone().into_iter().collect::<Vec<_>>();
var_to_origin_sorted.sort_by_key(|vto| vto.0);
let mut debug_str = "region variables to origins:\n".to_string();

let mut reg_vars_to_origins_str = "region variables to origins:\n".to_string();
for (reg_var, origin) in var_to_origin_sorted.into_iter() {
debug_str.push_str(&format!("{:?}: {:?}\n", reg_var, origin));
reg_vars_to_origins_str.push_str(&format!("{:?}: {:?}\n", reg_var, origin));
}
debug!(debug_str);
debug!("{}", reg_vars_to_origins_str);

let num_components = sccs.scc_data().ranges().len();
let mut components = vec![FxIndexSet::default(); num_components];
Expand All @@ -275,12 +276,12 @@ fn sccs_info<'cx, 'tcx>(
for (scc_idx, reg_vars_origins) in components.iter().enumerate() {
let regions_info = reg_vars_origins.clone().into_iter().collect::<Vec<_>>();
components_str.push_str(&format!(
"{:?}: {:?})",
"{:?}: {:?},\n)",
ConstraintSccIndex::from_usize(scc_idx),
regions_info,
))
}
debug!(components_str);
debug!("{}", components_str);

// calculate the best representative for each component
let components_representatives = components
Expand Down
18 changes: 8 additions & 10 deletions compiler/rustc_borrowck/src/type_check/relate_tys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,12 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>

let reg_var =
reg.as_var().unwrap_or_else(|| bug!("expected region {:?} to be of kind ReVar", reg));
let mut var_to_origin = self.type_checker.infcx.reg_var_to_origin.borrow_mut();
let prev = var_to_origin.insert(reg_var, RegionCtxt::Placeholder(reg_info));
assert!(matches!(prev, None));

if cfg!(debug_assertions) && !self.type_checker.infcx.inside_canonicalization_ctxt() {
let mut var_to_origin = self.type_checker.infcx.reg_var_to_origin.borrow_mut();
debug!(?reg_var);
var_to_origin.insert(reg_var, RegionCtxt::Placeholder(reg_info));
}

reg
}
Expand All @@ -149,14 +152,9 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx>
let reg_var =
reg.as_var().unwrap_or_else(|| bug!("expected region {:?} to be of kind ReVar", reg));

if cfg!(debug_assertions) {
if cfg!(debug_assertions) && !self.type_checker.infcx.inside_canonicalization_ctxt() {
let mut var_to_origin = self.type_checker.infcx.reg_var_to_origin.borrow_mut();
let prev = var_to_origin.insert(reg_var, RegionCtxt::Existential(None));

// It only makes sense to track region vars in non-canonicalization contexts. If this
// ever changes we either want to get rid of `BorrowckInferContext::reg_var_to_origin`
// or modify how we track nll region vars for that map.
assert!(matches!(prev, None));
var_to_origin.insert(reg_var, RegionCtxt::Existential(None));
}

reg
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_infer/src/infer/at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use super::*;
use rustc_middle::ty::relate::{Relate, TypeRelation};
use rustc_middle::ty::{Const, ImplSubject};

use std::cell::Cell;

/// Whether we should define opaque types or just treat them opaquely.
///
/// Currently only used to prevent predicate matching from matching anything
Expand Down Expand Up @@ -82,6 +84,7 @@ impl<'tcx> InferCtxt<'tcx> {
in_snapshot: self.in_snapshot.clone(),
universe: self.universe.clone(),
intercrate: self.intercrate,
inside_canonicalization_ctxt: Cell::new(self.inside_canonicalization_ctxt()),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_infer/src/infer/canonical/canonicalizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,8 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
where
V: TypeFoldable<TyCtxt<'tcx>>,
{
let _inside_canonical_ctxt_guard = infcx.set_canonicalization_ctxt();

let needs_canonical_flags = if canonicalize_region_mode.any() {
TypeFlags::NEEDS_INFER |
TypeFlags::HAS_FREE_REGIONS | // `HAS_RE_PLACEHOLDER` implies `HAS_FREE_REGIONS`
Expand Down
32 changes: 32 additions & 0 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use rustc_span::Span;

use std::cell::{Cell, RefCell};
use std::fmt;
use std::ops::Drop;

use self::combine::CombineFields;
use self::error_reporting::TypeErrCtxt;
Expand Down Expand Up @@ -342,6 +343,11 @@ pub struct InferCtxt<'tcx> {
/// there is no type that the user could *actually name* that
/// would satisfy it. This avoids crippling inference, basically.
pub intercrate: bool,

/// Flag that is set when we enter canonicalization. Used for debugging to ensure
/// that we only collect region information for `BorrowckInferCtxt::reg_var_to_origin`
/// inside non-canonicalization contexts.
inside_canonicalization_ctxt: Cell<bool>,
}

/// See the `error_reporting` module for more details.
Expand Down Expand Up @@ -633,6 +639,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
skip_leak_check: Cell::new(false),
universe: Cell::new(ty::UniverseIndex::ROOT),
intercrate,
inside_canonicalization_ctxt: Cell::new(false),
}
}
}
Expand Down Expand Up @@ -1728,6 +1735,31 @@ impl<'tcx> InferCtxt<'tcx> {
}
}
}

pub fn inside_canonicalization_ctxt(&self) -> bool {
self.inside_canonicalization_ctxt.get()
}

pub fn set_canonicalization_ctxt(&self) -> CanonicalizationCtxtGuard<'_, 'tcx> {
let prev_ctxt = self.inside_canonicalization_ctxt();
self.inside_canonicalization_ctxt.set(true);
CanonicalizationCtxtGuard { prev_ctxt, infcx: self }
}

fn set_canonicalization_ctxt_to(&self, ctxt: bool) {
self.inside_canonicalization_ctxt.set(ctxt);
}
}

pub struct CanonicalizationCtxtGuard<'cx, 'tcx> {
prev_ctxt: bool,
infcx: &'cx InferCtxt<'tcx>,
}

impl<'cx, 'tcx> Drop for CanonicalizationCtxtGuard<'cx, 'tcx> {
fn drop(&mut self) {
self.infcx.set_canonicalization_ctxt_to(self.prev_ctxt)
}
}

impl<'tcx> TypeErrCtxt<'_, 'tcx> {
Expand Down
Loading

0 comments on commit 1c771fe

Please sign in to comment.