@@ -11,11 +11,12 @@ use std::fmt;
1111use rustc_data_structures:: fx:: FxIndexMap ;
1212use rustc_data_structures:: unord:: UnordMap ;
1313use rustc_hir as hir;
14- use rustc_hir:: { HirId , HirIdMap , Node } ;
14+ use rustc_hir:: { HirId , ItemLocalMap , Node } ;
1515use rustc_macros:: { HashStable , TyDecodable , TyEncodable } ;
1616use rustc_span:: { DUMMY_SP , Span } ;
1717use tracing:: debug;
1818
19+ use crate :: thir:: TempLifetime ;
1920use crate :: ty:: { self , TyCtxt } ;
2021
2122/// Represents a statically-describable scope that can be used to
@@ -221,29 +222,17 @@ pub struct ScopeTree {
221222 /// variable is declared.
222223 var_map : FxIndexMap < hir:: ItemLocalId , Scope > ,
223224
224- /// Identifies expressions which, if captured into a temporary, ought to
225- /// have a temporary whose lifetime extends to the end of the enclosing *block*,
226- /// and not the enclosing *statement*. Expressions that are not present in this
227- /// table are not rvalue candidates. The set of rvalue candidates is computed
228- /// during type check based on a traversal of the AST.
229- pub rvalue_candidates : HirIdMap < RvalueCandidate > ,
225+ /// Tracks the rvalue scoping rules which defines finer scoping for rvalue expressions
226+ /// by applying extended parameter rules.
227+ /// Further details may be found in `rustc_hir_analysis::check::region`.
228+ rvalue_scopes : ItemLocalMap < TempLifetime > ,
230229
231230 /// Backwards incompatible scoping that will be introduced in future editions.
232231 /// This information is used later for linting to identify locals and
233232 /// temporary values that will receive backwards-incompatible drop orders.
234233 pub backwards_incompatible_scope : UnordMap < hir:: ItemLocalId , Scope > ,
235234}
236235
237- /// See the `rvalue_candidates` field for more information on rvalue
238- /// candidates in general.
239- /// The `lifetime` field is None to indicate that certain expressions escape
240- /// into 'static and should have no local cleanup scope.
241- #[ derive( Debug , Copy , Clone , HashStable ) ]
242- pub struct RvalueCandidate {
243- pub target : hir:: ItemLocalId ,
244- pub lifetime : Option < Scope > ,
245- }
246-
247236impl ScopeTree {
248237 pub fn record_scope_parent ( & mut self , child : Scope , parent : Option < Scope > ) {
249238 debug ! ( "{:?}.parent = {:?}" , child, parent) ;
@@ -260,12 +249,14 @@ impl ScopeTree {
260249 self . var_map . insert ( var, lifetime) ;
261250 }
262251
263- pub fn record_rvalue_candidate ( & mut self , var : HirId , candidate : RvalueCandidate ) {
264- debug ! ( "record_rvalue_candidate(var={var:?}, candidate={candidate:?})" ) ;
265- if let Some ( lifetime) = & candidate. lifetime {
266- assert ! ( var. local_id != lifetime. local_id)
252+ /// Make an association between a sub-expression and an extended lifetime
253+ pub fn record_rvalue_scope ( & mut self , var : hir:: ItemLocalId , lifetime : TempLifetime ) {
254+ debug ! ( "record_rvalue_scope(var={var:?}, lifetime={lifetime:?})" ) ;
255+ if let Some ( lifetime) = lifetime. temp_lifetime {
256+ assert ! ( var != lifetime. local_id) ;
267257 }
268- self . rvalue_candidates . insert ( var, candidate) ;
258+ let old_lifetime = self . rvalue_scopes . insert ( var, lifetime) ;
259+ assert ! ( old_lifetime. is_none_or( |old| old == lifetime) ) ;
269260 }
270261
271262 /// Returns the narrowest scope that encloses `id`, if any.
@@ -337,4 +328,20 @@ impl ScopeTree {
337328
338329 span_bug ! ( ty:: tls:: with( |tcx| inner. span( tcx, self ) ) , "no enclosing temporary scope" )
339330 }
331+
332+ /// Returns the scope when the temp created by `expr_id` will be cleaned up.
333+ /// It also emits a lint on potential backwards incompatible change to the temporary scope
334+ /// which is *for now* always shortening.
335+ pub fn temporary_scope ( & self , expr_id : hir:: ItemLocalId ) -> TempLifetime {
336+ // Check for a designated rvalue scope.
337+ if let Some ( & s) = self . rvalue_scopes . get ( & expr_id) {
338+ debug ! ( "temporary_scope({expr_id:?}) = {s:?} [custom]" ) ;
339+ return s;
340+ }
341+
342+ // Otherwise, locate the innermost terminating scope.
343+ let ( scope, backwards_incompatible) =
344+ self . default_temporary_scope ( Scope { local_id : expr_id, data : ScopeData :: Node } ) ;
345+ TempLifetime { temp_lifetime : Some ( scope) , backwards_incompatible }
346+ }
340347}
0 commit comments