Skip to content

Commit aae7b23

Browse files
authored
Rollup merge of rust-lang#99818 - aliemjay:fix-closure-normalize, r=estebank
don't ICE when normalizing closure input tys We were ICEing while rendering diagnostics because `universe_causes` is expected to track every universe created in the typeck's infcx. `normalize_and_add_constraints` doesn't update `universe_causes` when creating new universes, causing an ICE. Remove it! Add spans to better track normalization constraints. Fix couple places where `universe_causes` is not updated correctly to track newly added universes. Fixes rust-lang#99665
2 parents a53e417 + 4eae7a1 commit aae7b23

6 files changed

+138
-58
lines changed

compiler/rustc_borrowck/src/type_check/canonical.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
5050
Some(error_info) => error_info.to_universe_info(old_universe),
5151
None => UniverseInfo::other(),
5252
};
53-
for u in old_universe..universe {
54-
self.borrowck_context
55-
.constraints
56-
.universe_causes
57-
.insert(u + 1, universe_info.clone());
53+
for u in (old_universe + 1)..=universe {
54+
self.borrowck_context.constraints.universe_causes.insert(u, universe_info.clone());
5855
}
5956
}
6057

@@ -69,15 +66,13 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
6966
where
7067
T: TypeFoldable<'tcx>,
7168
{
69+
let old_universe = self.infcx.universe();
70+
7271
let (instantiated, _) =
7372
self.infcx.instantiate_canonical_with_fresh_inference_vars(span, canonical);
7473

75-
for u in 0..canonical.max_universe.as_u32() {
76-
let info = UniverseInfo::other();
77-
self.borrowck_context
78-
.constraints
79-
.universe_causes
80-
.insert(ty::UniverseIndex::from_u32(u), info);
74+
for u in (old_universe + 1)..=self.infcx.universe() {
75+
self.borrowck_context.constraints.universe_causes.insert(u, UniverseInfo::other());
8176
}
8277

8378
instantiated

compiler/rustc_borrowck/src/type_check/free_region_relations.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_infer::infer::InferCtxt;
88
use rustc_middle::mir::ConstraintCategory;
99
use rustc_middle::traits::query::OutlivesBound;
1010
use rustc_middle::ty::{self, RegionVid, Ty};
11-
use rustc_span::DUMMY_SP;
1211
use rustc_trait_selection::traits::query::type_op::{self, TypeOp};
1312
use std::rc::Rc;
1413
use type_op::TypeOpOutput;
@@ -219,6 +218,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
219218
}
220219

221220
pub(crate) fn create(mut self) -> CreateResult<'tcx> {
221+
let span = self.infcx.tcx.def_span(self.universal_regions.defining_ty.def_id());
222222
let unnormalized_input_output_tys = self
223223
.universal_regions
224224
.unnormalized_input_tys
@@ -250,7 +250,7 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
250250
self.infcx
251251
.tcx
252252
.sess
253-
.delay_span_bug(DUMMY_SP, &format!("failed to normalize {:?}", ty));
253+
.delay_span_bug(span, &format!("failed to normalize {:?}", ty));
254254
TypeOpOutput {
255255
output: self.infcx.tcx.ty_error(),
256256
constraints: None,
@@ -301,8 +301,8 @@ impl<'tcx> UniversalRegionRelationsBuilder<'_, 'tcx> {
301301
&self.region_bound_pairs,
302302
self.implicit_region_bound,
303303
self.param_env,
304-
Locations::All(DUMMY_SP),
305-
DUMMY_SP,
304+
Locations::All(span),
305+
span,
306306
ConstraintCategory::Internal,
307307
&mut self.constraints,
308308
)

compiler/rustc_borrowck/src/type_check/input_output.rs

+2-37
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,11 @@
77
//! `RETURN_PLACE` the MIR arguments) are always fully normalized (and
88
//! contain revealed `impl Trait` values).
99
10-
use crate::type_check::constraint_conversion::ConstraintConversion;
1110
use rustc_index::vec::Idx;
1211
use rustc_infer::infer::LateBoundRegionConversionTime;
1312
use rustc_middle::mir::*;
1413
use rustc_middle::ty::Ty;
1514
use rustc_span::Span;
16-
use rustc_span::DUMMY_SP;
17-
use rustc_trait_selection::traits::query::type_op::{self, TypeOp};
18-
use rustc_trait_selection::traits::query::Fallible;
19-
use type_op::TypeOpOutput;
2015

2116
use crate::universal_regions::UniversalRegions;
2217

@@ -185,7 +180,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
185180
}
186181
}
187182

188-
#[instrument(skip(self, span), level = "debug")]
183+
#[instrument(skip(self), level = "debug")]
189184
fn equate_normalized_input_or_output(&mut self, a: Ty<'tcx>, b: Ty<'tcx>, span: Span) {
190185
if let Err(_) =
191186
self.eq_types(a, b, Locations::All(span), ConstraintCategory::BoringNoLocation)
@@ -194,13 +189,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
194189
// `rustc_traits::normalize_after_erasing_regions`. Ideally, we'd
195190
// like to normalize *before* inserting into `local_decls`, but
196191
// doing so ends up causing some other trouble.
197-
let b = match self.normalize_and_add_constraints(b) {
198-
Ok(n) => n,
199-
Err(_) => {
200-
debug!("equate_inputs_and_outputs: NoSolution");
201-
b
202-
}
203-
};
192+
let b = self.normalize(b, Locations::All(span));
204193

205194
// Note: if we have to introduce new placeholders during normalization above, then we won't have
206195
// added those universes to the universe info, which we would want in `relate_tys`.
@@ -218,28 +207,4 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
218207
}
219208
}
220209
}
221-
222-
pub(crate) fn normalize_and_add_constraints(&mut self, t: Ty<'tcx>) -> Fallible<Ty<'tcx>> {
223-
let TypeOpOutput { output: norm_ty, constraints, .. } =
224-
self.param_env.and(type_op::normalize::Normalize::new(t)).fully_perform(self.infcx)?;
225-
226-
debug!("{:?} normalized to {:?}", t, norm_ty);
227-
228-
for data in constraints {
229-
ConstraintConversion::new(
230-
self.infcx,
231-
&self.borrowck_context.universal_regions,
232-
&self.region_bound_pairs,
233-
self.implicit_region_bound,
234-
self.param_env,
235-
Locations::All(DUMMY_SP),
236-
DUMMY_SP,
237-
ConstraintCategory::Internal,
238-
&mut self.borrowck_context.constraints,
239-
)
240-
.convert_all(&*data);
241-
}
242-
243-
Ok(norm_ty)
244-
}
245210
}

compiler/rustc_borrowck/src/type_check/mod.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,6 @@ pub(crate) fn type_check<'mir, 'tcx>(
137137
use_polonius: bool,
138138
) -> MirTypeckResults<'tcx> {
139139
let implicit_region_bound = infcx.tcx.mk_region(ty::ReVar(universal_regions.fr_fn_body));
140-
let mut universe_causes = FxHashMap::default();
141-
universe_causes.insert(ty::UniverseIndex::from_u32(0), UniverseInfo::other());
142140
let mut constraints = MirTypeckRegionConstraints {
143141
placeholder_indices: PlaceholderIndices::default(),
144142
placeholder_index_to_region: IndexVec::default(),
@@ -147,7 +145,7 @@ pub(crate) fn type_check<'mir, 'tcx>(
147145
member_constraints: MemberConstraintSet::default(),
148146
closure_bounds_mapping: Default::default(),
149147
type_tests: Vec::default(),
150-
universe_causes,
148+
universe_causes: FxHashMap::default(),
151149
};
152150

153151
let CreateResult {
@@ -164,9 +162,8 @@ pub(crate) fn type_check<'mir, 'tcx>(
164162

165163
debug!(?normalized_inputs_and_output);
166164

167-
for u in ty::UniverseIndex::ROOT..infcx.universe() {
168-
let info = UniverseInfo::other();
169-
constraints.universe_causes.insert(u, info);
165+
for u in ty::UniverseIndex::ROOT..=infcx.universe() {
166+
constraints.universe_causes.insert(u, UniverseInfo::other());
170167
}
171168

172169
let mut borrowck_context = BorrowCheckContext {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Regression test for #99665
2+
//
3+
// Here we are generating region constraints
4+
// when normalizing input types of the closure.
5+
6+
// check-fail
7+
8+
pub trait MyComponent {
9+
type Properties;
10+
}
11+
12+
struct Ty1<T>(T);
13+
struct Ty2<T>(T);
14+
15+
impl<M> MyComponent for Ty1<M>
16+
where
17+
M: 'static,
18+
{
19+
type Properties = ();
20+
}
21+
22+
impl<M> MyComponent for Ty2<M>
23+
where
24+
M: 'static,
25+
{
26+
type Properties = &'static M;
27+
}
28+
29+
fn fail() {
30+
// This should fail because `Ty1<&u8>` is inferred to be higher-ranked.
31+
// So effectively we're trying to prove `for<'a> Ty1<&'a u8>: MyComponent`.
32+
|_: <Ty1<&u8> as MyComponent>::Properties| {};
33+
//~^ ERROR lifetime may not live long enough
34+
//~| ERROR higher-ranked subtype error
35+
//~| ERROR higher-ranked lifetime error
36+
//~| ERROR higher-ranked lifetime error
37+
//~| ERROR higher-ranked lifetime error
38+
39+
|_: <Ty2<&u8> as MyComponent>::Properties| {};
40+
//~^ ERROR higher-ranked subtype error
41+
//~| ERROR higher-ranked lifetime error
42+
//~| ERROR higher-ranked lifetime error
43+
//~| ERROR higher-ranked lifetime error
44+
}
45+
46+
fn pass() {
47+
// Here both are not higher-ranked, so they sould pass.
48+
|| -> <Ty1<&u8> as MyComponent>::Properties { panic!() };
49+
|| -> <Ty2<&u8> as MyComponent>::Properties { panic!() };
50+
}
51+
52+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/closure-malformed-projection-input-issue-99665.rs:32:5
3+
|
4+
LL | |_: <Ty1<&u8> as MyComponent>::Properties| {};
5+
| ^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| ||
7+
| |has type `<Ty1<&'1 u8> as MyComponent>::Properties`
8+
| requires that `'1` must outlive `'static`
9+
10+
error: higher-ranked subtype error
11+
--> $DIR/closure-malformed-projection-input-issue-99665.rs:32:5
12+
|
13+
LL | |_: <Ty1<&u8> as MyComponent>::Properties| {};
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
15+
16+
error: higher-ranked lifetime error
17+
--> $DIR/closure-malformed-projection-input-issue-99665.rs:32:5
18+
|
19+
LL | |_: <Ty1<&u8> as MyComponent>::Properties| {};
20+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
21+
|
22+
= note: could not normalize `&[closure@$DIR/closure-malformed-projection-input-issue-99665.rs:32:5: 32:47]`
23+
24+
error: higher-ranked subtype error
25+
--> $DIR/closure-malformed-projection-input-issue-99665.rs:39:5
26+
|
27+
LL | |_: <Ty2<&u8> as MyComponent>::Properties| {};
28+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
29+
30+
error: higher-ranked lifetime error
31+
--> $DIR/closure-malformed-projection-input-issue-99665.rs:39:5
32+
|
33+
LL | |_: <Ty2<&u8> as MyComponent>::Properties| {};
34+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
35+
|
36+
= note: could not normalize `&[closure@$DIR/closure-malformed-projection-input-issue-99665.rs:39:5: 39:47]`
37+
38+
error: higher-ranked lifetime error
39+
--> $DIR/closure-malformed-projection-input-issue-99665.rs:32:5
40+
|
41+
LL | |_: <Ty1<&u8> as MyComponent>::Properties| {};
42+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
43+
|
44+
= note: could not normalize `[closure@$DIR/closure-malformed-projection-input-issue-99665.rs:32:5: 32:47]`
45+
46+
error: higher-ranked lifetime error
47+
--> $DIR/closure-malformed-projection-input-issue-99665.rs:32:5
48+
|
49+
LL | |_: <Ty1<&u8> as MyComponent>::Properties| {};
50+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
51+
|
52+
= note: could not normalize `[closure@$DIR/closure-malformed-projection-input-issue-99665.rs:32:5: 32:47]`
53+
54+
error: higher-ranked lifetime error
55+
--> $DIR/closure-malformed-projection-input-issue-99665.rs:39:5
56+
|
57+
LL | |_: <Ty2<&u8> as MyComponent>::Properties| {};
58+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
59+
|
60+
= note: could not normalize `[closure@$DIR/closure-malformed-projection-input-issue-99665.rs:39:5: 39:47]`
61+
62+
error: higher-ranked lifetime error
63+
--> $DIR/closure-malformed-projection-input-issue-99665.rs:39:5
64+
|
65+
LL | |_: <Ty2<&u8> as MyComponent>::Properties| {};
66+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
67+
|
68+
= note: could not normalize `[closure@$DIR/closure-malformed-projection-input-issue-99665.rs:39:5: 39:47]`
69+
70+
error: aborting due to 9 previous errors
71+

0 commit comments

Comments
 (0)