Skip to content

Try blocks give false errors (type-mismatch false positive) #11843

@Kixiron

Description

@Kixiron

Rust Analyzer sometimes gives errors on try blocks where there shouldn't be any, e.g.

let _: Option<()> = try {
    let first_rhs = self.values.get(graph.input_source(sub.rhs()))?;

    let lhs_sub = *graph.cast_input_source::<Sub>(sub.lhs())?;
    let second_rhs = self.values.get(graph.input_source(lhs_sub.rhs()))?;

    let sum = first_rhs + second_rhs;
    let new_rhs = graph.constant(sum);
    self.values.add(new_rhs.value(), sum);

    graph.remove_inputs(sub.node());

    let value = graph.input_source(lhs_sub.lhs());
    graph.add_value_edge(value, sub.lhs());
    graph.add_value_edge(new_rhs.value(), sub.rhs());

    self.changes.inc::<"sub-sub">();
};

This code is correct (and rustc doesn't complain), but RA emits this error:
image

Changing it as suggested (adding a Some(()) or None to the end of the block) is incorrect and (correctly) causes both rustc and RA to emit errors

rust-analyzer version: bc08b8eff 2022-03-28 stable

rustc version: (eg. output of rustc -V)

rustc 1.60.0-nightly (777bb86bc 2022-01-20)
binary: rustc
commit-hash: 777bb86bcdbc568be7cff6eeeaaf81a89b4aa50b
commit-date: 2022-01-20
host: x86_64-pc-windows-msvc
release: 1.60.0-nightly
LLVM version: 13.0.0

relevant settings: N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-tytype system / type inference / traits / method resolutionC-featureCategory: feature request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions