-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
librustc: Forbid duplicate name bindings in the same parameter or type #17880
Merged
bors
merged 1 commit into
rust-lang:master
from
pcwalton:duplicate-bindings-in-parameter-list
Oct 9, 2014
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4232,15 +4232,25 @@ impl<'a> Resolver<'a> { | |
type_parameters: TypeParameters, | ||
f: |&mut Resolver|) { | ||
match type_parameters { | ||
HasTypeParameters(generics, space, node_id, | ||
rib_kind) => { | ||
|
||
HasTypeParameters(generics, space, node_id, rib_kind) => { | ||
let mut function_type_rib = Rib::new(rib_kind); | ||
|
||
let mut seen_bindings = HashSet::new(); | ||
for (index, type_parameter) in generics.ty_params.iter().enumerate() { | ||
let ident = type_parameter.ident; | ||
debug!("with_type_parameter_rib: {} {}", node_id, | ||
type_parameter.id); | ||
|
||
if seen_bindings.contains(&ident) { | ||
self.resolve_error(type_parameter.span, | ||
format!("the name `{}` is already \ | ||
used for a type \ | ||
parameter in this type \ | ||
parameter list", | ||
token::get_ident( | ||
ident)).as_slice()) | ||
} | ||
seen_bindings.insert(ident); | ||
|
||
let def_like = DlDef(DefTyParam(space, | ||
local_def(type_parameter.id), | ||
index)); | ||
|
@@ -4313,8 +4323,8 @@ impl<'a> Resolver<'a> { | |
// Nothing to do. | ||
} | ||
Some(declaration) => { | ||
let mut bindings_list = HashMap::new(); | ||
for argument in declaration.inputs.iter() { | ||
let mut bindings_list = HashMap::new(); | ||
this.resolve_pattern(&*argument.pat, | ||
ArgumentIrrefutableMode, | ||
&mut bindings_list); | ||
|
@@ -5056,12 +5066,24 @@ impl<'a> Resolver<'a> { | |
// must not add it if it's in the bindings list | ||
// because that breaks the assumptions later | ||
// passes make about or-patterns.) | ||
|
||
if !bindings_list.contains_key(&renamed) { | ||
let this = &mut *self; | ||
let last_rib = this.value_ribs.last_mut().unwrap(); | ||
last_rib.bindings.insert(renamed, DlDef(def)); | ||
bindings_list.insert(renamed, pat_id); | ||
} else if mode == ArgumentIrrefutableMode && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this mean that the code is allowed? fn foo(ref a: uint, ref a: uint) {} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, nevermind, I think I see what's going on |
||
bindings_list.contains_key(&renamed) { | ||
// Forbid duplicate bindings in the same | ||
// parameter list. | ||
self.resolve_error(pattern.span, | ||
format!("identifier `{}` \ | ||
is bound more \ | ||
than once in \ | ||
this parameter \ | ||
list", | ||
token::get_ident( | ||
ident)) | ||
.as_slice()) | ||
} else if bindings_list.find(&renamed) == | ||
Some(&pat_id) { | ||
// Then this is a duplicate variable in the | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright 2014 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. | ||
|
||
fn f(a: int, a: int) {} | ||
//~^ ERROR identifier `a` is bound more than once in this parameter list | ||
|
||
fn main() { | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright 2014 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. | ||
|
||
type Foo<T,T> = Option<T>; | ||
//~^ ERROR the name `T` is already used | ||
|
||
struct Bar<T,T>(T); | ||
//~^ ERROR the name `T` is already used | ||
|
||
struct Baz<T,T> { | ||
//~^ ERROR the name `T` is already used | ||
x: T, | ||
} | ||
|
||
enum Boo<T,T> { | ||
//~^ ERROR the name `T` is already used | ||
A(T), | ||
B, | ||
} | ||
|
||
fn quux<T,T>(x: T) {} | ||
//~^ ERROR the name `T` is already used | ||
|
||
trait Qux<T,T> {} | ||
//~^ ERROR the name `T` is already used | ||
|
||
impl<T,T> Qux<T,T> for Option<T> {} | ||
//~^ ERROR the name `T` is already used | ||
|
||
fn main() { | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could just be
if !seen_bindings.insert(ident) { ... }