Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions compiler/rustc_middle/src/mir/terminator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,8 @@ pub use helper::*;

mod helper {
use super::*;
// Note: the methods below use a `slice.chain(Option).chain(Option)` pattern so that all paths
// produce an iterator with the same concrete type.
pub type Successors<'a> = impl DoubleEndedIterator<Item = BasicBlock> + 'a;

impl SwitchTargets {
Expand All @@ -510,7 +512,7 @@ mod helper {
#[define_opaque(Successors)]
pub fn successors_for_value(&self, value: u128) -> Successors<'_> {
let target = self.target_for_value(value);
(&[]).into_iter().copied().chain(Some(target).into_iter().chain(None))
(&[]).into_iter().copied().chain(Some(target)).chain(None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the iterator type changed here, something like Chain<Copied<slice::Iter>, Chain<OptIter, OptIter>> to Chain<Chain<Copied<slice::Iter>, OptIter>, OptIter>.

This slice is empty anyway, but for the rest of the methods returning the same type, it probably hurts access to the slice items to go through two Chain layers.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the original explicit into_iter() could still be avoided with std::iter::chain, plus iter() for slices:

Suggested change
(&[]).into_iter().copied().chain(Some(target)).chain(None)
[].iter().copied().chain(chain(Some(target), None))

}
}

Expand All @@ -522,10 +524,7 @@ mod helper {
match *self {
// 3-successors for async drop: target, unwind, dropline (parent coroutine drop)
Drop { target: ref t, unwind: UnwindAction::Cleanup(u), drop: Some(d), .. } => {
slice::from_ref(t)
.into_iter()
.copied()
.chain(Some(u).into_iter().chain(Some(d)))
slice::from_ref(t).into_iter().copied().chain(Some(u)).chain(Some(d))
}
// 2-successors
Call { target: Some(ref t), unwind: UnwindAction::Cleanup(u), .. }
Expand All @@ -534,7 +533,7 @@ mod helper {
| Drop { target: ref t, unwind: _, drop: Some(u), .. }
| Assert { target: ref t, unwind: UnwindAction::Cleanup(u), .. }
| FalseUnwind { real_target: ref t, unwind: UnwindAction::Cleanup(u) } => {
slice::from_ref(t).into_iter().copied().chain(Some(u).into_iter().chain(None))
slice::from_ref(t).into_iter().copied().chain(Some(u)).chain(None)
}
// single successor
Goto { target: ref t }
Expand All @@ -544,7 +543,7 @@ mod helper {
| Drop { target: ref t, unwind: _, .. }
| Assert { target: ref t, unwind: _, .. }
| FalseUnwind { real_target: ref t, unwind: _ } => {
slice::from_ref(t).into_iter().copied().chain(None.into_iter().chain(None))
slice::from_ref(t).into_iter().copied().chain(None).chain(None)
}
// No successors
UnwindResume
Expand All @@ -554,23 +553,24 @@ mod helper {
| Unreachable
| TailCall { .. }
| Call { target: None, unwind: _, .. } => {
(&[]).into_iter().copied().chain(None.into_iter().chain(None))
(&[]).into_iter().copied().chain(None).chain(None)
}
// Multiple successors
InlineAsm { ref targets, unwind: UnwindAction::Cleanup(u), .. } => {
targets.iter().copied().chain(Some(u).into_iter().chain(None))
targets.iter().copied().chain(Some(u)).chain(None)
}
InlineAsm { ref targets, unwind: _, .. } => {
targets.iter().copied().chain(None.into_iter().chain(None))
targets.iter().copied().chain(None).chain(None)
}
SwitchInt { ref targets, .. } => {
targets.targets.iter().copied().chain(None.into_iter().chain(None))
targets.targets.iter().copied().chain(None).chain(None)
}
// FalseEdge
FalseEdge { ref real_target, imaginary_target } => slice::from_ref(real_target)
.into_iter()
.copied()
.chain(Some(imaginary_target).into_iter().chain(None)),
.chain(Some(imaginary_target))
.chain(None),
}
}

Expand Down
Loading