Skip to content

Missing lifetimes in pointer signatures #1249

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

Closed
yyny opened this issue Mar 18, 2025 · 2 comments
Closed

Missing lifetimes in pointer signatures #1249

yyny opened this issue Mar 18, 2025 · 2 comments

Comments

@yyny
Copy link

yyny commented Mar 18, 2025

Commit c44a7aa removes the lifetimes on pointer and pointer_mut, which makes the inferred lifetimes too restrictive:

fn bug(val: &mut serde_json::Value, pointer: &str) -> &mut serde_json::Value {
    match val.pointer_mut(pointer) {
        Some(v) => v,
        None => val,
    }
}
 error[E0106]: missing lifetime specifier
 --> bug.rs:1:55
  |
1 | fn bug(val: &mut serde_json::Value, pointer: &str) -> &mut serde_json_Value {
  |             ----------------------           ----     ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `val` or `pointer`
help: consider introducing a named lifetime parameter
  |
1 | fn bug<'a>(val: &'a mut serde_json::Value, pointer: &'a str) -> &'a mut serde_json_Value {
  |       ++++       ++                                  ++          ++

fn bug<'a>(val: &'a mut serde_json::Value, pointer: &str) -> &'a mut serde_json::Value {
    match val.pointer_mut(pointer) {
        Some(v) => v,
        None => val,
    }
}
error[E0505]: cannot move out of `val` because it is borrowed
 --> bug.rs:4:17
  |
1 |   fn bug<'a>(val: &'a mut serde_json::Value, pointer: &str) -> &'a mut serde_json::Value {
  |          --  --- binding `val` declared here
  |          |
  |          lifetime `'a` defined here
2 |       match val.pointer_mut(pointer) {
  |       -     --- borrow of `*val` occurs here
  |  _____|
  | |
3 | |         Some(v) => v,
4 | |         None => val,
  | |                 ^^^ move out of `val` occurs here
5 | |     }
  | |_____- returning this value requires that `*val` is borrowed for `'a`


fn bug<'a>(val: &'a mut serde_json::Value, pointer: &str) -> &'a mut serde_json::Value {
    match unsafe { std::mem::transmute::<Option<&mut serde_json::Value, &'a mut serde_json::Value>(val.pointer_mut(pointer)) } {
        Some(v) => v,
        None => val,
    }
}
@dtolnay
Copy link
Member

dtolnay commented Mar 18, 2025

The lifetime error in your second snippet is rust-lang/rust#21906, and the lifetime error in your first snippet is unrelated to any signatures in serde_json. The 5-year-old commit you linked doesn't have relevance to either one.

@dtolnay dtolnay closed this as completed Mar 18, 2025
@yyny
Copy link
Author

yyny commented Mar 18, 2025

Well, the first snippet is just to show why the second snippet is necessary, but you're right, this looks like a compiler bug, thanks for the link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants