mirror of
https://github.com/rust-lang/rust.git
synced 2024-10-30 14:01:51 +00:00
Handle reporting invariance of fn pointer
This commit is contained in:
parent
60e50fc1cf
commit
a8877cf738
@ -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}",));
|
||||
|
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…
Reference in New Issue
Block a user