Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign up[nll] cast to raw pointer creates an "enduring" borrow #53123
Comments
nikomatsakis
added
T-compiler
WG-compiler-nll
NLL-complete
labels
Aug 6, 2018
nikomatsakis
added this to the Rust 2018 RC milestone
Aug 6, 2018
This comment has been minimized.
This comment has been minimized.
|
OK, the problem here is due to the If we change to: #![feature(nll)]
#![allow(unused_variables)]
pub trait TryTransform {
fn try_transform<F>(self, f: F)
where
Self: Sized,
F: FnOnce(Self);
}
impl<'a, T> TryTransform for &'a mut T {
fn try_transform<F>(self, f: F)
where
// Self: Sized,
F: FnOnce(Self),
{
let this: *mut T = self as *mut T;
f(self);
}
}
fn main() {
}then the code compiles as expected. I'm actually not sure why the AST code avoids this problem. I guess because it just doesn't happen to try proving Probably the best fix is to special-case |
nikomatsakis
referenced this issue
Aug 7, 2018
Closed
`Borrow::borrow()` causes compile error with NLL, but compiles without NLL #53151
This comment has been minimized.
This comment has been minimized.
|
OK, so one possible fix is to hack in the trait system. Right now, we arbitrarily give where-clauses higher priority than impls. We could tweak this by hacking on rust/src/librustc/traits/select.rs Lines 2019 to 2029 in 18925de The implications are...not entirely obvious, but still, I can't see much harm in modifying that function to give |
nikomatsakis
added
I-nominated
E-mentor
labels
Aug 7, 2018
This comment has been minimized.
This comment has been minimized.
|
cc @matthewjasper — I know you're familiar with this code, though you've already got a lot of branches. Thoughts? |
This comment has been minimized.
This comment has been minimized.
|
@nikomatsakis built in traits used to be more special cased than they are now, so it might be worth checking if I changed something here. The proposed solution sounds good, although I suspect it might break type inference in some obscure cases. |
This comment has been minimized.
This comment has been minimized.
Never say never, but the scenarios I can construct in my head are pretty obscure indeed. The other option is to special case this in MIR borrow check. In particular, we could skip proving rust/src/librustc_mir/borrow_check/nll/type_check/mod.rs Lines 902 to 906 in 39e9516 That is a total hack but seems obviously "fine". That said, the tweak to select just might also fix #53156, hard to say. |
nikomatsakis commentedAug 6, 2018
The following program (extracted from
try_transform_mutv0.1.0) fails to compile with MIR-based borrow check:The error is:
I'm not entirely sure what is going on here, but it seems strange. I would not consider the
self as _to be a "borrow" ofselfreally.