Skip to content

Using error as variable name in a pattern #4647

Closed
@SimonSapin

Description

@SimonSapin

So, I just wasted an hour pulling my hair out about this. Minimal program:

fn main(){
    let result = Some(~"Something went wrong!");
    match copy result {
        Some(error) => io::println(error),
        None => ()
    }
}

rustc version: 09bb07b (master from a few weeks ago)
rustc output:

fuuu.rs:4:35: 4:40 error: mismatched types: expected `&/str` but found `u32` (expected &/str but found u32)
fuuu.rs:4         Some(error) => io::println(error),
                                             ^~~~~
error: aborting due to previous error

This error message is terrible. What’s going on here is that error is a constant from libcore.

The message when using the name of a constant for a new variable is not much better:

fuuu.rs:2:8: 2:15 error: only refutable patterns allowed here
fuuu.rs:2     let error = 4;
                  ^~~~~~~
error: aborting due to previous error

Shouldn’t scoping take care of this? Variables should be able to "mask" constants as long as they’re in scope.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lints

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions