Skip to content

Commit

Permalink
fix function arguments in constant promotion
Browse files Browse the repository at this point in the history
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 rust-lang#38985.
  • Loading branch information
arielb1 authored and brson committed Jan 17, 2017
1 parent 3ccb0a4 commit d81140a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/librustc_mir/transform/promote_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
self.visit_rvalue(&mut rvalue, loc);
self.assign(new_temp, rvalue, source_info.span);
} else {
let mut terminator = if self.keep_original {
let terminator = if self.keep_original {
self.source[loc.block].terminator().clone()
} else {
let terminator = self.source[loc.block].terminator_mut();
Expand All @@ -256,28 +256,30 @@ impl<'a, 'tcx> Promoter<'a, 'tcx> {
}
};

let last = self.promoted.basic_blocks().last().unwrap();
let new_target = self.new_block();

terminator.kind = match terminator.kind {
match terminator.kind {
TerminatorKind::Call { mut func, mut args, .. } => {
self.visit_operand(&mut func, loc);
for arg in &mut args {
self.visit_operand(arg, loc);
}
TerminatorKind::Call {
func: func,
args: args,
cleanup: None,
destination: Some((Lvalue::Local(new_temp), new_target))
}

let last = self.promoted.basic_blocks().last().unwrap();
let new_target = self.new_block();

*self.promoted[last].terminator_mut() = Terminator {
kind: TerminatorKind::Call {
func: func,
args: args,
cleanup: None,
destination: Some((Lvalue::Local(new_temp), new_target))
},
..terminator
};
}
ref kind => {
span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
}
};

*self.promoted[last].terminator_mut() = terminator;
};

self.keep_original = old_keep_original;
Expand Down
7 changes: 7 additions & 0 deletions src/test/run-pass/issue-37991.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,14 @@ const fn foo() -> i64 {
3
}

const fn bar(x: i64) -> i64 {
x*2
}

fn main() {
let val = &(foo() % 2);
assert_eq!(*val, 1);

let val2 = &(bar(1+1) % 3);
assert_eq!(*val2, 1);
}

0 comments on commit d81140a

Please sign in to comment.