Skip to content
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

Asking for a 'static bound when not needed #14901

Closed
carllerche opened this issue Jun 14, 2014 · 8 comments
Closed

Asking for a 'static bound when not needed #14901

carllerche opened this issue Jun 14, 2014 · 8 comments
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@carllerche
Copy link
Member

Repro:

use std::io::{Reader,MemReader};

enum Wrapper<'a> {
  WrapReader(&'a Reader)
}

trait Wrap<'a> {
  fn wrap(self) -> Wrapper<'a>;
}

impl<'a, R: Reader> Wrap<'a> for &'a mut R {
  fn wrap(self) -> Wrapper<'a> {
    WrapReader(self as &'a mut Reader)
  }
}
pub fn main() {
  println!("Zomg!");
}

Error:

repro.rs:13:16: 13:20 error: value may contain references; add `'static` bound to `R`
repro.rs:13     WrapReader(self as &'a mut Reader)
                           ^~~~
error: aborting due to previous error
@alexcrichton
Copy link
Member

cc @luqmana

@emberian
Copy link
Member

I don't think it is unneeded, but I'm not sure.

@carllerche
Copy link
Member Author

Converting everything to 100% trait objects compiles:

use std::io::{Reader,BufReader};

enum Wrapper<'a> {
  WrapReader(&'a Reader)
}

trait Wrap<'a> {
  fn wrap(self) -> Wrapper<'a>;
}

impl<'a> Wrap<'a> for &'a mut Reader {
  fn wrap(self) -> Wrapper<'a> {
    WrapReader(self)
  }
}

fn doit<'a>(b: &'a [u8]) {
  let mut r = BufReader::new(b);
  (&mut r as &mut Reader).wrap();
}

pub fn main() {
  let s = "zomg".to_string();
  doit(s.as_slice().as_bytes());
  println!("Zomg!");
}

So it seems that the compile error, at least in the given example, is not a safety problem.

@nikomatsakis
Copy link
Contributor

cc me

@nikomatsakis
Copy link
Contributor

I've been working on a fix for this (and general cleanup of this issue). Unfortunately, I've put it on hold for #5527, it seems like this kind of thing keeps coming up though.

@nikomatsakis
Copy link
Contributor

I think the original error is legit, but there is no great way to fix it right now. I expect one to write something like R:'a+Reader -- basically, we can't cast R to an &'a mut Reader because we don't know whether R contains borrowed pointers with a lifetime shorter than 'a. Enabling this sort of syntax is what I was working on (among other related things).

@carllerche
Copy link
Member Author

Looks like this is fixed by #16453

@sfackler
Copy link
Member

Fixed by #18324

bors added a commit to rust-lang-ci/rust that referenced this issue Jun 5, 2023
internal: Move flycheck and config errors to status notification

cc rust-lang/rust-analyzer#14193
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.
Projects
None yet
Development

No branches or pull requests

5 participants