Skip to content

Commit 4da69df

Browse files
committed
Auto merge of #147235 - matthiaskrgr:rollup-a0es1x9, r=matthiaskrgr
Rollup of 8 pull requests Successful merges: - #146593 (Allow specifying multiple bounds for same associated item, except in trait objects) - #147177 ([DebugInfo] Fix MSVC tuple child creation) - #147195 (iter repeat: add tests for new count and last behavior) - #147202 (Swap order of `resolve_coroutine_interiors` and `handle_opaque_type_uses`) - #147204 (Refactor ArrayWindows to use a slice) - #147219 (Add proper error handling for closure in impl) - #147226 (include `outer_inclusive_binder` of pattern types) - #147230 (Fix typo in 'unfulfilled_lint_expectation' to plural) r? `@ghost` `@rustbot` modify labels: rollup
2 parents d4ae855 + de67301 commit 4da69df

File tree

31 files changed

+827
-1181
lines changed

31 files changed

+827
-1181
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/coherence/inherent_impls.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,10 @@ impl<'tcx> InherentCollect<'tcx> {
195195
| ty::Closure(..)
196196
| ty::CoroutineClosure(..)
197197
| ty::Coroutine(..)
198-
| ty::CoroutineWitness(..)
199-
| ty::Alias(ty::Free, _)
200-
| ty::Bound(..)
201-
| ty::Placeholder(_)
202-
| ty::Infer(_) => {
198+
| ty::CoroutineWitness(..) => {
199+
Err(self.tcx.dcx().delayed_bug("cannot define inherent `impl` for closure types"))
200+
}
201+
ty::Alias(ty::Free, _) | ty::Bound(..) | ty::Placeholder(_) | ty::Infer(_) => {
203202
bug!("unexpected impl self type of impl: {:?} {:?}", id, self_ty);
204203
}
205204
// We could bail out here, but that will silence other useful errors.

compiler/rustc_hir_analysis/src/coherence/orphan.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,12 @@ pub(crate) fn orphan_check_impl(
230230
ty::Closure(..)
231231
| ty::CoroutineClosure(..)
232232
| ty::Coroutine(..)
233-
| ty::CoroutineWitness(..)
234-
| ty::Bound(..)
235-
| ty::Placeholder(..)
236-
| ty::Infer(..) => {
233+
| ty::CoroutineWitness(..) => {
234+
return Err(tcx
235+
.dcx()
236+
.delayed_bug("cannot define inherent `impl` for closure types"));
237+
}
238+
ty::Bound(..) | ty::Placeholder(..) | ty::Infer(..) => {
237239
let sp = tcx.def_span(impl_def_id);
238240
span_bug!(sp, "weird self type for autotrait impl")
239241
}

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,

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -611,19 +611,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
611611
typeck_results.rvalue_scopes = rvalue_scopes;
612612
}
613613

614-
/// Unify the inference variables corresponding to coroutine witnesses, and save all the
615-
/// predicates that were stalled on those inference variables.
616-
///
617-
/// This process allows to conservatively save all predicates that do depend on the coroutine
618-
/// interior types, for later processing by `check_coroutine_obligations`.
619-
///
620-
/// We must not attempt to select obligations after this method has run, or risk query cycle
621-
/// ICE.
614+
/// Drain all obligations that are stalled on coroutines defined in this body.
622615
#[instrument(level = "debug", skip(self))]
623-
pub(crate) fn resolve_coroutine_interiors(&self) {
624-
// Try selecting all obligations that are not blocked on inference variables.
625-
// Once we start unifying coroutine witnesses, trying to select obligations on them will
626-
// trigger query cycle ICEs, as doing so requires MIR.
616+
pub(crate) fn drain_stalled_coroutine_obligations(&self) {
617+
// Make as much inference progress as possible before
618+
// draining the stalled coroutine obligations as this may
619+
// change obligations from being stalled on infer vars to
620+
// being stalled on a coroutine.
627621
self.select_obligations_where_possible(|_| {});
628622

629623
let ty::TypingMode::Analysis { defining_opaque_types_and_generators } = self.typing_mode()

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,18 +243,15 @@ fn typeck_with_inspect<'tcx>(
243243

244244
debug!(pending_obligations = ?fcx.fulfillment_cx.borrow().pending_obligations());
245245

246-
// This must be the last thing before `report_ambiguity_errors`.
247-
fcx.resolve_coroutine_interiors();
248-
249-
debug!(pending_obligations = ?fcx.fulfillment_cx.borrow().pending_obligations());
250-
251246
// We need to handle opaque types before emitting ambiguity errors as applying
252247
// defining uses may guide type inference.
253248
if fcx.next_trait_solver() {
254249
fcx.handle_opaque_type_uses_next();
255250
}
256251

257-
fcx.select_obligations_where_possible(|_| {});
252+
// This must be the last thing before `report_ambiguity_errors` below except `select_obligations_where_possible`.
253+
// So don't put anything after this.
254+
fcx.drain_stalled_coroutine_obligations();
258255
if fcx.infcx.tainted_by_errors().is_none() {
259256
fcx.report_ambiguity_errors();
260257
}

0 commit comments

Comments
 (0)