@@ -11,7 +11,7 @@ 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;
@@ -221,27 +221,28 @@ pub struct ScopeTree {
221221 /// variable is declared.
222222 var_map : FxIndexMap < hir:: ItemLocalId , Scope > ,
223223
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 > ,
224+ /// Tracks expressions with extended temporary scopes, based on the syntactic rules for
225+ /// temporary lifetime extension. Further details may be found in
226+ /// `rustc_hir_analysis::check::region` and in the [Reference].
227+ ///
228+ /// [Reference]: https://doc.rust-lang.org/nightly/reference/destructors.html#temporary-lifetime-extension
229+ extended_temp_scopes : ItemLocalMap < Option < Scope > > ,
230230
231231 /// Backwards incompatible scoping that will be introduced in future editions.
232232 /// This information is used later for linting to identify locals and
233233 /// temporary values that will receive backwards-incompatible drop orders.
234234 pub backwards_incompatible_scope : UnordMap < hir:: ItemLocalId , Scope > ,
235235}
236236
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 > ,
237+ /// Temporary lifetime information for expressions, used when lowering to MIR.
238+ #[ derive( Clone , Copy , Debug , HashStable ) ]
239+ pub struct TempLifetime {
240+ /// The scope in which a temporary should be dropped. If `None`, no drop is scheduled; this is
241+ /// the case for lifetime-extended temporaries extended by a const/static item or const block.
242+ pub temp_lifetime : Option < Scope > ,
243+ /// If `Some(lt)`, indicates that the lifetime of this temporary will change to `lt` in a future edition.
244+ /// If `None`, then no changes are expected, or lints are disabled.
245+ pub backwards_incompatible : Option < Scope > ,
245246}
246247
247248impl ScopeTree {
@@ -260,12 +261,13 @@ impl ScopeTree {
260261 self . var_map . insert ( var, lifetime) ;
261262 }
262263
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)
264+ /// Make an association between a sub-expression and an extended lifetime
265+ pub fn record_extended_temp_scope ( & mut self , var : hir:: ItemLocalId , lifetime : Option < Scope > ) {
266+ debug ! ( ?var, ?lifetime) ;
267+ if let Some ( lifetime) = lifetime {
268+ assert ! ( var != lifetime. local_id) ;
267269 }
268- self . rvalue_candidates . insert ( var, candidate ) ;
270+ self . extended_temp_scopes . insert ( var, lifetime ) ;
269271 }
270272
271273 /// Returns the narrowest scope that encloses `id`, if any.
@@ -337,4 +339,20 @@ impl ScopeTree {
337339
338340 span_bug ! ( ty:: tls:: with( |tcx| inner. span( tcx, self ) ) , "no enclosing temporary scope" )
339341 }
342+
343+ /// Returns the scope when the temp created by `expr_id` will be cleaned up.
344+ /// It also emits a lint on potential backwards incompatible change to the temporary scope
345+ /// which is *for now* always shortening.
346+ pub fn temporary_scope ( & self , expr_id : hir:: ItemLocalId ) -> TempLifetime {
347+ // Check for a designated extended temporary scope.
348+ if let Some ( & s) = self . extended_temp_scopes . get ( & expr_id) {
349+ debug ! ( "temporary_scope({expr_id:?}) = {s:?} [custom]" ) ;
350+ return TempLifetime { temp_lifetime : s, backwards_incompatible : None } ;
351+ }
352+
353+ // Otherwise, locate the innermost terminating scope.
354+ let ( scope, backwards_incompatible) =
355+ self . default_temporary_scope ( Scope { local_id : expr_id, data : ScopeData :: Node } ) ;
356+ TempLifetime { temp_lifetime : Some ( scope) , backwards_incompatible }
357+ }
340358}
0 commit comments