Skip to content

Commit fd589e6

Browse files
Deduplicate bounds on associated types when deriving
1 parent 794e50b commit fd589e6

File tree

1 file changed

+11
-7
lines changed
  • compiler/rustc_builtin_macros/src/deriving/generic

1 file changed

+11
-7
lines changed

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,12 @@ use rustc_ast::ptr::P;
185185
use rustc_ast::{self as ast, BinOpKind, EnumDef, Expr, Generics, PatKind};
186186
use rustc_ast::{GenericArg, GenericParamKind, VariantData};
187187
use rustc_attr as attr;
188+
use rustc_data_structures::fx::FxHashSet;
188189
use rustc_data_structures::map_in_place::MapInPlace;
189190
use rustc_expand::base::{Annotatable, ExtCtxt};
190191
use rustc_span::symbol::{kw, sym, Ident, Symbol};
191192
use rustc_span::Span;
192-
use smallvec::SmallVec;
193+
use smallvec::{smallvec, SmallVec};
193194

194195
use ty::{Bounds, Path, Ptr, PtrTy, Self_, Ty};
195196

@@ -668,20 +669,23 @@ impl<'a> TraitDef<'a> {
668669

669670
if ty_params.peek().is_some() {
670671
let ty_param_names: Vec<Symbol> =
671-
ty_params.map(|ty_param| ty_param.ident.name).collect();
672+
ty_params.clone().map(|ty_param| ty_param.ident.name).collect();
673+
674+
let mut seen_simple_field_ty_params: FxHashSet<_> =
675+
ty_params.map(|ty_param| smallvec![ty_param.ident]).collect();
672676

673677
for field_ty in field_tys {
674678
let field_ty_params = find_type_parameters(&field_ty, &ty_param_names, cx);
675679

676680
for field_ty_param in field_ty_params {
677681
// if we have already handled this type, skip it
678-
if let ast::TyKind::Path(_, ref p) = field_ty_param.ty.kind {
679-
if p.segments.len() == 1
680-
&& ty_param_names.contains(&p.segments[0].ident.name)
681-
{
682+
if let Some(field_ty_param_path) = field_ty_param.to_simple_path() {
683+
let changed = seen_simple_field_ty_params.insert(field_ty_param_path);
684+
if !changed {
682685
continue;
683-
};
686+
}
684687
}
688+
685689
let mut bounds: Vec<_> = self
686690
.additional_bounds
687691
.iter()

0 commit comments

Comments
 (0)