Skip to content

Commit f9c577e

Browse files
committed
Tests
1 parent 8122ce8 commit f9c577e

File tree

4 files changed

+131
-4
lines changed

4 files changed

+131
-4
lines changed

src/libsyntax/print/pprust.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1025,11 +1025,11 @@ impl<'a> State<'a> {
10251025
self.print_path(&t.path, false)
10261026
}
10271027

1028-
fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) -> IoResult<()> {
1029-
if !t.bound_lifetimes.is_empty() {
1028+
fn print_formal_lifetime_list(&mut self, lifetimes: &[ast::LifetimeDef]) -> IoResult<()> {
1029+
if !lifetimes.is_empty() {
10301030
try!(word(&mut self.s, "for<"));
10311031
let mut comma = false;
1032-
for lifetime_def in &t.bound_lifetimes {
1032+
for lifetime_def in lifetimes {
10331033
if comma {
10341034
try!(self.word_space(","))
10351035
}
@@ -1038,7 +1038,11 @@ impl<'a> State<'a> {
10381038
}
10391039
try!(word(&mut self.s, ">"));
10401040
}
1041+
Ok(())
1042+
}
10411043

1044+
fn print_poly_trait_ref(&mut self, t: &ast::PolyTraitRef) -> IoResult<()> {
1045+
try!(self.print_formal_lifetime_list(&t.bound_lifetimes));
10421046
self.print_trait_ref(&t.trait_ref)
10431047
}
10441048

@@ -2517,9 +2521,11 @@ impl<'a> State<'a> {
25172521
}
25182522

25192523
match predicate {
2520-
&ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bounded_ty,
2524+
&ast::WherePredicate::BoundPredicate(ast::WhereBoundPredicate{ref bound_lifetimes,
2525+
ref bounded_ty,
25212526
ref bounds,
25222527
..}) => {
2528+
try!(self.print_formal_lifetime_list(bound_lifetimes));
25232529
try!(self.print_type(&**bounded_ty));
25242530
try!(self.print_bounds(":", bounds));
25252531
}
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
// Test that we can quantify lifetimes outside a constraint (i.e., including
12+
// the self type) in a where clause. Specifically, test that implementing for a
13+
// specific lifetime is not enough to satisify the `for<'a> ...` constraint, which
14+
// should require *all* lifetimes.
15+
16+
static X: &'static u32 = &42;
17+
18+
trait Bar {
19+
fn bar(&self);
20+
}
21+
22+
impl Bar for &'static u32 {
23+
fn bar(&self) {}
24+
}
25+
26+
fn foo<T>(x: &T)
27+
where for<'a> &'a T: Bar
28+
{}
29+
30+
fn main() {
31+
foo(&X);
32+
//~^ error: `for<'a> Bar` is not implemented
33+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
// Test that we can quantify lifetimes outside a constraint (i.e., including
12+
// the self type) in a where clause. Specifically, test that we cannot nest
13+
// quantification in constraints (to be clear, there is no reason this should not
14+
// we're testing we don't crash or do something stupid).
15+
16+
trait Bar<'a> {
17+
fn bar(&self);
18+
}
19+
20+
impl<'a, 'b> Bar<'b> for &'a u32 {
21+
fn bar(&self) {}
22+
}
23+
24+
fn foo<T>(x: &T)
25+
where for<'a> &'a T: for<'b> Bar<'b>
26+
//~^ error: nested quantification of lifetimes
27+
{}
28+
29+
fn main() {}

src/test/run-pass/where-for-self.rs

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
// Test that we can quantify lifetimes outside a constraint (i.e., including
12+
// the self type) in a where clause.
13+
14+
static mut COUNT: u32 = 1;
15+
16+
trait Bar<'a> {
17+
fn bar(&self);
18+
}
19+
20+
trait Baz<'a> {
21+
fn baz(&self);
22+
}
23+
24+
impl<'a, 'b> Bar<'b> for &'a u32 {
25+
fn bar(&self) {
26+
unsafe { COUNT *= 2; }
27+
}
28+
}
29+
30+
impl<'a, 'b> Baz<'b> for &'a u32 {
31+
fn baz(&self) {
32+
unsafe { COUNT *= 3; }
33+
}
34+
}
35+
36+
// Test we can use the syntax for HRL including the self type.
37+
fn foo1<T>(x: &T)
38+
where for<'a, 'b> &'a T: Bar<'b>
39+
{
40+
x.bar()
41+
}
42+
43+
// Test we can quantify multiple bounds (i.e., the precedence is sensible).
44+
fn foo2<T>(x: &T)
45+
where for<'a, 'b> &'a T: Bar<'b> + Baz<'b>
46+
{
47+
x.baz();
48+
x.bar()
49+
}
50+
51+
fn main() {
52+
let x = 42u32;
53+
foo1(&x);
54+
foo2(&x);
55+
unsafe {
56+
assert!(COUNT == 12);
57+
}
58+
}
59+

0 commit comments

Comments
 (0)