Open
Description
Both of these test cases should pass borrowck: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2015&gist=50a263a9626cb0b08f5cee334df8a852
mod case1 {
struct MyTy<'x, 'a, 'b>(std::cell::Cell<(&'x &'a u8, &'x &'b u8)>);
fn wf<T>(_: T) {}
fn test<'a, 'b>() {
|_: &'a u8, x: MyTy<'_, 'a, 'b>| wf(x); // FAIL!
|x: MyTy<'_, 'a, 'b>, _: &'a u8| wf(x); // PASS!!!
}
}
mod case2 {
struct MyTy<'a, 'b, 'x>(std::cell::Cell<(&'a &'x u8, &'b &'x u8)>);
fn wf<T>(_: T) {}
fn test<'a, 'b>() {
|x: MyTy<'a, 'b, '_>| wf(x); // FAIL!
}
}
They both fail because of the unnecessarily restrictive algorithm in try_propagate_universal_region_error:
case1
fails because'_
has multiple "non-local upper bounds"['a, 'b]
and we do propagate the error to both of them while choosing a single one would be sufficient.case2
fails because'_
has multiple "non-local lower bounds"['a, 'b]
and the call to non_lcal_lower_bound simply returnsNone
in this case!- The different behavior in
case1
when swapping arguments is due to the shortcut in which is sensitive to region numbering, but I think that's irrelevant here.
@rustbot label C-bug T-types A-NLL NLL-complete A-borrow-checker
@rustbot claim
Metadata
Metadata
Assignees
Labels
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
Rageking8 commentedon Nov 16, 2022
compiler-errors commentedon Nov 16, 2022
lcnr commentedon Jan 15, 2025
This issue also affects type tests (and i expect to this error even if region outlives were fixed)
rust/compiler/rustc_borrowck/src/region_infer/mod.rs
Lines 1012 to 1016 in 2776bdf
There's no reason to look at all lower bounds of our region in case that region is already external.
aliemjay commentedon Jan 15, 2025
@lcnr Your issue can be reproduced and fixed independently using this alternative example:
I think the way to fix it is a bit more subtle. Consider the example above where
'x
is not external. The real problem is thatnon_local_upper_bounds('x)
incorrectly returns['a, 'b]
when it should return['a]
only.rust/compiler/rustc_borrowck/src/region_infer/mod.rs
Line 1019 in 2776bdf