From cb35a7b4815774f7d8c8b9770df5a9719532755f Mon Sep 17 00:00:00 2001 From: b-naber Date: Mon, 7 Nov 2022 18:24:07 +0100 Subject: [PATCH 01/10] add BorrowckInferCtxt --- compiler/rustc_borrowck/src/lib.rs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 98103af779d..fef5f4bf466 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -25,7 +25,7 @@ use rustc_hir as hir; use rustc_hir::def_id::LocalDefId; use rustc_index::bit_set::ChunkedBitSet; use rustc_index::vec::IndexVec; -use rustc_infer::infer::{DefiningAnchor, InferCtxt, TyCtxtInferExt}; +use rustc_infer::infer::{DefiningAnchor, InferCtxt, NllRegionVariableOrigin, TyCtxtInferExt}; use rustc_middle::mir::{ traversal, Body, ClearCrossCrate, Local, Location, Mutability, NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef, VarDebugInfoContents, @@ -43,6 +43,7 @@ use smallvec::SmallVec; use std::cell::OnceCell; use std::cell::RefCell; use std::collections::BTreeMap; +use std::ops::Deref; use std::rc::Rc; use rustc_mir_dataflow::impls::{ @@ -481,6 +482,33 @@ pub struct BodyWithBorrowckFacts<'tcx> { pub location_table: LocationTable, } +struct BorrowckInferCtxt<'cx, 'tcx> { + pub(crate) infcx: &'cx InferCtxt<'tcx>, + + #[cfg(debug_assertions)] + pub(crate) _reg_var_to_origin: RefCell, NllRegionVariableOrigin>>, +} + +impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> { + #[cfg(not(debug_assertions))] + pub(crate) fn _new(infcx: &'cx InferCtxt<'tcx>) -> Self { + BorrowckInferCtxt { infcx } + } + + #[cfg(debug_assertions)] + pub(crate) fn _new(infcx: &'cx InferCtxt<'tcx>) -> Self { + BorrowckInferCtxt { infcx, _reg_var_to_origin: RefCell::new(Default::default()) } + } +} + +impl<'cx, 'tcx> Deref for BorrowckInferCtxt<'cx, 'tcx> { + type Target = InferCtxt<'tcx>; + + fn deref(&self) -> &'cx Self::Target { + self.infcx + } +} + struct MirBorrowckCtxt<'cx, 'tcx> { infcx: &'cx InferCtxt<'tcx>, param_env: ParamEnv<'tcx>, From 2f79f73821db1f1f169abe526fc89c8b376636ea Mon Sep 17 00:00:00 2001 From: b-naber Date: Tue, 8 Nov 2022 16:27:01 +0100 Subject: [PATCH 02/10] collect region contexts during mir renumbering --- Cargo.lock | 23 +++- compiler/rustc_borrowck/src/lib.rs | 90 +++++++++++--- compiler/rustc_borrowck/src/nll.rs | 16 ++- compiler/rustc_borrowck/src/renumber.rs | 115 ++++++++++++++++- compiler/rustc_borrowck/src/type_check/mod.rs | 34 ++++- .../rustc_borrowck/src/universal_regions.rs | 117 +++++++++++++++++- compiler/rustc_infer/src/infer/mod.rs | 2 + compiler/rustc_middle/src/mir/visit.rs | 2 +- compiler/rustc_middle/src/ty/sty.rs | 7 ++ 9 files changed, 364 insertions(+), 42 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d02cab38ae8..60fe88764f4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -782,7 +782,7 @@ dependencies = [ "declare_clippy_lint", "if_chain", "itertools", - "pulldown-cmark", + "pulldown-cmark 0.9.2", "quine-mc_cluskey", "regex-syntax", "rustc-semver", @@ -2003,9 +2003,9 @@ dependencies = [ [[package]] name = "http-auth" -version = "0.1.6" +version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0b40b39d66c28829a0cf4d09f7e139ff8201f7500a5083732848ed3b4b4d850" +checksum = "5430cacd7a1f9a02fbeb350dfc81a0e5ed42d81f3398cb0ba184017f85bdcfbc" dependencies = [ "memchr", ] @@ -2555,7 +2555,7 @@ dependencies = [ "memchr", "once_cell", "opener", - "pulldown-cmark", + "pulldown-cmark 0.9.2", "regex", "serde", "serde_json", @@ -2572,7 +2572,7 @@ dependencies = [ "anyhow", "handlebars 3.5.5", "pretty_assertions", - "pulldown-cmark", + "pulldown-cmark 0.7.2", "same-file", "serde_json", "url", @@ -3269,6 +3269,17 @@ dependencies = [ "cc", ] +[[package]] +name = "pulldown-cmark" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca36dea94d187597e104a5c8e4b07576a8a45aa5db48a65e12940d3eb7461f55" +dependencies = [ + "bitflags", + "memchr", + "unicase", +] + [[package]] name = "pulldown-cmark" version = "0.9.2" @@ -4572,7 +4583,7 @@ name = "rustc_resolve" version = "0.0.0" dependencies = [ "bitflags", - "pulldown-cmark", + "pulldown-cmark 0.9.2", "rustc_arena", "rustc_ast", "rustc_ast_pretty", diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index fef5f4bf466..70f6a323e88 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -25,7 +25,9 @@ use rustc_hir as hir; use rustc_hir::def_id::LocalDefId; use rustc_index::bit_set::ChunkedBitSet; use rustc_index::vec::IndexVec; -use rustc_infer::infer::{DefiningAnchor, InferCtxt, NllRegionVariableOrigin, TyCtxtInferExt}; +use rustc_infer::infer::{ + DefiningAnchor, InferCtxt, NllRegionVariableOrigin, RegionVariableOrigin, TyCtxtInferExt, +}; use rustc_middle::mir::{ traversal, Body, ClearCrossCrate, Local, Location, Mutability, NonDivergingIntrinsic, Operand, Place, PlaceElem, PlaceRef, VarDebugInfoContents, @@ -95,6 +97,7 @@ use nll::{PoloniusOutput, ToRegionVid}; use place_ext::PlaceExt; use places_conflict::{places_conflict, PlaceConflictBias}; use region_infer::RegionInferenceContext; +use renumber::RegionCtxt; // FIXME(eddyb) perhaps move this somewhere more centrally. #[derive(Debug)] @@ -168,10 +171,10 @@ fn do_mir_borrowck<'tcx>( return_body_with_facts: bool, ) -> (BorrowCheckResult<'tcx>, Option>>) { let def = input_body.source.with_opt_param().as_local().unwrap(); - debug!(?def); let tcx = infcx.tcx; + let infcx = BorrowckInferCtxt::new(infcx); let param_env = tcx.param_env(def.did); let mut local_names = IndexVec::from_elem(None, &input_body.local_decls); @@ -219,7 +222,7 @@ fn do_mir_borrowck<'tcx>( let mut body_owned = input_body.clone(); let mut promoted = input_promoted.clone(); let free_regions = - nll::replace_regions_in_mir(infcx, param_env, &mut body_owned, &mut promoted); + nll::replace_regions_in_mir(&infcx, param_env, &mut body_owned, &mut promoted); let body = &body_owned; // no further changes let location_table_owned = LocationTable::new(body); @@ -257,7 +260,7 @@ fn do_mir_borrowck<'tcx>( opt_closure_req, nll_errors, } = nll::compute_regions( - infcx, + &infcx, free_regions, body, &promoted, @@ -272,12 +275,12 @@ fn do_mir_borrowck<'tcx>( // Dump MIR results into a file, if that is enabled. This let us // write unit-tests, as well as helping with debugging. - nll::dump_mir_results(infcx, &body, ®ioncx, &opt_closure_req); + nll::dump_mir_results(&infcx, &body, ®ioncx, &opt_closure_req); // We also have a `#[rustc_regions]` annotation that causes us to dump // information. nll::dump_annotation( - infcx, + &infcx, &body, ®ioncx, &opt_closure_req, @@ -321,7 +324,7 @@ fn do_mir_borrowck<'tcx>( if let Err((move_data, move_errors)) = move_data_results { let mut promoted_mbcx = MirBorrowckCtxt { - infcx, + infcx: &infcx, param_env, body: promoted_body, move_data: &move_data, @@ -350,7 +353,7 @@ fn do_mir_borrowck<'tcx>( } let mut mbcx = MirBorrowckCtxt { - infcx, + infcx: &infcx, param_env, body, move_data: &mdpe.move_data, @@ -482,22 +485,81 @@ pub struct BodyWithBorrowckFacts<'tcx> { pub location_table: LocationTable, } -struct BorrowckInferCtxt<'cx, 'tcx> { +pub struct BorrowckInferCtxt<'cx, 'tcx> { pub(crate) infcx: &'cx InferCtxt<'tcx>, #[cfg(debug_assertions)] - pub(crate) _reg_var_to_origin: RefCell, NllRegionVariableOrigin>>, + pub(crate) reg_var_to_origin: RefCell>, } impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> { #[cfg(not(debug_assertions))] - pub(crate) fn _new(infcx: &'cx InferCtxt<'tcx>) -> Self { + pub(crate) fn new(infcx: &'cx InferCtxt<'tcx>) -> Self { BorrowckInferCtxt { infcx } } #[cfg(debug_assertions)] - pub(crate) fn _new(infcx: &'cx InferCtxt<'tcx>) -> Self { - BorrowckInferCtxt { infcx, _reg_var_to_origin: RefCell::new(Default::default()) } + pub(crate) fn new(infcx: &'cx InferCtxt<'tcx>) -> Self { + BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()) } + } + + #[cfg(not(debug_assertions))] + pub(crate) fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> { + self.infcx.next_region_var(origin) + } + + #[cfg(debug_assertions)] + pub(crate) fn next_region_var( + &self, + origin: RegionVariableOrigin, + ctxt: RegionCtxt, + ) -> ty::Region<'tcx> { + let next_region = self.infcx.next_region_var(origin); + let vid = next_region + .try_get_var() + .unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region)); + + debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin); + let mut var_to_origin = self.reg_var_to_origin.borrow_mut(); + let prev = var_to_origin.insert(vid, ctxt); + debug!("var_to_origin after insertion: {:?}", var_to_origin); + + // 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)); + + next_region + } + + #[cfg(not(debug_assertions))] + pub(crate) fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> { + self.infcx.next_nll_region_var(origin) + } + + #[cfg(debug_assertions)] + #[instrument(skip(self), level = "debug")] + pub(crate) fn next_nll_region_var( + &self, + origin: NllRegionVariableOrigin, + ctxt: RegionCtxt, + ) -> ty::Region<'tcx> { + let next_region = self.infcx.next_nll_region_var(origin.clone()); + let vid = next_region + .try_get_var() + .unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region)); + + debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin); + let mut var_to_origin = self.reg_var_to_origin.borrow_mut(); + let prev = var_to_origin.insert(vid, ctxt); + debug!("var_to_origin after insertion: {:?}", var_to_origin); + + // 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)); + + next_region } } @@ -510,7 +572,7 @@ impl<'cx, 'tcx> Deref for BorrowckInferCtxt<'cx, 'tcx> { } struct MirBorrowckCtxt<'cx, 'tcx> { - infcx: &'cx InferCtxt<'tcx>, + infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>, param_env: ParamEnv<'tcx>, body: &'cx Body<'tcx>, move_data: &'cx MoveData<'tcx>, diff --git a/compiler/rustc_borrowck/src/nll.rs b/compiler/rustc_borrowck/src/nll.rs index c71413e8e7c..c3301ac1442 100644 --- a/compiler/rustc_borrowck/src/nll.rs +++ b/compiler/rustc_borrowck/src/nll.rs @@ -5,7 +5,6 @@ use rustc_data_structures::vec_map::VecMap; use rustc_hir::def_id::LocalDefId; use rustc_index::vec::IndexVec; -use rustc_infer::infer::InferCtxt; use rustc_middle::mir::{create_dump_file, dump_enabled, dump_mir, PassWhere}; use rustc_middle::mir::{ BasicBlock, Body, ClosureOutlivesSubject, ClosureRegionRequirements, LocalKind, Location, @@ -37,7 +36,7 @@ use crate::{ renumber, type_check::{self, MirTypeckRegionConstraints, MirTypeckResults}, universal_regions::UniversalRegions, - Upvar, + BorrowckInferCtxt, Upvar, }; pub type PoloniusOutput = Output; @@ -58,7 +57,7 @@ pub(crate) struct NllOutput<'tcx> { /// `compute_regions`. #[instrument(skip(infcx, param_env, body, promoted), level = "debug")] pub(crate) fn replace_regions_in_mir<'tcx>( - infcx: &InferCtxt<'tcx>, + infcx: &BorrowckInferCtxt<'_, 'tcx>, param_env: ty::ParamEnv<'tcx>, body: &mut Body<'tcx>, promoted: &mut IndexVec>, @@ -157,7 +156,7 @@ fn populate_polonius_move_facts( /// /// This may result in errors being reported. pub(crate) fn compute_regions<'cx, 'tcx>( - infcx: &InferCtxt<'tcx>, + infcx: &BorrowckInferCtxt<'_, 'tcx>, universal_regions: UniversalRegions<'tcx>, body: &Body<'tcx>, promoted: &IndexVec>, @@ -258,6 +257,11 @@ pub(crate) fn compute_regions<'cx, 'tcx>( borrow_set, ); + if cfg!(debug_assertions) { + let var_to_origin = infcx.reg_var_to_origin.borrow(); + debug!("var_to_origin: {:#?}", var_to_origin); + } + let mut regioncx = RegionInferenceContext::new( var_origins, universal_regions, @@ -322,7 +326,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>( } pub(super) fn dump_mir_results<'tcx>( - infcx: &InferCtxt<'tcx>, + infcx: &BorrowckInferCtxt<'_, 'tcx>, body: &Body<'tcx>, regioncx: &RegionInferenceContext<'tcx>, closure_region_requirements: &Option>, @@ -372,7 +376,7 @@ pub(super) fn dump_mir_results<'tcx>( #[allow(rustc::diagnostic_outside_of_impl)] #[allow(rustc::untranslatable_diagnostic)] pub(super) fn dump_annotation<'tcx>( - infcx: &InferCtxt<'tcx>, + infcx: &BorrowckInferCtxt<'_, 'tcx>, body: &Body<'tcx>, regioncx: &RegionInferenceContext<'tcx>, closure_region_requirements: &Option>, diff --git a/compiler/rustc_borrowck/src/renumber.rs b/compiler/rustc_borrowck/src/renumber.rs index 084754830bd..adf9869c34b 100644 --- a/compiler/rustc_borrowck/src/renumber.rs +++ b/compiler/rustc_borrowck/src/renumber.rs @@ -1,18 +1,23 @@ +<<<<<<< HEAD #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)] +======= +use crate::BorrowckInferCtxt; +>>>>>>> 2464f768a17 (collect region contexts during mir renumbering) use rustc_index::vec::IndexVec; -use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin}; +use rustc_infer::infer::NllRegionVariableOrigin; use rustc_middle::mir::visit::{MutVisitor, TyContext}; use rustc_middle::mir::Constant; use rustc_middle::mir::{Body, Location, Promoted}; use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable}; +use rustc_span::Symbol; /// Replaces all free regions appearing in the MIR with fresh /// inference variables, returning the number of variables created. #[instrument(skip(infcx, body, promoted), level = "debug")] pub fn renumber_mir<'tcx>( - infcx: &InferCtxt<'tcx>, + infcx: &BorrowckInferCtxt<'_, 'tcx>, body: &mut Body<'tcx>, promoted: &mut IndexVec>, ) { @@ -29,8 +34,9 @@ pub fn renumber_mir<'tcx>( /// Replaces all regions appearing in `value` with fresh inference /// variables. +#[cfg(not(debug_assertions))] #[instrument(skip(infcx), level = "debug")] -pub fn renumber_regions<'tcx, T>(infcx: &InferCtxt<'tcx>, value: T) -> T +pub(crate) fn renumber_regions<'tcx, T>(infcx: &BorrowckInferCtxt<'_, 'tcx>, value: T) -> T where T: TypeFoldable<'tcx>, { @@ -40,11 +46,73 @@ where }) } +/// Replaces all regions appearing in `value` with fresh inference +/// variables. +#[cfg(debug_assertions)] +#[instrument(skip(infcx), level = "debug")] +pub(crate) fn renumber_regions<'tcx, T>( + infcx: &BorrowckInferCtxt<'_, 'tcx>, + value: T, + ctxt: RegionCtxt, +) -> T +where + T: TypeFoldable<'tcx>, +{ + infcx.tcx.fold_regions(value, |_region, _depth| { + let origin = NllRegionVariableOrigin::Existential { from_forall: false }; + infcx.next_nll_region_var(origin, ctxt) + }) +} + +#[cfg(debug_assertions)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] +pub(crate) enum RegionCtxt { + Location(Location), + TyContext(TyContext), + Free(Symbol), + Bound(Symbol), + LateBound(Symbol), + Existential(Option), + Placeholder(Symbol), + Unknown, +} + +#[cfg(debug_assertions)] +impl RegionCtxt { + /// Used to determine the representative of a component in the strongly connected + /// constraint graph + /// FIXME: don't use underscore here. Got a 'not used' error for some reason + pub(crate) fn _preference_value(self) -> usize { + let _anon = Symbol::intern("anon"); + + match self { + RegionCtxt::Unknown => 1, + RegionCtxt::Existential(None) => 2, + RegionCtxt::Existential(Some(_anon)) + | RegionCtxt::Free(_anon) + | RegionCtxt::Bound(_anon) + | RegionCtxt::LateBound(_anon) => 2, + RegionCtxt::Location(_) => 3, + RegionCtxt::TyContext(_) => 4, + _ => 5, + } + } +} + struct NllVisitor<'a, 'tcx> { - infcx: &'a InferCtxt<'tcx>, + infcx: &'a BorrowckInferCtxt<'a, 'tcx>, } impl<'a, 'tcx> NllVisitor<'a, 'tcx> { + #[cfg(debug_assertions)] + fn renumber_regions(&mut self, value: T, ctxt: RegionCtxt) -> T + where + T: TypeFoldable<'tcx>, + { + renumber_regions(self.infcx, value, ctxt) + } + + #[cfg(not(debug_assertions))] fn renumber_regions(&mut self, value: T) -> T where T: TypeFoldable<'tcx>, @@ -58,13 +126,23 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { self.infcx.tcx } + #[cfg(not(debug_assertions))] #[instrument(skip(self), level = "debug")] - fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) { + fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _ty_context: TyContext) { *ty = self.renumber_regions(*ty); debug!(?ty); } + #[cfg(debug_assertions)] + #[instrument(skip(self), level = "debug")] + fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _ty_context: TyContext) { + *ty = self.renumber_regions(*ty, RegionCtxt::TyContext(_ty_context)); + + debug!(?ty); + } + + #[cfg(not(debug_assertions))] #[instrument(skip(self), level = "debug")] fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) { *substs = self.renumber_regions(*substs); @@ -72,6 +150,15 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { debug!(?substs); } + #[cfg(debug_assertions)] + #[instrument(skip(self), level = "debug")] + fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) { + *substs = self.renumber_regions(*substs, RegionCtxt::Location(location)); + + debug!(?substs); + } + + #[cfg(not(debug_assertions))] #[instrument(skip(self), level = "debug")] fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) { let old_region = *region; @@ -80,10 +167,28 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { debug!(?region); } + #[cfg(debug_assertions)] + #[instrument(skip(self), level = "debug")] + fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) { + let old_region = *region; + *region = self.renumber_regions(old_region, RegionCtxt::Location(location)); + + debug!(?region); + } + + #[cfg(not(debug_assertions))] #[instrument(skip(self), level = "debug")] fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) { let literal = constant.literal; constant.literal = self.renumber_regions(literal); debug!("constant: {:#?}", constant); } + + #[cfg(debug_assertions)] + #[instrument(skip(self), level = "debug")] + fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) { + let literal = constant.literal; + constant.literal = self.renumber_regions(literal, RegionCtxt::Location(_location)); + debug!("constant: {:#?}", constant); + } } diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 004b945eada..8eb42ade65b 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -1335,11 +1335,35 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { } }; let (sig, map) = tcx.replace_late_bound_regions(sig, |br| { - self.infcx.next_region_var(LateBoundRegion( - term.source_info.span, - br.kind, - LateBoundRegionConversionTime::FnCall, - )) + #[cfg(not(debug_assertions))] + { + self.infcx.next_region_var(LateBoundRegion( + term.source_info.span, + br.kind, + LateBoundRegionConversionTime::FnCall, + )) + } + + #[cfg(debug_assertions)] + { + use crate::renumber::RegionCtxt; + use rustc_span::Symbol; + + let name = match br.kind { + ty::BoundRegionKind::BrAnon(_) => Symbol::intern("anon"), + ty::BoundRegionKind::BrNamed(_, name) => name, + ty::BoundRegionKind::BrEnv => Symbol::intern("env"), + }; + + self.infcx.next_region_var( + LateBoundRegion( + term.source_info.span, + br.kind, + LateBoundRegionConversionTime::FnCall, + ), + RegionCtxt::LateBound(name), + ) + } }); debug!(?sig); // IMPORTANT: We have to prove well formed for the function signature before diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index efa5a29c5dd..919c772a203 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -20,15 +20,18 @@ use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::lang_items::LangItem; use rustc_hir::{BodyOwnerKind, HirId}; use rustc_index::vec::{Idx, IndexVec}; -use rustc_infer::infer::{InferCtxt, NllRegionVariableOrigin}; +use rustc_infer::infer::NllRegionVariableOrigin; use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::{ self, DefIdTree, InlineConstSubsts, InlineConstSubstsParts, RegionVid, Ty, TyCtxt, }; use rustc_middle::ty::{InternalSubsts, SubstsRef}; +use rustc_span::Symbol; use std::iter; use crate::nll::ToRegionVid; +use crate::renumber::RegionCtxt; +use crate::BorrowckInferCtxt; #[derive(Debug)] pub struct UniversalRegions<'tcx> { @@ -224,7 +227,7 @@ impl<'tcx> UniversalRegions<'tcx> { /// signature. This will also compute the relationships that are /// known between those regions. pub fn new( - infcx: &InferCtxt<'tcx>, + infcx: &BorrowckInferCtxt<'_, 'tcx>, mir_def: ty::WithOptConstParam, param_env: ty::ParamEnv<'tcx>, ) -> Self { @@ -385,7 +388,7 @@ impl<'tcx> UniversalRegions<'tcx> { } struct UniversalRegionsBuilder<'cx, 'tcx> { - infcx: &'cx InferCtxt<'tcx>, + infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>, mir_def: ty::WithOptConstParam, mir_hir_id: HirId, param_env: ty::ParamEnv<'tcx>, @@ -403,7 +406,13 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { assert_eq!(FIRST_GLOBAL_INDEX, self.infcx.num_region_vars()); // Create the "global" region that is always free in all contexts: 'static. + #[cfg(not(debug_assertions))] let fr_static = self.infcx.next_nll_region_var(FR).to_region_vid(); + #[cfg(debug_assertions)] + let fr_static = self + .infcx + .next_nll_region_var(FR, RegionCtxt::Free(Symbol::intern("static"))) + .to_region_vid(); // We've now added all the global regions. The next ones we // add will be external. @@ -480,6 +489,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { LangItem::VaList, Some(self.infcx.tcx.def_span(self.mir_def.did)), ); + let reg_vid = self.infcx.next_nll_region_var(FR, RegionCtxt::Free(Symbol::intern("c-variadic")).to_region_vid(); let region = self.infcx.tcx.mk_re_var(self.infcx.next_nll_region_var(FR).to_region_vid()); let va_list_ty = @@ -491,7 +501,14 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { } } + #[cfg(not(debug_assertions))] let fr_fn_body = self.infcx.next_nll_region_var(FR).to_region_vid(); + #[cfg(debug_assertions)] + let fr_fn_body = self + .infcx + .next_nll_region_var(FR, RegionCtxt::Free(Symbol::intern("fn_body"))) + .to_region_vid(); + let num_universals = self.infcx.num_region_vars(); debug!("build: global regions = {}..{}", FIRST_GLOBAL_INDEX, first_extern_index); @@ -718,7 +735,8 @@ trait InferCtxtExt<'tcx> { ); } -impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { +impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { + #[cfg(not(debug_assertions))] fn replace_free_regions_with_nll_infer_vars( &self, origin: NllRegionVariableOrigin, @@ -727,9 +745,33 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { where T: TypeFoldable<'tcx>, { - self.tcx.fold_regions(value, |_region, _depth| self.next_nll_region_var(origin)) + self.tcx.fold_regions(value, |_region, _depth| self.infcx.next_nll_region_var(origin)) } + #[cfg(debug_assertions)] + #[instrument(skip(self), level = "debug")] + fn replace_free_regions_with_nll_infer_vars( + &self, + origin: NllRegionVariableOrigin, + value: T, + ) -> T + where + T: TypeFoldable<'tcx>, + { + self.infcx.tcx.fold_regions(value, |region, _depth| { + let name = match region.get_name() { + Some(name) => name, + _ => Symbol::intern("anon"), + }; + debug!(?region, ?name); + + let reg_var = self.next_nll_region_var(origin, RegionCtxt::Free(name)); + + reg_var + }) + } + + #[cfg(not(debug_assertions))] #[instrument(level = "debug", skip(self, indices))] fn replace_bound_regions_with_nll_infer_vars( &self, @@ -752,6 +794,38 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { value } + #[cfg(debug_assertions)] + #[instrument(level = "debug", skip(self, indices))] + fn replace_bound_regions_with_nll_infer_vars( + &self, + origin: NllRegionVariableOrigin, + all_outlive_scope: LocalDefId, + value: ty::Binder<'tcx, T>, + indices: &mut UniversalRegionIndices<'tcx>, + ) -> T + where + T: TypeFoldable<'tcx>, + { + let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| { + debug!(?br); + let liberated_region = self.tcx.mk_region(ty::ReFree(ty::FreeRegion { + scope: all_outlive_scope.to_def_id(), + bound_region: br.kind, + })); + + let name = match br.kind.get_name() { + Some(name) => name, + _ => Symbol::intern("anon"), + }; + + let region_vid = self.next_nll_region_var(origin, RegionCtxt::Bound(name)); + indices.insert_late_bound_region(liberated_region, region_vid.to_region_vid()); + debug!(?liberated_region, ?region_vid); + region_vid + }); + value + } + /// Finds late-bound regions that do not appear in the parameter listing and adds them to the /// indices vector. Typically, we identify late-bound regions as we process the inputs and /// outputs of the closure/function. However, sometimes there are late-bound regions which do @@ -761,6 +835,7 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { /// entries for them and store them in the indices map. This code iterates over the complete /// set of late-bound regions and checks for any that we have not yet seen, adding them to the /// inputs vector. + #[cfg(not(debug_assertions))] #[instrument(skip(self, indices))] fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope( &self, @@ -792,6 +867,38 @@ impl<'tcx> InferCtxtExt<'tcx> for InferCtxt<'tcx> { } }); } + + /// Finds late-bound regions that do not appear in the parameter listing and adds them to the + /// indices vector. Typically, we identify late-bound regions as we process the inputs and + /// outputs of the closure/function. However, sometimes there are late-bound regions which do + /// not appear in the fn parameters but which are nonetheless in scope. The simplest case of + /// this are unused functions, like fn foo<'a>() { } (see e.g., #51351). Despite not being used, + /// users can still reference these regions (e.g., let x: &'a u32 = &22;), so we need to create + /// entries for them and store them in the indices map. This code iterates over the complete + /// set of late-bound regions and checks for any that we have not yet seen, adding them to the + /// inputs vector. + #[cfg(debug_assertions)] + #[instrument(skip(self, indices))] + fn replace_late_bound_regions_with_nll_infer_vars( + &self, + mir_def_id: LocalDefId, + indices: &mut UniversalRegionIndices<'tcx>, + ) { + let typeck_root_def_id = self.tcx.typeck_root_def_id(mir_def_id.to_def_id()); + for_each_late_bound_region_defined_on(self.tcx, typeck_root_def_id, |r| { + debug!(?r); + if !indices.indices.contains_key(&r) { + let name = match r.get_name() { + Some(name) => name, + _ => Symbol::intern("anon"), + }; + + let region_vid = self.next_nll_region_var(FR, RegionCtxt::LateBound(name)); + debug!(?region_vid); + indices.insert_late_bound_region(r, region_vid.to_region_vid()); + } + }); + } } impl<'tcx> UniversalRegionIndices<'tcx> { diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index aa316b2dadb..64384fadc63 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -1111,11 +1111,13 @@ impl<'tcx> InferCtxt<'tcx> { } /// Just a convenient wrapper of `next_region_var` for using during NLL. + #[instrument(skip(self), level = "debug")] pub fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> { self.next_region_var(RegionVariableOrigin::Nll(origin)) } /// Just a convenient wrapper of `next_region_var` for using during NLL. + #[instrument(skip(self), level = "debug")] pub fn next_nll_region_var_in_universe( &self, origin: NllRegionVariableOrigin, diff --git a/compiler/rustc_middle/src/mir/visit.rs b/compiler/rustc_middle/src/mir/visit.rs index 443c1b2d261..7f0935fb149 100644 --- a/compiler/rustc_middle/src/mir/visit.rs +++ b/compiler/rustc_middle/src/mir/visit.rs @@ -1214,7 +1214,7 @@ impl<'tcx> MirVisitable<'tcx> for Option> { /// Extra information passed to `visit_ty` and friends to give context /// about where the type etc appears. -#[derive(Debug)] +#[derive(Copy, Clone, Debug, Hash, Eq, PartialEq)] pub enum TyContext { LocalDecl { /// The index of the local variable we are visiting. diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index f8d17433cf7..c5d6df5b1ad 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1751,6 +1751,13 @@ impl<'tcx> Region<'tcx> { pub fn is_var(self) -> bool { matches!(self.kind(), ty::ReVar(_)) } + + pub fn try_get_var(self) -> Option { + match self.kind() { + ty::ReVar(vid) => Some(vid), + _ => None, + } + } } /// Type utilities From 960ebaf899cfceddf7edaf936f460491dcbf4733 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 9 Nov 2022 12:53:04 +0100 Subject: [PATCH 03/10] collect existentials and placeholders --- compiler/rustc_borrowck/src/renumber.rs | 3 - compiler/rustc_borrowck/src/type_check/mod.rs | 8 +-- .../src/type_check/relate_tys.rs | 61 ++++++++++++++++--- .../src/infer/canonical/query_response.rs | 14 ++++- .../rustc_infer/src/infer/nll_relate/mod.rs | 10 ++- 5 files changed, 76 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_borrowck/src/renumber.rs b/compiler/rustc_borrowck/src/renumber.rs index adf9869c34b..eeba0e3c2bc 100644 --- a/compiler/rustc_borrowck/src/renumber.rs +++ b/compiler/rustc_borrowck/src/renumber.rs @@ -1,9 +1,6 @@ -<<<<<<< HEAD #![deny(rustc::untranslatable_diagnostic)] #![deny(rustc::diagnostic_outside_of_impl)] -======= use crate::BorrowckInferCtxt; ->>>>>>> 2464f768a17 (collect region contexts during mir renumbering) use rustc_index::vec::IndexVec; use rustc_infer::infer::NllRegionVariableOrigin; use rustc_middle::mir::visit::{MutVisitor, TyContext}; diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 8eb42ade65b..1232c524fe4 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -64,7 +64,7 @@ use crate::{ region_infer::TypeTest, type_check::free_region_relations::{CreateResult, UniversalRegionRelations}, universal_regions::{DefiningTy, UniversalRegions}, - Upvar, + BorrowckInferCtxt, Upvar, }; macro_rules! span_mirbug { @@ -123,7 +123,7 @@ mod relate_tys; /// - `move_data` -- move-data constructed when performing the maybe-init dataflow analysis /// - `elements` -- MIR region map pub(crate) fn type_check<'mir, 'tcx>( - infcx: &InferCtxt<'tcx>, + infcx: &BorrowckInferCtxt<'_, 'tcx>, param_env: ty::ParamEnv<'tcx>, body: &Body<'tcx>, promoted: &IndexVec>, @@ -845,7 +845,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> { /// way, it accrues region constraints -- these can later be used by /// NLL region checking. struct TypeChecker<'a, 'tcx> { - infcx: &'a InferCtxt<'tcx>, + infcx: &'a BorrowckInferCtxt<'a, 'tcx>, param_env: ty::ParamEnv<'tcx>, last_span: Span, body: &'a Body<'tcx>, @@ -998,7 +998,7 @@ impl Locations { impl<'a, 'tcx> TypeChecker<'a, 'tcx> { fn new( - infcx: &'a InferCtxt<'tcx>, + infcx: &'a BorrowckInferCtxt<'a, 'tcx>, body: &'a Body<'tcx>, param_env: ty::ParamEnv<'tcx>, region_bound_pairs: &'a RegionBoundPairs<'tcx>, diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index 8dd06187877..91b26fc4e53 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -4,11 +4,12 @@ use rustc_infer::traits::PredicateObligations; use rustc_middle::mir::ConstraintCategory; use rustc_middle::ty::relate::TypeRelation; use rustc_middle::ty::{self, Ty}; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; use rustc_trait_selection::traits::query::Fallible; use crate::constraints::OutlivesConstraint; use crate::diagnostics::UniverseInfo; +use crate::renumber::RegionCtxt; use crate::type_check::{InstantiateOpaqueType, Locations, TypeChecker}; impl<'a, 'tcx> TypeChecker<'a, 'tcx> { @@ -100,23 +101,69 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> universe } - fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> { + #[instrument(skip(self), level = "debug")] + fn next_existential_region_var( + &mut self, + from_forall: bool, + _name: Option, + ) -> ty::Region<'tcx> { let origin = NllRegionVariableOrigin::Existential { from_forall }; - self.type_checker.infcx.next_nll_region_var(origin) + + #[cfg(not(debug_assertions))] + let reg_var = self.type_checker.infcx.next_nll_region_var(origin); + + #[cfg(debug_assertions)] + let reg_var = + self.type_checker.infcx.next_nll_region_var(origin, RegionCtxt::Existential(_name)); + + reg_var } + #[instrument(skip(self), level = "debug")] fn next_placeholder_region(&mut self, placeholder: ty::PlaceholderRegion) -> ty::Region<'tcx> { - self.type_checker + let reg = self + .type_checker .borrowck_context .constraints - .placeholder_region(self.type_checker.infcx, placeholder) + .placeholder_region(self.type_checker.infcx, placeholder); + + #[cfg(debug_assertions)] + { + let name = match placeholder.name { + ty::BoundRegionKind::BrAnon(_) => Symbol::intern("anon"), + ty::BoundRegionKind::BrNamed(_, name) => name, + ty::BoundRegionKind::BrEnv => Symbol::intern("env"), + }; + + let reg_var = reg + .try_get_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(name)); + assert!(matches!(prev, None)); + } + + reg } + #[instrument(skip(self), level = "debug")] fn generalize_existential(&mut self, universe: ty::UniverseIndex) -> ty::Region<'tcx> { - self.type_checker.infcx.next_nll_region_var_in_universe( + let reg = self.type_checker.infcx.next_nll_region_var_in_universe( NllRegionVariableOrigin::Existential { from_forall: false }, universe, - ) + ); + + #[cfg(debug_assertions)] + { + let reg_var = reg + .try_get_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::Existential(None)); + assert!(matches!(prev, None)); + } + + reg } fn push_outlives( diff --git a/compiler/rustc_infer/src/infer/canonical/query_response.rs b/compiler/rustc_infer/src/infer/canonical/query_response.rs index b9cb9732ca3..1ca9e87e056 100644 --- a/compiler/rustc_infer/src/infer/canonical/query_response.rs +++ b/compiler/rustc_infer/src/infer/canonical/query_response.rs @@ -27,7 +27,7 @@ use rustc_middle::ty::fold::TypeFoldable; use rustc_middle::ty::relate::TypeRelation; use rustc_middle::ty::subst::{GenericArg, GenericArgKind}; use rustc_middle::ty::{self, BoundVar, ToPredicate, Ty, TyCtxt}; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; use std::fmt::Debug; use std::iter; @@ -318,7 +318,11 @@ impl<'tcx> InferCtxt<'tcx> { // Screen out `'a: 'a` cases. let ty::OutlivesPredicate(k1, r2) = r_c.0; - if k1 != r2.into() { Some(r_c) } else { None } + if k1 != r2.into() { + Some(r_c) + } else { + None + } }), ); @@ -683,7 +687,11 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for QueryTypeRelatingDelegate<'_, 'tcx> { self.infcx.create_next_universe() } - fn next_existential_region_var(&mut self, from_forall: bool) -> ty::Region<'tcx> { + fn next_existential_region_var( + &mut self, + from_forall: bool, + _name: Option, + ) -> ty::Region<'tcx> { let origin = NllRegionVariableOrigin::Existential { from_forall }; self.infcx.next_nll_region_var(origin) } diff --git a/compiler/rustc_infer/src/infer/nll_relate/mod.rs b/compiler/rustc_infer/src/infer/nll_relate/mod.rs index 644774c93c2..954334e88b0 100644 --- a/compiler/rustc_infer/src/infer/nll_relate/mod.rs +++ b/compiler/rustc_infer/src/infer/nll_relate/mod.rs @@ -31,7 +31,7 @@ use rustc_middle::ty::error::TypeError; use rustc_middle::ty::relate::{self, Relate, RelateResult, TypeRelation}; use rustc_middle::ty::visit::{ir::TypeVisitor, TypeSuperVisitable, TypeVisitable}; use rustc_middle::ty::{self, InferConst, Ty, TyCtxt}; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; use std::fmt::Debug; use std::ops::ControlFlow; @@ -100,7 +100,11 @@ pub trait TypeRelatingDelegate<'tcx> { /// we will invoke this method to instantiate `'a` with an /// inference variable (though `'b` would be instantiated first, /// as a placeholder). - fn next_existential_region_var(&mut self, was_placeholder: bool) -> ty::Region<'tcx>; + fn next_existential_region_var( + &mut self, + was_placeholder: bool, + name: Option, + ) -> ty::Region<'tcx>; /// Creates a new region variable representing a /// higher-ranked region that is instantiated universally. @@ -188,7 +192,7 @@ where let placeholder = ty::PlaceholderRegion { universe, name: br.kind }; delegate.next_placeholder_region(placeholder) } else { - delegate.next_existential_region_var(true) + delegate.next_existential_region_var(true, br.kind.get_name()) } } }; From e2bf960fe1c011e75cf0d6a06d3f955444d11043 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 9 Nov 2022 21:21:36 +0100 Subject: [PATCH 04/10] sccs info --- .../rustc_borrowck/src/constraints/mod.rs | 2 +- compiler/rustc_borrowck/src/nll.rs | 1 + .../rustc_borrowck/src/region_infer/mod.rs | 67 ++++++++++++++++++- .../rustc_borrowck/src/region_infer/values.rs | 2 +- .../src/graph/scc/mod.rs | 10 +-- 5 files changed, 74 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_borrowck/src/constraints/mod.rs b/compiler/rustc_borrowck/src/constraints/mod.rs index 1f0b8adeaf1..f370c02161b 100644 --- a/compiler/rustc_borrowck/src/constraints/mod.rs +++ b/compiler/rustc_borrowck/src/constraints/mod.rs @@ -17,7 +17,7 @@ pub(crate) mod graph; /// constraints of the form `R1: R2`. Each constraint is identified by /// a unique `OutlivesConstraintIndex` and you can index into the set /// (`constraint_set[i]`) to access the constraint details. -#[derive(Clone, Default)] +#[derive(Clone, Debug, Default)] pub(crate) struct OutlivesConstraintSet<'tcx> { outlives: IndexVec>, } diff --git a/compiler/rustc_borrowck/src/nll.rs b/compiler/rustc_borrowck/src/nll.rs index c3301ac1442..28d49c19c75 100644 --- a/compiler/rustc_borrowck/src/nll.rs +++ b/compiler/rustc_borrowck/src/nll.rs @@ -263,6 +263,7 @@ pub(crate) fn compute_regions<'cx, 'tcx>( } let mut regioncx = RegionInferenceContext::new( + infcx, var_origins, universal_regions, placeholder_indices, diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 83fdb6066c6..66d2850c67b 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -34,6 +34,7 @@ use crate::{ }, type_check::{free_region_relations::UniversalRegionRelations, Locations}, universal_regions::UniversalRegions, + BorrowckInferCtxt, }; mod dump_mir; @@ -243,6 +244,59 @@ pub enum ExtraConstraintInfo { PlaceholderFromPredicate(Span), } +#[cfg(debug_assertions)] +#[instrument(skip(infcx, sccs), level = "debug")] +fn sccs_info<'cx, 'tcx>( + infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>, + sccs: Rc>, +) { + use crate::renumber::RegionCtxt; + + let var_to_origin = infcx.reg_var_to_origin.borrow(); + let num_components = sccs.scc_data.ranges.len(); + let mut components = vec![FxHashSet::default(); num_components]; + + for (reg_var_idx, scc_idx) in sccs.scc_indices.iter().enumerate() { + let reg_var = ty::RegionVid::from_usize(reg_var_idx); + let origin = var_to_origin.get(®_var).unwrap_or_else(|| &RegionCtxt::Unknown); + components[scc_idx.as_usize()].insert(*origin); + } + + debug!( + "strongly connected components: {:#?}", + components + .iter() + .enumerate() + .map(|(idx, origin)| { (ConstraintSccIndex::from_usize(idx), origin) }) + .collect::>() + ); + + // Now let's calculate the best representative for each component + let components_representatives = components + .into_iter() + .enumerate() + .map(|(scc_idx, region_ctxts)| { + let repr = region_ctxts + .into_iter() + .max_by(|x, y| x._preference_value().cmp(&y._preference_value())) + .unwrap(); + + (ConstraintSccIndex::from_usize(scc_idx), repr) + }) + .collect::>(); + + let mut scc_node_to_edges = FxHashMap::default(); + for (scc_idx, repr) in components_representatives.iter() { + let edges_range = sccs.scc_data.ranges[*scc_idx].clone(); + let edges = &sccs.scc_data.all_successors[edges_range]; + let edge_representatives = + edges.iter().map(|scc_idx| components_representatives[scc_idx]).collect::>(); + scc_node_to_edges.insert((scc_idx, repr), edge_representatives); + } + + debug!("SCC edges {:#?}", scc_node_to_edges); +} + impl<'tcx> RegionInferenceContext<'tcx> { /// Creates a new region inference context with a total of /// `num_region_variables` valid inference variables; the first N @@ -251,7 +305,8 @@ impl<'tcx> RegionInferenceContext<'tcx> { /// /// The `outlives_constraints` and `type_tests` are an initial set /// of constraints produced by the MIR type check. - pub(crate) fn new( + pub(crate) fn new<'cx>( + _infcx: &BorrowckInferCtxt<'cx, 'tcx>, var_infos: VarInfos, universal_regions: Rc>, placeholder_indices: Rc, @@ -263,6 +318,11 @@ impl<'tcx> RegionInferenceContext<'tcx> { liveness_constraints: LivenessValues, elements: &Rc, ) -> Self { + debug!("universal_regions: {:#?}", universal_regions); + debug!("outlives constraints: {:#?}", outlives_constraints); + debug!("placeholder_indices: {:#?}", placeholder_indices); + debug!("type tests: {:#?}", type_tests); + // Create a RegionDefinition for each inference variable. let definitions: IndexVec<_, _> = var_infos .iter() @@ -274,6 +334,11 @@ impl<'tcx> RegionInferenceContext<'tcx> { let fr_static = universal_regions.fr_static; let constraint_sccs = Rc::new(constraints.compute_sccs(&constraint_graph, fr_static)); + #[cfg(debug_assertions)] + { + sccs_info(_infcx, constraint_sccs.clone()); + } + let mut scc_values = RegionValues::new(elements, universal_regions.len(), &placeholder_indices); diff --git a/compiler/rustc_borrowck/src/region_infer/values.rs b/compiler/rustc_borrowck/src/region_infer/values.rs index 6a3748fded5..c361357ca21 100644 --- a/compiler/rustc_borrowck/src/region_infer/values.rs +++ b/compiler/rustc_borrowck/src/region_infer/values.rs @@ -181,7 +181,7 @@ impl LivenessValues { /// Maps from `ty::PlaceholderRegion` values that are used in the rest of /// rustc to the internal `PlaceholderIndex` values that are used in /// NLL. -#[derive(Default)] +#[derive(Debug, Default)] pub(crate) struct PlaceholderIndices { indices: FxIndexSet, } diff --git a/compiler/rustc_data_structures/src/graph/scc/mod.rs b/compiler/rustc_data_structures/src/graph/scc/mod.rs index c8e66eb672c..e2fe5285aad 100644 --- a/compiler/rustc_data_structures/src/graph/scc/mod.rs +++ b/compiler/rustc_data_structures/src/graph/scc/mod.rs @@ -21,21 +21,21 @@ mod tests; pub struct Sccs { /// For each node, what is the SCC index of the SCC to which it /// belongs. - scc_indices: IndexVec, + pub scc_indices: IndexVec, /// Data about each SCC. - scc_data: SccData, + pub scc_data: SccData, } -struct SccData { +pub struct SccData { /// For each SCC, the range of `all_successors` where its /// successors can be found. - ranges: IndexVec>, + pub ranges: IndexVec>, /// Contains the successors for all the Sccs, concatenated. The /// range of indices corresponding to a given SCC is found in its /// SccData. - all_successors: Vec, + pub all_successors: Vec, } impl Sccs { From 46bd77aa808ad270a4bda28104b5f9a1b42ea159 Mon Sep 17 00:00:00 2001 From: b-naber Date: Wed, 9 Nov 2022 21:58:04 +0100 Subject: [PATCH 05/10] some conditional imports --- compiler/rustc_borrowck/src/lib.rs | 1 + compiler/rustc_borrowck/src/nll.rs | 5 ----- compiler/rustc_borrowck/src/renumber.rs | 1 + compiler/rustc_borrowck/src/type_check/relate_tys.rs | 1 + compiler/rustc_borrowck/src/universal_regions.rs | 2 ++ 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 70f6a323e88..4a5abb05149 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -97,6 +97,7 @@ use nll::{PoloniusOutput, ToRegionVid}; use place_ext::PlaceExt; use places_conflict::{places_conflict, PlaceConflictBias}; use region_infer::RegionInferenceContext; +#[cfg(debug_assertions)] use renumber::RegionCtxt; // FIXME(eddyb) perhaps move this somewhere more centrally. diff --git a/compiler/rustc_borrowck/src/nll.rs b/compiler/rustc_borrowck/src/nll.rs index 28d49c19c75..e5dbb83dd07 100644 --- a/compiler/rustc_borrowck/src/nll.rs +++ b/compiler/rustc_borrowck/src/nll.rs @@ -257,11 +257,6 @@ pub(crate) fn compute_regions<'cx, 'tcx>( borrow_set, ); - if cfg!(debug_assertions) { - let var_to_origin = infcx.reg_var_to_origin.borrow(); - debug!("var_to_origin: {:#?}", var_to_origin); - } - let mut regioncx = RegionInferenceContext::new( infcx, var_origins, diff --git a/compiler/rustc_borrowck/src/renumber.rs b/compiler/rustc_borrowck/src/renumber.rs index eeba0e3c2bc..cf0b944a4c5 100644 --- a/compiler/rustc_borrowck/src/renumber.rs +++ b/compiler/rustc_borrowck/src/renumber.rs @@ -8,6 +8,7 @@ use rustc_middle::mir::Constant; use rustc_middle::mir::{Body, Location, Promoted}; use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable}; +#[cfg(debug_assertions)] use rustc_span::Symbol; /// Replaces all free regions appearing in the MIR with fresh diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index 91b26fc4e53..bb05349b2e9 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -9,6 +9,7 @@ use rustc_trait_selection::traits::query::Fallible; use crate::constraints::OutlivesConstraint; use crate::diagnostics::UniverseInfo; +#[cfg(debug_assertions)] use crate::renumber::RegionCtxt; use crate::type_check::{InstantiateOpaqueType, Locations, TypeChecker}; diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index 919c772a203..e22b9de6fc1 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -26,10 +26,12 @@ use rustc_middle::ty::{ self, DefIdTree, InlineConstSubsts, InlineConstSubstsParts, RegionVid, Ty, TyCtxt, }; use rustc_middle::ty::{InternalSubsts, SubstsRef}; +#[cfg(debug_assertions)] use rustc_span::Symbol; use std::iter; use crate::nll::ToRegionVid; +#[cfg(debug_assertions)] use crate::renumber::RegionCtxt; use crate::BorrowckInferCtxt; From 2d2bccf7515710b38edb45fe359e84d4a617a402 Mon Sep 17 00:00:00 2001 From: b-naber Date: Thu, 10 Nov 2022 13:59:01 +0100 Subject: [PATCH 06/10] rebase --- compiler/rustc_borrowck/src/renumber.rs | 20 +-- compiler/rustc_borrowck/src/type_check/mod.rs | 20 ++- .../src/type_check/relate_tys.rs | 14 ++- .../rustc_borrowck/src/universal_regions.rs | 119 +++++++++--------- 4 files changed, 92 insertions(+), 81 deletions(-) diff --git a/compiler/rustc_borrowck/src/renumber.rs b/compiler/rustc_borrowck/src/renumber.rs index cf0b944a4c5..c0cb74098fb 100644 --- a/compiler/rustc_borrowck/src/renumber.rs +++ b/compiler/rustc_borrowck/src/renumber.rs @@ -9,7 +9,7 @@ use rustc_middle::mir::{Body, Location, Promoted}; use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable}; #[cfg(debug_assertions)] -use rustc_span::Symbol; +use rustc_span::{Span, Symbol}; /// Replaces all free regions appearing in the MIR with fresh /// inference variables, returning the number of variables created. @@ -62,16 +62,23 @@ where }) } +#[cfg(debug_assertions)] +#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] +pub(crate) enum BoundRegionInfo { + Name(Symbol), + Span(Span), +} + #[cfg(debug_assertions)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub(crate) enum RegionCtxt { Location(Location), TyContext(TyContext), Free(Symbol), - Bound(Symbol), - LateBound(Symbol), + Bound(BoundRegionInfo), + LateBound(BoundRegionInfo), Existential(Option), - Placeholder(Symbol), + Placeholder(BoundRegionInfo), Unknown, } @@ -86,10 +93,7 @@ impl RegionCtxt { match self { RegionCtxt::Unknown => 1, RegionCtxt::Existential(None) => 2, - RegionCtxt::Existential(Some(_anon)) - | RegionCtxt::Free(_anon) - | RegionCtxt::Bound(_anon) - | RegionCtxt::LateBound(_anon) => 2, + RegionCtxt::Existential(Some(_anon)) | RegionCtxt::Free(_anon) => 2, RegionCtxt::Location(_) => 3, RegionCtxt::TyContext(_) => 4, _ => 5, diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 1232c524fe4..5d3828b14c9 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -1346,13 +1346,21 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { #[cfg(debug_assertions)] { - use crate::renumber::RegionCtxt; + use crate::renumber::{BoundRegionInfo, RegionCtxt}; use rustc_span::Symbol; - let name = match br.kind { - ty::BoundRegionKind::BrAnon(_) => Symbol::intern("anon"), - ty::BoundRegionKind::BrNamed(_, name) => name, - ty::BoundRegionKind::BrEnv => Symbol::intern("env"), + let reg_info = match br.kind { + // FIXME Probably better to use the `Span` here + ty::BoundRegionKind::BrAnon(_, Some(span)) => { + BoundRegionInfo::Span(span) + } + ty::BoundRegionKind::BrAnon(..) => { + BoundRegionInfo::Name(Symbol::intern("anon")) + } + ty::BoundRegionKind::BrNamed(_, name) => BoundRegionInfo::Name(name), + ty::BoundRegionKind::BrEnv => { + BoundRegionInfo::Name(Symbol::intern("env")) + } }; self.infcx.next_region_var( @@ -1361,7 +1369,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { br.kind, LateBoundRegionConversionTime::FnCall, ), - RegionCtxt::LateBound(name), + RegionCtxt::LateBound(reg_info), ) } }); diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index bb05349b2e9..51ddf02629c 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -10,7 +10,7 @@ use rustc_trait_selection::traits::query::Fallible; use crate::constraints::OutlivesConstraint; use crate::diagnostics::UniverseInfo; #[cfg(debug_assertions)] -use crate::renumber::RegionCtxt; +use crate::renumber::{BoundRegionInfo, RegionCtxt}; use crate::type_check::{InstantiateOpaqueType, Locations, TypeChecker}; impl<'a, 'tcx> TypeChecker<'a, 'tcx> { @@ -130,17 +130,19 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> #[cfg(debug_assertions)] { - let name = match placeholder.name { - ty::BoundRegionKind::BrAnon(_) => Symbol::intern("anon"), - ty::BoundRegionKind::BrNamed(_, name) => name, - ty::BoundRegionKind::BrEnv => Symbol::intern("env"), + let reg_info = match placeholder.name { + // FIXME Probably better to use the `Span` here + ty::BoundRegionKind::BrAnon(_, Some(span)) => BoundRegionInfo::Span(span), + ty::BoundRegionKind::BrAnon(..) => BoundRegionInfo::Name(Symbol::intern("anon")), + ty::BoundRegionKind::BrNamed(_, name) => BoundRegionInfo::Name(name), + ty::BoundRegionKind::BrEnv => BoundRegionInfo::Name(Symbol::intern("env")), }; let reg_var = reg .try_get_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(name)); + let prev = var_to_origin.insert(reg_var, RegionCtxt::Placeholder(reg_info)); assert!(matches!(prev, None)); } diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index e22b9de6fc1..e6abdd9b271 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -32,7 +32,7 @@ use std::iter; use crate::nll::ToRegionVid; #[cfg(debug_assertions)] -use crate::renumber::RegionCtxt; +use crate::renumber::{BoundRegionInfo, RegionCtxt}; use crate::BorrowckInferCtxt; #[derive(Debug)] @@ -446,7 +446,22 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { |r| { debug!(?r); if !indices.indices.contains_key(&r) { + #[cfg(not(debug_assertions))] let region_vid = self.infcx.next_nll_region_var(FR); + + #[cfg(debug_assertions)] + let region_vid = { + let name = match r.get_name() { + Some(name) => name, + _ => Symbol::intern("anon"), + }; + + self.infcx.next_nll_region_var( + FR, + RegionCtxt::LateBound(BoundRegionInfo::Name(name)), + ) + }; + debug!(?region_vid); indices.insert_late_bound_region(r, region_vid.to_region_vid()); } @@ -474,7 +489,20 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { for_each_late_bound_region_in_item(self.infcx.tcx, self.mir_def.did, |r| { debug!(?r); if !indices.indices.contains_key(&r) { + #[cfg(not(debug_assertions))] let region_vid = self.infcx.next_nll_region_var(FR); + + #[cfg(debug_assertions)] + let region_vid = { + let name = match r.get_name() { + Some(name) => name, + _ => Symbol::intern("anon"), + }; + + self.infcx + .next_nll_region_var(FR, RegionCtxt::LateBound(BoundRegionInfo::Name(name))) + }; + debug!(?region_vid); indices.insert_late_bound_region(r, region_vid.to_region_vid()); } @@ -773,7 +801,6 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { }) } - #[cfg(not(debug_assertions))] #[instrument(level = "debug", skip(self, indices))] fn replace_bound_regions_with_nll_infer_vars( &self, @@ -788,39 +815,19 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| { debug!(?br); let liberated_region = self.tcx.mk_re_free(all_outlive_scope.to_def_id(), br.kind); + #[cfg(not(debug_assertions))] let region_vid = self.next_nll_region_var(origin); - indices.insert_late_bound_region(liberated_region, region_vid.to_region_vid()); - debug!(?liberated_region, ?region_vid); - region_vid - }); - value - } - #[cfg(debug_assertions)] - #[instrument(level = "debug", skip(self, indices))] - fn replace_bound_regions_with_nll_infer_vars( - &self, - origin: NllRegionVariableOrigin, - all_outlive_scope: LocalDefId, - value: ty::Binder<'tcx, T>, - indices: &mut UniversalRegionIndices<'tcx>, - ) -> T - where - T: TypeFoldable<'tcx>, - { - let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| { - debug!(?br); - let liberated_region = self.tcx.mk_region(ty::ReFree(ty::FreeRegion { - scope: all_outlive_scope.to_def_id(), - bound_region: br.kind, - })); + #[cfg(debug_assertions)] + let region_vid = { + let name = match br.kind.get_name() { + Some(name) => name, + _ => Symbol::intern("anon"), + }; - let name = match br.kind.get_name() { - Some(name) => name, - _ => Symbol::intern("anon"), + self.next_nll_region_var(origin, RegionCtxt::Bound(BoundRegionInfo::Name(name))) }; - let region_vid = self.next_nll_region_var(origin, RegionCtxt::Bound(name)); indices.insert_late_bound_region(liberated_region, region_vid.to_region_vid()); debug!(?liberated_region, ?region_vid); region_vid @@ -837,7 +844,6 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { /// entries for them and store them in the indices map. This code iterates over the complete /// set of late-bound regions and checks for any that we have not yet seen, adding them to the /// inputs vector. - #[cfg(not(debug_assertions))] #[instrument(skip(self, indices))] fn replace_late_bound_regions_with_nll_infer_vars_in_recursive_scope( &self, @@ -847,7 +853,19 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { for_each_late_bound_region_in_recursive_scope(self.tcx, mir_def_id, |r| { debug!(?r); if !indices.indices.contains_key(&r) { + #[cfg(not(debug_assertions))] let region_vid = self.next_nll_region_var(FR); + + #[cfg(debug_assertions)] + let region_vid = { + let name = match r.get_name() { + Some(name) => name, + _ => Symbol::intern("anon"), + }; + + self.next_nll_region_var(FR, RegionCtxt::LateBound(BoundRegionInfo::Name(name))) + }; + debug!(?region_vid); indices.insert_late_bound_region(r, region_vid.to_region_vid()); } @@ -863,40 +881,19 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { for_each_late_bound_region_in_item(self.tcx, mir_def_id, |r| { debug!(?r); if !indices.indices.contains_key(&r) { + #[cfg(not(debug_assertions))] let region_vid = self.next_nll_region_var(FR); - debug!(?region_vid); - indices.insert_late_bound_region(r, region_vid.to_region_vid()); - } - }); - } - /// Finds late-bound regions that do not appear in the parameter listing and adds them to the - /// indices vector. Typically, we identify late-bound regions as we process the inputs and - /// outputs of the closure/function. However, sometimes there are late-bound regions which do - /// not appear in the fn parameters but which are nonetheless in scope. The simplest case of - /// this are unused functions, like fn foo<'a>() { } (see e.g., #51351). Despite not being used, - /// users can still reference these regions (e.g., let x: &'a u32 = &22;), so we need to create - /// entries for them and store them in the indices map. This code iterates over the complete - /// set of late-bound regions and checks for any that we have not yet seen, adding them to the - /// inputs vector. - #[cfg(debug_assertions)] - #[instrument(skip(self, indices))] - fn replace_late_bound_regions_with_nll_infer_vars( - &self, - mir_def_id: LocalDefId, - indices: &mut UniversalRegionIndices<'tcx>, - ) { - let typeck_root_def_id = self.tcx.typeck_root_def_id(mir_def_id.to_def_id()); - for_each_late_bound_region_defined_on(self.tcx, typeck_root_def_id, |r| { - debug!(?r); - if !indices.indices.contains_key(&r) { - let name = match r.get_name() { - Some(name) => name, - _ => Symbol::intern("anon"), + #[cfg(debug_assertions)] + let region_vid = { + let name = match r.get_name() { + Some(name) => name, + _ => Symbol::intern("anon"), + }; + + self.next_nll_region_var(FR, RegionCtxt::LateBound(BoundRegionInfo::Name(name))) }; - let region_vid = self.next_nll_region_var(FR, RegionCtxt::LateBound(name)); - debug!(?region_vid); indices.insert_late_bound_region(r, region_vid.to_region_vid()); } }); From aefc5ec11065728c99dd9636b736733e2c6544f0 Mon Sep 17 00:00:00 2001 From: b-naber Date: Fri, 9 Dec 2022 18:26:20 +0100 Subject: [PATCH 07/10] remove cfgs --- Cargo.lock | 36 ++++++++++ compiler/rustc_borrowck/src/lib.rs | 21 ------ .../rustc_borrowck/src/region_infer/mod.rs | 8 +-- compiler/rustc_borrowck/src/renumber.rs | 67 +------------------ compiler/rustc_borrowck/src/type_check/mod.rs | 52 +++++--------- .../src/type_check/relate_tys.rs | 48 +++++-------- .../rustc_borrowck/src/universal_regions.rs | 53 +-------------- 7 files changed, 77 insertions(+), 208 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 60fe88764f4..de09f9626a5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2003,9 +2003,15 @@ dependencies = [ [[package]] name = "http-auth" +<<<<<<< HEAD version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5430cacd7a1f9a02fbeb350dfc81a0e5ed42d81f3398cb0ba184017f85bdcfbc" +======= +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0b40b39d66c28829a0cf4d09f7e139ff8201f7500a5083732848ed3b4b4d850" +>>>>>>> 570ad623189 (remove cfgs) dependencies = [ "memchr", ] @@ -6271,6 +6277,7 @@ checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" [[package]] name = "windows_aarch64_msvc" +<<<<<<< HEAD version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" @@ -6292,6 +6299,29 @@ name = "windows_x86_64_gnu" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" +======= +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" + +[[package]] +name = "windows_i686_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" + +[[package]] +name = "windows_i686_msvc" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" +>>>>>>> 570ad623189 (remove cfgs) [[package]] name = "windows_x86_64_gnullvm" @@ -6301,9 +6331,15 @@ checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" [[package]] name = "windows_x86_64_msvc" +<<<<<<< HEAD version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" +======= +version = "0.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" +>>>>>>> 570ad623189 (remove cfgs) [[package]] name = "writeable" diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 4a5abb05149..bc0b0d30f81 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -97,7 +97,6 @@ use nll::{PoloniusOutput, ToRegionVid}; use place_ext::PlaceExt; use places_conflict::{places_conflict, PlaceConflictBias}; use region_infer::RegionInferenceContext; -#[cfg(debug_assertions)] use renumber::RegionCtxt; // FIXME(eddyb) perhaps move this somewhere more centrally. @@ -488,28 +487,14 @@ pub struct BodyWithBorrowckFacts<'tcx> { pub struct BorrowckInferCtxt<'cx, 'tcx> { pub(crate) infcx: &'cx InferCtxt<'tcx>, - - #[cfg(debug_assertions)] pub(crate) reg_var_to_origin: RefCell>, } impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> { - #[cfg(not(debug_assertions))] - pub(crate) fn new(infcx: &'cx InferCtxt<'tcx>) -> Self { - BorrowckInferCtxt { infcx } - } - - #[cfg(debug_assertions)] pub(crate) fn new(infcx: &'cx InferCtxt<'tcx>) -> Self { BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()) } } - #[cfg(not(debug_assertions))] - pub(crate) fn next_region_var(&self, origin: RegionVariableOrigin) -> ty::Region<'tcx> { - self.infcx.next_region_var(origin) - } - - #[cfg(debug_assertions)] pub(crate) fn next_region_var( &self, origin: RegionVariableOrigin, @@ -533,12 +518,6 @@ impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> { next_region } - #[cfg(not(debug_assertions))] - pub(crate) fn next_nll_region_var(&self, origin: NllRegionVariableOrigin) -> ty::Region<'tcx> { - self.infcx.next_nll_region_var(origin) - } - - #[cfg(debug_assertions)] #[instrument(skip(self), level = "debug")] pub(crate) fn next_nll_region_var( &self, diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 66d2850c67b..79a72d46353 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -244,7 +244,6 @@ pub enum ExtraConstraintInfo { PlaceholderFromPredicate(Span), } -#[cfg(debug_assertions)] #[instrument(skip(infcx, sccs), level = "debug")] fn sccs_info<'cx, 'tcx>( infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>, @@ -278,7 +277,7 @@ fn sccs_info<'cx, 'tcx>( .map(|(scc_idx, region_ctxts)| { let repr = region_ctxts .into_iter() - .max_by(|x, y| x._preference_value().cmp(&y._preference_value())) + .max_by(|x, y| x.preference_value().cmp(&y.preference_value())) .unwrap(); (ConstraintSccIndex::from_usize(scc_idx), repr) @@ -334,10 +333,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { let fr_static = universal_regions.fr_static; let constraint_sccs = Rc::new(constraints.compute_sccs(&constraint_graph, fr_static)); - #[cfg(debug_assertions)] - { - sccs_info(_infcx, constraint_sccs.clone()); - } + sccs_info(_infcx, constraint_sccs.clone()); let mut scc_values = RegionValues::new(elements, universal_regions.len(), &placeholder_indices); diff --git a/compiler/rustc_borrowck/src/renumber.rs b/compiler/rustc_borrowck/src/renumber.rs index c0cb74098fb..89b9fd18ac2 100644 --- a/compiler/rustc_borrowck/src/renumber.rs +++ b/compiler/rustc_borrowck/src/renumber.rs @@ -8,7 +8,6 @@ use rustc_middle::mir::Constant; use rustc_middle::mir::{Body, Location, Promoted}; use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::{self, Ty, TyCtxt, TypeFoldable}; -#[cfg(debug_assertions)] use rustc_span::{Span, Symbol}; /// Replaces all free regions appearing in the MIR with fresh @@ -32,21 +31,6 @@ pub fn renumber_mir<'tcx>( /// Replaces all regions appearing in `value` with fresh inference /// variables. -#[cfg(not(debug_assertions))] -#[instrument(skip(infcx), level = "debug")] -pub(crate) fn renumber_regions<'tcx, T>(infcx: &BorrowckInferCtxt<'_, 'tcx>, value: T) -> T -where - T: TypeFoldable<'tcx>, -{ - infcx.tcx.fold_regions(value, |_region, _depth| { - let origin = NllRegionVariableOrigin::Existential { from_forall: false }; - infcx.next_nll_region_var(origin) - }) -} - -/// Replaces all regions appearing in `value` with fresh inference -/// variables. -#[cfg(debug_assertions)] #[instrument(skip(infcx), level = "debug")] pub(crate) fn renumber_regions<'tcx, T>( infcx: &BorrowckInferCtxt<'_, 'tcx>, @@ -62,14 +46,12 @@ where }) } -#[cfg(debug_assertions)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub(crate) enum BoundRegionInfo { Name(Symbol), Span(Span), } -#[cfg(debug_assertions)] #[derive(Copy, Clone, Debug, Eq, PartialEq, Hash)] pub(crate) enum RegionCtxt { Location(Location), @@ -82,12 +64,11 @@ pub(crate) enum RegionCtxt { Unknown, } -#[cfg(debug_assertions)] impl RegionCtxt { /// Used to determine the representative of a component in the strongly connected /// constraint graph /// FIXME: don't use underscore here. Got a 'not used' error for some reason - pub(crate) fn _preference_value(self) -> usize { + pub(crate) fn preference_value(self) -> usize { let _anon = Symbol::intern("anon"); match self { @@ -106,21 +87,12 @@ struct NllVisitor<'a, 'tcx> { } impl<'a, 'tcx> NllVisitor<'a, 'tcx> { - #[cfg(debug_assertions)] fn renumber_regions(&mut self, value: T, ctxt: RegionCtxt) -> T where T: TypeFoldable<'tcx>, { renumber_regions(self.infcx, value, ctxt) } - - #[cfg(not(debug_assertions))] - fn renumber_regions(&mut self, value: T) -> T - where - T: TypeFoldable<'tcx>, - { - renumber_regions(self.infcx, value) - } } impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { @@ -128,15 +100,6 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { self.infcx.tcx } - #[cfg(not(debug_assertions))] - #[instrument(skip(self), level = "debug")] - fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _ty_context: TyContext) { - *ty = self.renumber_regions(*ty); - - debug!(?ty); - } - - #[cfg(debug_assertions)] #[instrument(skip(self), level = "debug")] fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _ty_context: TyContext) { *ty = self.renumber_regions(*ty, RegionCtxt::TyContext(_ty_context)); @@ -144,15 +107,6 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { debug!(?ty); } - #[cfg(not(debug_assertions))] - #[instrument(skip(self), level = "debug")] - fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) { - *substs = self.renumber_regions(*substs); - - debug!(?substs); - } - - #[cfg(debug_assertions)] #[instrument(skip(self), level = "debug")] fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) { *substs = self.renumber_regions(*substs, RegionCtxt::Location(location)); @@ -160,16 +114,6 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { debug!(?substs); } - #[cfg(not(debug_assertions))] - #[instrument(skip(self), level = "debug")] - fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) { - let old_region = *region; - *region = self.renumber_regions(old_region); - - debug!(?region); - } - - #[cfg(debug_assertions)] #[instrument(skip(self), level = "debug")] fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) { let old_region = *region; @@ -178,15 +122,6 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { debug!(?region); } - #[cfg(not(debug_assertions))] - #[instrument(skip(self), level = "debug")] - fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) { - let literal = constant.literal; - constant.literal = self.renumber_regions(literal); - debug!("constant: {:#?}", constant); - } - - #[cfg(debug_assertions)] #[instrument(skip(self), level = "debug")] fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) { let literal = constant.literal; diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index 5d3828b14c9..e7f22fc79a7 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -1335,43 +1335,27 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { } }; let (sig, map) = tcx.replace_late_bound_regions(sig, |br| { - #[cfg(not(debug_assertions))] - { - self.infcx.next_region_var(LateBoundRegion( + use crate::renumber::{BoundRegionInfo, RegionCtxt}; + use rustc_span::Symbol; + + let reg_info = match br.kind { + // FIXME Probably better to use the `Span` here + ty::BoundRegionKind::BrAnon(_, Some(span)) => BoundRegionInfo::Span(span), + ty::BoundRegionKind::BrAnon(..) => { + BoundRegionInfo::Name(Symbol::intern("anon")) + } + ty::BoundRegionKind::BrNamed(_, name) => BoundRegionInfo::Name(name), + ty::BoundRegionKind::BrEnv => BoundRegionInfo::Name(Symbol::intern("env")), + }; + + self.infcx.next_region_var( + LateBoundRegion( term.source_info.span, br.kind, LateBoundRegionConversionTime::FnCall, - )) - } - - #[cfg(debug_assertions)] - { - use crate::renumber::{BoundRegionInfo, RegionCtxt}; - use rustc_span::Symbol; - - let reg_info = match br.kind { - // FIXME Probably better to use the `Span` here - ty::BoundRegionKind::BrAnon(_, Some(span)) => { - BoundRegionInfo::Span(span) - } - ty::BoundRegionKind::BrAnon(..) => { - BoundRegionInfo::Name(Symbol::intern("anon")) - } - ty::BoundRegionKind::BrNamed(_, name) => BoundRegionInfo::Name(name), - ty::BoundRegionKind::BrEnv => { - BoundRegionInfo::Name(Symbol::intern("env")) - } - }; - - self.infcx.next_region_var( - LateBoundRegion( - term.source_info.span, - br.kind, - LateBoundRegionConversionTime::FnCall, - ), - RegionCtxt::LateBound(reg_info), - ) - } + ), + RegionCtxt::LateBound(reg_info), + ) }); debug!(?sig); // IMPORTANT: We have to prove well formed for the function signature before diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index 51ddf02629c..0b7858ec04d 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -9,7 +9,6 @@ use rustc_trait_selection::traits::query::Fallible; use crate::constraints::OutlivesConstraint; use crate::diagnostics::UniverseInfo; -#[cfg(debug_assertions)] use crate::renumber::{BoundRegionInfo, RegionCtxt}; use crate::type_check::{InstantiateOpaqueType, Locations, TypeChecker}; @@ -110,10 +109,6 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> ) -> ty::Region<'tcx> { let origin = NllRegionVariableOrigin::Existential { from_forall }; - #[cfg(not(debug_assertions))] - let reg_var = self.type_checker.infcx.next_nll_region_var(origin); - - #[cfg(debug_assertions)] let reg_var = self.type_checker.infcx.next_nll_region_var(origin, RegionCtxt::Existential(_name)); @@ -128,23 +123,19 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> .constraints .placeholder_region(self.type_checker.infcx, placeholder); - #[cfg(debug_assertions)] - { - let reg_info = match placeholder.name { - // FIXME Probably better to use the `Span` here - ty::BoundRegionKind::BrAnon(_, Some(span)) => BoundRegionInfo::Span(span), - ty::BoundRegionKind::BrAnon(..) => BoundRegionInfo::Name(Symbol::intern("anon")), - ty::BoundRegionKind::BrNamed(_, name) => BoundRegionInfo::Name(name), - ty::BoundRegionKind::BrEnv => BoundRegionInfo::Name(Symbol::intern("env")), - }; + let reg_info = match placeholder.name { + ty::BoundRegionKind::BrAnon(_, Some(span)) => BoundRegionInfo::Span(span), + ty::BoundRegionKind::BrAnon(..) => BoundRegionInfo::Name(Symbol::intern("anon")), + ty::BoundRegionKind::BrNamed(_, name) => BoundRegionInfo::Name(name), + ty::BoundRegionKind::BrEnv => BoundRegionInfo::Name(Symbol::intern("env")), + }; - let reg_var = reg - .try_get_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)); - } + let reg_var = reg + .try_get_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)); reg } @@ -156,15 +147,12 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> universe, ); - #[cfg(debug_assertions)] - { - let reg_var = reg - .try_get_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::Existential(None)); - assert!(matches!(prev, None)); - } + let reg_var = reg + .try_get_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::Existential(None)); + assert!(matches!(prev, None)); reg } diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index e6abdd9b271..242995a6a29 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -26,12 +26,10 @@ use rustc_middle::ty::{ self, DefIdTree, InlineConstSubsts, InlineConstSubstsParts, RegionVid, Ty, TyCtxt, }; use rustc_middle::ty::{InternalSubsts, SubstsRef}; -#[cfg(debug_assertions)] use rustc_span::Symbol; use std::iter; use crate::nll::ToRegionVid; -#[cfg(debug_assertions)] use crate::renumber::{BoundRegionInfo, RegionCtxt}; use crate::BorrowckInferCtxt; @@ -408,9 +406,6 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { assert_eq!(FIRST_GLOBAL_INDEX, self.infcx.num_region_vars()); // Create the "global" region that is always free in all contexts: 'static. - #[cfg(not(debug_assertions))] - let fr_static = self.infcx.next_nll_region_var(FR).to_region_vid(); - #[cfg(debug_assertions)] let fr_static = self .infcx .next_nll_region_var(FR, RegionCtxt::Free(Symbol::intern("static"))) @@ -446,10 +441,6 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { |r| { debug!(?r); if !indices.indices.contains_key(&r) { - #[cfg(not(debug_assertions))] - let region_vid = self.infcx.next_nll_region_var(FR); - - #[cfg(debug_assertions)] let region_vid = { let name = match r.get_name() { Some(name) => name, @@ -489,10 +480,6 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { for_each_late_bound_region_in_item(self.infcx.tcx, self.mir_def.did, |r| { debug!(?r); if !indices.indices.contains_key(&r) { - #[cfg(not(debug_assertions))] - let region_vid = self.infcx.next_nll_region_var(FR); - - #[cfg(debug_assertions)] let region_vid = { let name = match r.get_name() { Some(name) => name, @@ -521,7 +508,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { ); let reg_vid = self.infcx.next_nll_region_var(FR, RegionCtxt::Free(Symbol::intern("c-variadic")).to_region_vid(); let region = - self.infcx.tcx.mk_re_var(self.infcx.next_nll_region_var(FR).to_region_vid()); + self.infcx.tcx.mk_re_var(reg_vid); let va_list_ty = self.infcx.tcx.type_of(va_list_did).subst(self.infcx.tcx, &[region.into()]); @@ -531,9 +518,6 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { } } - #[cfg(not(debug_assertions))] - let fr_fn_body = self.infcx.next_nll_region_var(FR).to_region_vid(); - #[cfg(debug_assertions)] let fr_fn_body = self .infcx .next_nll_region_var(FR, RegionCtxt::Free(Symbol::intern("fn_body"))) @@ -766,19 +750,6 @@ trait InferCtxtExt<'tcx> { } impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { - #[cfg(not(debug_assertions))] - fn replace_free_regions_with_nll_infer_vars( - &self, - origin: NllRegionVariableOrigin, - value: T, - ) -> T - where - T: TypeFoldable<'tcx>, - { - self.tcx.fold_regions(value, |_region, _depth| self.infcx.next_nll_region_var(origin)) - } - - #[cfg(debug_assertions)] #[instrument(skip(self), level = "debug")] fn replace_free_regions_with_nll_infer_vars( &self, @@ -815,19 +786,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| { debug!(?br); let liberated_region = self.tcx.mk_re_free(all_outlive_scope.to_def_id(), br.kind); - #[cfg(not(debug_assertions))] - let region_vid = self.next_nll_region_var(origin); - - #[cfg(debug_assertions)] - let region_vid = { - let name = match br.kind.get_name() { - Some(name) => name, - _ => Symbol::intern("anon"), - }; - - self.next_nll_region_var(origin, RegionCtxt::Bound(BoundRegionInfo::Name(name))) - }; - + let region_vid = self.next_nll_region_var(origin, RegionCtxt::Bound(BoundRegionInfo::Name(name))); indices.insert_late_bound_region(liberated_region, region_vid.to_region_vid()); debug!(?liberated_region, ?region_vid); region_vid @@ -853,10 +812,6 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { for_each_late_bound_region_in_recursive_scope(self.tcx, mir_def_id, |r| { debug!(?r); if !indices.indices.contains_key(&r) { - #[cfg(not(debug_assertions))] - let region_vid = self.next_nll_region_var(FR); - - #[cfg(debug_assertions)] let region_vid = { let name = match r.get_name() { Some(name) => name, @@ -881,10 +836,6 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { for_each_late_bound_region_in_item(self.tcx, mir_def_id, |r| { debug!(?r); if !indices.indices.contains_key(&r) { - #[cfg(not(debug_assertions))] - let region_vid = self.next_nll_region_var(FR); - - #[cfg(debug_assertions)] let region_vid = { let name = match r.get_name() { Some(name) => name, From 0725d0ceee13d3484b2b7117e4cff82c01d8f868 Mon Sep 17 00:00:00 2001 From: b-naber Date: Sun, 19 Feb 2023 21:44:01 +0000 Subject: [PATCH 08/10] add some cfgs back --- Cargo.lock | 55 ++---------------- compiler/rustc_borrowck/src/lib.rs | 56 +++++++++++-------- .../rustc_borrowck/src/region_infer/mod.rs | 34 +++++++---- compiler/rustc_borrowck/src/renumber.rs | 27 +++++---- compiler/rustc_borrowck/src/type_check/mod.rs | 25 ++++++--- .../src/type_check/relate_tys.rs | 15 +++-- .../rustc_borrowck/src/universal_regions.rs | 46 ++++++++++----- 7 files changed, 134 insertions(+), 124 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de09f9626a5..d02cab38ae8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -782,7 +782,7 @@ dependencies = [ "declare_clippy_lint", "if_chain", "itertools", - "pulldown-cmark 0.9.2", + "pulldown-cmark", "quine-mc_cluskey", "regex-syntax", "rustc-semver", @@ -2003,15 +2003,9 @@ dependencies = [ [[package]] name = "http-auth" -<<<<<<< HEAD -version = "0.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5430cacd7a1f9a02fbeb350dfc81a0e5ed42d81f3398cb0ba184017f85bdcfbc" -======= version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0b40b39d66c28829a0cf4d09f7e139ff8201f7500a5083732848ed3b4b4d850" ->>>>>>> 570ad623189 (remove cfgs) dependencies = [ "memchr", ] @@ -2561,7 +2555,7 @@ dependencies = [ "memchr", "once_cell", "opener", - "pulldown-cmark 0.9.2", + "pulldown-cmark", "regex", "serde", "serde_json", @@ -2578,7 +2572,7 @@ dependencies = [ "anyhow", "handlebars 3.5.5", "pretty_assertions", - "pulldown-cmark 0.7.2", + "pulldown-cmark", "same-file", "serde_json", "url", @@ -3275,17 +3269,6 @@ dependencies = [ "cc", ] -[[package]] -name = "pulldown-cmark" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ca36dea94d187597e104a5c8e4b07576a8a45aa5db48a65e12940d3eb7461f55" -dependencies = [ - "bitflags", - "memchr", - "unicase", -] - [[package]] name = "pulldown-cmark" version = "0.9.2" @@ -4589,7 +4572,7 @@ name = "rustc_resolve" version = "0.0.0" dependencies = [ "bitflags", - "pulldown-cmark 0.9.2", + "pulldown-cmark", "rustc_arena", "rustc_ast", "rustc_ast_pretty", @@ -6277,7 +6260,6 @@ checksum = "8c9864e83243fdec7fc9c5444389dcbbfd258f745e7853198f365e3c4968a608" [[package]] name = "windows_aarch64_msvc" -<<<<<<< HEAD version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c8b1b673ffc16c47a9ff48570a9d85e25d265735c503681332589af6253c6c7" @@ -6299,29 +6281,6 @@ name = "windows_x86_64_gnu" version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1040f221285e17ebccbc2591ffdc2d44ee1f9186324dd3e84e99ac68d699c45" -======= -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd0f252f5a35cac83d6311b2e795981f5ee6e67eb1f9a7f64eb4500fbc4dcdb4" - -[[package]] -name = "windows_i686_gnu" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbeae19f6716841636c28d695375df17562ca208b2b7d0dc47635a50ae6c5de7" - -[[package]] -name = "windows_i686_msvc" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "84c12f65daa39dd2babe6e442988fc329d6243fdce47d7d2d155b8d874862246" - -[[package]] -name = "windows_x86_64_gnu" -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bf7b1b21b5362cbc318f686150e5bcea75ecedc74dd157d874d754a2ca44b0ed" ->>>>>>> 570ad623189 (remove cfgs) [[package]] name = "windows_x86_64_gnullvm" @@ -6331,15 +6290,9 @@ checksum = "628bfdf232daa22b0d64fdb62b09fcc36bb01f05a3939e20ab73aaf9470d0463" [[package]] name = "windows_x86_64_msvc" -<<<<<<< HEAD version = "0.42.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "447660ad36a13288b1db4d4248e857b510e8c3a225c822ba4fb748c0aafecffd" -======= -version = "0.42.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40009d85759725a34da6d89a94e63d7bdc50a862acf0dbc7c8e488f1edcb6f5" ->>>>>>> 570ad623189 (remove cfgs) [[package]] name = "writeable" diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index bc0b0d30f81..aedc030ea0a 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -495,49 +495,59 @@ impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> { BorrowckInferCtxt { infcx, reg_var_to_origin: RefCell::new(Default::default()) } } - pub(crate) fn next_region_var( + pub(crate) fn next_region_var( &self, origin: RegionVariableOrigin, - ctxt: RegionCtxt, - ) -> ty::Region<'tcx> { + get_ctxt_fn: F, + ) -> ty::Region<'tcx> + where + F: Fn() -> RegionCtxt, + { let next_region = self.infcx.next_region_var(origin); let vid = next_region .try_get_var() .unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region)); - debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin); - let mut var_to_origin = self.reg_var_to_origin.borrow_mut(); - let prev = var_to_origin.insert(vid, ctxt); - debug!("var_to_origin after insertion: {:?}", var_to_origin); + if cfg!(debug_assertions) { + 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)); + // 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)); + } next_region } - #[instrument(skip(self), level = "debug")] - pub(crate) fn next_nll_region_var( + #[instrument(skip(self, get_ctxt_fn), level = "debug")] + pub(crate) fn next_nll_region_var( &self, origin: NllRegionVariableOrigin, - ctxt: RegionCtxt, - ) -> ty::Region<'tcx> { + get_ctxt_fn: F, + ) -> ty::Region<'tcx> + where + F: Fn() -> RegionCtxt, + { let next_region = self.infcx.next_nll_region_var(origin.clone()); let vid = next_region .try_get_var() .unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region)); - debug!("inserting vid {:?} with origin {:?} into var_to_origin", vid, origin); - let mut var_to_origin = self.reg_var_to_origin.borrow_mut(); - let prev = var_to_origin.insert(vid, ctxt); - debug!("var_to_origin after insertion: {:?}", var_to_origin); + if cfg!(debug_assertions) { + 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)); + // 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)); + } next_region } diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 79a72d46353..0d339dac379 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -244,6 +244,7 @@ pub enum ExtraConstraintInfo { PlaceholderFromPredicate(Span), } +#[cfg(debug_assertions)] #[instrument(skip(infcx, sccs), level = "debug")] fn sccs_info<'cx, 'tcx>( infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>, @@ -252,31 +253,43 @@ fn sccs_info<'cx, 'tcx>( use crate::renumber::RegionCtxt; let var_to_origin = infcx.reg_var_to_origin.borrow(); + + let mut var_to_origin_sorted = var_to_origin.clone().into_iter().collect::>(); + var_to_origin_sorted.sort_by(|a, b| a.0.cmp(&b.0)); + let mut debug_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)); + } + debug!(debug_str); + let num_components = sccs.scc_data.ranges.len(); let mut components = vec![FxHashSet::default(); num_components]; for (reg_var_idx, scc_idx) in sccs.scc_indices.iter().enumerate() { let reg_var = ty::RegionVid::from_usize(reg_var_idx); let origin = var_to_origin.get(®_var).unwrap_or_else(|| &RegionCtxt::Unknown); - components[scc_idx.as_usize()].insert(*origin); + components[scc_idx.as_usize()].insert((reg_var, *origin)); } - debug!( - "strongly connected components: {:#?}", - components - .iter() - .enumerate() - .map(|(idx, origin)| { (ConstraintSccIndex::from_usize(idx), origin) }) - .collect::>() - ); + let mut components_str = "strongly connected components:"; + for (scc_idx, reg_vars_origins) in components.iter().enumerate() { + let regions_info = reg_vars_origins.clone().into_iter().collect::>(); + components_str.push(&format( + "{:?}: {:?})", + ConstraintSccIndex::from_usize(scc_idx), + regions_info, + )) + } + debug!(components_str); - // Now let's calculate the best representative for each component + // calculate the best representative for each component let components_representatives = components .into_iter() .enumerate() .map(|(scc_idx, region_ctxts)| { let repr = region_ctxts .into_iter() + .map(|reg_var_origin| reg_var_origin.1) .max_by(|x, y| x.preference_value().cmp(&y.preference_value())) .unwrap(); @@ -333,6 +346,7 @@ impl<'tcx> RegionInferenceContext<'tcx> { let fr_static = universal_regions.fr_static; let constraint_sccs = Rc::new(constraints.compute_sccs(&constraint_graph, fr_static)); + #[cfg(debug_assertions)] sccs_info(_infcx, constraint_sccs.clone()); let mut scc_values = diff --git a/compiler/rustc_borrowck/src/renumber.rs b/compiler/rustc_borrowck/src/renumber.rs index 89b9fd18ac2..eb8c86f800b 100644 --- a/compiler/rustc_borrowck/src/renumber.rs +++ b/compiler/rustc_borrowck/src/renumber.rs @@ -31,18 +31,19 @@ pub fn renumber_mir<'tcx>( /// Replaces all regions appearing in `value` with fresh inference /// variables. -#[instrument(skip(infcx), level = "debug")] -pub(crate) fn renumber_regions<'tcx, T>( +#[instrument(skip(infcx, get_ctxt_fn), level = "debug")] +pub(crate) fn renumber_regions<'tcx, T, F>( infcx: &BorrowckInferCtxt<'_, 'tcx>, value: T, - ctxt: RegionCtxt, + get_ctxt_fn: F, ) -> T where T: TypeFoldable<'tcx>, + F: Fn() -> RegionCtxt, { infcx.tcx.fold_regions(value, |_region, _depth| { let origin = NllRegionVariableOrigin::Existential { from_forall: false }; - infcx.next_nll_region_var(origin, ctxt) + infcx.next_nll_region_var(origin, || get_ctxt_fn()) }) } @@ -61,13 +62,14 @@ pub(crate) enum RegionCtxt { LateBound(BoundRegionInfo), Existential(Option), Placeholder(BoundRegionInfo), + #[cfg(debug_assertions)] Unknown, } impl RegionCtxt { /// Used to determine the representative of a component in the strongly connected /// constraint graph - /// FIXME: don't use underscore here. Got a 'not used' error for some reason + #[cfg(debug_assertions)] pub(crate) fn preference_value(self) -> usize { let _anon = Symbol::intern("anon"); @@ -87,11 +89,12 @@ struct NllVisitor<'a, 'tcx> { } impl<'a, 'tcx> NllVisitor<'a, 'tcx> { - fn renumber_regions(&mut self, value: T, ctxt: RegionCtxt) -> T + fn renumber_regions(&mut self, value: T, region_ctxt_fn: F) -> T where T: TypeFoldable<'tcx>, + F: Fn() -> RegionCtxt, { - renumber_regions(self.infcx, value, ctxt) + renumber_regions(self.infcx, value, region_ctxt_fn) } } @@ -101,15 +104,15 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { } #[instrument(skip(self), level = "debug")] - fn visit_ty(&mut self, ty: &mut Ty<'tcx>, _ty_context: TyContext) { - *ty = self.renumber_regions(*ty, RegionCtxt::TyContext(_ty_context)); + fn visit_ty(&mut self, ty: &mut Ty<'tcx>, ty_context: TyContext) { + *ty = self.renumber_regions(*ty, || RegionCtxt::TyContext(ty_context)); debug!(?ty); } #[instrument(skip(self), level = "debug")] fn visit_substs(&mut self, substs: &mut SubstsRef<'tcx>, location: Location) { - *substs = self.renumber_regions(*substs, RegionCtxt::Location(location)); + *substs = self.renumber_regions(*substs, || RegionCtxt::Location(location)); debug!(?substs); } @@ -117,7 +120,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { #[instrument(skip(self), level = "debug")] fn visit_region(&mut self, region: &mut ty::Region<'tcx>, location: Location) { let old_region = *region; - *region = self.renumber_regions(old_region, RegionCtxt::Location(location)); + *region = self.renumber_regions(old_region, || RegionCtxt::Location(location)); debug!(?region); } @@ -125,7 +128,7 @@ impl<'a, 'tcx> MutVisitor<'tcx> for NllVisitor<'a, 'tcx> { #[instrument(skip(self), level = "debug")] fn visit_constant(&mut self, constant: &mut Constant<'tcx>, _location: Location) { let literal = constant.literal; - constant.literal = self.renumber_regions(literal, RegionCtxt::Location(_location)); + constant.literal = self.renumber_regions(literal, || RegionCtxt::Location(_location)); debug!("constant: {:#?}", constant); } } diff --git a/compiler/rustc_borrowck/src/type_check/mod.rs b/compiler/rustc_borrowck/src/type_check/mod.rs index e7f22fc79a7..78bdace6b60 100644 --- a/compiler/rustc_borrowck/src/type_check/mod.rs +++ b/compiler/rustc_borrowck/src/type_check/mod.rs @@ -1338,14 +1338,21 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { use crate::renumber::{BoundRegionInfo, RegionCtxt}; use rustc_span::Symbol; - let reg_info = match br.kind { - // FIXME Probably better to use the `Span` here - ty::BoundRegionKind::BrAnon(_, Some(span)) => BoundRegionInfo::Span(span), - ty::BoundRegionKind::BrAnon(..) => { - BoundRegionInfo::Name(Symbol::intern("anon")) - } - ty::BoundRegionKind::BrNamed(_, name) => BoundRegionInfo::Name(name), - ty::BoundRegionKind::BrEnv => BoundRegionInfo::Name(Symbol::intern("env")), + let region_ctxt_fn = || { + let reg_info = match br.kind { + ty::BoundRegionKind::BrAnon(_, Some(span)) => { + BoundRegionInfo::Span(span) + } + ty::BoundRegionKind::BrAnon(..) => { + BoundRegionInfo::Name(Symbol::intern("anon")) + } + ty::BoundRegionKind::BrNamed(_, name) => BoundRegionInfo::Name(name), + ty::BoundRegionKind::BrEnv => { + BoundRegionInfo::Name(Symbol::intern("env")) + } + }; + + RegionCtxt::LateBound(reg_info) }; self.infcx.next_region_var( @@ -1354,7 +1361,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { br.kind, LateBoundRegionConversionTime::FnCall, ), - RegionCtxt::LateBound(reg_info), + region_ctxt_fn, ) }); debug!(?sig); diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index 0b7858ec04d..1ea3dccc910 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -110,7 +110,7 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> let origin = NllRegionVariableOrigin::Existential { from_forall }; let reg_var = - self.type_checker.infcx.next_nll_region_var(origin, RegionCtxt::Existential(_name)); + self.type_checker.infcx.next_nll_region_var(origin, || RegionCtxt::Existential(_name)); reg_var } @@ -150,9 +150,16 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> let reg_var = reg .try_get_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::Existential(None)); - assert!(matches!(prev, None)); + + if cfg!(debug_assertions) { + 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)); + } reg } diff --git a/compiler/rustc_borrowck/src/universal_regions.rs b/compiler/rustc_borrowck/src/universal_regions.rs index 242995a6a29..f3ca896cd0d 100644 --- a/compiler/rustc_borrowck/src/universal_regions.rs +++ b/compiler/rustc_borrowck/src/universal_regions.rs @@ -408,7 +408,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { // Create the "global" region that is always free in all contexts: 'static. let fr_static = self .infcx - .next_nll_region_var(FR, RegionCtxt::Free(Symbol::intern("static"))) + .next_nll_region_var(FR, || RegionCtxt::Free(Symbol::intern("static"))) .to_region_vid(); // We've now added all the global regions. The next ones we @@ -447,10 +447,9 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { _ => Symbol::intern("anon"), }; - self.infcx.next_nll_region_var( - FR, - RegionCtxt::LateBound(BoundRegionInfo::Name(name)), - ) + self.infcx.next_nll_region_var(FR, || { + RegionCtxt::LateBound(BoundRegionInfo::Name(name)) + }) }; debug!(?region_vid); @@ -486,8 +485,9 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { _ => Symbol::intern("anon"), }; - self.infcx - .next_nll_region_var(FR, RegionCtxt::LateBound(BoundRegionInfo::Name(name))) + self.infcx.next_nll_region_var(FR, || { + RegionCtxt::LateBound(BoundRegionInfo::Name(name)) + }) }; debug!(?region_vid); @@ -506,9 +506,13 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { LangItem::VaList, Some(self.infcx.tcx.def_span(self.mir_def.did)), ); - let reg_vid = self.infcx.next_nll_region_var(FR, RegionCtxt::Free(Symbol::intern("c-variadic")).to_region_vid(); - let region = - self.infcx.tcx.mk_re_var(reg_vid); + + let reg_vid = self + .infcx + .next_nll_region_var(FR, || RegionCtxt::Free(Symbol::intern("c-variadic"))) + .to_region_vid(); + + let region = self.infcx.tcx.mk_re_var(reg_vid); let va_list_ty = self.infcx.tcx.type_of(va_list_did).subst(self.infcx.tcx, &[region.into()]); @@ -520,7 +524,7 @@ impl<'cx, 'tcx> UniversalRegionsBuilder<'cx, 'tcx> { let fr_fn_body = self .infcx - .next_nll_region_var(FR, RegionCtxt::Free(Symbol::intern("fn_body"))) + .next_nll_region_var(FR, || RegionCtxt::Free(Symbol::intern("fn_body"))) .to_region_vid(); let num_universals = self.infcx.num_region_vars(); @@ -766,7 +770,7 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { }; debug!(?region, ?name); - let reg_var = self.next_nll_region_var(origin, RegionCtxt::Free(name)); + let reg_var = self.next_nll_region_var(origin, || RegionCtxt::Free(name)); reg_var }) @@ -786,7 +790,15 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { let (value, _map) = self.tcx.replace_late_bound_regions(value, |br| { debug!(?br); let liberated_region = self.tcx.mk_re_free(all_outlive_scope.to_def_id(), br.kind); - let region_vid = self.next_nll_region_var(origin, RegionCtxt::Bound(BoundRegionInfo::Name(name))); + let region_vid = { + let name = match br.kind.get_name() { + Some(name) => name, + _ => Symbol::intern("anon"), + }; + + self.next_nll_region_var(origin, || RegionCtxt::Bound(BoundRegionInfo::Name(name))) + }; + indices.insert_late_bound_region(liberated_region, region_vid.to_region_vid()); debug!(?liberated_region, ?region_vid); region_vid @@ -818,7 +830,9 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { _ => Symbol::intern("anon"), }; - self.next_nll_region_var(FR, RegionCtxt::LateBound(BoundRegionInfo::Name(name))) + self.next_nll_region_var(FR, || { + RegionCtxt::LateBound(BoundRegionInfo::Name(name)) + }) }; debug!(?region_vid); @@ -842,7 +856,9 @@ impl<'cx, 'tcx> InferCtxtExt<'tcx> for BorrowckInferCtxt<'cx, 'tcx> { _ => Symbol::intern("anon"), }; - self.next_nll_region_var(FR, RegionCtxt::LateBound(BoundRegionInfo::Name(name))) + self.next_nll_region_var(FR, || { + RegionCtxt::LateBound(BoundRegionInfo::Name(name)) + }) }; indices.insert_late_bound_region(r, region_vid.to_region_vid()); From c9843d61440edb19642920f2f8f33652e9c83971 Mon Sep 17 00:00:00 2001 From: b-naber Date: Mon, 20 Feb 2023 21:57:03 +0000 Subject: [PATCH 09/10] remove cfg attributes --- compiler/rustc_borrowck/src/region_infer/mod.rs | 10 +++++----- compiler/rustc_borrowck/src/renumber.rs | 2 -- .../rustc_infer/src/infer/canonical/query_response.rs | 6 +----- 3 files changed, 6 insertions(+), 12 deletions(-) diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index 0d339dac379..b2548b61cc5 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -244,7 +244,6 @@ pub enum ExtraConstraintInfo { PlaceholderFromPredicate(Span), } -#[cfg(debug_assertions)] #[instrument(skip(infcx, sccs), level = "debug")] fn sccs_info<'cx, 'tcx>( infcx: &'cx BorrowckInferCtxt<'cx, 'tcx>, @@ -271,10 +270,10 @@ fn sccs_info<'cx, 'tcx>( components[scc_idx.as_usize()].insert((reg_var, *origin)); } - let mut components_str = "strongly connected components:"; + let mut components_str = "strongly connected components:".to_string(); for (scc_idx, reg_vars_origins) in components.iter().enumerate() { let regions_info = reg_vars_origins.clone().into_iter().collect::>(); - components_str.push(&format( + components_str.push_str(&format!( "{:?}: {:?})", ConstraintSccIndex::from_usize(scc_idx), regions_info, @@ -346,8 +345,9 @@ impl<'tcx> RegionInferenceContext<'tcx> { let fr_static = universal_regions.fr_static; let constraint_sccs = Rc::new(constraints.compute_sccs(&constraint_graph, fr_static)); - #[cfg(debug_assertions)] - sccs_info(_infcx, constraint_sccs.clone()); + if cfg!(debug_assertions) { + sccs_info(_infcx, constraint_sccs.clone()); + } let mut scc_values = RegionValues::new(elements, universal_regions.len(), &placeholder_indices); diff --git a/compiler/rustc_borrowck/src/renumber.rs b/compiler/rustc_borrowck/src/renumber.rs index eb8c86f800b..960dedc74ef 100644 --- a/compiler/rustc_borrowck/src/renumber.rs +++ b/compiler/rustc_borrowck/src/renumber.rs @@ -62,14 +62,12 @@ pub(crate) enum RegionCtxt { LateBound(BoundRegionInfo), Existential(Option), Placeholder(BoundRegionInfo), - #[cfg(debug_assertions)] Unknown, } impl RegionCtxt { /// Used to determine the representative of a component in the strongly connected /// constraint graph - #[cfg(debug_assertions)] pub(crate) fn preference_value(self) -> usize { let _anon = Symbol::intern("anon"); diff --git a/compiler/rustc_infer/src/infer/canonical/query_response.rs b/compiler/rustc_infer/src/infer/canonical/query_response.rs index 1ca9e87e056..e993fb4c22b 100644 --- a/compiler/rustc_infer/src/infer/canonical/query_response.rs +++ b/compiler/rustc_infer/src/infer/canonical/query_response.rs @@ -318,11 +318,7 @@ impl<'tcx> InferCtxt<'tcx> { // Screen out `'a: 'a` cases. let ty::OutlivesPredicate(k1, r2) = r_c.0; - if k1 != r2.into() { - Some(r_c) - } else { - None - } + if k1 != r2.into() { Some(r_c) } else { None } }), ); From 8252a6eddfc59328fe6ac36ef09feb2844f28fa2 Mon Sep 17 00:00:00 2001 From: b-naber Date: Tue, 21 Feb 2023 21:54:53 +0000 Subject: [PATCH 10/10] address review --- compiler/rustc_borrowck/src/lib.rs | 4 ++-- .../rustc_borrowck/src/region_infer/mod.rs | 8 +++---- .../src/type_check/relate_tys.rs | 10 ++++---- .../src/graph/scc/mod.rs | 24 +++++++++++++++---- compiler/rustc_middle/src/ty/sty.rs | 2 +- 5 files changed, 31 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index aedc030ea0a..622b57c7b7f 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -505,7 +505,7 @@ impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> { { let next_region = self.infcx.next_region_var(origin); let vid = next_region - .try_get_var() + .as_var() .unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region)); if cfg!(debug_assertions) { @@ -534,7 +534,7 @@ impl<'cx, 'tcx> BorrowckInferCtxt<'cx, 'tcx> { { let next_region = self.infcx.next_nll_region_var(origin.clone()); let vid = next_region - .try_get_var() + .as_var() .unwrap_or_else(|| bug!("expected RegionKind::RegionVar on {:?}", next_region)); if cfg!(debug_assertions) { diff --git a/compiler/rustc_borrowck/src/region_infer/mod.rs b/compiler/rustc_borrowck/src/region_infer/mod.rs index b2548b61cc5..8c374c2164c 100644 --- a/compiler/rustc_borrowck/src/region_infer/mod.rs +++ b/compiler/rustc_borrowck/src/region_infer/mod.rs @@ -261,10 +261,10 @@ fn sccs_info<'cx, 'tcx>( } debug!(debug_str); - let num_components = sccs.scc_data.ranges.len(); + let num_components = sccs.scc_data().ranges().len(); let mut components = vec![FxHashSet::default(); num_components]; - for (reg_var_idx, scc_idx) in sccs.scc_indices.iter().enumerate() { + for (reg_var_idx, scc_idx) in sccs.scc_indices().iter().enumerate() { let reg_var = ty::RegionVid::from_usize(reg_var_idx); let origin = var_to_origin.get(®_var).unwrap_or_else(|| &RegionCtxt::Unknown); components[scc_idx.as_usize()].insert((reg_var, *origin)); @@ -298,8 +298,8 @@ fn sccs_info<'cx, 'tcx>( let mut scc_node_to_edges = FxHashMap::default(); for (scc_idx, repr) in components_representatives.iter() { - let edges_range = sccs.scc_data.ranges[*scc_idx].clone(); - let edges = &sccs.scc_data.all_successors[edges_range]; + let edges_range = sccs.scc_data().ranges()[*scc_idx].clone(); + let edges = &sccs.scc_data().all_successors()[edges_range]; let edge_representatives = edges.iter().map(|scc_idx| components_representatives[scc_idx]).collect::>(); scc_node_to_edges.insert((scc_idx, repr), edge_representatives); diff --git a/compiler/rustc_borrowck/src/type_check/relate_tys.rs b/compiler/rustc_borrowck/src/type_check/relate_tys.rs index 1ea3dccc910..d96372fb99b 100644 --- a/compiler/rustc_borrowck/src/type_check/relate_tys.rs +++ b/compiler/rustc_borrowck/src/type_check/relate_tys.rs @@ -130,9 +130,8 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> ty::BoundRegionKind::BrEnv => BoundRegionInfo::Name(Symbol::intern("env")), }; - let reg_var = reg - .try_get_var() - .unwrap_or_else(|| bug!("expected region {:?} to be of kind ReVar", reg)); + 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)); @@ -147,9 +146,8 @@ impl<'tcx> TypeRelatingDelegate<'tcx> for NllTypeRelatingDelegate<'_, '_, 'tcx> universe, ); - let reg_var = reg - .try_get_var() - .unwrap_or_else(|| bug!("expected region {:?} to be of kind ReVar", reg)); + let reg_var = + reg.as_var().unwrap_or_else(|| bug!("expected region {:?} to be of kind ReVar", reg)); if cfg!(debug_assertions) { let mut var_to_origin = self.type_checker.infcx.reg_var_to_origin.borrow_mut(); diff --git a/compiler/rustc_data_structures/src/graph/scc/mod.rs b/compiler/rustc_data_structures/src/graph/scc/mod.rs index e2fe5285aad..c4b11951ab7 100644 --- a/compiler/rustc_data_structures/src/graph/scc/mod.rs +++ b/compiler/rustc_data_structures/src/graph/scc/mod.rs @@ -21,21 +21,21 @@ mod tests; pub struct Sccs { /// For each node, what is the SCC index of the SCC to which it /// belongs. - pub scc_indices: IndexVec, + scc_indices: IndexVec, /// Data about each SCC. - pub scc_data: SccData, + scc_data: SccData, } pub struct SccData { /// For each SCC, the range of `all_successors` where its /// successors can be found. - pub ranges: IndexVec>, + ranges: IndexVec>, /// Contains the successors for all the Sccs, concatenated. The /// range of indices corresponding to a given SCC is found in its /// SccData. - pub all_successors: Vec, + all_successors: Vec, } impl Sccs { @@ -43,6 +43,14 @@ impl Sccs { SccsConstruction::construct(graph) } + pub fn scc_indices(&self) -> &IndexVec { + &self.scc_indices + } + + pub fn scc_data(&self) -> &SccData { + &self.scc_data + } + /// Returns the number of SCCs in the graph. pub fn num_sccs(&self) -> usize { self.scc_data.len() @@ -115,6 +123,14 @@ impl SccData { self.ranges.len() } + pub fn ranges(&self) -> &IndexVec> { + &self.ranges + } + + pub fn all_successors(&self) -> &Vec { + &self.all_successors + } + /// Returns the successors of the given SCC. fn successors(&self, scc: S) -> &[S] { // Annoyingly, `range` does not implement `Copy`, so we have diff --git a/compiler/rustc_middle/src/ty/sty.rs b/compiler/rustc_middle/src/ty/sty.rs index c5d6df5b1ad..35ff71d8885 100644 --- a/compiler/rustc_middle/src/ty/sty.rs +++ b/compiler/rustc_middle/src/ty/sty.rs @@ -1752,7 +1752,7 @@ impl<'tcx> Region<'tcx> { matches!(self.kind(), ty::ReVar(_)) } - pub fn try_get_var(self) -> Option { + pub fn as_var(self) -> Option { match self.kind() { ty::ReVar(vid) => Some(vid), _ => None,