-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Suggest constraint on
impl Trait
in return type
Fix #71035.
- Loading branch information
Showing
3 changed files
with
100 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/ui/associated-types/impl-trait-return-missing-constraint.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
trait Foo { | ||
type Item; | ||
} | ||
|
||
trait Bar: Foo {} | ||
|
||
struct S; | ||
|
||
impl Foo for S { | ||
type Item = i32; | ||
} | ||
impl Bar for S {} | ||
|
||
struct T; | ||
|
||
impl Foo for T { | ||
type Item = u32; | ||
} | ||
impl Bar for T {} | ||
|
||
fn bar() -> impl Bar { | ||
T | ||
} | ||
|
||
fn baz() -> impl Bar<Item = i32> { | ||
//~^ ERROR type mismatch resolving `<impl Bar as Foo>::Item == i32` | ||
bar() | ||
} | ||
|
||
fn main() { | ||
let _ = baz(); | ||
} | ||
|
20 changes: 20 additions & 0 deletions
20
src/test/ui/associated-types/impl-trait-return-missing-constraint.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
error[E0271]: type mismatch resolving `<impl Bar as Foo>::Item == i32` | ||
--> $DIR/impl-trait-return-missing-constraint.rs:25:13 | ||
| | ||
LL | fn bar() -> impl Bar { | ||
| -------- the expected opaque type | ||
... | ||
LL | fn baz() -> impl Bar<Item = i32> { | ||
| ^^^^^^^^^^^^^^^^^^^^ expected associated type, found `i32` | ||
| | ||
= note: expected associated type `<impl Bar as Foo>::Item` | ||
found type `i32` | ||
= note: the return type of a function must have a statically known size | ||
help: consider constraining the associated type `<impl Bar as Foo>::Item` to `i32` | ||
| | ||
LL | fn bar() -> impl Bar<Item = i32> { | ||
| ^^^^^^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0271`. |