Skip to content

Commit 4bb3996

Browse files
committed
re-implement
1 parent 3ea2491 commit 4bb3996

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

src/librustc/lint/builtin.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,12 @@ declare_lint! {
279279
"detects labels that are never used"
280280
}
281281

282+
declare_lint! {
283+
pub DUPLICATE_ASSOCIATED_TYPE_BINDING,
284+
Warn,
285+
"warns about duplicate associated type bindings in generics"
286+
}
287+
282288
/// Does nothing as a lint pass, but registers some `Lint`s
283289
/// which are used by other parts of the compiler.
284290
#[derive(Copy, Clone)]
@@ -330,6 +336,7 @@ impl LintPass for HardwiredLints {
330336
BARE_TRAIT_OBJECT,
331337
ABSOLUTE_PATH_NOT_STARTING_WITH_CRATE,
332338
UNSTABLE_NAME_COLLISION,
339+
DUPLICATE_ASSOCIATED_TYPE_BINDING,
333340
)
334341
}
335342
}

src/librustc_typeck/astconv.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_target::spec::abi;
2727
use std::slice;
2828
use require_c_abi_if_variadic;
2929
use util::common::ErrorReported;
30-
use util::nodemap::FxHashSet;
30+
use util::nodemap::{FxHashSet, FxHashMap};
3131
use errors::FatalError;
3232

3333
use std::iter;
@@ -398,13 +398,24 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
398398
trait_ref.path.segments.last().unwrap());
399399
let poly_trait_ref = ty::Binder::bind(ty::TraitRef::new(trait_def_id, substs));
400400

401+
let mut dup_bindings = FxHashMap::default();
401402
poly_projections.extend(assoc_bindings.iter().filter_map(|binding| {
402403
// specify type to assert that error was already reported in Err case:
403404
let predicate: Result<_, ErrorReported> =
404-
self.ast_type_binding_to_poly_projection_predicate(trait_ref.ref_id, poly_trait_ref,
405-
binding, speculative);
405+
self.ast_type_binding_to_poly_projection_predicate(
406+
trait_ref.ref_id, poly_trait_ref, binding, speculative, &mut dup_bindings);
406407
predicate.ok() // ok to ignore Err() because ErrorReported (see above)
407408
}));
409+
for (_id, spans) in dup_bindings {
410+
if spans.len() > 1 {
411+
self.tcx().struct_span_lint_node(
412+
::rustc::lint::builtin::DUPLICATE_ASSOCIATED_TYPE_BINDING,
413+
trait_ref.ref_id,
414+
spans,
415+
"duplicate associated type binding"
416+
).emit();
417+
}
418+
}
408419

409420
debug!("ast_path_to_poly_trait_ref({:?}, projections={:?}) -> {:?}",
410421
trait_ref, poly_projections, poly_trait_ref);
@@ -487,7 +498,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
487498
ref_id: ast::NodeId,
488499
trait_ref: ty::PolyTraitRef<'tcx>,
489500
binding: &ConvertedBinding<'tcx>,
490-
speculative: bool)
501+
speculative: bool,
502+
dup_bindings: &mut FxHashMap<DefId, Vec<Span>>)
491503
-> Result<ty::PolyProjectionPredicate<'tcx>, ErrorReported>
492504
{
493505
let tcx = self.tcx();
@@ -565,6 +577,7 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
565577
tcx.sess.span_err(binding.span, &msg);
566578
}
567579
tcx.check_stability(assoc_ty.def_id, Some(ref_id), binding.span);
580+
dup_bindings.entry(assoc_ty.def_id).or_insert(Vec::new()).push(binding.span);
568581

569582
Ok(candidate.map_bound(|trait_ref| {
570583
ty::ProjectionPredicate {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2018 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+
// compile-pass
12+
13+
use std::iter::Iterator;
14+
15+
type Unit = ();
16+
17+
fn test() -> Box<Iterator<Item = (), Item = Unit>> {
18+
Box::new(None.into_iter())
19+
}
20+
21+
fn main() {
22+
test();
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
warning: duplicate associated type binding
2+
--> $DIR/issue-50589-multiple-associated-types.rs:17:28
3+
|
4+
LL | fn test() -> Box<Iterator<Item = (), Item = Unit>> {
5+
| ^^^^^^^^^ ^^^^^^^^^^^
6+
|
7+
= note: #[warn(duplicate_associated_type_binding)] on by default
8+
9+
warning: duplicate associated type binding
10+
--> $DIR/issue-50589-multiple-associated-types.rs:17:28
11+
|
12+
LL | fn test() -> Box<Iterator<Item = (), Item = Unit>> {
13+
| ^^^^^^^^^ ^^^^^^^^^^^
14+

0 commit comments

Comments
 (0)