Handle reporting invariance of fn pointer

This commit is contained in:
Michael Goulet 2022-04-02 15:27:14 -07:00
parent 60e50fc1cf
commit a8877cf738
3 changed files with 45 additions and 0 deletions

View File

@ -358,6 +358,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
);
(desc, note)
}
ty::FnDef(def_id, _) => {
let name = self.infcx.tcx.item_name(*def_id);
let identity_substs =
InternalSubsts::identity_for_item(self.infcx.tcx, *def_id);
let desc = format!("a function pointer to `{name}`");
let note = format!(
"the function `{name}` is invariant over the parameter `{}`",
identity_substs[param_index as usize]
);
(desc, note)
}
_ => panic!("Unexpected type {:?}", ty),
};
diag.note(&format!("requirement occurs because of {desc}",));

View File

@ -0,0 +1,17 @@
#![feature(nll)]
use std::cell::Cell;
fn check<'a, 'b>(x: Cell<&'a ()>, y: Cell<&'b ()>)
where
'a: 'b,
{
}
fn test<'a, 'b>(x: Cell<&'a ()>, y: Cell<&'b ()>) {
let f = check;
//~^ ERROR lifetime may not live long enough
f(x, y);
}
fn main() {}

View File

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