Skip to content

Commit a8877cf

Browse files
Handle reporting invariance of fn pointer
1 parent 60e50fc commit a8877cf

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

Diff for: compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+11
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
358358
);
359359
(desc, note)
360360
}
361+
ty::FnDef(def_id, _) => {
362+
let name = self.infcx.tcx.item_name(*def_id);
363+
let identity_substs =
364+
InternalSubsts::identity_for_item(self.infcx.tcx, *def_id);
365+
let desc = format!("a function pointer to `{name}`");
366+
let note = format!(
367+
"the function `{name}` is invariant over the parameter `{}`",
368+
identity_substs[param_index as usize]
369+
);
370+
(desc, note)
371+
}
361372
_ => panic!("Unexpected type {:?}", ty),
362373
};
363374
diag.note(&format!("requirement occurs because of {desc}",));

Diff for: src/test/ui/nll/issue-95272.rs

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(nll)]
2+
3+
use std::cell::Cell;
4+
5+
fn check<'a, 'b>(x: Cell<&'a ()>, y: Cell<&'b ()>)
6+
where
7+
'a: 'b,
8+
{
9+
}
10+
11+
fn test<'a, 'b>(x: Cell<&'a ()>, y: Cell<&'b ()>) {
12+
let f = check;
13+
//~^ ERROR lifetime may not live long enough
14+
f(x, y);
15+
}
16+
17+
fn main() {}

Diff for: src/test/ui/nll/issue-95272.stderr

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/issue-95272.rs:12:13
3+
|
4+
LL | fn test<'a, 'b>(x: Cell<&'a ()>, y: Cell<&'b ()>) {
5+
| -- -- lifetime `'b` defined here
6+
| |
7+
| lifetime `'a` defined here
8+
LL | let f = check;
9+
| ^^^^^ assignment requires that `'a` must outlive `'b`
10+
|
11+
= help: consider adding the following bound: `'a: 'b`
12+
= note: requirement occurs because of a function pointer to `check`
13+
= note: the function `check` is invariant over the parameter `'a`
14+
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
15+
16+
error: aborting due to previous error
17+

0 commit comments

Comments
 (0)