Skip to content

Commit 9b0dfe1

Browse files
committed
Add tests for trivial bounds
1 parent dabb820 commit 9b0dfe1

17 files changed

+587
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2018 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+
// run-pass
12+
// Inconsistent bounds with trait implementations
13+
14+
#![feature(trivial_bounds)]
15+
#![allow(unused)]
16+
17+
trait A {
18+
fn foo(&self) -> Self where Self: Copy;
19+
}
20+
21+
impl A for str {
22+
fn foo(&self) -> Self where Self: Copy { *"" }
23+
}
24+
25+
impl A for i32 {
26+
fn foo(&self) -> Self { 3 }
27+
}
28+
29+
fn main() {}

src/test/ui/trivial-bounds-inconsistent-associated-functions.stderr

Whitespace-only changes.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0596]: cannot borrow immutable item `**t` as mutable
2+
--> $DIR/trivial-bounds-inconsistent-copy-reborrow.rs:16:5
3+
|
4+
LL | *t //~ ERROR
5+
| ^^ cannot borrow as mutable
6+
|
7+
= note: the value which is causing this path not to be mutable is...: `*t`
8+
9+
error[E0596]: cannot borrow immutable item `**t` as mutable
10+
--> $DIR/trivial-bounds-inconsistent-copy-reborrow.rs:20:6
11+
|
12+
LL | {*t} //~ ERROR
13+
| ^^ cannot borrow as mutable
14+
|
15+
= note: the value which is causing this path not to be mutable is...: `*t`
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0596`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 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+
// Check that reborrows are still illegal with Copy mutable references
12+
#![feature(trivial_bounds)]
13+
#![allow(unused)]
14+
15+
fn reborrow_mut<'a>(t: &'a &'a mut i32) -> &'a mut i32 where &'a mut i32: Copy {
16+
*t //~ ERROR
17+
}
18+
19+
fn copy_reborrow_mut<'a>(t: &'a &'a mut i32) -> &'a mut i32 where &'a mut i32: Copy {
20+
{*t} //~ ERROR
21+
}
22+
23+
fn main() {}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0389]: cannot borrow data mutably in a `&` reference
2+
--> $DIR/trivial-bounds-inconsistent-copy-reborrow.rs:16:5
3+
|
4+
LL | fn reborrow_mut<'a>(t: &'a &'a mut i32) -> &'a mut i32 where &'a mut i32: Copy {
5+
| --------------- use `&'a mut &'a mut i32` here to make mutable
6+
LL | *t //~ ERROR
7+
| ^^ assignment into an immutable reference
8+
9+
error[E0389]: cannot borrow data mutably in a `&` reference
10+
--> $DIR/trivial-bounds-inconsistent-copy-reborrow.rs:20:6
11+
|
12+
LL | fn copy_reborrow_mut<'a>(t: &'a &'a mut i32) -> &'a mut i32 where &'a mut i32: Copy {
13+
| --------------- use `&'a mut &'a mut i32` here to make mutable
14+
LL | {*t} //~ ERROR
15+
| ^^ assignment into an immutable reference
16+
17+
error: aborting due to 2 previous errors
18+
19+
For more information about this error, try `rustc --explain E0389`.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Copyright 2018 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+
// run-pass
12+
// Check tautalogically false `Copy` bounds
13+
#![feature(trivial_bounds)]
14+
#![allow(unused)]
15+
16+
fn copy_string(t: String) -> String where String: Copy {
17+
is_copy(&t);
18+
let x = t;
19+
drop(t);
20+
t
21+
}
22+
23+
fn copy_out_string(t: &String) -> String where String: Copy {
24+
*t
25+
}
26+
27+
fn copy_string_with_param<T>(x: String) where String: Copy {
28+
let y = x;
29+
let z = x;
30+
}
31+
32+
// Check that no reborrowing occurs
33+
fn copy_mut<'a>(t: &&'a mut i32) -> &'a mut i32 where for<'b> &'b mut i32: Copy {
34+
is_copy(t);
35+
let x = *t;
36+
drop(x);
37+
x
38+
}
39+
40+
fn is_copy<T: Copy>(t: &T) {}
41+
42+
43+
fn main() {}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
warning: Trait bound std::string::String: std::marker::Copy does not depend on any type or lifetime parameters
2+
--> $DIR/trivial-bounds-inconsistent-copy.rs:16:1
3+
|
4+
LL | / fn copy_string(t: String) -> String where String: Copy {
5+
LL | | is_copy(&t);
6+
LL | | let x = t;
7+
LL | | drop(t);
8+
LL | | t
9+
LL | | }
10+
| |_^
11+
|
12+
= note: #[warn(trivial_bounds)] on by default
13+
14+
warning: Trait bound std::string::String: std::marker::Copy does not depend on any type or lifetime parameters
15+
--> $DIR/trivial-bounds-inconsistent-copy.rs:23:1
16+
|
17+
LL | / fn copy_out_string(t: &String) -> String where String: Copy {
18+
LL | | *t
19+
LL | | }
20+
| |_^
21+
22+
warning: Trait bound std::string::String: std::marker::Copy does not depend on any type or lifetime parameters
23+
--> $DIR/trivial-bounds-inconsistent-copy.rs:27:1
24+
|
25+
LL | / fn copy_string_with_param<T>(x: String) where String: Copy {
26+
LL | | let y = x;
27+
LL | | let z = x;
28+
LL | | }
29+
| |_^
30+
31+
warning: Trait bound for<'b> &'b mut i32: std::marker::Copy does not depend on any type or lifetime parameters
32+
--> $DIR/trivial-bounds-inconsistent-copy.rs:33:1
33+
|
34+
LL | / fn copy_mut<'a>(t: &&'a mut i32) -> &'a mut i32 where for<'b> &'b mut i32: Copy {
35+
LL | | is_copy(t);
36+
LL | | let x = *t;
37+
LL | | drop(x);
38+
LL | | x
39+
LL | | }
40+
| |_^
41+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2018 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+
// run-pass
12+
// Check tautalogically false `Sized` bounds
13+
#![feature(trivial_bounds)]
14+
#![allow(unused)]
15+
16+
trait A {}
17+
18+
impl A for i32 {}
19+
20+
struct T<X: ?Sized> {
21+
x: X,
22+
}
23+
24+
struct S(str, str) where str: Sized;
25+
26+
fn unsized_local() where for<'a> T<A + 'a>: Sized {
27+
let x: T<A> = *(Box::new(T { x: 1 }) as Box<T<A>>);
28+
}
29+
30+
fn return_str() -> str where str: Sized {
31+
*"Sized".to_string().into_boxed_str()
32+
}
33+
34+
fn main() {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
warning: Trait bound str: std::marker::Sized does not depend on any type or lifetime parameters
2+
--> $DIR/trivial-bounds-inconsistent-sized.rs:24:1
3+
|
4+
LL | struct S(str, str) where str: Sized;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: #[warn(trivial_bounds)] on by default
8+
9+
warning: Trait bound for<'a> T<A + 'a>: std::marker::Sized does not depend on any type or lifetime parameters
10+
--> $DIR/trivial-bounds-inconsistent-sized.rs:26:1
11+
|
12+
LL | / fn unsized_local() where for<'a> T<A + 'a>: Sized {
13+
LL | | let x: T<A> = *(Box::new(T { x: 1 }) as Box<T<A>>);
14+
LL | | }
15+
| |_^
16+
17+
warning: Trait bound str: std::marker::Sized does not depend on any type or lifetime parameters
18+
--> $DIR/trivial-bounds-inconsistent-sized.rs:30:1
19+
|
20+
LL | / fn return_str() -> str where str: Sized {
21+
LL | | *"Sized".to_string().into_boxed_str()
22+
LL | | }
23+
| |_^
24+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 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+
// run-pass
12+
// Test that inconsistent bounds are used in well-formedness checks
13+
#![feature(trivial_bounds)]
14+
15+
use std::fmt::Debug;
16+
17+
pub fn foo() where Vec<str>: Debug, str: Copy {
18+
let x = vec![*"1"];
19+
println!("{:?}", x);
20+
}
21+
22+
fn main() {}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: Trait bound std::vec::Vec<str>: std::fmt::Debug does not depend on any type or lifetime parameters
2+
--> $DIR/trivial-bounds-inconsistent-well-formed.rs:17:1
3+
|
4+
LL | / pub fn foo() where Vec<str>: Debug, str: Copy {
5+
LL | | let x = vec![*"1"];
6+
LL | | println!("{:?}", x);
7+
LL | | }
8+
| |_^
9+
|
10+
= note: #[warn(trivial_bounds)] on by default
11+
12+
warning: Trait bound str: std::marker::Copy does not depend on any type or lifetime parameters
13+
--> $DIR/trivial-bounds-inconsistent-well-formed.rs:17:1
14+
|
15+
LL | / pub fn foo() where Vec<str>: Debug, str: Copy {
16+
LL | | let x = vec![*"1"];
17+
LL | | println!("{:?}", x);
18+
LL | | }
19+
| |_^
20+
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// Copyright 2018 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+
// run-pass
12+
13+
// Check that tautalogically false bounds are accepted, and are used
14+
// in type inference.
15+
#![feature(trivial_bounds)]
16+
#![allow(unused)]
17+
18+
pub trait Foo {
19+
fn test(&self);
20+
}
21+
22+
fn generic_function<X: Foo>(x: X) {}
23+
24+
enum E where i32: Foo { V }
25+
26+
struct S where i32: Foo;
27+
28+
trait T where i32: Foo {}
29+
30+
union U where i32: Foo { f: i32 }
31+
32+
type Y where i32: Foo = ();
33+
34+
impl Foo for () where i32: Foo {
35+
fn test(&self) {
36+
3i32.test();
37+
Foo::test(&4i32);
38+
generic_function(5i32);
39+
}
40+
}
41+
42+
fn f() where i32: Foo {
43+
let s = S;
44+
3i32.test();
45+
Foo::test(&4i32);
46+
generic_function(5i32);
47+
}
48+
49+
fn g() where &'static str: Foo {
50+
"Foo".test();
51+
Foo::test(&"Foo");
52+
generic_function("Foo");
53+
}
54+
55+
trait A {}
56+
57+
impl A for i32 {}
58+
59+
struct Dst<X: ?Sized> {
60+
x: X,
61+
}
62+
63+
struct TwoStrs(str, str) where str: Sized;
64+
65+
fn unsized_local() where for<'a> Dst<A + 'a>: Sized {
66+
let x: Dst<A> = *(Box::new(Dst { x: 1 }) as Box<Dst<A>>);
67+
}
68+
69+
fn return_str() -> str where str: Sized {
70+
*"Sized".to_string().into_boxed_str()
71+
}
72+
73+
fn use_op(s: String) -> String where String: ::std::ops::Neg<Output=String> {
74+
-s
75+
}
76+
77+
fn use_for() where i32: Iterator {
78+
for _ in 2i32 {}
79+
}
80+
81+
fn main() {}

0 commit comments

Comments
 (0)