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

librustc: Forbid duplicate name bindings in the same parameter or type #17880

Merged
Merged
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
34 changes: 28 additions & 6 deletions src/librustc/middle/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

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) { ... }

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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 &&
Copy link
Member

Choose a reason for hiding this comment

The 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) {}

Copy link
Member

Choose a reason for hiding this comment

The 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
Expand Down
16 changes: 16 additions & 0 deletions src/test/compile-fail/duplicate-parameter.rs
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() {
}

39 changes: 39 additions & 0 deletions src/test/compile-fail/duplicate-type-parameter.rs
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() {
}