-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #131549 - compiler-errors:try-in-sync, r=spastorino
Add a note for `?` on a `impl Future<Output = Result<..>>` in sync function It's confusing to `?` a future of a result in a sync function. We have a suggestion to `.await` it if we're in an async function, but not a sync function. Note that this is the case for sync functions, at least. Let's be a bit more vague about a fix, since it's somewhat context dependent. For example, you could block on it, or you could make your function asynchronous. 🤷
- Loading branch information
Showing
7 changed files
with
135 additions
and
52 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
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
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,13 @@ | ||
//@ edition: 2021 | ||
|
||
struct S { | ||
field: (), | ||
} | ||
|
||
async fn foo() -> S { todo!() } | ||
|
||
fn main() -> Result<(), ()> { | ||
foo().field; | ||
//~^ ERROR no field `field` on type `impl Future<Output = S>` | ||
Ok(()) | ||
} |
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,17 @@ | ||
error[E0609]: no field `field` on type `impl Future<Output = S>` | ||
--> $DIR/field-in-sync.rs:10:11 | ||
| | ||
LL | foo().field; | ||
| ^^^^^ field not available in `impl Future`, but it is available in its `Output` | ||
| | ||
note: this implements `Future` and its output type has the field, but the future cannot be awaited in a synchronous function | ||
--> $DIR/field-in-sync.rs:10:5 | ||
| | ||
LL | fn main() -> Result<(), ()> { | ||
| --------------------------- this is not `async` | ||
LL | foo().field; | ||
| ^^^^^ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0609`. |
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
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,9 @@ | ||
//@ edition: 2021 | ||
|
||
async fn foo() -> Result<(), ()> { todo!() } | ||
|
||
fn main() -> Result<(), ()> { | ||
foo()?; | ||
//~^ ERROR the `?` operator can only be applied to values that implement `Try` | ||
Ok(()) | ||
} |
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,18 @@ | ||
error[E0277]: the `?` operator can only be applied to values that implement `Try` | ||
--> $DIR/try-in-sync.rs:6:5 | ||
| | ||
LL | foo()?; | ||
| ^^^^^^ the `?` operator cannot be applied to type `impl Future<Output = Result<(), ()>>` | ||
| | ||
= help: the trait `Try` is not implemented for `impl Future<Output = Result<(), ()>>` | ||
note: this implements `Future` and its output type supports `?`, but the future cannot be awaited in a synchronous function | ||
--> $DIR/try-in-sync.rs:6:10 | ||
| | ||
LL | fn main() -> Result<(), ()> { | ||
| --------------------------- this is not `async` | ||
LL | foo()?; | ||
| ^ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |