Skip to content

Commit 580cbd8

Browse files
arielb1brson
authored andcommitted
stop having identity casts be lexprs
that made no sense (see test), and was incompatible with borrowck. Fixes rust-lang#36936.
1 parent 566df6c commit 580cbd8

File tree

7 files changed

+47
-2
lines changed

7 files changed

+47
-2
lines changed

src/librustc_mir/build/expr/as_lvalue.rs

+1
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
9696
ExprKind::LogicalOp { .. } |
9797
ExprKind::Box { .. } |
9898
ExprKind::Cast { .. } |
99+
ExprKind::Use { .. } |
99100
ExprKind::NeverToAny { .. } |
100101
ExprKind::ReifyFnPointer { .. } |
101102
ExprKind::UnsafeFnPointer { .. } |

src/librustc_mir/build/expr/as_rvalue.rs

+4
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,10 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
115115
let source = unpack!(block = this.as_operand(block, source));
116116
block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
117117
}
118+
ExprKind::Use { source } => {
119+
let source = unpack!(block = this.as_operand(block, source));
120+
block.and(Rvalue::Use(source))
121+
}
118122
ExprKind::ReifyFnPointer { source } => {
119123
let source = unpack!(block = this.as_operand(block, source));
120124
block.and(Rvalue::Cast(CastKind::ReifyFnPointer, source, expr.ty))

src/librustc_mir/build/expr/category.rs

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ impl Category {
6868
ExprKind::Binary { .. } |
6969
ExprKind::Box { .. } |
7070
ExprKind::Cast { .. } |
71+
ExprKind::Use { .. } |
7172
ExprKind::ReifyFnPointer { .. } |
7273
ExprKind::UnsafeFnPointer { .. } |
7374
ExprKind::Unsize { .. } |

src/librustc_mir/build/expr/into.rs

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
249249
ExprKind::Binary { .. } |
250250
ExprKind::Box { .. } |
251251
ExprKind::Cast { .. } |
252+
ExprKind::Use { .. } |
252253
ExprKind::ReifyFnPointer { .. } |
253254
ExprKind::UnsafeFnPointer { .. } |
254255
ExprKind::Unsize { .. } |

src/librustc_mir/hair/cx/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -602,8 +602,8 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>,
602602
// Check to see if this cast is a "coercion cast", where the cast is actually done
603603
// using a coercion (or is a no-op).
604604
if let Some(&TyCastKind::CoercionCast) = cx.tcx.cast_kinds.borrow().get(&source.id) {
605-
// Skip the actual cast itexpr, as it's now a no-op.
606-
return source.make_mirror(cx);
605+
// Convert the lexpr to a vexpr.
606+
ExprKind::Use { source: source.to_ref() }
607607
} else {
608608
ExprKind::Cast { source: source.to_ref() }
609609
}

src/librustc_mir/hair/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ pub enum ExprKind<'tcx> {
139139
Cast {
140140
source: ExprRef<'tcx>,
141141
},
142+
Use {
143+
source: ExprRef<'tcx>,
144+
}, // Use a lexpr to get a vexpr.
142145
NeverToAny {
143146
source: ExprRef<'tcx>,
144147
},

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

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2016 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+
// check that casts are not being treated as lexprs.
12+
13+
fn main() {
14+
let mut a = 0i32;
15+
let b = &(a as i32);
16+
a = 1;
17+
assert!((&a as *const i32) != (b as *const i32));
18+
assert_eq!(*b, 0);
19+
20+
assert_eq!(issue_36936(), 1);
21+
}
22+
23+
24+
struct A(u32);
25+
26+
impl Drop for A {
27+
fn drop(&mut self) {
28+
self.0 = 0;
29+
}
30+
}
31+
32+
fn issue_36936() -> u32 {
33+
let a = &(A(1) as A);
34+
a.0
35+
}

0 commit comments

Comments
 (0)