Skip to content

Using error as variable name in a pattern #4647

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
SimonSapin opened this issue Jan 26, 2013 · 5 comments
Closed

Using error as variable name in a pattern #4647

SimonSapin opened this issue Jan 26, 2013 · 5 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@SimonSapin
Copy link
Contributor

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.

@SimonSapin
Copy link
Contributor Author

Related to #3941, but even if that is fixed I’d like scoping to allow variables (in let or in a match pattern) to mask constants. If this is decided against, at least have better error messages.

@catamorphism
Copy link
Contributor

It's intentional that variables can't shadow constants. I suppose there's a possibility to change that, but it's not something we've talked about changing.

However, I agree that the error is very bad in this case. Ideally, the error would say something like "there is a constant in scope called error with type u32, you probably meant to introduce a new variable..."

@osaut
Copy link
Contributor

osaut commented Mar 18, 2013

FWIW, the code now works as expected on incoming f54adca.

% rust run test.rs                                                          
warning: no debug symbols in executable (-arch x86_64)
Something went wrong!

@graydon
Copy link
Contributor

graydon commented Apr 29, 2013

fixed in cb37d09

@graydon graydon closed this as completed Apr 29, 2013
@brendanzab
Copy link
Member

Yeah I ran into this only refutable patterns allowed here error today too. I'm guessing it's still hanging around in stage0?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

5 participants