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

static lifetime being required too often #13323

Closed
carllerche opened this issue Apr 4, 2014 · 5 comments
Closed

static lifetime being required too often #13323

carllerche opened this issue Apr 4, 2014 · 5 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:
https://gist.github.com/carllerche/c426ece39be0705ba6c2

I am not really sure what is going on. Maybe this is a dup of #8874? Unsure.

@alexcrichton
Copy link
Member

cc @nikomatsakis

Replacing equal_to("zomg") with equal_to("zomg".as_slice()) makes the error go away, which makes me think that this should not be a compiler error.

@carllerche
Copy link
Member Author

Woops, I guess I forgot to add the .as_slice() fix in the Gist... sorry.

@huonw huonw added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Nov 10, 2014
@huonw
Copy link
Member

huonw commented Nov 10, 2014

This now works fine (only changes were updating to make it compile):

struct StrWrap {
  s: String
}

impl StrWrap {
  fn new(s: &str) -> StrWrap {
    StrWrap { s: s.into_string() }
  }

  fn get_s<'a>(&'a self) -> &'a str {
    self.s.as_slice()
  }
}

struct MyStruct {
  s: StrWrap
}

impl MyStruct {
  fn new(s: &str) -> MyStruct {
    MyStruct { s: StrWrap::new(s) }
  }

  fn get_str_wrap<'a>(&'a self) -> &'a StrWrap {
    &self.s
  }
}

trait Matcher<T> {
  fn matches(&self, actual: T) -> bool;
}

fn assert_that<T, U: Matcher<T>>(actual: T, matcher: &U) {
  assert!(matcher.matches(actual));
}

struct EqualTo<T> {
  expected: T
}

impl<T: Eq> Matcher<T> for EqualTo<T> {
  fn matches(&self, actual: T) -> bool {
    self.expected.eq(&actual)
  }
}

fn equal_to<T: Eq>(expected: T) -> Box<EqualTo<T>> {
  box EqualTo { expected: expected }
}

pub fn main() {
  let my_struct = MyStruct::new("zomg");
  let s = my_struct.get_str_wrap();

  assert_that(s.get_s(), &*equal_to("zomg"));
}

Changing the second last line to

  assert_that(s.get_s(), &*equal_to::<&'static str>("zomg"));

causes the same compilation error as in the original code, suggesting that this is actually rustc being more intelligent, and is not just being disguised by the language changes since this was filed; yay!

Flagging as needstest.

@tamird
Copy link
Contributor

tamird commented Dec 23, 2014

This should have been closed by #19780! See https://github.com/rust-lang/rust/blob/master/src/test/run-pass/issue-13323.rs

cc @jakub-

@alexcrichton
Copy link
Member

Thanks @tamird!

flip1995 pushed a commit to flip1995/rust that referenced this issue Sep 5, 2024
Changelog for Clippy 1.81 🔰

Roses are red,
Violets are blue,
Expectations are stable,
And reasons are set

---

### The cat of this release is *Keepy* submitted by `@blyxyas:`

<img height=500 src="https://github.com/rust-lang/rust-clippy/assets/73757586/902dd802-5ac8-471e-bb93-e195526ba580" alt="The cats of this Clippy release" />

Cats for the next release can be nominated in the comments :D

---

changelog: none
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

4 participants