-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compile error if lifetime parameter is not bounded by itself #88884
Comments
I was able to find a smaller minimal example (here's the playground):
I think the compiler error is correct, i.e. @rustbot claim UPDATE: Found a smaller minimal example (here's the playground):
|
Thank you for finding a smaller minimal example. I tried based on this and I have a different opinion. My hypothesis is that it is a bug that it does not compile. It is possible that I am not understanding something.
The compiler says The next one isn't about // will not compile
fn ident<'a>(arg: &'a &'a ()) -> &'a &'a () { arg }
// will compile
// two different lifetime parameters
fn ident<'a, 'b>(arg: &'a &'b ()) -> &'a &'b () { arg }
// will not compile
// Return a reference with the shorter of the two lifetime parameters (if my understanding of lifetimes is correct)
fn ident<'a, 'b: 'a>(arg: &'a &'b ()) -> &'a &'a () { arg }
fn do_nothing<'z>(arg: &'z ()) {
(|s: &'z ()| s)(*ident(&arg));
} Again, it's possible that I'm not understanding something. |
I think you may be misunderstanding the borrow checker and the effect the Ergo, for The other function |
I didn't understand the code correctly. I think I understand it now thanks to your explanation. Incidentally, I also didn't understand that references with an unacceptable lifetime, such as By the way, I have a new discovery. This code may have the same problem as fn ident<'a>(arg: &'a &'a ()) -> &'a &'a () { arg }
fn do_nothing<'z: 'y, 'y>(arg: &'z ()) {
(|s: &'y ()| s)(ident(&arg));
} It can be compiled. However, I think it should be a compile error because the lifetime fn ident<'a>(arg: &'a &'a ()) -> &'a &'a () { arg }
fn do_nothing<'z: 'y, 'y>(arg: &'z ()) {
ident(&arg) as &'y &'y ();
} |
My understanding is that you are correct: Any lifetime parameter of a function must outlive the body of that function. Also, I agree that your new example should not compile, it has the same problem as the original example. |
Maybe this issue should be closed. version: 1.65.0-nightly (2022-08-06 2befdef) |
I used Rust Playground to check the behavior. Stable version: 1.62.1 This code can be compiled in stable and cannot be compiled in nightly. Code that should compile error if there is no bug
Copied from #88884 (comment)
Copied from #88884 (comment)
Copied from #88884 (comment) fn main() {
let wrapped_vec = WrappedVec {
vec: vec!["1"],
};
let first = first_str_len(wrapped_vec);
println!("first: {}", first);
}
struct WrappedVec<'a> {
vec: Vec<&'a str>,
}
impl<'a> WrappedVec<'a> {
fn iter(&'a self) -> impl Iterator<Item=&'a &'a str> {
self.vec.iter()
}
}
fn first_str_len<'a: 'a>(arg: WrappedVec<'a>) -> usize {
let first = arg.iter().next().unwrap();
let return_arg = |str: &'a str| -> &'a str {
str
};
return_arg(first).len()
} from #88884 (comment) |
With rust 1.64.0, the expected behavior occurs, i.e. compile errors. So I will close this as resolved. |
The compiler behaves strangely. For a given lifetime, if it bounds itself (
'a: 'a
), it will compile, otherwise it will not. I think it is a bug that this subtle difference can change whether it can be compiled or not.I tried this code:
This code does not compile.
You can compile it by rewriting the part shown below.
The code is unnatural because it was rebuilt by extracting only the parts that seemed necessary for replication from the code where the problem was found.
Meta
rustc --version --verbose
:I tried it in the Rust playground, and the same thing happened in beta and nightly.
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=05a374855198719d56d41ac02be0d59e
Backtrace
There was no backtrace
The text was updated successfully, but these errors were encountered: