Skip to content

Commit cbcfa2c

Browse files
committed
Rollup merge of rust-lang#53223 - ljedrz:cleanup_data_structures, r=oli-obk
A few cleanups for rustc_data_structures - remove a redundant `clone()` - make some calls to `.iter()` implicit - collapse/simplify a few operations - remove some explicit `return`s - make `SnapshotMap::{commit, rollback_to}` take references - remove unnecessary struct field names - change `transmute()`s in `IdxSet::{from_slice, from_slice_mut}` to casts - remove some unnecessary lifetime annotations - split 2 long literals
2 parents a012abc + 94c3856 commit cbcfa2c

File tree

14 files changed

+42
-43
lines changed

14 files changed

+42
-43
lines changed

src/librustc/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
709709

710710
self.projection_cache
711711
.borrow_mut()
712-
.commit(projection_cache_snapshot);
712+
.commit(&projection_cache_snapshot);
713713
self.type_variables
714714
.borrow_mut()
715715
.commit(type_snapshot);

src/librustc/traits/project.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1668,15 +1668,15 @@ impl<'tcx> ProjectionCache<'tcx> {
16681668
}
16691669

16701670
pub fn rollback_to(&mut self, snapshot: ProjectionCacheSnapshot) {
1671-
self.map.rollback_to(snapshot.snapshot);
1671+
self.map.rollback_to(&snapshot.snapshot);
16721672
}
16731673

16741674
pub fn rollback_skolemized(&mut self, snapshot: &ProjectionCacheSnapshot) {
16751675
self.map.partial_rollback(&snapshot.snapshot, &|k| k.ty.has_re_skol());
16761676
}
16771677

1678-
pub fn commit(&mut self, snapshot: ProjectionCacheSnapshot) {
1679-
self.map.commit(snapshot.snapshot);
1678+
pub fn commit(&mut self, snapshot: &ProjectionCacheSnapshot) {
1679+
self.map.commit(&snapshot.snapshot);
16801680
}
16811681

16821682
/// Try to start normalize `key`; returns an error if

src/librustc_data_structures/base_n.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub const MAX_BASE: usize = 64;
1717
pub const ALPHANUMERIC_ONLY: usize = 62;
1818
pub const CASE_INSENSITIVE: usize = 36;
1919

20-
const BASE_64: &'static [u8; MAX_BASE as usize] =
20+
const BASE_64: &[u8; MAX_BASE as usize] =
2121
b"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ@$";
2222

2323
#[inline]
@@ -37,7 +37,8 @@ pub fn push_str(mut n: u128, base: usize, output: &mut String) {
3737
break;
3838
}
3939
}
40-
&mut s[0..index].reverse();
40+
s[0..index].reverse();
41+
4142
output.push_str(str::from_utf8(&s[0..index]).unwrap());
4243
}
4344

src/librustc_data_structures/bitslice.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fn bit_lookup(bit: usize) -> BitLookup {
7575
let word = bit / word_bits;
7676
let bit_in_word = bit % word_bits;
7777
let bit_mask = 1 << bit_in_word;
78-
BitLookup { word: word, bit_in_word: bit_in_word, bit_mask: bit_mask }
78+
BitLookup { word, bit_in_word, bit_mask }
7979
}
8080

8181
pub fn bits_to_string(words: &[Word], bits: usize) -> String {
@@ -105,7 +105,8 @@ pub fn bits_to_string(words: &[Word], bits: usize) -> String {
105105
sep = '|';
106106
}
107107
result.push(']');
108-
return result
108+
109+
result
109110
}
110111

111112
#[inline]

src/librustc_data_structures/bitvec.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ impl<'a, C: Idx> Iterator for BitIter<'a, C> {
196196
self.current >>= offset;
197197
self.current >>= 1; // shift otherwise overflows for 0b1000_0000_…_0000
198198
self.idx += offset + 1;
199-
return Some(C::new(self.idx - 1));
199+
200+
Some(C::new(self.idx - 1))
200201
}
201202

202203
fn size_hint(&self) -> (usize, Option<usize>) {
@@ -299,7 +300,7 @@ impl<R: Idx, C: Idx> BitMatrix<R, C> {
299300
let v1 = vector[write_index];
300301
let v2 = v1 | vector[read_index];
301302
vector[write_index] = v2;
302-
changed = changed | (v1 != v2);
303+
changed |= v1 != v2;
303304
}
304305
changed
305306
}

src/librustc_data_structures/flock.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,8 +254,8 @@ mod imp {
254254
type ULONG_PTR = usize;
255255

256256
type LPOVERLAPPED = *mut OVERLAPPED;
257-
const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x00000002;
258-
const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x00000001;
257+
const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x0000_0002;
258+
const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x0000_0001;
259259

260260
const FILE_SHARE_DELETE: DWORD = 0x4;
261261
const FILE_SHARE_READ: DWORD = 0x1;

src/librustc_data_structures/graph/dominators/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ fn intersect<Node: Idx>(
107107
node2 = immediate_dominators[node2].unwrap();
108108
}
109109
}
110-
return node1;
110+
111+
node1
111112
}
112113

113114
#[derive(Clone, Debug)]

src/librustc_data_structures/graph/implementation/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ pub const INCOMING: Direction = Direction { repr: 1 };
9090

9191
impl NodeIndex {
9292
/// Returns unique id (unique with respect to the graph holding associated node).
93-
pub fn node_id(&self) -> usize {
93+
pub fn node_id(self) -> usize {
9494
self.0
9595
}
9696
}
@@ -187,7 +187,7 @@ impl<N: Debug, E: Debug> Graph<N, E> {
187187
self.nodes[source.0].first_edge[OUTGOING.repr] = idx;
188188
self.nodes[target.0].first_edge[INCOMING.repr] = idx;
189189

190-
return idx;
190+
idx
191191
}
192192

193193
pub fn edge(&self, idx: EdgeIndex) -> &Edge<E> {
@@ -261,8 +261,8 @@ impl<N: Debug, E: Debug> Graph<N, E> {
261261
DepthFirstTraversal::with_start_node(self, start, direction)
262262
}
263263

264-
pub fn nodes_in_postorder<'a>(
265-
&'a self,
264+
pub fn nodes_in_postorder(
265+
&self,
266266
direction: Direction,
267267
entry_node: NodeIndex,
268268
) -> Vec<NodeIndex> {

src/librustc_data_structures/indexed_set.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,13 @@ impl<T: Idx> rustc_serialize::Decodable for IdxSetBuf<T> {
5959

6060
// pnkfelix wants to have this be `IdxSet<T>([Word]) and then pass
6161
// around `&mut IdxSet<T>` or `&IdxSet<T>`.
62-
//
63-
// WARNING: Mapping a `&IdxSetBuf<T>` to `&IdxSet<T>` (at least today)
64-
// requires a transmute relying on representation guarantees that may
65-
// not hold in the future.
6662

6763
/// Represents a set (or packed family of sets), of some element type
6864
/// E, where each E is identified by some unique index type `T`.
6965
///
7066
/// In other words, `T` is the type used to index into the bitslice
7167
/// this type uses to represent the set of object it holds.
68+
#[repr(transparent)]
7269
pub struct IdxSet<T: Idx> {
7370
_pd: PhantomData<fn(&T)>,
7471
bits: [Word],
@@ -134,11 +131,11 @@ impl<T: Idx> IdxSetBuf<T> {
134131

135132
impl<T: Idx> IdxSet<T> {
136133
unsafe fn from_slice(s: &[Word]) -> &Self {
137-
mem::transmute(s) // (see above WARNING)
134+
&*(s as *const [Word] as *const Self)
138135
}
139136

140137
unsafe fn from_slice_mut(s: &mut [Word]) -> &mut Self {
141-
mem::transmute(s) // (see above WARNING)
138+
&mut *(s as *mut [Word] as *mut Self)
142139
}
143140
}
144141

src/librustc_data_structures/obligation_forest/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ impl<O: ForestObligation> ObligationForest<O> {
573573
}
574574

575575
let mut kill_list = vec![];
576-
for (predicate, index) in self.waiting_cache.iter_mut() {
576+
for (predicate, index) in &mut self.waiting_cache {
577577
let new_index = node_rewrites[index.get()];
578578
if new_index >= nodes_len {
579579
kill_list.push(predicate.clone());

src/librustc_data_structures/snapshot_map/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<K, V> SnapshotMap<K, V>
9292
pub fn snapshot(&mut self) -> Snapshot {
9393
self.undo_log.push(UndoLog::OpenSnapshot);
9494
let len = self.undo_log.len() - 1;
95-
Snapshot { len: len }
95+
Snapshot { len }
9696
}
9797

9898
fn assert_open_snapshot(&self, snapshot: &Snapshot) {
@@ -103,8 +103,8 @@ impl<K, V> SnapshotMap<K, V>
103103
});
104104
}
105105

106-
pub fn commit(&mut self, snapshot: Snapshot) {
107-
self.assert_open_snapshot(&snapshot);
106+
pub fn commit(&mut self, snapshot: &Snapshot) {
107+
self.assert_open_snapshot(snapshot);
108108
if snapshot.len == 0 {
109109
// The root snapshot.
110110
self.undo_log.truncate(0);
@@ -135,8 +135,8 @@ impl<K, V> SnapshotMap<K, V>
135135
}
136136
}
137137

138-
pub fn rollback_to(&mut self, snapshot: Snapshot) {
139-
self.assert_open_snapshot(&snapshot);
138+
pub fn rollback_to(&mut self, snapshot: &Snapshot) {
139+
self.assert_open_snapshot(snapshot);
140140
while self.undo_log.len() > snapshot.len + 1 {
141141
let entry = self.undo_log.pop().unwrap();
142142
self.reverse(entry);

src/librustc_data_structures/snapshot_map/test.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn basic() {
2020
map.insert(44, "fourty-four");
2121
assert_eq!(map[&44], "fourty-four");
2222
assert_eq!(map.get(&33), None);
23-
map.rollback_to(snapshot);
23+
map.rollback_to(&snapshot);
2424
assert_eq!(map[&22], "twenty-two");
2525
assert_eq!(map.get(&33), None);
2626
assert_eq!(map.get(&44), None);
@@ -33,7 +33,7 @@ fn out_of_order() {
3333
map.insert(22, "twenty-two");
3434
let snapshot1 = map.snapshot();
3535
let _snapshot2 = map.snapshot();
36-
map.rollback_to(snapshot1);
36+
map.rollback_to(&snapshot1);
3737
}
3838

3939
#[test]
@@ -43,8 +43,8 @@ fn nested_commit_then_rollback() {
4343
let snapshot1 = map.snapshot();
4444
let snapshot2 = map.snapshot();
4545
map.insert(22, "thirty-three");
46-
map.commit(snapshot2);
46+
map.commit(&snapshot2);
4747
assert_eq!(map[&22], "thirty-three");
48-
map.rollback_to(snapshot1);
48+
map.rollback_to(&snapshot1);
4949
assert_eq!(map[&22], "twenty-two");
5050
}

src/librustc_data_structures/tiny_list.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ impl<T: PartialEq> Element<T> {
107107
};
108108

109109
self.next = new_next;
110-
return true
110+
111+
true
111112
}
112113

113114
fn len(&self) -> usize {

src/librustc_data_structures/transitive_relation.rs

+6-9
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
7777
..
7878
} = self;
7979

80-
map.entry(a.clone())
80+
*map.entry(a.clone())
8181
.or_insert_with(|| {
8282
elements.push(a);
8383

@@ -86,7 +86,6 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
8686

8787
Index(elements.len() - 1)
8888
})
89-
.clone()
9089
}
9190

9291
/// Applies the (partial) function to each edge and returns a new
@@ -98,14 +97,12 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
9897
{
9998
let mut result = TransitiveRelation::new();
10099
for edge in &self.edges {
101-
let r = f(&self.elements[edge.source.0]).and_then(|source| {
100+
f(&self.elements[edge.source.0]).and_then(|source| {
102101
f(&self.elements[edge.target.0]).and_then(|target| {
103-
Some(result.add(source, target))
102+
result.add(source, target);
103+
Some(())
104104
})
105-
});
106-
if r.is_none() {
107-
return None;
108-
}
105+
})?;
109106
}
110107
Some(result)
111108
}
@@ -372,7 +369,7 @@ impl<T: Clone + Debug + Eq + Hash> TransitiveRelation<T> {
372369
let mut changed = true;
373370
while changed {
374371
changed = false;
375-
for edge in self.edges.iter() {
372+
for edge in &self.edges {
376373
// add an edge from S -> T
377374
changed |= matrix.add(edge.source.0, edge.target.0);
378375

0 commit comments

Comments
 (0)