- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
rustc: remove Res::Upvar. #61276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    rustc: remove Res::Upvar. #61276
Changes from all commits
      Commits
    
    
            Show all changes
          
          
            11 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      26e61dd
              
                rustc: replace Res in hir::Upvar with only Local/Upvar data.
              
              
                eddyb 4b9670a
              
                rustc: remove the index field from Res::Upvar.
              
              
                eddyb 9fe0052
              
                rustc: remove the closure ID from hir::Upvar's parent field.
              
              
                eddyb 961fe54
              
                rustc: use indexmap instead of a plain vector for upvars.
              
              
                eddyb 1768030
              
                rustc: track the body owner DefId in MC and EUV.
              
              
                eddyb b13d040
              
                rustc: remove closure ID from Res::Upvar.
              
              
                eddyb a0ca2a2
              
                rustc: track the body owner in liveness.
              
              
                eddyb 340b91e
              
                rustc: remove `has_parent` from `hir::Upvar`.
              
              
                eddyb ed1bbbb
              
                rustc: remove Res::Upvar.
              
              
                eddyb 648b4d8
              
                rustc_resolve: never push `ClosureRibKind` to `label_ribs`.
              
              
                eddyb f7a4c9d
              
                rustc: collect upvars from HIR, instead of during name resolution.
              
              
                eddyb File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| //! Upvar (closure capture) collection from cross-body HIR uses of `Res::Local`s. | ||
|  | ||
| use crate::hir::{self, HirId}; | ||
| use crate::hir::def::Res; | ||
| use crate::hir::intravisit::{self, Visitor, NestedVisitorMap}; | ||
| use crate::ty::TyCtxt; | ||
| use crate::ty::query::Providers; | ||
| use syntax_pos::Span; | ||
| use rustc_data_structures::fx::{FxIndexMap, FxHashSet}; | ||
|  | ||
| pub fn provide(providers: &mut Providers<'_>) { | ||
| providers.upvars = |tcx, def_id| { | ||
| if !tcx.is_closure(def_id) { | ||
| return None; | ||
| } | ||
|  | ||
| let node_id = tcx.hir().as_local_node_id(def_id).unwrap(); | ||
| let body = tcx.hir().body(tcx.hir().maybe_body_owned_by(node_id)?); | ||
|  | ||
| let mut local_collector = LocalCollector::default(); | ||
| local_collector.visit_body(body); | ||
|  | ||
| let mut capture_collector = CaptureCollector { | ||
| tcx, | ||
| locals: &local_collector.locals, | ||
| upvars: FxIndexMap::default(), | ||
| }; | ||
| capture_collector.visit_body(body); | ||
|  | ||
| if !capture_collector.upvars.is_empty() { | ||
| Some(tcx.arena.alloc(capture_collector.upvars)) | ||
| } else { | ||
| None | ||
| } | ||
| }; | ||
| } | ||
|  | ||
| #[derive(Default)] | ||
| struct LocalCollector { | ||
| // FIXME(eddyb) perhaps use `ItemLocalId` instead? | ||
| locals: FxHashSet<HirId>, | ||
| } | ||
|  | ||
| impl Visitor<'tcx> for LocalCollector { | ||
| fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> { | ||
| NestedVisitorMap::None | ||
| } | ||
|  | ||
| fn visit_pat(&mut self, pat: &'tcx hir::Pat) { | ||
| if let hir::PatKind::Binding(_, hir_id, ..) = pat.node { | ||
| self.locals.insert(hir_id); | ||
| } | ||
| intravisit::walk_pat(self, pat); | ||
| } | ||
| } | ||
|  | ||
| struct CaptureCollector<'a, 'tcx> { | ||
| tcx: TyCtxt<'a, 'tcx, 'tcx>, | ||
| locals: &'a FxHashSet<HirId>, | ||
| upvars: FxIndexMap<HirId, hir::Upvar>, | ||
| } | ||
|  | ||
| impl CaptureCollector<'_, '_> { | ||
| fn visit_local_use(&mut self, var_id: HirId, span: Span) { | ||
| if !self.locals.contains(&var_id) { | ||
| self.upvars.entry(var_id).or_insert(hir::Upvar { span }); | ||
| } | ||
| } | ||
| } | ||
|  | ||
| impl Visitor<'tcx> for CaptureCollector<'a, 'tcx> { | ||
| fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> { | ||
| NestedVisitorMap::None | ||
| } | ||
|  | ||
| fn visit_path(&mut self, path: &'tcx hir::Path, _: hir::HirId) { | ||
| if let Res::Local(var_id) = path.res { | ||
| self.visit_local_use(var_id, path.span); | ||
| } | ||
|  | ||
| intravisit::walk_path(self, path); | ||
| } | ||
|  | ||
| fn visit_expr(&mut self, expr: &'tcx hir::Expr) { | ||
| if let hir::ExprKind::Closure(..) = expr.node { | ||
| let closure_def_id = self.tcx.hir().local_def_id_from_hir_id(expr.hir_id); | ||
| if let Some(upvars) = self.tcx.upvars(closure_def_id) { | ||
| // Every capture of a closure expression is a local in scope, | ||
| // that is moved/copied/borrowed into the closure value, and | ||
| // for this analysis they are like any other access to a local. | ||
| // | ||
| // E.g. in `|b| |c| (a, b, c)`, the upvars of the inner closure | ||
| // are `a` and `b`, and while `a` is not directly used in the | ||
| // outer closure, it needs to be an upvar there too, so that | ||
| // the inner closure can take it (from the outer closure's env). | ||
| for (&var_id, upvar) in upvars { | ||
| self.visit_local_use(var_id, upvar.span); | ||
|         
                  eddyb marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| } | ||
| } | ||
| } | ||
|  | ||
| intravisit::walk_expr(self, expr); | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this data structure have any "classic" name?
Search by "IndexMap" only leads to the Rust version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't really know (for the record, this crate used to be called
ordermap). cc @bluss