@@ -39,6 +39,7 @@ use rustc_span::Span;
3939
4040use std:: cell:: { Cell , RefCell } ;
4141use std:: fmt;
42+ use std:: ops:: Drop ;
4243
4344use self :: combine:: CombineFields ;
4445use self :: error_reporting:: TypeErrCtxt ;
@@ -342,6 +343,11 @@ pub struct InferCtxt<'tcx> {
342343 /// there is no type that the user could *actually name* that
343344 /// would satisfy it. This avoids crippling inference, basically.
344345 pub intercrate : bool ,
346+
347+ /// Flag that is set when we enter canonicalization. Used for debugging to ensure
348+ /// that we only collect region information for `BorrowckInferCtxt::reg_var_to_origin`
349+ /// inside non-canonicalization contexts.
350+ inside_canonicalization_ctxt : Cell < bool > ,
345351}
346352
347353/// See the `error_reporting` module for more details.
@@ -633,6 +639,7 @@ impl<'tcx> InferCtxtBuilder<'tcx> {
633639 skip_leak_check : Cell :: new ( false ) ,
634640 universe : Cell :: new ( ty:: UniverseIndex :: ROOT ) ,
635641 intercrate,
642+ inside_canonicalization_ctxt : Cell :: new ( false ) ,
636643 }
637644 }
638645}
@@ -1728,6 +1735,31 @@ impl<'tcx> InferCtxt<'tcx> {
17281735 }
17291736 }
17301737 }
1738+
1739+ pub fn inside_canonicalization_ctxt ( & self ) -> bool {
1740+ self . inside_canonicalization_ctxt . get ( )
1741+ }
1742+
1743+ pub fn set_canonicalization_ctxt ( & self ) -> CanonicalizationCtxtGuard < ' _ , ' tcx > {
1744+ let prev_ctxt = self . inside_canonicalization_ctxt ( ) ;
1745+ self . inside_canonicalization_ctxt . set ( true ) ;
1746+ CanonicalizationCtxtGuard { prev_ctxt, infcx : self }
1747+ }
1748+
1749+ fn set_canonicalization_ctxt_to ( & self , ctxt : bool ) {
1750+ self . inside_canonicalization_ctxt . set ( ctxt) ;
1751+ }
1752+ }
1753+
1754+ pub struct CanonicalizationCtxtGuard < ' cx , ' tcx > {
1755+ prev_ctxt : bool ,
1756+ infcx : & ' cx InferCtxt < ' tcx > ,
1757+ }
1758+
1759+ impl < ' cx , ' tcx > Drop for CanonicalizationCtxtGuard < ' cx , ' tcx > {
1760+ fn drop ( & mut self ) {
1761+ self . infcx . set_canonicalization_ctxt_to ( self . prev_ctxt )
1762+ }
17311763}
17321764
17331765impl < ' tcx > TypeErrCtxt < ' _ , ' tcx > {
0 commit comments