Skip to content

Commit 15b7792

Browse files
authored
Rollup merge of rust-lang#146593 - Jules-Bertholet:restrict-e0719, r=BoxyUwU
Allow specifying multiple bounds for same associated item, except in trait objects Supersedes rust-lang#143146, fixes rust-lang#143143. This PR proposes to stop enforcing E0719 in all contexts other than trait object types. E0719 forbids constraining the same associated item twice within the same angle-bracket delimited associated item bound list (the `…` inside `T: Trait<…>`). For example, the following are forbidden: | Forbidden | Working alternative | |--------------------------------------------|--------------------------------------------------------------------| | `T: Trait<Gat<u32> = u32, Gat<u64> = u64>` | `T: Trait<Gat<u32> = u32> + Trait<Gat<u64> = u64>` | | `T: Iterator<Item = u32, Item = i32>` | `T: Iterator<Item = u32> + Iterator<Item = i32>` (trivially false) | | `T: Iterator<Item = u32, Item = u32>` | `T: Iterator<Item = u32>` | | `T: Iterator<Item: Send, Item: Sync>` | `T: Iterator<Item: Send + Sync>` | | `T: Trait<ASSOC = 3, ASSOC = 4>` | `T: Trait<ASSOC = 3> + Trait<ASSOC = 4>` (trivially false) | | `T: Trait<ASSOC = 3, ASSOC = 3>` | `T: Trait<ASSOC = 3>` | With this PR, all those previously forbidden examples would start working, as well as their APIT and RPIT equivalents. Types like `dyn Iterator<Item = u32, Item = u32>` will continue to be rejected, however. See rust-lang#143146 (comment) for the reason why. ```@rustbot``` label T-lang T-types needs-fcp
2 parents 1e1a394 + 9f667cd commit 15b7792

File tree

15 files changed

+704
-1102
lines changed

15 files changed

+704
-1102
lines changed

compiler/rustc_error_codes/src/error_codes/E0719.md

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
An associated type value was specified more than once.
1+
An associated item was specified more than once in a trait object.
22

33
Erroneous code example:
44

@@ -7,21 +7,15 @@ trait FooTrait {}
77
trait BarTrait {}
88
99
// error: associated type `Item` in trait `Iterator` is specified twice
10-
struct Foo<T: Iterator<Item: FooTrait, Item: BarTrait>> { f: T }
10+
type Foo = dyn Iterator<Item = u32, Item = u32>;
1111
```
1212

13-
`Item` in trait `Iterator` cannot be specified multiple times for struct `Foo`.
14-
To fix this, create a new trait that is a combination of the desired traits and
15-
specify the associated type with the new trait.
13+
To fix this, remove the duplicate specifier:
1614

1715
Corrected example:
1816

1917
```
20-
trait FooTrait {}
21-
trait BarTrait {}
22-
trait FooBarTrait: FooTrait + BarTrait {}
23-
24-
struct Foo<T: Iterator<Item: FooBarTrait>> { f: T } // ok!
18+
type Foo = dyn Iterator<Item = u32>; // ok!
2519
```
2620

2721
For more information about associated types, see [the book][bk-at]. For more

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tracing::{debug, instrument};
1212

1313
use super::ItemCtxt;
1414
use super::predicates_of::assert_only_contains_predicates_from;
15-
use crate::hir_ty_lowering::{HirTyLowerer, PredicateFilter};
15+
use crate::hir_ty_lowering::{HirTyLowerer, OverlappingAsssocItemConstraints, PredicateFilter};
1616

1717
/// For associated types we include both bounds written on the type
1818
/// (`type X: Trait`) and predicates from the trait: `where Self::X: Trait`.
@@ -37,7 +37,14 @@ fn associated_type_bounds<'tcx>(
3737

3838
let icx = ItemCtxt::new(tcx, assoc_item_def_id);
3939
let mut bounds = Vec::new();
40-
icx.lowerer().lower_bounds(item_ty, hir_bounds, &mut bounds, ty::List::empty(), filter);
40+
icx.lowerer().lower_bounds(
41+
item_ty,
42+
hir_bounds,
43+
&mut bounds,
44+
ty::List::empty(),
45+
filter,
46+
OverlappingAsssocItemConstraints::Allowed,
47+
);
4148

4249
match filter {
4350
PredicateFilter::All
@@ -347,7 +354,14 @@ fn opaque_type_bounds<'tcx>(
347354
ty::print::with_reduced_queries!({
348355
let icx = ItemCtxt::new(tcx, opaque_def_id);
349356
let mut bounds = Vec::new();
350-
icx.lowerer().lower_bounds(item_ty, hir_bounds, &mut bounds, ty::List::empty(), filter);
357+
icx.lowerer().lower_bounds(
358+
item_ty,
359+
hir_bounds,
360+
&mut bounds,
361+
ty::List::empty(),
362+
filter,
363+
OverlappingAsssocItemConstraints::Allowed,
364+
);
351365
// Implicit bounds are added to opaque types unless a `?Trait` bound is found
352366
match filter {
353367
PredicateFilter::All

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use super::item_bounds::explicit_item_bounds_with_filter;
1818
use crate::collect::ItemCtxt;
1919
use crate::constrained_generic_params as cgp;
2020
use crate::delegation::inherit_predicates_for_delegation_item;
21-
use crate::hir_ty_lowering::{HirTyLowerer, PredicateFilter, RegionInferReason};
21+
use crate::hir_ty_lowering::{
22+
HirTyLowerer, OverlappingAsssocItemConstraints, PredicateFilter, RegionInferReason,
23+
};
2224

2325
/// Returns a list of all type predicates (explicit and implicit) for the definition with
2426
/// ID `def_id`. This includes all predicates returned by `explicit_predicates_of`, plus
@@ -187,6 +189,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
187189
&mut bounds,
188190
ty::List::empty(),
189191
PredicateFilter::All,
192+
OverlappingAsssocItemConstraints::Allowed,
190193
);
191194
icx.lowerer().add_sizedness_bounds(
192195
&mut bounds,
@@ -289,6 +292,7 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
289292
&mut bounds,
290293
bound_vars,
291294
PredicateFilter::All,
295+
OverlappingAsssocItemConstraints::Allowed,
292296
);
293297
predicates.extend(bounds);
294298
}
@@ -659,7 +663,14 @@ pub(super) fn implied_predicates_with_filter<'tcx>(
659663

660664
let self_param_ty = tcx.types.self_param;
661665
let mut bounds = Vec::new();
662-
icx.lowerer().lower_bounds(self_param_ty, superbounds, &mut bounds, ty::List::empty(), filter);
666+
icx.lowerer().lower_bounds(
667+
self_param_ty,
668+
superbounds,
669+
&mut bounds,
670+
ty::List::empty(),
671+
filter,
672+
OverlappingAsssocItemConstraints::Allowed,
673+
);
663674
match filter {
664675
PredicateFilter::All
665676
| PredicateFilter::SelfOnly
@@ -984,6 +995,7 @@ impl<'tcx> ItemCtxt<'tcx> {
984995
&mut bounds,
985996
bound_vars,
986997
filter,
998+
OverlappingAsssocItemConstraints::Allowed,
987999
);
9881000
}
9891001

@@ -1063,6 +1075,7 @@ pub(super) fn const_conditions<'tcx>(
10631075
&mut bounds,
10641076
bound_vars,
10651077
PredicateFilter::ConstIfConst,
1078+
OverlappingAsssocItemConstraints::Allowed,
10661079
);
10671080
}
10681081
_ => {}
@@ -1083,6 +1096,7 @@ pub(super) fn const_conditions<'tcx>(
10831096
&mut bounds,
10841097
ty::List::empty(),
10851098
PredicateFilter::ConstIfConst,
1099+
OverlappingAsssocItemConstraints::Allowed,
10861100
);
10871101
}
10881102

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use tracing::{debug, instrument};
2121
use super::errors::GenericsArgsErrExtend;
2222
use crate::errors;
2323
use crate::hir_ty_lowering::{
24-
AssocItemQSelf, FeedConstTy, HirTyLowerer, PredicateFilter, RegionInferReason,
24+
AssocItemQSelf, FeedConstTy, HirTyLowerer, OverlappingAsssocItemConstraints, PredicateFilter,
25+
RegionInferReason,
2526
};
2627

2728
#[derive(Debug, Default)]
@@ -338,6 +339,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
338339
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
339340
bound_vars: &'tcx ty::List<ty::BoundVariableKind>,
340341
predicate_filter: PredicateFilter,
342+
overlapping_assoc_constraints: OverlappingAsssocItemConstraints,
341343
) where
342344
'tcx: 'hir,
343345
{
@@ -362,6 +364,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
362364
param_ty,
363365
bounds,
364366
predicate_filter,
367+
overlapping_assoc_constraints,
365368
);
366369
}
367370
hir::GenericBound::Outlives(lifetime) => {
@@ -402,7 +405,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
402405
trait_ref: ty::PolyTraitRef<'tcx>,
403406
constraint: &hir::AssocItemConstraint<'tcx>,
404407
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
405-
duplicates: &mut FxIndexMap<DefId, Span>,
408+
duplicates: Option<&mut FxIndexMap<DefId, Span>>,
406409
path_span: Span,
407410
predicate_filter: PredicateFilter,
408411
) -> Result<(), ErrorGuaranteed> {
@@ -458,17 +461,19 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
458461
)
459462
.expect("failed to find associated item");
460463

461-
duplicates
462-
.entry(assoc_item.def_id)
463-
.and_modify(|prev_span| {
464-
self.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
465-
span: constraint.span,
466-
prev_span: *prev_span,
467-
item_name: constraint.ident,
468-
def_path: tcx.def_path_str(assoc_item.container_id(tcx)),
469-
});
470-
})
471-
.or_insert(constraint.span);
464+
if let Some(duplicates) = duplicates {
465+
duplicates
466+
.entry(assoc_item.def_id)
467+
.and_modify(|prev_span| {
468+
self.dcx().emit_err(errors::ValueOfAssociatedStructAlreadySpecified {
469+
span: constraint.span,
470+
prev_span: *prev_span,
471+
item_name: constraint.ident,
472+
def_path: tcx.def_path_str(assoc_item.container_id(tcx)),
473+
});
474+
})
475+
.or_insert(constraint.span);
476+
}
472477

473478
let projection_term = if let ty::AssocTag::Fn = assoc_tag {
474479
let bound_vars = tcx.late_bound_vars(constraint.hir_id);
@@ -600,6 +605,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
600605
bounds,
601606
projection_ty.bound_vars(),
602607
predicate_filter,
608+
OverlappingAsssocItemConstraints::Allowed,
603609
);
604610
}
605611
PredicateFilter::SelfOnly

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ use tracing::{debug, instrument};
2323

2424
use super::HirTyLowerer;
2525
use crate::errors::SelfInTypeAlias;
26-
use crate::hir_ty_lowering::{GenericArgCountMismatch, PredicateFilter, RegionInferReason};
26+
use crate::hir_ty_lowering::{
27+
GenericArgCountMismatch, OverlappingAsssocItemConstraints, PredicateFilter, RegionInferReason,
28+
};
2729

2830
impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
2931
/// Lower a trait object type from the HIR to our internal notion of a type.
@@ -60,6 +62,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
6062
dummy_self,
6163
&mut user_written_bounds,
6264
PredicateFilter::SelfOnly,
65+
OverlappingAsssocItemConstraints::Forbidden,
6366
);
6467
if let Err(GenericArgCountMismatch { invalid_args, .. }) = result.correct {
6568
potential_assoc_types.extend(invalid_args);
@@ -157,10 +160,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
157160
self.dcx()
158161
.struct_span_err(
159162
span,
160-
format!(
161-
"conflicting associated type bounds for `{item}` when \
162-
expanding trait alias"
163-
),
163+
format!("conflicting associated type bounds for `{item}`"),
164164
)
165165
.with_span_label(
166166
old_proj_span,

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,15 @@ pub(crate) enum GenericArgPosition {
332332
MethodCall,
333333
}
334334

335+
/// Whether to allow duplicate associated iten constraints in a trait ref, e.g.
336+
/// `Trait<Assoc = Ty, Assoc = Ty>`. This is forbidden in `dyn Trait<...>`
337+
/// but allowed everywhere else.
338+
#[derive(Clone, Copy, Debug, PartialEq)]
339+
pub(crate) enum OverlappingAsssocItemConstraints {
340+
Allowed,
341+
Forbidden,
342+
}
343+
335344
/// A marker denoting that the generic arguments that were
336345
/// provided did not match the respective generic parameters.
337346
#[derive(Clone, Debug)]
@@ -752,6 +761,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
752761
self_ty: Ty<'tcx>,
753762
bounds: &mut Vec<(ty::Clause<'tcx>, Span)>,
754763
predicate_filter: PredicateFilter,
764+
overlapping_assoc_item_constraints: OverlappingAsssocItemConstraints,
755765
) -> GenericArgCountResult {
756766
let tcx = self.tcx();
757767

@@ -908,7 +918,10 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
908918
}
909919
}
910920

911-
let mut dup_constraints = FxIndexMap::default();
921+
let mut dup_constraints = (overlapping_assoc_item_constraints
922+
== OverlappingAsssocItemConstraints::Forbidden)
923+
.then_some(FxIndexMap::default());
924+
912925
for constraint in trait_segment.args().constraints {
913926
// Don't register any associated item constraints for negative bounds,
914927
// since we should have emitted an error for them earlier, and they
@@ -927,7 +940,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
927940
poly_trait_ref,
928941
constraint,
929942
bounds,
930-
&mut dup_constraints,
943+
dup_constraints.as_mut(),
931944
constraint.span,
932945
predicate_filter,
933946
);
@@ -2484,6 +2497,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24842497
&mut bounds,
24852498
ty::List::empty(),
24862499
PredicateFilter::All,
2500+
OverlappingAsssocItemConstraints::Allowed,
24872501
);
24882502
self.add_sizedness_bounds(
24892503
&mut bounds,

0 commit comments

Comments
 (0)