@@ -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,29 +221,19 @@ 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 > ,
245- }
246-
247237impl ScopeTree {
248238 pub fn record_scope_parent ( & mut self , child : Scope , parent : Option < Scope > ) {
249239 debug ! ( "{:?}.parent = {:?}" , child, parent) ;
@@ -260,12 +250,13 @@ impl ScopeTree {
260250 self . var_map . insert ( var, lifetime) ;
261251 }
262252
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)
253+ /// Make an association between a sub-expression and an extended lifetime
254+ pub fn record_extended_temp_scope ( & mut self , var : hir:: ItemLocalId , lifetime : Option < Scope > ) {
255+ debug ! ( ?var, ?lifetime) ;
256+ if let Some ( lifetime) = lifetime {
257+ assert ! ( var != lifetime. local_id) ;
267258 }
268- self . rvalue_candidates . insert ( var, candidate ) ;
259+ self . extended_temp_scopes . insert ( var, 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 ) -> ( Option < Scope > , Option < Scope > ) {
336+ // Check for a designated extended temporary scope.
337+ if let Some ( & s) = self . extended_temp_scopes . get ( & expr_id) {
338+ debug ! ( "temporary_scope({expr_id:?}) = {s:?} [custom]" ) ;
339+ return ( s, None ) ;
340+ }
341+
342+ // Otherwise, locate the innermost terminating scope.
343+ let ( scope, backward_incompatible) =
344+ self . default_temporary_scope ( Scope { local_id : expr_id, data : ScopeData :: Node } ) ;
345+ ( Some ( scope) , backward_incompatible)
346+ }
340347}
0 commit comments