Skip to content

Commit 2e741a8

Browse files
authored
Rollup merge of rust-lang#78268 - JohnTitor:issue-78262, r=estebank
Do not try to report on closures to avoid ICE Fixes rust-lang#78262
2 parents b829cef + 4ec396e commit 2e741a8

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-0
lines changed

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+8
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
3939
) if **sub_r == RegionKind::ReStatic => {
4040
// This is for an implicit `'static` requirement coming from `impl dyn Trait {}`.
4141
if let ObligationCauseCode::UnifyReceiver(ctxt) = &cause.code {
42+
// This may have a closure and it would cause ICE
43+
// through `find_param_with_region` (#78262).
44+
let anon_reg_sup = tcx.is_suitable_region(sup_r)?;
45+
let fn_returns = tcx.return_type_impl_or_dyn_traits(anon_reg_sup.def_id);
46+
if fn_returns.is_empty() {
47+
return None;
48+
}
49+
4250
let param = self.find_param_with_region(sup_r, sub_r)?;
4351
let lifetime = if sup_r.has_name() {
4452
format!("lifetime `{}`", sup_r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/issue-78262.rs:12:28
3+
|
4+
LL | let f = |x: &dyn TT| x.func();
5+
| ^^^^ lifetime mismatch
6+
|
7+
= note: expected reference `&(dyn TT + 'static)`
8+
found reference `&dyn TT`
9+
note: the anonymous lifetime #1 defined on the body at 12:13...
10+
--> $DIR/issue-78262.rs:12:13
11+
|
12+
LL | let f = |x: &dyn TT| x.func();
13+
| ^^^^^^^^^^^^^^^^^^^^^
14+
= note: ...does not necessarily outlive the static lifetime
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0308`.
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error[E0521]: borrowed data escapes outside of closure
2+
--> $DIR/issue-78262.rs:12:26
3+
|
4+
LL | let f = |x: &dyn TT| x.func();
5+
| - ^^^^^^^^ `x` escapes the closure body here
6+
| |
7+
| `x` is a reference that is only valid in the closure body
8+
9+
error: aborting due to previous error
10+

src/test/ui/regions/issue-78262.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// revisions: nll default
2+
// ignore-compare-mode-nll
3+
//[nll]compile-flags: -Z borrowck=mir
4+
5+
trait TT {}
6+
7+
impl dyn TT {
8+
fn func(&self) {}
9+
}
10+
11+
fn main() {
12+
let f = |x: &dyn TT| x.func(); //[default]~ ERROR: mismatched types
13+
//[nll]~^ ERROR: borrowed data escapes outside of closure
14+
}

0 commit comments

Comments
 (0)