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.
This commit is contained in:
parent
9f1ead8fad
commit
92cd8ea96a
@ -24,6 +24,7 @@
|
||||
use syntax::ast_util::{local_def};
|
||||
use syntax::attr;
|
||||
use syntax::codemap::Span;
|
||||
use syntax::parse::token;
|
||||
use syntax::visit;
|
||||
use syntax::visit::Visitor;
|
||||
|
||||
@ -262,11 +263,64 @@ fn is_ty_param(ty: ty::Ty) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
fn reject_shadowing_type_parameters<'tcx>(tcx: &ty::ctxt<'tcx>,
|
||||
span: Span,
|
||||
generics: &ty::Generics<'tcx>) {
|
||||
let impl_params = generics.types.get_slice(subst::TypeSpace).iter()
|
||||
.map(|tp| tp.name).collect::<HashSet<_>>();
|
||||
|
||||
for method_param in generics.types.get_slice(subst::FnSpace).iter() {
|
||||
if impl_params.contains(&method_param.name) {
|
||||
tcx.sess.span_err(
|
||||
span,
|
||||
&*format!("type parameter `{}` shadows another type parameter of the same name",
|
||||
token::get_name(method_param.name)));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'ccx, 'tcx, 'v> Visitor<'v> for CheckTypeWellFormedVisitor<'ccx, 'tcx> {
|
||||
fn visit_item(&mut self, i: &ast::Item) {
|
||||
self.check_item_well_formed(i);
|
||||
visit::walk_item(self, i);
|
||||
}
|
||||
|
||||
fn visit_fn(&mut self,
|
||||
fk: visit::FnKind<'v>, fd: &'v ast::FnDecl,
|
||||
b: &'v ast::Block, span: Span, id: ast::NodeId) {
|
||||
match fk {
|
||||
visit::FkFnBlock | visit::FkItemFn(..) => {}
|
||||
visit::FkMethod(..) => {
|
||||
match ty::impl_or_trait_item(self.ccx.tcx, local_def(id)) {
|
||||
ty::ImplOrTraitItem::MethodTraitItem(ty_method) => {
|
||||
reject_shadowing_type_parameters(self.ccx.tcx, span, &ty_method.generics)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
visit::walk_fn(self, fk, fd, b, span)
|
||||
}
|
||||
|
||||
fn visit_trait_item(&mut self, t: &'v ast::TraitItem) {
|
||||
match t {
|
||||
&ast::TraitItem::ProvidedMethod(_) |
|
||||
&ast::TraitItem::TypeTraitItem(_) => {},
|
||||
&ast::TraitItem::RequiredMethod(ref method) => {
|
||||
match ty::impl_or_trait_item(self.ccx.tcx, local_def(method.id)) {
|
||||
ty::ImplOrTraitItem::MethodTraitItem(ty_method) => {
|
||||
reject_shadowing_type_parameters(
|
||||
self.ccx.tcx,
|
||||
method.span,
|
||||
&ty_method.generics)
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
visit::walk_trait_item(self, t)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BoundsChecker<'cx,'tcx:'cx> {
|
||||
|
@ -1396,7 +1396,7 @@ fn ch_is(&self, c: char) -> bool {
|
||||
self.ch == Some(c)
|
||||
}
|
||||
|
||||
fn error<T>(&self, reason: ErrorCode) -> Result<T, ParserError> {
|
||||
fn error<U>(&self, reason: ErrorCode) -> Result<U, ParserError> {
|
||||
Err(SyntaxError(reason, self.line, self.col))
|
||||
}
|
||||
|
||||
|
36
src/test/compile-fail/shadowed-type-parameter.rs
Normal file
36
src/test/compile-fail/shadowed-type-parameter.rs
Normal file
@ -0,0 +1,36 @@
|
||||
// Copyright 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.
|
||||
|
||||
// Test that shadowed lifetimes generate an error.
|
||||
|
||||
struct Foo<T>;
|
||||
|
||||
impl<T> Foo<T> {
|
||||
fn shadow_in_method<T>(&self) {}
|
||||
//~^ ERROR type parameter `T` shadows another type parameter
|
||||
|
||||
fn not_shadow_in_item<U>(&self) {
|
||||
struct Bar<T, U>; // not a shadow, separate item
|
||||
fn foo<T, U>() {} // same
|
||||
}
|
||||
}
|
||||
|
||||
trait<T> Bar<T> {
|
||||
fn shadow_in_required<T>(&self);
|
||||
//~^ ERROR type parameter `T` shadows another type parameter
|
||||
|
||||
fn shadow_in_provided<T>(&self) {}
|
||||
//~^ ERROR type parameter `T` shadows another type parameter
|
||||
|
||||
fn not_shadow_in_required<U>(&self);
|
||||
fn not_shadow_in_provided<U>(&self) {}
|
||||
}
|
||||
|
||||
fn main() {}
|
Loading…
Reference in New Issue
Block a user