-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
collector: recursively traverse 'mentioned' items to evaluate their c…
…onstants
- Loading branch information
Showing
17 changed files
with
343 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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,56 @@ | ||
use rustc_middle::mir::visit::Visitor; | ||
use rustc_middle::mir::{self, ConstOperand, Location, MentionedItem, MirPass}; | ||
use rustc_middle::ty::{self, 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 { | ||
// We need to do always do that as the collector will only check the 'mentioned' items of | ||
// 'mentioned' items, so if some item X gets compiled without optimizations but later called | ||
// from somewhere that has optimizations enabled, we need the `mentioned_items` of X to be | ||
// filled. | ||
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; | ||
} | ||
} | ||
|
||
impl<'tcx> Visitor<'tcx> for MentionedItemsVisitor<'_, 'tcx> { | ||
fn visit_constant(&mut self, constant: &ConstOperand<'tcx>, _: Location) { | ||
let const_ = constant.const_; | ||
// This is how function items get referenced: via constants of `FnDef` type. | ||
if let ty::FnDef(def_id, args) = const_.ty().kind() { | ||
debug!("adding to required_items: {def_id:?}"); | ||
self.mentioned_items | ||
.push(Spanned { node: MentionedItem::Fn(*def_id, args), span: constant.span }); | ||
} | ||
} | ||
|
||
fn visit_terminator(&mut self, terminator: &mir::Terminator<'tcx>, location: Location) { | ||
self.super_terminator(terminator, location); | ||
match terminator.kind { | ||
// We don't need to handle `Call` as we already handled all function type operands in | ||
// `visit_constant`. But we do need to handle `Drop`. | ||
mir::TerminatorKind::Drop { place, .. } => { | ||
let ty = place.ty(self.body, self.tcx).ty; | ||
let span = self.body.source_info(location).span; | ||
self.mentioned_items.push(Spanned { node: MentionedItem::Drop(ty), span }); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} |
This file contains 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.