Skip to content

Commit ddbb27a

Browse files
committed
Auto merge of #46558 - arielb1:union-borrow-refactor, r=nikomatsakis
Clean up the MIR borrowck code I want to avoid the new code to start with so much technical debt. r? @nikomatsakis
2 parents 33245fe + e798cb0 commit ddbb27a

14 files changed

+1358
-1336
lines changed

src/librustc_mir/borrow_check/error_reporting.rs

+574
Large diffs are not rendered by default.
+159
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// Copyright 2017 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+
//! Manages the dataflow bits required for borrowck.
12+
//!
13+
//! FIXME: this might be better as a "generic" fixed-point combinator,
14+
//! but is not as ugly as it is right now.
15+
16+
use rustc::mir::{BasicBlock, Location};
17+
18+
use dataflow::{MaybeInitializedLvals, MaybeUninitializedLvals};
19+
use dataflow::{EverInitializedLvals, MovingOutStatements};
20+
use dataflow::{Borrows, FlowAtLocation, FlowsAtLocation};
21+
use dataflow::move_paths::HasMoveData;
22+
use std::fmt;
23+
24+
// (forced to be `pub` due to its use as an associated type below.)
25+
pub struct Flows<'b, 'gcx: 'tcx, 'tcx: 'b> {
26+
pub borrows: FlowAtLocation<Borrows<'b, 'gcx, 'tcx>>,
27+
pub inits: FlowAtLocation<MaybeInitializedLvals<'b, 'gcx, 'tcx>>,
28+
pub uninits: FlowAtLocation<MaybeUninitializedLvals<'b, 'gcx, 'tcx>>,
29+
pub move_outs: FlowAtLocation<MovingOutStatements<'b, 'gcx, 'tcx>>,
30+
pub ever_inits: FlowAtLocation<EverInitializedLvals<'b, 'gcx, 'tcx>>,
31+
}
32+
33+
impl<'b, 'gcx, 'tcx> Flows<'b, 'gcx, 'tcx> {
34+
pub fn new(
35+
borrows: FlowAtLocation<Borrows<'b, 'gcx, 'tcx>>,
36+
inits: FlowAtLocation<MaybeInitializedLvals<'b, 'gcx, 'tcx>>,
37+
uninits: FlowAtLocation<MaybeUninitializedLvals<'b, 'gcx, 'tcx>>,
38+
move_outs: FlowAtLocation<MovingOutStatements<'b, 'gcx, 'tcx>>,
39+
ever_inits: FlowAtLocation<EverInitializedLvals<'b, 'gcx, 'tcx>>,
40+
) -> Self {
41+
Flows {
42+
borrows,
43+
inits,
44+
uninits,
45+
move_outs,
46+
ever_inits,
47+
}
48+
}
49+
}
50+
51+
macro_rules! each_flow {
52+
($this:ident, $meth:ident($arg:ident)) => {
53+
FlowAtLocation::$meth(&mut $this.borrows, $arg);
54+
FlowAtLocation::$meth(&mut $this.inits, $arg);
55+
FlowAtLocation::$meth(&mut $this.uninits, $arg);
56+
FlowAtLocation::$meth(&mut $this.move_outs, $arg);
57+
FlowAtLocation::$meth(&mut $this.ever_inits, $arg);
58+
}
59+
}
60+
61+
impl<'b, 'gcx, 'tcx> FlowsAtLocation for Flows<'b, 'gcx, 'tcx> {
62+
fn reset_to_entry_of(&mut self, bb: BasicBlock) {
63+
each_flow!(self, reset_to_entry_of(bb));
64+
}
65+
66+
fn reconstruct_statement_effect(&mut self, location: Location) {
67+
each_flow!(self, reconstruct_statement_effect(location));
68+
}
69+
70+
fn reconstruct_terminator_effect(&mut self, location: Location) {
71+
each_flow!(self, reconstruct_terminator_effect(location));
72+
}
73+
74+
fn apply_local_effect(&mut self, location: Location) {
75+
each_flow!(self, apply_local_effect(location));
76+
}
77+
}
78+
79+
impl<'b, 'gcx, 'tcx> fmt::Display for Flows<'b, 'gcx, 'tcx> {
80+
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
81+
let mut s = String::new();
82+
83+
s.push_str("borrows in effect: [");
84+
let mut saw_one = false;
85+
self.borrows.each_state_bit(|borrow| {
86+
if saw_one {
87+
s.push_str(", ");
88+
};
89+
saw_one = true;
90+
let borrow_data = &self.borrows.operator().borrows()[borrow];
91+
s.push_str(&format!("{}", borrow_data));
92+
});
93+
s.push_str("] ");
94+
95+
s.push_str("borrows generated: [");
96+
let mut saw_one = false;
97+
self.borrows.each_gen_bit(|borrow| {
98+
if saw_one {
99+
s.push_str(", ");
100+
};
101+
saw_one = true;
102+
let borrow_data = &self.borrows.operator().borrows()[borrow];
103+
s.push_str(&format!("{}", borrow_data));
104+
});
105+
s.push_str("] ");
106+
107+
s.push_str("inits: [");
108+
let mut saw_one = false;
109+
self.inits.each_state_bit(|mpi_init| {
110+
if saw_one {
111+
s.push_str(", ");
112+
};
113+
saw_one = true;
114+
let move_path = &self.inits.operator().move_data().move_paths[mpi_init];
115+
s.push_str(&format!("{}", move_path));
116+
});
117+
s.push_str("] ");
118+
119+
s.push_str("uninits: [");
120+
let mut saw_one = false;
121+
self.uninits.each_state_bit(|mpi_uninit| {
122+
if saw_one {
123+
s.push_str(", ");
124+
};
125+
saw_one = true;
126+
let move_path =
127+
&self.uninits.operator().move_data().move_paths[mpi_uninit];
128+
s.push_str(&format!("{}", move_path));
129+
});
130+
s.push_str("] ");
131+
132+
s.push_str("move_out: [");
133+
let mut saw_one = false;
134+
self.move_outs.each_state_bit(|mpi_move_out| {
135+
if saw_one {
136+
s.push_str(", ");
137+
};
138+
saw_one = true;
139+
let move_out = &self.move_outs.operator().move_data().moves[mpi_move_out];
140+
s.push_str(&format!("{:?}", move_out));
141+
});
142+
s.push_str("] ");
143+
144+
s.push_str("ever_init: [");
145+
let mut saw_one = false;
146+
self.ever_inits.each_state_bit(|mpi_ever_init| {
147+
if saw_one {
148+
s.push_str(", ");
149+
};
150+
saw_one = true;
151+
let ever_init =
152+
&self.ever_inits.operator().move_data().inits[mpi_ever_init];
153+
s.push_str(&format!("{:?}", ever_init));
154+
});
155+
s.push_str("]");
156+
157+
fmt::Display::fmt(&s, fmt)
158+
}
159+
}

0 commit comments

Comments
 (0)