Skip to content

Commit bfd3501

Browse files
committed
fix the new unsoundness
1 parent eea5604 commit bfd3501

File tree

4 files changed

+26
-5
lines changed

4 files changed

+26
-5
lines changed

compiler/rustc_infer/src/infer/region_constraints/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,14 @@ impl<'tcx> RegionConstraintCollector<'_, 'tcx> {
642642
) -> ty::Region<'tcx> {
643643
let mut ut = self.unification_table_mut(); // FIXME(rust-lang/ena#42): unnecessary mut
644644
let root_vid = ut.find(vid).vid;
645-
ut.probe_value(root_vid).0.unwrap_or_else(|| tcx.mk_re_var(root_vid))
645+
let resolved = ut.probe_value(root_vid).0.unwrap_or_else(|| tcx.mk_re_var(root_vid));
646+
647+
// Don't resolve a variable to a region that it cannot name.
648+
if self.var_universe(vid).can_name(self.universe(resolved)) {
649+
resolved
650+
} else {
651+
tcx.mk_re_var(vid)
652+
}
646653
}
647654

648655
fn combine_map(&mut self, t: CombineMapType) -> &mut CombineMap<'tcx> {

tests/ui/higher-rank-trait-bounds/hrtb-exists-forall-trait-invariant.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
//
33
// In particular, we test this pattern in trait solving, where it is not connected
44
// to any part of the source code.
5-
//
6-
// check-pass
7-
// Oops!
85

96
use std::cell::Cell;
107

@@ -28,5 +25,5 @@ fn main() {
2825
// yielding `fn(&!b u32)`, in a fresh universe U1
2926
// - So we get `?a = !b` but the universe U0 assigned to `?a` cannot name `!b`.
3027

31-
foo::<()>();
28+
foo::<()>(); //~ ERROR implementation of `Trait` is not general enough
3229
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: implementation of `Trait` is not general enough
2+
--> $DIR/hrtb-exists-forall-trait-invariant.rs:28:5
3+
|
4+
LL | foo::<()>();
5+
| ^^^^^^^^^^^ implementation of `Trait` is not general enough
6+
|
7+
= note: `()` must implement `Trait<for<'b> fn(Cell<&'b u32>)>`
8+
= note: ...but it actually implements `Trait<fn(Cell<&'0 u32>)>`, for some specific lifetime `'0`
9+
10+
error: aborting due to previous error
11+

tests/ui/higher-rank-trait-bounds/hrtb-just-for-static.stderr

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ LL | fn give_some<'a>() {
1414
| -- lifetime `'a` defined here
1515
LL | want_hrtb::<&'a u32>()
1616
| ^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
17+
|
18+
note: due to current limitations in the borrow checker, this implies a `'static` lifetime
19+
--> $DIR/hrtb-just-for-static.rs:9:15
20+
|
21+
LL | where T : for<'a> Foo<&'a isize>
22+
| ^^^^^^^^^^^^^^^^^^^^^^
1723

1824
error: implementation of `Foo` is not general enough
1925
--> $DIR/hrtb-just-for-static.rs:30:5

0 commit comments

Comments
 (0)