Skip to content

Commit 110777b

Browse files
committed
Auto merge of rust-lang#99796 - compiler-errors:issue-53475, r=oli-obk
use `check_region_obligations_and_report_errors` to avoid ICEs If we don't call `process_registered_region_obligations` before `resolve_regions_and_report_errors` then we'll ICE if we have any region obligations, and `check_region_obligations_and_report_errors` just does both of these for us in a nice convenient function. Fixes rust-lang#53475 r? types
2 parents 760d8a2 + 16f4980 commit 110777b

File tree

8 files changed

+45
-13
lines changed

8 files changed

+45
-13
lines changed

compiler/rustc_infer/src/infer/mod.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1325,6 +1325,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
13251325
/// result. After this, no more unification operations should be
13261326
/// done -- or the compiler will panic -- but it is legal to use
13271327
/// `resolve_vars_if_possible` as well as `fully_resolve`.
1328+
///
1329+
/// Make sure to call [`InferCtxt::process_registered_region_obligations`]
1330+
/// first, or preferrably use [`InferCtxt::check_region_obligations_and_report_errors`]
1331+
/// to do both of these operations together.
13281332
pub fn resolve_regions_and_report_errors(
13291333
&self,
13301334
generic_param_scope: LocalDefId,

compiler/rustc_infer/src/infer/outlives/obligations.rs

+6
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
111111
std::mem::take(&mut self.inner.borrow_mut().region_obligations)
112112
}
113113

114+
/// NOTE: Prefer using [`InferCtxt::check_region_obligations_and_report_errors`]
115+
/// instead of calling this directly.
116+
///
114117
/// Process the region obligations that must be proven (during
115118
/// `regionck`) for the given `body_id`, given information about
116119
/// the region bounds in scope and so forth. This function must be
@@ -162,6 +165,9 @@ impl<'cx, 'tcx> InferCtxt<'cx, 'tcx> {
162165
}
163166
}
164167

168+
/// Processes registered region obliations and resolves regions, reporting
169+
/// any errors if any were raised. Prefer using this function over manually
170+
/// calling `resolve_regions_and_report_errors`.
165171
pub fn check_region_obligations_and_report_errors(
166172
&self,
167173
generic_param_scope: LocalDefId,

compiler/rustc_trait_selection/src/traits/coherence.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,7 @@ fn resolve_negative_obligation<'cx, 'tcx>(
398398
let outlives_env = OutlivesEnvironment::new(param_env);
399399
infcx.process_registered_region_obligations(outlives_env.region_bound_pairs(), param_env);
400400

401-
let errors = infcx.resolve_regions(&outlives_env);
402-
403-
if !errors.is_empty() {
404-
return false;
405-
}
406-
407-
true
401+
infcx.resolve_regions(&outlives_env).is_empty()
408402
}
409403

410404
pub fn trait_ref_is_knowable<'tcx>(

compiler/rustc_typeck/src/check/compare_method.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,10 @@ pub(crate) fn compare_const_impl<'tcx>(
11541154
}
11551155

11561156
let outlives_environment = OutlivesEnvironment::new(param_env);
1157-
infcx
1158-
.resolve_regions_and_report_errors(impl_c.def_id.expect_local(), &outlives_environment);
1157+
infcx.check_region_obligations_and_report_errors(
1158+
impl_c.def_id.expect_local(),
1159+
&outlives_environment,
1160+
);
11591161
});
11601162
}
11611163

compiler/rustc_typeck/src/coherence/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ fn visit_implementation_of_dispatch_from_dyn<'tcx>(tcx: TyCtxt<'tcx>, impl_did:
349349

350350
// Finally, resolve all regions.
351351
let outlives_env = OutlivesEnvironment::new(param_env);
352-
infcx.resolve_regions_and_report_errors(impl_did, &outlives_env);
352+
infcx.check_region_obligations_and_report_errors(impl_did, &outlives_env);
353353
}
354354
}
355355
_ => {
@@ -606,7 +606,7 @@ pub fn coerce_unsized_info<'tcx>(tcx: TyCtxt<'tcx>, impl_did: DefId) -> CoerceUn
606606

607607
// Finally, resolve all regions.
608608
let outlives_env = OutlivesEnvironment::new(param_env);
609-
infcx.resolve_regions_and_report_errors(impl_did, &outlives_env);
609+
infcx.check_region_obligations_and_report_errors(impl_did, &outlives_env);
610610

611611
CoerceUnsizedInfo { custom_kind: kind }
612612
})

compiler/rustc_typeck/src/impl_wf_check/min_specialization.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,7 @@ fn get_impl_substs<'tcx>(
158158
implied_bounds,
159159
tcx.hir().local_def_id_to_hir_id(impl1_def_id),
160160
);
161-
infcx.process_registered_region_obligations(outlives_env.region_bound_pairs(), param_env);
162-
infcx.resolve_regions_and_report_errors(impl1_def_id, &outlives_env);
161+
infcx.check_region_obligations_and_report_errors(impl1_def_id, &outlives_env);
163162
let Ok(impl2_substs) = infcx.fully_resolve(impl2_substs) else {
164163
let span = tcx.def_span(impl1_def_id);
165164
tcx.sess.emit_err(SubstsOnOverriddenImpl { span });

src/test/ui/coercion/issue-53475.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#![feature(coerce_unsized)]
2+
3+
use std::any::Any;
4+
use std::ops::CoerceUnsized;
5+
6+
struct Foo<T> {
7+
data: Box<T>,
8+
}
9+
10+
impl<T> CoerceUnsized<Foo<dyn Any>> for Foo<T> {}
11+
//~^ ERROR the parameter type `T` may not live long enough
12+
13+
fn main() {}
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0310]: the parameter type `T` may not live long enough
2+
--> $DIR/issue-53475.rs:10:1
3+
|
4+
LL | impl<T> CoerceUnsized<Foo<dyn Any>> for Foo<T> {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
6+
|
7+
help: consider adding an explicit lifetime bound...
8+
|
9+
LL | impl<T: 'static> CoerceUnsized<Foo<dyn Any>> for Foo<T> {}
10+
| +++++++++
11+
12+
error: aborting due to previous error
13+
14+
For more information about this error, try `rustc --explain E0310`.

0 commit comments

Comments
 (0)