- 
                Notifications
    You must be signed in to change notification settings 
- Fork 13.9k
recursively evaluate the constants in everything that is 'mentioned' #122568
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
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            10 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      712fe36
              
                collector: recursively traverse 'mentioned' items to evaluate their c…
              
              
                RalfJung 91b35a1
              
                fix comments in required-consts tests
              
              
                RalfJung ee4b758
              
                avoid processing mentioned items that are also still used
              
              
                RalfJung 347ca50
              
                mentioned items: also handle vtables
              
              
                RalfJung f1ec494
              
                mentioned items: also handle closure-to-fn-ptr coercions
              
              
                RalfJung 0d6a16a
              
                mentioned_items: record all callee and coerced closure types, whether…
              
              
                RalfJung 682991d
              
                explicitly set opt-level=0
              
              
                RalfJung feeffae
              
                mentioned_items: avoid adding str/slice unsizing casts
              
              
                RalfJung 0cb1065
              
                collector: move functions around so that the 'root collection' sectio…
              
              
                RalfJung 8c01b85
              
                make sure we don't inline these generic fn as that could monomorphize…
              
              
                RalfJung 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,117 @@ | ||
| use rustc_middle::mir::visit::Visitor; | ||
| use rustc_middle::mir::{self, Location, MentionedItem, MirPass}; | ||
| use rustc_middle::ty::{self, adjustment::PointerCoercion, TyCtxt}; | ||
| use rustc_session::Session; | ||
| use rustc_span::source_map::Spanned; | ||
|  | ||
| pub struct MentionedItems; | ||
|  | ||
| struct MentionedItemsVisitor<'a, 'tcx> { | ||
| tcx: TyCtxt<'tcx>, | ||
| body: &'a mir::Body<'tcx>, | ||
| mentioned_items: &'a mut Vec<Spanned<MentionedItem<'tcx>>>, | ||
| } | ||
|  | ||
| impl<'tcx> MirPass<'tcx> for MentionedItems { | ||
| fn is_enabled(&self, _sess: &Session) -> bool { | ||
| // If this pass is skipped the collector assume that nothing got mentioned! We could | ||
| // potentially skip it in opt-level 0 if we are sure that opt-level will never *remove* uses | ||
| // of anything, but that still seems fragile. Furthermore, even debug builds use level 1, so | ||
| // special-casing level 0 is just not worth it. | ||
| true | ||
| } | ||
|  | ||
| fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut mir::Body<'tcx>) { | ||
| debug_assert!(body.mentioned_items.is_empty()); | ||
| let mut mentioned_items = Vec::new(); | ||
| MentionedItemsVisitor { tcx, body, mentioned_items: &mut mentioned_items }.visit_body(body); | ||
| body.mentioned_items = mentioned_items; | ||
| } | ||
| } | ||
|  | ||
| // This visitor is carefully in sync with the one in `rustc_monomorphize::collector`. We are | ||
| // visiting the exact same places but then instead of monomorphizing and creating `MonoItems`, we | ||
| // have to remain generic and just recording the relevant information in `mentioned_items`, where it | ||
| // will then be monomorphized later during "mentioned items" collection. | ||
| impl<'tcx> Visitor<'tcx> for MentionedItemsVisitor<'_, 'tcx> { | ||
| fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) { | ||
| self.super_terminator(terminator, location); | ||
| let span = || self.body.source_info(location).span; | ||
| match &terminator.kind { | ||
| mir::TerminatorKind::Call { func, .. } => { | ||
| let callee_ty = func.ty(self.body, self.tcx); | ||
| self.mentioned_items | ||
| .push(Spanned { node: MentionedItem::Fn(callee_ty), span: span() }); | ||
| } | ||
| mir::TerminatorKind::Drop { place, .. } => { | ||
| let ty = place.ty(self.body, self.tcx).ty; | ||
| self.mentioned_items.push(Spanned { node: MentionedItem::Drop(ty), span: span() }); | ||
| } | ||
| mir::TerminatorKind::InlineAsm { ref operands, .. } => { | ||
| for op in operands { | ||
| match *op { | ||
| mir::InlineAsmOperand::SymFn { ref value } => { | ||
| self.mentioned_items.push(Spanned { | ||
| node: MentionedItem::Fn(value.const_.ty()), | ||
| span: span(), | ||
| }); | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
|  | ||
| fn visit_rvalue(&mut self, rvalue: &mir::Rvalue<'tcx>, location: Location) { | ||
| self.super_rvalue(rvalue, location); | ||
| let span = || self.body.source_info(location).span; | ||
| match *rvalue { | ||
| // We need to detect unsizing casts that required vtables. | ||
| mir::Rvalue::Cast( | ||
| mir::CastKind::PointerCoercion(PointerCoercion::Unsize), | ||
| ref operand, | ||
| target_ty, | ||
| ) | ||
| | mir::Rvalue::Cast(mir::CastKind::DynStar, ref operand, target_ty) => { | ||
| // This isn't monomorphized yet so we can't tell what the actual types are -- just | ||
| // add everything that may involve a vtable. | ||
| let source_ty = operand.ty(self.body, self.tcx); | ||
| let may_involve_vtable = match ( | ||
| source_ty.builtin_deref(true).map(|t| t.ty.kind()), | ||
| target_ty.builtin_deref(true).map(|t| t.ty.kind()), | ||
| ) { | ||
| (Some(ty::Array(..)), Some(ty::Str | ty::Slice(..))) => false, // &str/&[T] unsizing | ||
| _ => true, | ||
| }; | ||
| if may_involve_vtable { | ||
| self.mentioned_items.push(Spanned { | ||
| node: MentionedItem::UnsizeCast { source_ty, target_ty }, | ||
| span: span(), | ||
| }); | ||
| } | ||
| } | ||
| // Similarly, record closures that are turned into function pointers. | ||
| mir::Rvalue::Cast( | ||
| mir::CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_)), | ||
| ref operand, | ||
| _, | ||
| ) => { | ||
| let source_ty = operand.ty(self.body, self.tcx); | ||
| self.mentioned_items | ||
| .push(Spanned { node: MentionedItem::Closure(source_ty), span: span() }); | ||
| } | ||
| // And finally, function pointer reification casts. | ||
| mir::Rvalue::Cast( | ||
| mir::CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer), | ||
| ref operand, | ||
| _, | ||
| ) => { | ||
| let fn_ty = operand.ty(self.body, self.tcx); | ||
| self.mentioned_items.push(Spanned { node: MentionedItem::Fn(fn_ty), span: span() }); | ||
| } | ||
| _ => {} | ||
| } | ||
| } | ||
| } | ||
  
    
      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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.