Skip to content

Commit 92cd8ea

Browse files
committed
Prohibit type parameter shadowing with a clunky hammer.
This is a [breaking-change]. Change impl<T> Foo<T> { fn bar<T>(... to (for example) impl<T> Foo<T> { fn bar<U>(... Per RFC 459. Closes #19390.
1 parent 9f1ead8 commit 92cd8ea

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-1
lines changed

src/librustc_typeck/check/wf.rs

+54
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

@@ -262,11 +263,64 @@ fn reject_non_type_param_bounds<'tcx>(tcx: &ty::ctxt<'tcx>,
262263
}
263264
}
264265

266+
fn reject_shadowing_type_parameters<'tcx>(tcx: &ty::ctxt<'tcx>,
267+
span: Span,
268+
generics: &ty::Generics<'tcx>) {
269+
let impl_params = generics.types.get_slice(subst::TypeSpace).iter()
270+
.map(|tp| tp.name).collect::<HashSet<_>>();
271+
272+
for method_param in generics.types.get_slice(subst::FnSpace).iter() {
273+
if impl_params.contains(&method_param.name) {
274+
tcx.sess.span_err(
275+
span,
276+
&*format!("type parameter `{}` shadows another type parameter of the same name",
277+
token::get_name(method_param.name)));
278+
}
279+
}
280+
}
281+
265282
impl<'ccx, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'ccx, 'tcx> {
266283
fn visit_item(&mut self, i: &ast::Item) {
267284
self.check_item_well_formed(i);
268285
visit::walk_item(self, i);
269286
}
287+
288+
fn visit_fn(&mut self,
289+
fk: visit::FnKind<'v>, fd: &'v ast::FnDecl,
290+
b: &'v ast::Block, span: Span, id: ast::NodeId) {
291+
match fk {
292+
visit::FkFnBlock | visit::FkItemFn(..) => {}
293+
visit::FkMethod(..) => {
294+
match ty::impl_or_trait_item(self.ccx.tcx, local_def(id)) {
295+
ty::ImplOrTraitItem::MethodTraitItem(ty_method) => {
296+
reject_shadowing_type_parameters(self.ccx.tcx, span, &ty_method.generics)
297+
}
298+
_ => {}
299+
}
300+
}
301+
}
302+
visit::walk_fn(self, fk, fd, b, span)
303+
}
304+
305+
fn visit_trait_item(&mut self, t: &'v ast::TraitItem) {
306+
match t {
307+
&ast::TraitItem::ProvidedMethod(_) |
308+
&ast::TraitItem::TypeTraitItem(_) => {},
309+
&ast::TraitItem::RequiredMethod(ref method) => {
310+
match ty::impl_or_trait_item(self.ccx.tcx, local_def(method.id)) {
311+
ty::ImplOrTraitItem::MethodTraitItem(ty_method) => {
312+
reject_shadowing_type_parameters(
313+
self.ccx.tcx,
314+
method.span,
315+
&ty_method.generics)
316+
}
317+
_ => {}
318+
}
319+
}
320+
}
321+
322+
visit::walk_trait_item(self, t)
323+
}
270324
}
271325

272326
pub struct BoundsChecker<'cx,'tcx:'cx> {

src/libserialize/json.rs

+1-1
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

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)