Add tests

This commit is contained in:
Michael Goulet 2022-08-31 04:46:54 +00:00
parent e8fa74ae45
commit 81badff860
8 changed files with 140 additions and 0 deletions

View File

@ -0,0 +1,5 @@
trait Foo {
fn bar() -> impl Sized; //~ ERROR `impl Trait` only allowed in function and inherent method return types, not in trait method return
}
fn main() {}

View File

@ -0,0 +1,9 @@
error[E0562]: `impl Trait` only allowed in function and inherent method return types, not in trait method return
--> $DIR/feature-gate-return_position_impl_trait_in_trait.rs:2:17
|
LL | fn bar() -> impl Sized;
| ^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0562`.

View File

@ -0,0 +1,13 @@
#![feature(return_position_impl_trait_in_trait)]
#![allow(incomplete_features)]
trait Foo {
fn bar() -> impl std::fmt::Display;
}
impl Foo for () {
fn bar() -> () {}
//~^ ERROR `()` doesn't implement `std::fmt::Display`
}
fn main() {}

View File

@ -0,0 +1,17 @@
error[E0277]: `()` doesn't implement `std::fmt::Display`
--> $DIR/doesnt-satisfy.rs:9:17
|
LL | fn bar() -> () {}
| ^^ `()` cannot be formatted with the default formatter
|
= help: the trait `std::fmt::Display` is not implemented for `()`
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
note: required by a bound in `Foo::bar::{opaque#0}`
--> $DIR/doesnt-satisfy.rs:5:22
|
LL | fn bar() -> impl std::fmt::Display;
| ^^^^^^^^^^^^^^^^^ required by this bound in `Foo::bar::{opaque#0}`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0277`.

View File

@ -0,0 +1,19 @@
#![feature(return_position_impl_trait_in_trait)]
#![allow(incomplete_features)]
use std::fmt::Display;
trait Foo {
fn bar(&self) -> impl Display;
}
impl Foo for () {
fn bar(&self) -> impl Display {
"Hello, world"
}
}
fn main() {
let x: &str = ().bar();
//~^ ERROR mismatched types
}

View File

@ -0,0 +1,17 @@
error[E0308]: mismatched types
--> $DIR/opaque-in-impl-is-opaque.rs:17:19
|
LL | fn bar(&self) -> impl Display {
| ------------ the found opaque type
...
LL | let x: &str = ().bar();
| ---- ^^^^^^^^ expected `&str`, found opaque type
| |
| expected due to this
|
= note: expected reference `&str`
found opaque type `impl std::fmt::Display`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.

View File

@ -0,0 +1,20 @@
// check-pass
#![feature(return_position_impl_trait_in_trait)]
#![allow(incomplete_features)]
use std::fmt::Display;
trait Foo {
fn bar(&self) -> impl Display;
}
impl Foo for () {
fn bar(&self) -> impl Display {
"Hello, world"
}
}
fn main() {
println!("{}", ().bar());
}

View File

@ -0,0 +1,40 @@
// check-pass
#![feature(return_position_impl_trait_in_trait)]
#![allow(incomplete_features)]
use std::fmt::Display;
trait Foo {
fn bar(&self) -> impl Display;
}
impl Foo for i32 {
fn bar(&self) -> i32 {
*self
}
}
impl Foo for &'static str {
fn bar(&self) -> &'static str {
*self
}
}
struct Yay;
impl Foo for Yay {
fn bar(&self) -> String {
String::from(":^)")
}
}
fn foo_generically<T: Foo>(t: T) {
println!("{}", t.bar());
}
fn main() {
println!("{}", "Hello, world.".bar());
println!("The answer is {}!", 42.bar());
foo_generically(Yay);
}