Skip to content

Commit e962a1d

Browse files
committed
Rollup merge of #23702 - dotdash:match_reass, r=eddyb
The reassignment checker effectively only checks whether the last assignment in a body affects the discriminant, but it should of course check all the assignments. Fixes #23698
2 parents 2354fc9 + cc259fb commit e962a1d

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

src/librustc_trans/trans/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1352,12 +1352,12 @@ impl<'tcx> euv::Delegate<'tcx> for ReassignmentChecker {
13521352
fn mutate(&mut self, _: ast::NodeId, _: Span, cmt: mc::cmt, _: euv::MutateMode) {
13531353
match cmt.cat {
13541354
mc::cat_upvar(mc::Upvar { id: ty::UpvarId { var_id: vid, .. }, .. }) |
1355-
mc::cat_local(vid) => self.reassigned = self.node == vid,
1355+
mc::cat_local(vid) => self.reassigned |= self.node == vid,
13561356
mc::cat_interior(ref base_cmt, mc::InteriorField(field)) => {
13571357
match base_cmt.cat {
13581358
mc::cat_upvar(mc::Upvar { id: ty::UpvarId { var_id: vid, .. }, .. }) |
13591359
mc::cat_local(vid) => {
1360-
self.reassigned = self.node == vid && Some(field) == self.field
1360+
self.reassigned |= self.node == vid && Some(field) == self.field
13611361
},
13621362
_ => {}
13631363
}

src/test/run-pass/match-reassign.rs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Regression test for #23698: The reassignment checker only cared
12+
// about the last assigment in a match arm body
13+
14+
// Use an extra function to make sure no extra assignments
15+
// are introduced by macros in the match statement
16+
fn check_eq(x: i32, y: i32) {
17+
assert_eq!(x, y);
18+
}
19+
20+
#[allow(unused_assignments)]
21+
fn main() {
22+
let mut x = Box::new(1);
23+
match x {
24+
y => {
25+
x = Box::new(2);
26+
let _tmp = 1; // This assignment used to throw off the reassignment checker
27+
check_eq(*y, 1);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)