-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Description
Given the following code: [playground]
#![feature(try_blocks)]
fn main() {
let _ = try {
Err::<(), _>("lol")?
};
}
The current output is:
error[[E0284]](https://doc.rust-lang.org/nightly/error-index.html#E0284): type annotations needed
--> src/main.rs:4:9
|
4 | let _ = try {
| ^
5 | Err::<(), _>("lol")?
| -------------------- type must be known at this point
|
= note: cannot satisfy `<_ as Try>::Residual == _`
help: consider giving this pattern a type
|
4 | let _: _ = try {
| +++
... alright, I'll try it
error[[E0282]](https://doc.rust-lang.org/nightly/error-index.html#E0282): type annotations needed
--> src/main.rs:4:12
|
4 | let _: _ = try {
| ^ cannot infer type
The same <_ as Try>::Residual == _
error and : _
help occurs with just Err("lol")?
, but the example here gives the Ok
type to give the compiler maximal information to present a better error. Unless I misunderstand the try
desugaring (likely), the error should at least be <_ as Try>::Residual == Result<Infallible, &str>
?
The suggestion is perhaps fine, since it's (probably?) not possible to give a better suggestion, but if the suggestion is kept, at a minimum the follow-up error after applying the hint shouldn't lose information; ideally it should be possible to notice that this is a try
block type ascription and provide a hint as to what the compiler does know, and why it needs help.
The minimum amount of type information that I could get to compile is let _: Result<_, &str> = try { Err("lol")? };
.
This will likely get better "for free" if try
is changed to be less general. (I recall a discussed possibility about requiring the Residual
type to match, in order to make type inference work in more cases, of which I think this is one that's ideally supposed to work without further annotation.)
@rustbot label +F-try_blocks +requires-nightly +D-invalid-suggestion
(rather, D-unhelpful-suggestion)