Skip to content

Commit b5fdbbe

Browse files
Use correct names for things in ReplaceBodyWithLoops
1 parent d684855 commit b5fdbbe

File tree

1 file changed

+41
-89
lines changed

1 file changed

+41
-89
lines changed

src/librustc_interface/util.rs

+41-89
Original file line numberDiff line numberDiff line change
@@ -592,97 +592,48 @@ pub fn build_output_filenames(
592592
//
593593
// [#34511]: https://github.com/rust-lang/rust/issues/34511#issuecomment-322340401
594594
pub struct ReplaceBodyWithLoop<'a, 'b> {
595-
within_static_or_const: bool,
595+
ignore_item: bool,
596596
nested_blocks: Option<Vec<ast::Block>>,
597597
resolver: &'a mut Resolver<'b>,
598598
}
599599

600600
impl<'a, 'b> ReplaceBodyWithLoop<'a, 'b> {
601601
pub fn new(resolver: &'a mut Resolver<'b>) -> ReplaceBodyWithLoop<'a, 'b> {
602-
ReplaceBodyWithLoop { within_static_or_const: false, nested_blocks: None, resolver }
602+
ReplaceBodyWithLoop { ignore_item: false, nested_blocks: None, resolver }
603603
}
604604

605-
fn run<R, F: FnOnce(&mut Self) -> R>(&mut self, is_const: bool, action: F) -> R {
606-
let old_const = mem::replace(&mut self.within_static_or_const, is_const);
605+
fn run<R, F: FnOnce(&mut Self) -> R>(&mut self, ignore_item: bool, action: F) -> R {
606+
let old_ignore_item = mem::replace(&mut self.ignore_item, ignore_item);
607607
let old_blocks = self.nested_blocks.take();
608608
let ret = action(self);
609-
self.within_static_or_const = old_const;
609+
self.ignore_item = old_ignore_item;
610610
self.nested_blocks = old_blocks;
611611
ret
612612
}
613613

614-
fn should_ignore_fn(ret_ty: &ast::FnRetTy) -> bool {
615-
if let ast::FnRetTy::Ty(ref ty) = ret_ty {
616-
fn involves_impl_trait(ty: &ast::Ty) -> bool {
617-
match ty.kind {
618-
ast::TyKind::ImplTrait(..) => true,
619-
ast::TyKind::Slice(ref subty)
620-
| ast::TyKind::Array(ref subty, _)
621-
| ast::TyKind::Ptr(ast::MutTy { ty: ref subty, .. })
622-
| ast::TyKind::Rptr(_, ast::MutTy { ty: ref subty, .. })
623-
| ast::TyKind::Paren(ref subty) => involves_impl_trait(subty),
624-
ast::TyKind::Tup(ref tys) => any_involves_impl_trait(tys.iter()),
625-
ast::TyKind::Path(_, ref path) => {
626-
path.segments.iter().any(|seg| match seg.args.as_deref() {
627-
None => false,
628-
Some(&ast::GenericArgs::AngleBracketed(ref data)) => {
629-
data.args.iter().any(|arg| match arg {
630-
ast::AngleBracketedArg::Arg(arg) => match arg {
631-
ast::GenericArg::Type(ty) => involves_impl_trait(ty),
632-
ast::GenericArg::Lifetime(_)
633-
| ast::GenericArg::Const(_) => false,
634-
},
635-
ast::AngleBracketedArg::Constraint(c) => match c.kind {
636-
ast::AssocTyConstraintKind::Bound { .. } => true,
637-
ast::AssocTyConstraintKind::Equality { ref ty } => {
638-
involves_impl_trait(ty)
639-
}
640-
},
641-
})
642-
}
643-
Some(&ast::GenericArgs::Parenthesized(ref data)) => {
644-
any_involves_impl_trait(data.inputs.iter())
645-
|| ReplaceBodyWithLoop::should_ignore_fn(&data.output)
646-
}
647-
})
648-
}
649-
_ => false,
650-
}
651-
}
652-
653-
fn any_involves_impl_trait<'a, I: Iterator<Item = &'a P<ast::Ty>>>(mut it: I) -> bool {
654-
it.any(|subty| involves_impl_trait(subty))
655-
}
656-
657-
involves_impl_trait(ty)
658-
} else {
659-
false
660-
}
661-
}
662-
663-
fn is_sig_const(sig: &ast::FnSig) -> bool {
614+
fn should_ignore_fn(sig: &ast::FnSig) -> bool {
664615
matches!(sig.header.constness, ast::Const::Yes(_))
665-
|| ReplaceBodyWithLoop::should_ignore_fn(&sig.decl.output)
616+
|| matches!(&sig.decl.output, ast::FnRetTy::Ty(ty) if ty.contains_impl_trait())
666617
}
667618
}
668619

669620
impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
670621
fn visit_item_kind(&mut self, i: &mut ast::ItemKind) {
671-
let is_const = match i {
622+
let ignore_item = match i {
672623
ast::ItemKind::Static(..) | ast::ItemKind::Const(..) => true,
673-
ast::ItemKind::Fn(_, ref sig, _, _) => Self::is_sig_const(sig),
624+
ast::ItemKind::Fn(_, ref sig, _, _) => Self::should_ignore_fn(sig),
674625
_ => false,
675626
};
676-
self.run(is_const, |s| noop_visit_item_kind(i, s))
627+
self.run(ignore_item, |s| noop_visit_item_kind(i, s))
677628
}
678629

679630
fn flat_map_trait_item(&mut self, i: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
680-
let is_const = match i.kind {
631+
let ignore_item = match i.kind {
681632
ast::AssocItemKind::Const(..) => true,
682-
ast::AssocItemKind::Fn(_, ref sig, _, _) => Self::is_sig_const(sig),
633+
ast::AssocItemKind::Fn(_, ref sig, _, _) => Self::should_ignore_fn(sig),
683634
_ => false,
684635
};
685-
self.run(is_const, |s| noop_flat_map_assoc_item(i, s))
636+
self.run(ignore_item, |s| noop_flat_map_assoc_item(i, s))
686637
}
687638

688639
fn flat_map_impl_item(&mut self, i: P<ast::AssocItem>) -> SmallVec<[P<ast::AssocItem>; 1]> {
@@ -723,6 +674,11 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
723674
}
724675
}
725676

677+
if self.ignore_item {
678+
noop_visit_block(b, self);
679+
return;
680+
}
681+
726682
let empty_block = stmt_to_block(BlockCheckMode::Default, None, self.resolver);
727683
let loop_expr = P(ast::Expr {
728684
kind: ast::ExprKind::Loop(P(empty_block), None),
@@ -738,39 +694,35 @@ impl<'a> MutVisitor for ReplaceBodyWithLoop<'a, '_> {
738694
kind: ast::StmtKind::Expr(loop_expr),
739695
};
740696

741-
if self.within_static_or_const {
742-
noop_visit_block(b, self)
743-
} else {
744-
visit_clobber(b.deref_mut(), |b| {
745-
let mut stmts = vec![];
746-
for s in b.stmts {
747-
let old_blocks = self.nested_blocks.replace(vec![]);
697+
visit_clobber(b.deref_mut(), |b| {
698+
let mut stmts = vec![];
699+
for s in b.stmts {
700+
let old_blocks = self.nested_blocks.replace(vec![]);
748701

749-
stmts.extend(self.flat_map_stmt(s).into_iter().filter(|s| s.is_item()));
702+
stmts.extend(self.flat_map_stmt(s).into_iter().filter(|s| s.is_item()));
750703

751-
// we put a Some in there earlier with that replace(), so this is valid
752-
let new_blocks = self.nested_blocks.take().unwrap();
753-
self.nested_blocks = old_blocks;
754-
stmts.extend(new_blocks.into_iter().map(|b| block_to_stmt(b, self.resolver)));
755-
}
704+
// we put a Some in there earlier with that replace(), so this is valid
705+
let new_blocks = self.nested_blocks.take().unwrap();
706+
self.nested_blocks = old_blocks;
707+
stmts.extend(new_blocks.into_iter().map(|b| block_to_stmt(b, self.resolver)));
708+
}
756709

757-
let mut new_block = ast::Block { stmts, ..b };
710+
let mut new_block = ast::Block { stmts, ..b };
758711

759-
if let Some(old_blocks) = self.nested_blocks.as_mut() {
760-
//push our fresh block onto the cache and yield an empty block with `loop {}`
761-
if !new_block.stmts.is_empty() {
762-
old_blocks.push(new_block);
763-
}
712+
if let Some(old_blocks) = self.nested_blocks.as_mut() {
713+
//push our fresh block onto the cache and yield an empty block with `loop {}`
714+
if !new_block.stmts.is_empty() {
715+
old_blocks.push(new_block);
716+
}
764717

765-
stmt_to_block(b.rules, Some(loop_stmt), &mut self.resolver)
766-
} else {
767-
//push `loop {}` onto the end of our fresh block and yield that
768-
new_block.stmts.push(loop_stmt);
718+
stmt_to_block(b.rules, Some(loop_stmt), &mut self.resolver)
719+
} else {
720+
//push `loop {}` onto the end of our fresh block and yield that
721+
new_block.stmts.push(loop_stmt);
769722

770-
new_block
771-
}
772-
})
773-
}
723+
new_block
724+
}
725+
})
774726
}
775727

776728
// in general the pretty printer processes unexpanded code, so

0 commit comments

Comments
 (0)