Skip to content

Commit 880b6c2

Browse files
Ariel Ben-Yehudaarielb1
Ariel Ben-Yehuda
authored andcommitted
fix a few remaining bugs - make check runs!
1 parent 3c6f410 commit 880b6c2

File tree

4 files changed

+220
-17
lines changed

4 files changed

+220
-17
lines changed

src/librustc_driver/driver.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -844,6 +844,18 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
844844
"match checking",
845845
|| middle::check_match::check_crate(tcx));
846846

847+
// this must run before MIR dump, because
848+
// "not all control paths return a value" is reported here.
849+
//
850+
// maybe move the check to a MIR pass?
851+
time(time_passes,
852+
"liveness checking",
853+
|| middle::liveness::check_crate(tcx));
854+
855+
time(time_passes,
856+
"rvalue checking",
857+
|| rvalues::check_crate(tcx));
858+
847859
let mut mir_map =
848860
time(time_passes,
849861
"MIR dump",
@@ -853,18 +865,10 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
853865
"MIR passes",
854866
|| mir_map.run_passes(&mut sess.plugin_mir_passes.borrow_mut(), tcx));
855867

856-
time(time_passes,
857-
"liveness checking",
858-
|| middle::liveness::check_crate(tcx));
859-
860868
time(time_passes,
861869
"borrow checking",
862870
|| borrowck::check_crate(tcx));
863871

864-
time(time_passes,
865-
"rvalue checking",
866-
|| rvalues::check_crate(tcx));
867-
868872
// Avoid overwhelming user with errors if type checking failed.
869873
// I'm not sure how helpful this is, to be honest, but it avoids
870874
// a

src/librustc_mir/hair/cx/expr.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
3232
debug!("Expr::make_mirror(): id={}, span={:?}", self.id, self.span);
3333

3434
let expr_ty = cx.tcx.expr_ty(self); // note: no adjustments (yet)!
35+
let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
36+
let expr_extent = cx.tcx.region_maps.node_extent(self.id);
3537

3638
let kind = match self.node {
3739
// Here comes the interesting stuff:
@@ -72,7 +74,7 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
7274

7375
let tupled_args = Expr {
7476
ty: sig.inputs[1],
75-
temp_lifetime: cx.tcx.region_maps.temporary_scope(self.id),
77+
temp_lifetime: temp_lifetime,
7678
span: self.span,
7779
kind: ExprKind::Tuple {
7880
fields: args.iter().map(ToRef::to_ref).collect()
@@ -146,11 +148,20 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
146148
}
147149

148150
hir::ExprAssignOp(op, ref lhs, ref rhs) => {
149-
let op = bin_op(op.node);
150-
ExprKind::AssignOp {
151-
op: op,
152-
lhs: lhs.to_ref(),
153-
rhs: rhs.to_ref(),
151+
if cx.tcx.is_method_call(self.id) {
152+
let pass_args = if hir_util::is_by_value_binop(op.node) {
153+
PassArgs::ByValue
154+
} else {
155+
PassArgs::ByRef
156+
};
157+
overloaded_operator(cx, self, ty::MethodCall::expr(self.id),
158+
pass_args, lhs.to_ref(), vec![rhs])
159+
} else {
160+
ExprKind::AssignOp {
161+
op: bin_op(op.node),
162+
lhs: lhs.to_ref(),
163+
rhs: rhs.to_ref(),
164+
}
154165
}
155166
}
156167

@@ -416,9 +427,6 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
416427
ExprKind::Tuple { fields: fields.to_ref() },
417428
};
418429

419-
let temp_lifetime = cx.tcx.region_maps.temporary_scope(self.id);
420-
let expr_extent = cx.tcx.region_maps.node_extent(self.id);
421-
422430
let mut expr = Expr {
423431
temp_lifetime: temp_lifetime,
424432
ty: expr_ty,

src/librustc_mir/transform/type_check.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// except according to those terms.
1010

1111
//! This pass type-checks the MIR to ensure it is not broken.
12+
#![allow(unreachable_code)]
1213

1314
use rustc::middle::infer;
1415
use rustc::middle::ty::{self, Ty};
@@ -322,6 +323,13 @@ impl<'a, 'tcx> MirPass for TypeckMir<'a, 'tcx> {
322323
_tcx: &ty::ctxt<'tcx_>) {
323324
// FIXME: pass param_env to run_on_mir
324325
let mir: &mut Mir<'tcx> = unsafe { ::std::mem::transmute(mir) };
326+
327+
if self.tcx().sess.err_count() > 0 {
328+
// compiling a broken program can obviously result in a
329+
// broken MIR, so try not to report duplicate errors.
330+
return;
331+
}
332+
325333
let mut type_verifier = TypeVerifier::new(self.infcx, mir);
326334
type_verifier.visit_mir(mir);
327335

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
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+
#![feature(augmented_assignments)]
12+
#![feature(op_assign_traits)]
13+
#![feature(rustc_attrs)]
14+
15+
use std::mem;
16+
use std::ops::{
17+
AddAssign, BitAndAssign, BitOrAssign, BitXorAssign, DivAssign, MulAssign, RemAssign,
18+
ShlAssign, ShrAssign, SubAssign,
19+
};
20+
21+
#[derive(Debug, PartialEq)]
22+
struct Int(i32);
23+
24+
struct Slice([i32]);
25+
26+
impl Slice {
27+
fn new(slice: &mut [i32]) -> &mut Slice {
28+
unsafe {
29+
mem::transmute(slice)
30+
}
31+
}
32+
}
33+
34+
fn main() {
35+
main_mir();
36+
}
37+
38+
#[rustc_mir]
39+
fn main_mir() {
40+
let mut x = Int(1);
41+
42+
x += Int(2);
43+
assert_eq!(x, Int(0b11));
44+
45+
x &= Int(0b01);
46+
assert_eq!(x, Int(0b01));
47+
48+
x |= Int(0b10);
49+
assert_eq!(x, Int(0b11));
50+
51+
x ^= Int(0b01);
52+
assert_eq!(x, Int(0b10));
53+
54+
x /= Int(2);
55+
assert_eq!(x, Int(1));
56+
57+
x *= Int(3);
58+
assert_eq!(x, Int(3));
59+
60+
x %= Int(2);
61+
assert_eq!(x, Int(1));
62+
63+
// overloaded RHS
64+
x <<= 1u8;
65+
assert_eq!(x, Int(2));
66+
67+
x <<= 1u16;
68+
assert_eq!(x, Int(4));
69+
70+
x >>= 1u8;
71+
assert_eq!(x, Int(2));
72+
73+
x >>= 1u16;
74+
assert_eq!(x, Int(1));
75+
76+
x -= Int(1);
77+
assert_eq!(x, Int(0));
78+
79+
// indexed LHS
80+
let mut v = vec![Int(1), Int(2)];
81+
v[0] += Int(2);
82+
assert_eq!(v[0], Int(3));
83+
84+
// unsized RHS
85+
let mut array = [0, 1, 2];
86+
*Slice::new(&mut array) += 1;
87+
assert_eq!(array[0], 1);
88+
assert_eq!(array[1], 2);
89+
assert_eq!(array[2], 3);
90+
}
91+
92+
impl AddAssign for Int {
93+
#[rustc_mir]
94+
fn add_assign(&mut self, rhs: Int) {
95+
self.0 += rhs.0;
96+
}
97+
}
98+
99+
impl BitAndAssign for Int {
100+
#[rustc_mir]
101+
fn bitand_assign(&mut self, rhs: Int) {
102+
self.0 &= rhs.0;
103+
}
104+
}
105+
106+
impl BitOrAssign for Int {
107+
#[rustc_mir]
108+
fn bitor_assign(&mut self, rhs: Int) {
109+
self.0 |= rhs.0;
110+
}
111+
}
112+
113+
impl BitXorAssign for Int {
114+
#[rustc_mir]
115+
fn bitxor_assign(&mut self, rhs: Int) {
116+
self.0 ^= rhs.0;
117+
}
118+
}
119+
120+
impl DivAssign for Int {
121+
#[rustc_mir]
122+
fn div_assign(&mut self, rhs: Int) {
123+
self.0 /= rhs.0;
124+
}
125+
}
126+
127+
impl MulAssign for Int {
128+
#[rustc_mir]
129+
fn mul_assign(&mut self, rhs: Int) {
130+
self.0 *= rhs.0;
131+
}
132+
}
133+
134+
impl RemAssign for Int {
135+
#[rustc_mir]
136+
fn rem_assign(&mut self, rhs: Int) {
137+
self.0 %= rhs.0;
138+
}
139+
}
140+
141+
impl ShlAssign<u8> for Int {
142+
#[rustc_mir]
143+
fn shl_assign(&mut self, rhs: u8) {
144+
self.0 <<= rhs;
145+
}
146+
}
147+
148+
impl ShlAssign<u16> for Int {
149+
#[rustc_mir]
150+
fn shl_assign(&mut self, rhs: u16) {
151+
self.0 <<= rhs;
152+
}
153+
}
154+
155+
impl ShrAssign<u8> for Int {
156+
#[rustc_mir]
157+
fn shr_assign(&mut self, rhs: u8) {
158+
self.0 >>= rhs;
159+
}
160+
}
161+
162+
impl ShrAssign<u16> for Int {
163+
#[rustc_mir]
164+
fn shr_assign(&mut self, rhs: u16) {
165+
self.0 >>= rhs;
166+
}
167+
}
168+
169+
impl SubAssign for Int {
170+
#[rustc_mir]
171+
fn sub_assign(&mut self, rhs: Int) {
172+
self.0 -= rhs.0;
173+
}
174+
}
175+
176+
impl AddAssign<i32> for Slice {
177+
#[rustc_mir]
178+
fn add_assign(&mut self, rhs: i32) {
179+
for lhs in &mut self.0 {
180+
*lhs += rhs;
181+
}
182+
}
183+
}

0 commit comments

Comments
 (0)