Skip to content

Commit 6b01844

Browse files
committed
Generalize initial "not const" assignments
1 parent b8e9da7 commit 6b01844

File tree

2 files changed

+23
-23
lines changed

2 files changed

+23
-23
lines changed

src/librustc_mir/transform/promote_consts.rs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ pub enum TempState {
4444
impl TempState {
4545
pub fn is_promotable(&self) -> bool {
4646
debug!("is_promotable: self={:?}", self);
47-
if let TempState::Defined { uses, .. } = *self {
48-
uses > 0
47+
if let TempState::Defined { .. } = *self {
48+
true
4949
} else {
5050
false
5151
}
@@ -80,9 +80,14 @@ impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
8080
context: PlaceContext<'tcx>,
8181
location: Location) {
8282
debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
83-
// We're only interested in temporaries
84-
if self.mir.local_kind(index) != LocalKind::Temp {
85-
return;
83+
// We're only interested in temporaries and the return place
84+
match self.mir.local_kind(index) {
85+
| LocalKind::Temp
86+
| LocalKind::ReturnPointer
87+
=> {},
88+
| LocalKind::Arg
89+
| LocalKind::Var
90+
=> return,
8691
}
8792

8893
// Ignore drops, if the temp gets promoted,

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -631,26 +631,21 @@ impl<'a, 'tcx> Checker<'a, 'tcx> {
631631
};
632632

633633
for (local, decl) in mir.local_decls.iter_enumerated() {
634-
match mir.local_kind(local) {
635-
LocalKind::Arg => {
636-
let qualifs = cx.qualifs_in_any_value_of_ty(decl.ty);
637-
for (per_local, qualif) in &mut cx.per_local.as_mut().zip(qualifs).0 {
638-
if *qualif {
639-
per_local.insert(local);
640-
}
634+
if let LocalKind::Arg = mir.local_kind(local) {
635+
let qualifs = cx.qualifs_in_any_value_of_ty(decl.ty);
636+
for (per_local, qualif) in &mut cx.per_local.as_mut().zip(qualifs).0 {
637+
if *qualif {
638+
per_local.insert(local);
641639
}
642-
cx.per_local[IsNotConst].insert(local);
643-
}
644-
645-
LocalKind::Var if mode == Mode::Fn => {
646-
cx.per_local[IsNotConst].insert(local);
647-
}
648-
649-
LocalKind::Temp if !temps[local].is_promotable() => {
650-
cx.per_local[IsNotConst].insert(local);
651640
}
652-
653-
_ => {}
641+
}
642+
if !temps[local].is_promotable() {
643+
cx.per_local[IsNotConst].insert(local);
644+
}
645+
if let LocalKind::Var = mir.local_kind(local) {
646+
// Sanity check to prevent implicit and explicit promotion of
647+
// named locals
648+
assert!(cx.per_local[IsNotConst].contains(local));
654649
}
655650
}
656651

0 commit comments

Comments
 (0)