Skip to content

Commit d81140a

Browse files
arielb1brson
authored andcommitted
fix function arguments in constant promotion
we can't create the target block until *after* we promote the arguments - otherwise the arguments will be promoted into the target block. oops. Fixes #38985.
1 parent 3ccb0a4 commit d81140a

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

src/librustc_mir/transform/promote_consts.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
238238
self.visit_rvalue(&mut rvalue, loc);
239239
self.assign(new_temp, rvalue, source_info.span);
240240
} else {
241-
let mut terminator = if self.keep_original {
241+
let terminator = if self.keep_original {
242242
self.source[loc.block].terminator().clone()
243243
} else {
244244
let terminator = self.source[loc.block].terminator_mut();
@@ -256,28 +256,30 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
256256
}
257257
};
258258

259-
let last = self.promoted.basic_blocks().last().unwrap();
260-
let new_target = self.new_block();
261-
262-
terminator.kind = match terminator.kind {
259+
match terminator.kind {
263260
TerminatorKind::Call { mut func, mut args, .. } => {
264261
self.visit_operand(&mut func, loc);
265262
for arg in &mut args {
266263
self.visit_operand(arg, loc);
267264
}
268-
TerminatorKind::Call {
269-
func: func,
270-
args: args,
271-
cleanup: None,
272-
destination: Some((Lvalue::Local(new_temp), new_target))
273-
}
265+
266+
let last = self.promoted.basic_blocks().last().unwrap();
267+
let new_target = self.new_block();
268+
269+
*self.promoted[last].terminator_mut() = Terminator {
270+
kind: TerminatorKind::Call {
271+
func: func,
272+
args: args,
273+
cleanup: None,
274+
destination: Some((Lvalue::Local(new_temp), new_target))
275+
},
276+
..terminator
277+
};
274278
}
275279
ref kind => {
276280
span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
277281
}
278282
};
279-
280-
*self.promoted[last].terminator_mut() = terminator;
281283
};
282284

283285
self.keep_original = old_keep_original;

src/test/run-pass/issue-37991.rs

+7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ const fn foo() -> i64 {
1414
3
1515
}
1616

17+
const fn bar(x: i64) -> i64 {
18+
x*2
19+
}
20+
1721
fn main() {
1822
let val = &(foo() % 2);
1923
assert_eq!(*val, 1);
24+
25+
let val2 = &(bar(1+1) % 3);
26+
assert_eq!(*val2, 1);
2027
}

0 commit comments

Comments
 (0)