Skip to content

Commit 535d27a

Browse files
committed
Auto merge of #77236 - matthewjasper:defer-typeof-impl-trait, r=davidtwco
Compute underlying type of impl trait types later in compilation Also change a `bug!` to `delay_span_bug` Closes #74018
2 parents 1d5a865 + 3a81ade commit 535d27a

33 files changed

+375
-430
lines changed

compiler/rustc_mir/src/borrow_check/region_infer/mod.rs

+19-10
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
551551
mir_def_id: DefId,
552552
polonius_output: Option<Rc<PoloniusOutput>>,
553553
) -> (Option<ClosureRegionRequirements<'tcx>>, RegionErrors<'tcx>) {
554-
self.propagate_constraints(body);
554+
self.propagate_constraints(body, infcx.tcx);
555555

556556
let mut errors_buffer = RegionErrors::new();
557557

@@ -599,7 +599,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
599599
/// for each region variable until all the constraints are
600600
/// satisfied. Note that some values may grow **too** large to be
601601
/// feasible, but we check this later.
602-
fn propagate_constraints(&mut self, _body: &Body<'tcx>) {
602+
fn propagate_constraints(&mut self, _body: &Body<'tcx>, tcx: TyCtxt<'tcx>) {
603603
debug!("propagate_constraints()");
604604

605605
debug!("propagate_constraints: constraints={:#?}", {
@@ -617,7 +617,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
617617
// own.
618618
let constraint_sccs = self.constraint_sccs.clone();
619619
for scc in constraint_sccs.all_sccs() {
620-
self.compute_value_for_scc(scc);
620+
self.compute_value_for_scc(scc, tcx);
621621
}
622622

623623
// Sort the applied member constraints so we can binary search
@@ -629,7 +629,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
629629
/// computed, by unioning the values of its successors.
630630
/// Assumes that all successors have been computed already
631631
/// (which is assured by iterating over SCCs in dependency order).
632-
fn compute_value_for_scc(&mut self, scc_a: ConstraintSccIndex) {
632+
fn compute_value_for_scc(&mut self, scc_a: ConstraintSccIndex, tcx: TyCtxt<'tcx>) {
633633
let constraint_sccs = self.constraint_sccs.clone();
634634

635635
// Walk each SCC `B` such that `A: B`...
@@ -652,7 +652,12 @@ impl<'tcx> RegionInferenceContext<'tcx> {
652652
// Now take member constraints into account.
653653
let member_constraints = self.member_constraints.clone();
654654
for m_c_i in member_constraints.indices(scc_a) {
655-
self.apply_member_constraint(scc_a, m_c_i, member_constraints.choice_regions(m_c_i));
655+
self.apply_member_constraint(
656+
tcx,
657+
scc_a,
658+
m_c_i,
659+
member_constraints.choice_regions(m_c_i),
660+
);
656661
}
657662

658663
debug!(
@@ -675,6 +680,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
675680
/// If we make any changes, returns true, else false.
676681
fn apply_member_constraint(
677682
&mut self,
683+
tcx: TyCtxt<'tcx>,
678684
scc: ConstraintSccIndex,
679685
member_constraint_index: NllMemberConstraintIndex,
680686
choice_regions: &[ty::RegionVid],
@@ -688,12 +694,15 @@ impl<'tcx> RegionInferenceContext<'tcx> {
688694
// `impl_trait_in_bindings`, I believe, and we are just
689695
// opting not to handle it for now. See #61773 for
690696
// details.
691-
bug!(
692-
"member constraint for `{:?}` has an option region `{:?}` \
693-
that is not a universal region",
694-
self.member_constraints[member_constraint_index].opaque_type_def_id,
695-
uh_oh,
697+
tcx.sess.delay_span_bug(
698+
self.member_constraints[member_constraint_index].definition_span,
699+
&format!(
700+
"member constraint for `{:?}` has an option region `{:?}` \
701+
that is not a universal region",
702+
self.member_constraints[member_constraint_index].opaque_type_def_id, uh_oh,
703+
),
696704
);
705+
return false;
697706
}
698707

699708
// Create a mutable vector of the options. We'll try to winnow

compiler/rustc_typeck/src/check/check.rs

+1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ pub(super) fn check_opaque<'tcx>(
385385
origin: &hir::OpaqueTyOrigin,
386386
) {
387387
check_opaque_for_inheriting_lifetimes(tcx, def_id, span);
388+
tcx.ensure().type_of(def_id);
388389
check_opaque_for_cycles(tcx, def_id, substs, span, origin);
389390
}
390391

compiler/rustc_typeck/src/collect.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -693,8 +693,14 @@ fn convert_item(tcx: TyCtxt<'_>, item_id: hir::HirId) {
693693
// Desugared from `impl Trait`, so visited by the function's return type.
694694
hir::ItemKind::OpaqueTy(hir::OpaqueTy { impl_trait_fn: Some(_), .. }) => {}
695695

696-
hir::ItemKind::OpaqueTy(..)
697-
| hir::ItemKind::TyAlias(..)
696+
// Don't call `type_of` on opaque types, since that depends on type
697+
// checking function bodies. `check_item_type` ensures that it's called
698+
// instead.
699+
hir::ItemKind::OpaqueTy(..) => {
700+
tcx.ensure().generics_of(def_id);
701+
tcx.ensure().predicates_of(def_id);
702+
}
703+
hir::ItemKind::TyAlias(..)
698704
| hir::ItemKind::Static(..)
699705
| hir::ItemKind::Const(..)
700706
| hir::ItemKind::Fn(..) => {

compiler/rustc_typeck/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ pub fn check_crate(tcx: TyCtxt<'_>) -> Result<(), ErrorReported> {
359359

360360
// this ensures that later parts of type checking can assume that items
361361
// have valid types and not error
362-
// FIXME(matthewjasper) We shouldn't need to do this.
362+
// FIXME(matthewjasper) We shouldn't need to use `track_errors`.
363363
tcx.sess.track_errors(|| {
364364
tcx.sess.time("type_collecting", || {
365365
for &module in tcx.hir().krate().modules.keys() {

src/test/ui/associated-type-bounds/duplicate.rs

-21
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,8 @@ fn FW3<T>() where T: Iterator<Item: 'static, Item: 'static> {}
6060
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
6161

6262
fn FRPIT1() -> impl Iterator<Item: Copy, Item: Send> { iter::empty() }
63-
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
6463
fn FRPIT2() -> impl Iterator<Item: Copy, Item: Copy> { iter::empty() }
65-
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
6664
fn FRPIT3() -> impl Iterator<Item: 'static, Item: 'static> { iter::empty() }
67-
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
6865
fn FAPIT1(_: impl Iterator<Item: Copy, Item: Send>) {}
6966
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
7067
fn FAPIT2(_: impl Iterator<Item: Copy, Item: Copy>) {}
@@ -107,28 +104,16 @@ type TAW3<T> where T: Iterator<Item: 'static, Item: 'static> = T;
107104

108105
type ETAI1<T: Iterator<Item: Copy, Item: Send>> = impl Copy;
109106
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
110-
//~| ERROR could not find defining uses
111107
type ETAI2<T: Iterator<Item: Copy, Item: Copy>> = impl Copy;
112108
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
113-
//~| ERROR could not find defining uses
114109
type ETAI3<T: Iterator<Item: 'static, Item: 'static>> = impl Copy;
115110
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
116-
//~| ERROR could not find defining uses
117111
type ETAI4 = impl Iterator<Item: Copy, Item: Send>;
118112
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
119-
//~| ERROR could not find defining uses
120-
//~| ERROR could not find defining uses
121-
//~| ERROR could not find defining uses
122113
type ETAI5 = impl Iterator<Item: Copy, Item: Copy>;
123114
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
124-
//~| ERROR could not find defining uses
125-
//~| ERROR could not find defining uses
126-
//~| ERROR could not find defining uses
127115
type ETAI6 = impl Iterator<Item: 'static, Item: 'static>;
128116
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
129-
//~| ERROR could not find defining uses
130-
//~| ERROR could not find defining uses
131-
//~| ERROR could not find defining uses
132117

133118
trait TRI1<T: Iterator<Item: Copy, Item: Send>> {}
134119
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
@@ -166,15 +151,9 @@ trait TRA3 { type A: Iterator<Item: 'static, Item: 'static>; }
166151

167152
type TADyn1 = dyn Iterator<Item: Copy, Item: Send>;
168153
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
169-
//~| ERROR could not find defining uses
170-
//~| ERROR could not find defining uses
171154
type TADyn2 = Box<dyn Iterator<Item: Copy, Item: Copy>>;
172155
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
173-
//~| ERROR could not find defining uses
174-
//~| ERROR could not find defining uses
175156
type TADyn3 = dyn Iterator<Item: 'static, Item: 'static>;
176157
//~^ ERROR the value of the associated type `Item` (from trait `Iterator`) is already specified [E0719]
177-
//~| ERROR could not find defining uses
178-
//~| ERROR could not find defining uses
179158

180159
fn main() {}

0 commit comments

Comments
 (0)