Skip to content

Commit 9c910ae

Browse files
authored
Rollup merge of #129167 - cuviper:either-once-empty, r=Nadrieril
mir/pretty: use `Option` instead of `Either<Once, Empty>` `Either` is wasteful for a one-or-none iterator, especially since `Once` is already an `option::IntoIter` internally. We don't really need any of the iterator mechanisms in this case, just a single conditional insert.
2 parents cfeded4 + 29017e4 commit 9c910ae

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

compiler/rustc_middle/src/mir/pretty.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -1418,21 +1418,19 @@ pub fn write_allocations<'tcx>(
14181418
alloc.inner().provenance().ptrs().values().map(|p| p.alloc_id())
14191419
}
14201420

1421-
fn alloc_ids_from_const_val(val: ConstValue<'_>) -> impl Iterator<Item = AllocId> + '_ {
1421+
fn alloc_id_from_const_val(val: ConstValue<'_>) -> Option<AllocId> {
14221422
match val {
1423-
ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _)) => {
1424-
Either::Left(std::iter::once(ptr.provenance.alloc_id()))
1425-
}
1426-
ConstValue::Scalar(interpret::Scalar::Int { .. }) => Either::Right(std::iter::empty()),
1427-
ConstValue::ZeroSized => Either::Right(std::iter::empty()),
1423+
ConstValue::Scalar(interpret::Scalar::Ptr(ptr, _)) => Some(ptr.provenance.alloc_id()),
1424+
ConstValue::Scalar(interpret::Scalar::Int { .. }) => None,
1425+
ConstValue::ZeroSized => None,
14281426
ConstValue::Slice { .. } => {
14291427
// `u8`/`str` slices, shouldn't contain pointers that we want to print.
1430-
Either::Right(std::iter::empty())
1428+
None
14311429
}
14321430
ConstValue::Indirect { alloc_id, .. } => {
14331431
// FIXME: we don't actually want to print all of these, since some are printed nicely directly as values inline in MIR.
14341432
// Really we'd want `pretty_print_const_value` to decide which allocations to print, instead of having a separate visitor.
1435-
Either::Left(std::iter::once(alloc_id))
1433+
Some(alloc_id)
14361434
}
14371435
}
14381436
}
@@ -1443,7 +1441,9 @@ pub fn write_allocations<'tcx>(
14431441
match c.const_ {
14441442
Const::Ty(_, _) | Const::Unevaluated(..) => {}
14451443
Const::Val(val, _) => {
1446-
self.0.extend(alloc_ids_from_const_val(val));
1444+
if let Some(id) = alloc_id_from_const_val(val) {
1445+
self.0.insert(id);
1446+
}
14471447
}
14481448
}
14491449
}

0 commit comments

Comments
 (0)