@@ -1139,6 +1139,10 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
1139
1139
} )
1140
1140
} ) ;
1141
1141
1142
+ // HACK(#57057): see below.
1143
+ let extra_principal_trait = auto_traits. iter ( ) . skip ( 1 ) . any (
1144
+ |tr| tr. trait_ref ( ) . def_id ( ) == principal. def_id ( ) ) ;
1145
+
1142
1146
// Lint duplicate auto traits, and then remove duplicates.
1143
1147
auto_traits. sort_by_key ( |i| i. trait_ref ( ) . def_id ( ) ) ;
1144
1148
let emit_dup_traits_err = |range : Range < usize > | {
@@ -1166,14 +1170,19 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
1166
1170
if auto_traits. len ( ) - seq_start > 1 {
1167
1171
emit_dup_traits_err ( seq_start..auto_traits. len ( ) ) ;
1168
1172
}
1169
- // Remove first auto trait if it is the principal trait.
1170
- // HACK(alexreg): we really want to do this *after* dedup, but we must maintain this
1171
- // behaviour for the sake of backwards compatibility, until the duplicate traits lint
1172
- // becomes a hard error.
1173
- if auto_traits. first ( ) . map_or ( false , |tr| tr. trait_ref ( ) . def_id ( ) == principal. def_id ( ) ) {
1174
- auto_traits. remove ( 0 ) ;
1175
- }
1176
1173
auto_traits. dedup_by_key ( |i| i. trait_ref ( ) . def_id ( ) ) ;
1174
+
1175
+ // If principal is auto trait, remove it from list of auto traits.
1176
+ // HACK(#57057): we don't do this if the trait was mentioned multiple times.
1177
+ // Previous behaviour was buggy in this respect, but we want to maintain backwards
1178
+ // compatibility for now. This check will eventually be removed.
1179
+ if !extra_principal_trait {
1180
+ if let Some ( principal_index) = auto_traits. iter ( ) . position (
1181
+ |tr| tr. trait_ref ( ) . def_id ( ) == principal. def_id ( ) ) {
1182
+ auto_traits. remove ( principal_index) ;
1183
+ }
1184
+ }
1185
+
1177
1186
debug ! ( "auto_traits: {:?}" , auto_traits) ;
1178
1187
1179
1188
// Calling `skip_binder` is okay, since the predicates are re-bound.
0 commit comments