Skip to content

Commit

Permalink
Handle reporting invariance of fn pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed Apr 5, 2022
1 parent 60e50fc commit a8877cf
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
11 changes: 11 additions & 0 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}",));
Expand Down
17 changes: 17 additions & 0 deletions src/test/ui/nll/issue-95272.rs
Original file line number Diff line number Diff line change
@@ -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 changes: 17 additions & 0 deletions src/test/ui/nll/issue-95272.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit a8877cf

Please sign in to comment.