Skip to content

Commit 513d942

Browse files
committed
Auto merge of #38989 - arielb1:constant-mir-overflow2, r=eddyb
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. This is a regression introduced in the beta-nominated #38833, so beta-nominating this one too (sorry @brson). r? @eddyb
2 parents 52c03d1 + 61b0b21 commit 513d942

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
@@ -237,7 +237,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
237237
self.visit_rvalue(&mut rvalue, loc);
238238
self.assign(new_temp, rvalue, source_info.span);
239239
} else {
240-
let mut terminator = if self.keep_original {
240+
let terminator = if self.keep_original {
241241
self.source[loc.block].terminator().clone()
242242
} else {
243243
let terminator = self.source[loc.block].terminator_mut();
@@ -255,28 +255,30 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
255255
}
256256
};
257257

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

282284
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)