Skip to content
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

Better error on attempt to rebind identifier for static constant. #9683

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4336,9 +4336,11 @@ impl Resolver {
self.record_def(pattern.id, def);
}
FoundConst(_) => {
self.resolve_error(pattern.span,
"only refutable patterns \
allowed here");
let msg = format!("only irrefutable patterns \
allowed here; `{}` is a static \
constant",
interner_get(renamed));
self.resolve_error(pattern.span, msg);
}
BareIdentifierPatternUnresolved => {
debug2!("(resolving pattern) binding `{}`",
Expand Down
20 changes: 20 additions & 0 deletions src/test/compile-fail/refutable-static-in-binding-position.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use b = m::a;

mod m {
pub static a : int = 4;
}

fn main() {
let b = 4; //~ERROR only irrefutable patterns allowed here
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this basically testing what the behavior already was? It'd be nice to have the 'static constant' message also asserted here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I did fix the message to say "irrefutable" instead of "refutable", so technically, this test would flag the previous behavior as wrong. :)

I can throw in the static constant bit, I guess. I don't know what rhyme or reason is typically followed when deciding how much of the error message to include. (I've been idly trying to figure out if I could actually point the user at the span of the relevant import or definition of the static constant, to make things crystal clear...)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good point, sorry I missed that! I think that the ability is being added soon as well to have //~ ERROR long message [...] other contents with the discriminant pull request, so you'd be able to omit the middle stuff.

A NOTE would be nice, but probably not necessary, especially if you mention that it's already a static constant.

println!("{}", b);
}