Handle reporting invariance of fn pointer
This commit is contained in:
parent
60e50fc1cf
commit
a8877cf738
3 changed files with 45 additions and 0 deletions
|
@ -358,6 +358,17 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
|
||||||
);
|
);
|
||||||
(desc, note)
|
(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),
|
_ => panic!("Unexpected type {:?}", ty),
|
||||||
};
|
};
|
||||||
diag.note(&format!("requirement occurs because of {desc}",));
|
diag.note(&format!("requirement occurs because of {desc}",));
|
||||||
|
|
17
src/test/ui/nll/issue-95272.rs
Normal file
17
src/test/ui/nll/issue-95272.rs
Normal 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() {}
|
17
src/test/ui/nll/issue-95272.stderr
Normal file
17
src/test/ui/nll/issue-95272.stderr
Normal 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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue