Skip to content

Commit 7a4743f

Browse files
committed
review comment: use RFC example for compile-fail/issue28498-reject-ex1.rs
(It is not *exactly* the text from the RFC, but the only thing it adds is a call to a no-op function that is just an attempt to make it clear where the potential for impl specialization comes from.)
1 parent 5708f44 commit 7a4743f

File tree

1 file changed

+18
-44
lines changed

1 file changed

+18
-44
lines changed

src/test/compile-fail/issue28498-reject-ex1.rs

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,65 +10,39 @@
1010

1111
// Example taken from RFC 1238 text
1212

13-
// https://github.com/rust-lang/rfcs/blob/master/text/1238-nonparametric-dropck.md#examples-of-code-that-will-start-to-be-rejected
13+
// https://github.com/rust-lang/rfcs/blob/master/text/1238-nonparametric-dropck.md
14+
// #examples-of-code-that-will-start-to-be-rejected
1415

1516
// Compare against test/run-pass/issue28498-must-work-ex2.rs
1617

1718
use std::cell::Cell;
1819

19-
#[derive(Copy, Clone, Debug)]
20-
enum Validity { Valid, Invalid }
21-
use self::Validity::{Valid, Invalid};
20+
struct Concrete<'a>(u32, Cell<Option<&'a Concrete<'a>>>);
2221

23-
struct Abstract<T> {
24-
id: u32,
25-
nbor: Cell<Option<T>>,
26-
valid: Validity,
27-
observe: fn(&Cell<Option<T>>) -> (u32, Validity),
28-
}
29-
30-
#[derive(Copy, Clone)]
31-
struct Neighbor<'a>(&'a Abstract<Neighbor<'a>>);
32-
33-
fn observe(c: &Cell<Option<Neighbor>>) -> (u32, Validity) {
34-
let r = c.get().unwrap().0;
35-
(r.id, r.valid)
36-
}
37-
38-
impl<'a> Abstract<Neighbor<'a>> {
39-
fn new(id: u32) -> Self {
40-
Abstract {
41-
id: id,
42-
nbor: Cell::new(None),
43-
valid: Valid,
44-
observe: observe
45-
}
46-
}
47-
}
22+
struct Foo<T> { data: Vec<T> }
4823

49-
struct Foo<T> {
50-
data: Vec<T>,
24+
fn potentially_specialized_wrt_t<T>(t: &T) {
25+
// Hypothetical code that does one thing for generic T and then is
26+
// specialized for T == Concrete (and the specialized form can
27+
// then access a reference held in concrete tuple).
28+
//
29+
// (We don't have specialization yet, but we want to allow for it
30+
// in the future.)
5131
}
5232

53-
impl<T> Drop for Abstract<T> {
33+
impl<T> Drop for Foo<T> {
5434
fn drop(&mut self) {
55-
let (nbor_id, nbor_valid) = (self.observe)(&self.nbor);
56-
println!("dropping element {} ({:?}), observed neighbor {} ({:?})",
57-
self.id,
58-
self.valid,
59-
nbor_id,
60-
nbor_valid);
61-
self.valid = Invalid;
35+
potentially_specialized_wrt_t(&self.data[0])
6236
}
6337
}
6438

6539
fn main() {
66-
let mut foo: Foo<Abstract<Neighbor>> = Foo { data: Vec::new() };
67-
foo.data.push(Abstract::new(0));
68-
foo.data.push(Abstract::new(1));
40+
let mut foo = Foo { data: Vec::new() };
41+
foo.data.push(Concrete(0, Cell::new(None)));
42+
foo.data.push(Concrete(0, Cell::new(None)));
6943

70-
foo.data[0].nbor.set(Some(Neighbor(&foo.data[1])));
44+
foo.data[0].1.set(Some(&foo.data[1]));
7145
//~^ ERROR `foo.data` does not live long enough
72-
foo.data[1].nbor.set(Some(Neighbor(&foo.data[0])));
46+
foo.data[1].1.set(Some(&foo.data[0]));
7347
//~^ ERROR `foo.data` does not live long enough
7448
}

0 commit comments

Comments
 (0)