Skip to content

Commit 81badff

Browse files
Add tests
1 parent e8fa74a commit 81badff

8 files changed

+140
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
trait Foo {
2+
fn bar() -> impl Sized; //~ ERROR `impl Trait` only allowed in function and inherent method return types, not in trait method return
3+
}
4+
5+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in trait method return
2+
--> $DIR/feature-gate-return_position_impl_trait_in_trait.rs:2:17
3+
|
4+
LL | fn bar() -> impl Sized;
5+
| ^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0562`.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(return_position_impl_trait_in_trait)]
2+
#![allow(incomplete_features)]
3+
4+
trait Foo {
5+
fn bar() -> impl std::fmt::Display;
6+
}
7+
8+
impl Foo for () {
9+
fn bar() -> () {}
10+
//~^ ERROR `()` doesn't implement `std::fmt::Display`
11+
}
12+
13+
fn main() {}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0277]: `()` doesn't implement `std::fmt::Display`
2+
--> $DIR/doesnt-satisfy.rs:9:17
3+
|
4+
LL | fn bar() -> () {}
5+
| ^^ `()` cannot be formatted with the default formatter
6+
|
7+
= help: the trait `std::fmt::Display` is not implemented for `()`
8+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
9+
note: required by a bound in `Foo::bar::{opaque#0}`
10+
--> $DIR/doesnt-satisfy.rs:5:22
11+
|
12+
LL | fn bar() -> impl std::fmt::Display;
13+
| ^^^^^^^^^^^^^^^^^ required by this bound in `Foo::bar::{opaque#0}`
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0277`.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(return_position_impl_trait_in_trait)]
2+
#![allow(incomplete_features)]
3+
4+
use std::fmt::Display;
5+
6+
trait Foo {
7+
fn bar(&self) -> impl Display;
8+
}
9+
10+
impl Foo for () {
11+
fn bar(&self) -> impl Display {
12+
"Hello, world"
13+
}
14+
}
15+
16+
fn main() {
17+
let x: &str = ().bar();
18+
//~^ ERROR mismatched types
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/opaque-in-impl-is-opaque.rs:17:19
3+
|
4+
LL | fn bar(&self) -> impl Display {
5+
| ------------ the found opaque type
6+
...
7+
LL | let x: &str = ().bar();
8+
| ---- ^^^^^^^^ expected `&str`, found opaque type
9+
| |
10+
| expected due to this
11+
|
12+
= note: expected reference `&str`
13+
found opaque type `impl std::fmt::Display`
14+
15+
error: aborting due to previous error
16+
17+
For more information about this error, try `rustc --explain E0308`.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// check-pass
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
use std::fmt::Display;
7+
8+
trait Foo {
9+
fn bar(&self) -> impl Display;
10+
}
11+
12+
impl Foo for () {
13+
fn bar(&self) -> impl Display {
14+
"Hello, world"
15+
}
16+
}
17+
18+
fn main() {
19+
println!("{}", ().bar());
20+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// check-pass
2+
3+
#![feature(return_position_impl_trait_in_trait)]
4+
#![allow(incomplete_features)]
5+
6+
use std::fmt::Display;
7+
8+
trait Foo {
9+
fn bar(&self) -> impl Display;
10+
}
11+
12+
impl Foo for i32 {
13+
fn bar(&self) -> i32 {
14+
*self
15+
}
16+
}
17+
18+
impl Foo for &'static str {
19+
fn bar(&self) -> &'static str {
20+
*self
21+
}
22+
}
23+
24+
struct Yay;
25+
26+
impl Foo for Yay {
27+
fn bar(&self) -> String {
28+
String::from(":^)")
29+
}
30+
}
31+
32+
fn foo_generically<T: Foo>(t: T) {
33+
println!("{}", t.bar());
34+
}
35+
36+
fn main() {
37+
println!("{}", "Hello, world.".bar());
38+
println!("The answer is {}!", 42.bar());
39+
foo_generically(Yay);
40+
}

0 commit comments

Comments
 (0)