Skip to content

try_blocks type inference failure suggests illegal code #102582

@joshlf

Description

@joshlf

I have the following code:

#![feature(try_blocks)]

fn main() {
    println!(
        "{:?}",
        try {
            if true {
                Ok(0usize)
            } else {
                Err(true)
            }
        }
    );
}

I get the following error:

error[[E0282]](https://doc.rust-lang.org/nightly/error-index.html#E0282): type annotations needed
  --> src/main.rs:6:9
   |
6  | /         try {
7  | |             if true {
8  | |                 Ok(0usize)
9  | |             } else {
10 | |                 Err(true)
11 | |             }
12 | |         }
   | |_________^ cannot infer type of the type parameter `T` declared on the associated function `new_debug`
   |
   = note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider specifying the generic argument
   |
12 |         }::<T>
   |          +++++

If I try to follow the compiler's advice by adding a trailing ::<Result<usize, bool>> on line 12, it results in code that can't be parsed:

error: expected one of `,`, `.`, `?`, `catch`, or an operator, found `::`
  --> src/main.rs:12:10
   |
12 |         }::<Result<usize, bool>>
   |          ^^ expected one of `,`, `.`, `?`, `catch`, or an operator

Version

Current nightly (2022-10-01 57f097e)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions