Skip to content

Commit 1506b34

Browse files
committed
rollup merge of rust-lang#22286: nikomatsakis/variance-4b
Conflicts: src/librustc/middle/infer/combine.rs src/librustc_typeck/check/wf.rs
2 parents 3e7a04c + 9f8b9d6 commit 1506b34

File tree

383 files changed

+3307
-1347
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

383 files changed

+3307
-1347
lines changed

src/doc/reference.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ the final namespace qualifier is omitted.
572572
Two examples of paths with type arguments:
573573

574574
```
575-
# struct HashMap<K, V>;
575+
# struct HashMap<K, V>(K,V);
576576
# fn f() {
577577
# fn id<T>(t: T) -> T { t }
578578
type T = HashMap<i32,String>; // Type arguments used in a type expression
@@ -1599,7 +1599,7 @@ pointer values (pointing to a type for which an implementation of the given
15991599
trait is in scope) to pointers to the trait name, used as a type.
16001600

16011601
```
1602-
# trait Shape { }
1602+
# trait Shape { fn dummy(&self) { } }
16031603
# impl Shape for i32 { }
16041604
# let mycircle = 0i32;
16051605
let myshape: Box<Shape> = Box::new(mycircle) as Box<Shape>;
@@ -1630,8 +1630,8 @@ let x: f64 = Num::from_i32(42);
16301630
Traits may inherit from other traits. For example, in
16311631

16321632
```
1633-
trait Shape { fn area() -> f64; }
1634-
trait Circle : Shape { fn radius() -> f64; }
1633+
trait Shape { fn area(&self) -> f64; }
1634+
trait Circle : Shape { fn radius(&self) -> f64; }
16351635
```
16361636

16371637
the syntax `Circle : Shape` means that types that implement `Circle` must also
@@ -1725,7 +1725,7 @@ type parameters taken by the trait it implements. Implementation parameters
17251725
are written after the `impl` keyword.
17261726

17271727
```
1728-
# trait Seq<T> { }
1728+
# trait Seq<T> { fn dummy(&self, _: T) { } }
17291729
impl<T> Seq<T> for Vec<T> {
17301730
/* ... */
17311731
}

src/libarena/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ pub struct Arena<'longer_than_self> {
9696
head: RefCell<Chunk>,
9797
copy_head: RefCell<Chunk>,
9898
chunks: RefCell<Vec<Chunk>>,
99-
_invariant: marker::InvariantLifetime<'longer_than_self>,
99+
_marker: marker::PhantomData<*mut &'longer_than_self()>,
100100
}
101101

102102
impl<'a> Arena<'a> {
@@ -111,7 +111,7 @@ impl<'a> Arena<'a> {
111111
head: RefCell::new(chunk(initial_size, false)),
112112
copy_head: RefCell::new(chunk(initial_size, true)),
113113
chunks: RefCell::new(Vec::new()),
114-
_invariant: marker::InvariantLifetime,
114+
_marker: marker::PhantomData,
115115
}
116116
}
117117
}
@@ -361,6 +361,8 @@ pub struct TypedArena<T> {
361361
}
362362

363363
struct TypedArenaChunk<T> {
364+
marker: marker::PhantomData<T>,
365+
364366
/// Pointer to the next arena segment.
365367
next: *mut TypedArenaChunk<T>,
366368

src/libcollections/btree/map.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -512,13 +512,22 @@ mod stack {
512512
use super::super::node::handle;
513513
use vec::Vec;
514514

515+
struct InvariantLifetime<'id>(
516+
marker::PhantomData<::core::cell::Cell<&'id ()>>);
517+
518+
impl<'id> InvariantLifetime<'id> {
519+
fn new() -> InvariantLifetime<'id> {
520+
InvariantLifetime(marker::PhantomData)
521+
}
522+
}
523+
515524
/// A generic mutable reference, identical to `&mut` except for the fact that its lifetime
516525
/// parameter is invariant. This means that wherever an `IdRef` is expected, only an `IdRef`
517526
/// with the exact requested lifetime can be used. This is in contrast to normal references,
518527
/// where `&'static` can be used in any function expecting any lifetime reference.
519528
pub struct IdRef<'id, T: 'id> {
520529
inner: &'id mut T,
521-
marker: marker::InvariantLifetime<'id>
530+
_marker: InvariantLifetime<'id>,
522531
}
523532

524533
impl<'id, T> Deref for IdRef<'id, T> {
@@ -560,7 +569,7 @@ mod stack {
560569
pub struct Pusher<'id, 'a, K:'a, V:'a> {
561570
map: &'a mut BTreeMap<K, V>,
562571
stack: Stack<K, V>,
563-
marker: marker::InvariantLifetime<'id>
572+
_marker: InvariantLifetime<'id>,
564573
}
565574

566575
impl<'a, K, V> PartialSearchStack<'a, K, V> {
@@ -595,11 +604,11 @@ mod stack {
595604
let pusher = Pusher {
596605
map: self.map,
597606
stack: self.stack,
598-
marker: marker::InvariantLifetime
607+
_marker: InvariantLifetime::new(),
599608
};
600609
let node = IdRef {
601610
inner: unsafe { &mut *self.next },
602-
marker: marker::InvariantLifetime
611+
_marker: InvariantLifetime::new(),
603612
};
604613

605614
closure(pusher, node)

src/libcollections/btree/node.rs

Lines changed: 52 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ use core::prelude::*;
2020

2121
use core::cmp::Ordering::{Greater, Less, Equal};
2222
use core::iter::Zip;
23+
use core::marker::PhantomData;
2324
use core::ops::{Deref, DerefMut, Index, IndexMut};
2425
use core::ptr::Unique;
2526
use core::{slice, mem, ptr, cmp, num, raw};
26-
use alloc::heap;
27+
use alloc::heap::{self, EMPTY};
2728

2829
use borrow::Borrow;
2930

@@ -58,8 +59,8 @@ pub struct Node<K, V> {
5859
keys: Unique<K>,
5960
vals: Unique<V>,
6061

61-
// In leaf nodes, this will be null, and no space will be allocated for edges.
62-
edges: Unique<Node<K, V>>,
62+
// In leaf nodes, this will be None, and no space will be allocated for edges.
63+
edges: Option<Unique<Node<K, V>>>,
6364

6465
// At any given time, there will be `_len` keys, `_len` values, and (in an internal node)
6566
// `_len + 1` edges. In a leaf node, there will never be any edges.
@@ -279,8 +280,11 @@ impl<T> Drop for RawItems<T> {
279280
#[unsafe_destructor]
280281
impl<K, V> Drop for Node<K, V> {
281282
fn drop(&mut self) {
282-
if self.keys.ptr.is_null() {
283-
// We have already cleaned up this node.
283+
if self.keys.is_null() {
284+
// Since we have #[unsafe_no_drop_flag], we have to watch
285+
// out for a null value being stored in self.keys. (Using
286+
// null is technically a violation of the `Unique`
287+
// requirements, though.)
284288
return;
285289
}
286290

@@ -293,7 +297,7 @@ impl<K, V> Drop for Node<K, V> {
293297
self.destroy();
294298
}
295299

296-
self.keys.ptr = ptr::null_mut();
300+
self.keys = unsafe { Unique::new(0 as *mut K) };
297301
}
298302
}
299303

@@ -309,9 +313,9 @@ impl<K, V> Node<K, V> {
309313
let (vals_offset, edges_offset) = calculate_offsets_generic::<K, V>(capacity, false);
310314

311315
Node {
312-
keys: Unique(buffer as *mut K),
313-
vals: Unique(buffer.offset(vals_offset as isize) as *mut V),
314-
edges: Unique(buffer.offset(edges_offset as isize) as *mut Node<K, V>),
316+
keys: Unique::new(buffer as *mut K),
317+
vals: Unique::new(buffer.offset(vals_offset as isize) as *mut V),
318+
edges: Some(Unique::new(buffer.offset(edges_offset as isize) as *mut Node<K, V>)),
315319
_len: 0,
316320
_capacity: capacity,
317321
}
@@ -327,9 +331,9 @@ impl<K, V> Node<K, V> {
327331
let (vals_offset, _) = calculate_offsets_generic::<K, V>(capacity, true);
328332

329333
Node {
330-
keys: Unique(buffer as *mut K),
331-
vals: Unique(unsafe { buffer.offset(vals_offset as isize) as *mut V }),
332-
edges: Unique(ptr::null_mut()),
334+
keys: unsafe { Unique::new(buffer as *mut K) },
335+
vals: unsafe { Unique::new(buffer.offset(vals_offset as isize) as *mut V) },
336+
edges: None,
333337
_len: 0,
334338
_capacity: capacity,
335339
}
@@ -338,18 +342,18 @@ impl<K, V> Node<K, V> {
338342
unsafe fn destroy(&mut self) {
339343
let (alignment, size) =
340344
calculate_allocation_generic::<K, V>(self.capacity(), self.is_leaf());
341-
heap::deallocate(self.keys.ptr as *mut u8, size, alignment);
345+
heap::deallocate(*self.keys as *mut u8, size, alignment);
342346
}
343347

344348
#[inline]
345349
pub fn as_slices<'a>(&'a self) -> (&'a [K], &'a [V]) {
346350
unsafe {(
347351
mem::transmute(raw::Slice {
348-
data: self.keys.ptr,
352+
data: *self.keys as *const K,
349353
len: self.len()
350354
}),
351355
mem::transmute(raw::Slice {
352-
data: self.vals.ptr,
356+
data: *self.vals as *const V,
353357
len: self.len()
354358
})
355359
)}
@@ -368,8 +372,12 @@ impl<K, V> Node<K, V> {
368372
&[]
369373
} else {
370374
unsafe {
375+
let data = match self.edges {
376+
None => heap::EMPTY as *const Node<K,V>,
377+
Some(ref p) => **p as *const Node<K,V>,
378+
};
371379
mem::transmute(raw::Slice {
372-
data: self.edges.ptr,
380+
data: data,
373381
len: self.len() + 1
374382
})
375383
}
@@ -525,7 +533,8 @@ impl<K: Clone, V: Clone> Clone for Node<K, V> {
525533
#[derive(Copy)]
526534
pub struct Handle<NodeRef, Type, NodeType> {
527535
node: NodeRef,
528-
index: usize
536+
index: usize,
537+
marker: PhantomData<(Type, NodeType)>,
529538
}
530539

531540
pub mod handle {
@@ -549,8 +558,8 @@ impl<K: Ord, V> Node<K, V> {
549558
// For the B configured as of this writing (B = 6), binary search was *significantly*
550559
// worse for usizes.
551560
match node.as_slices_internal().search_linear(key) {
552-
(index, true) => Found(Handle { node: node, index: index }),
553-
(index, false) => GoDown(Handle { node: node, index: index }),
561+
(index, true) => Found(Handle { node: node, index: index, marker: PhantomData }),
562+
(index, false) => GoDown(Handle { node: node, index: index, marker: PhantomData }),
554563
}
555564
}
556565
}
@@ -587,7 +596,7 @@ impl <K, V> Node<K, V> {
587596

588597
/// If the node has any children
589598
pub fn is_leaf(&self) -> bool {
590-
self.edges.ptr.is_null()
599+
self.edges.is_none()
591600
}
592601

593602
/// if the node has too few elements
@@ -619,7 +628,8 @@ impl<K, V, NodeRef, Type, NodeType> Handle<NodeRef, Type, NodeType> where
619628
pub fn as_raw(&mut self) -> Handle<*mut Node<K, V>, Type, NodeType> {
620629
Handle {
621630
node: &mut *self.node as *mut _,
622-
index: self.index
631+
index: self.index,
632+
marker: PhantomData,
623633
}
624634
}
625635
}
@@ -631,7 +641,8 @@ impl<K, V, Type, NodeType> Handle<*mut Node<K, V>, Type, NodeType> {
631641
pub unsafe fn from_raw<'a>(&'a self) -> Handle<&'a Node<K, V>, Type, NodeType> {
632642
Handle {
633643
node: &*self.node,
634-
index: self.index
644+
index: self.index,
645+
marker: PhantomData,
635646
}
636647
}
637648

@@ -641,7 +652,8 @@ impl<K, V, Type, NodeType> Handle<*mut Node<K, V>, Type, NodeType> {
641652
pub unsafe fn from_raw_mut<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, Type, NodeType> {
642653
Handle {
643654
node: &mut *self.node,
644-
index: self.index
655+
index: self.index,
656+
marker: PhantomData,
645657
}
646658
}
647659
}
@@ -689,12 +701,14 @@ impl<K, V, NodeRef: Deref<Target=Node<K, V>>, Type> Handle<NodeRef, Type, handle
689701
if self.node.is_leaf() {
690702
Leaf(Handle {
691703
node: self.node,
692-
index: self.index
704+
index: self.index,
705+
marker: PhantomData,
693706
})
694707
} else {
695708
Internal(Handle {
696709
node: self.node,
697-
index: self.index
710+
index: self.index,
711+
marker: PhantomData,
698712
})
699713
}
700714
}
@@ -827,7 +841,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::Edge, NodeType> where
827841
unsafe fn left_kv<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::KV, NodeType> {
828842
Handle {
829843
node: &mut *self.node,
830-
index: self.index - 1
844+
index: self.index - 1,
845+
marker: PhantomData,
831846
}
832847
}
833848

@@ -837,7 +852,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::Edge, NodeType> where
837852
unsafe fn right_kv<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::KV, NodeType> {
838853
Handle {
839854
node: &mut *self.node,
840-
index: self.index
855+
index: self.index,
856+
marker: PhantomData,
841857
}
842858
}
843859
}
@@ -877,7 +893,8 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<&'a mut Node<K, V>, handle::KV, NodeType
877893
pub fn into_left_edge(self) -> Handle<&'a mut Node<K, V>, handle::Edge, NodeType> {
878894
Handle {
879895
node: &mut *self.node,
880-
index: self.index
896+
index: self.index,
897+
marker: PhantomData,
881898
}
882899
}
883900
}
@@ -927,7 +944,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
927944
pub fn left_edge<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::Edge, NodeType> {
928945
Handle {
929946
node: &mut *self.node,
930-
index: self.index
947+
index: self.index,
948+
marker: PhantomData,
931949
}
932950
}
933951

@@ -936,7 +954,8 @@ impl<K, V, NodeRef, NodeType> Handle<NodeRef, handle::KV, NodeType> where
936954
pub fn right_edge<'a>(&'a mut self) -> Handle<&'a mut Node<K, V>, handle::Edge, NodeType> {
937955
Handle {
938956
node: &mut *self.node,
939-
index: self.index + 1
957+
index: self.index + 1,
958+
marker: PhantomData,
940959
}
941960
}
942961
}
@@ -1045,7 +1064,8 @@ impl<K, V> Node<K, V> {
10451064
debug_assert!(index < self.len(), "kv_handle index out of bounds");
10461065
Handle {
10471066
node: self,
1048-
index: index
1067+
index: index,
1068+
marker: PhantomData,
10491069
}
10501070
}
10511071

@@ -1065,7 +1085,7 @@ impl<K, V> Node<K, V> {
10651085
vals: RawItems::from_slice(self.vals()),
10661086
edges: RawItems::from_slice(self.edges()),
10671087

1068-
ptr: self.keys.ptr as *mut u8,
1088+
ptr: *self.keys as *mut u8,
10691089
capacity: self.capacity(),
10701090
is_leaf: self.is_leaf()
10711091
},

0 commit comments

Comments
 (0)