Skip to content

Commit 6afda64

Browse files
committed
rollup merge of rust-lang#20728: huonw/type-param-shadowing
Conflicts: src/librustc_typeck/check/wf.rs
2 parents 773fdb3 + 92cd8ea commit 6afda64

File tree

3 files changed

+78
-2
lines changed

3 files changed

+78
-2
lines changed

src/librustc_typeck/check/wf.rs

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use syntax::ast;
2424
use syntax::ast_util::{local_def};
2525
use syntax::attr;
2626
use syntax::codemap::Span;
27+
use syntax::parse::token;
2728
use syntax::visit;
2829
use syntax::visit::Visitor;
2930

@@ -281,12 +282,45 @@ fn reject_non_type_param_bounds<'tcx>(tcx: &ty::ctxt<'tcx>,
281282
}
282283
}
283284

285+
fn reject_shadowing_type_parameters<'tcx>(tcx: &ty::ctxt<'tcx>,
286+
span: Span,
287+
generics: &ty::Generics<'tcx>) {
288+
let impl_params = generics.types.get_slice(subst::TypeSpace).iter()
289+
.map(|tp| tp.name).collect::<HashSet<_>>();
290+
291+
for method_param in generics.types.get_slice(subst::FnSpace).iter() {
292+
if impl_params.contains(&method_param.name) {
293+
tcx.sess.span_err(
294+
span,
295+
&*format!("type parameter `{}` shadows another type parameter of the same name",
296+
token::get_name(method_param.name)));
297+
}
298+
}
299+
}
300+
284301
impl<'ccx, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'ccx, 'tcx> {
285302
fn visit_item(&mut self, i: &ast::Item) {
286303
self.check_item_well_formed(i);
287304
visit::walk_item(self, i);
288305
}
289306

307+
fn visit_fn(&mut self,
308+
fk: visit::FnKind<'v>, fd: &'v ast::FnDecl,
309+
b: &'v ast::Block, span: Span, id: ast::NodeId) {
310+
match fk {
311+
visit::FkFnBlock | visit::FkItemFn(..) => {}
312+
visit::FkMethod(..) => {
313+
match ty::impl_or_trait_item(self.ccx.tcx, local_def(id)) {
314+
ty::ImplOrTraitItem::MethodTraitItem(ty_method) => {
315+
reject_shadowing_type_parameters(self.ccx.tcx, span, &ty_method.generics)
316+
}
317+
_ => {}
318+
}
319+
}
320+
}
321+
visit::walk_fn(self, fk, fd, b, span)
322+
}
323+
290324
fn visit_trait_item(&mut self, t: &'v ast::TraitItem) {
291325
match t {
292326
&ast::TraitItem::ProvidedMethod(_) |
@@ -297,12 +331,18 @@ impl<'ccx, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'ccx, 'tcx> {
297331
reject_non_type_param_bounds(
298332
self.ccx.tcx,
299333
method.span,
300-
&ty_method.generics)
334+
&ty_method.generics);
335+
reject_shadowing_type_parameters(
336+
self.ccx.tcx,
337+
method.span,
338+
&ty_method.generics);
301339
}
302340
_ => {}
303341
}
304342
}
305343
}
344+
345+
visit::walk_trait_item(self, t)
306346
}
307347
}
308348

src/libserialize/json.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ impl<T: Iterator<Item=char>> Parser<T> {
13961396
self.ch == Some(c)
13971397
}
13981398

1399-
fn error<T>(&self, reason: ErrorCode) -> Result<T, ParserError> {
1399+
fn error<U>(&self, reason: ErrorCode) -> Result<U, ParserError> {
14001400
Err(SyntaxError(reason, self.line, self.col))
14011401
}
14021402

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that shadowed lifetimes generate an error.
12+
13+
struct Foo<T>;
14+
15+
impl<T> Foo<T> {
16+
fn shadow_in_method<T>(&self) {}
17+
//~^ ERROR type parameter `T` shadows another type parameter
18+
19+
fn not_shadow_in_item<U>(&self) {
20+
struct Bar<T, U>; // not a shadow, separate item
21+
fn foo<T, U>() {} // same
22+
}
23+
}
24+
25+
trait<T> Bar<T> {
26+
fn shadow_in_required<T>(&self);
27+
//~^ ERROR type parameter `T` shadows another type parameter
28+
29+
fn shadow_in_provided<T>(&self) {}
30+
//~^ ERROR type parameter `T` shadows another type parameter
31+
32+
fn not_shadow_in_required<U>(&self);
33+
fn not_shadow_in_provided<U>(&self) {}
34+
}
35+
36+
fn main() {}

0 commit comments

Comments
 (0)