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

Trait methods can return &T types without lifetimes #8841

Closed
erickt opened this issue Aug 29, 2013 · 4 comments
Closed

Trait methods can return &T types without lifetimes #8841

erickt opened this issue Aug 29, 2013 · 4 comments

Comments

@erickt
Copy link
Contributor

erickt commented Aug 29, 2013

It doesn't seem to matter whether or not an abstract trait method returns a &T type without a region:

trait A {
    fn foo(&self) -> ∫
}

struct B {
    x: int,
}

/*
impl A for B {
    fn foo(&self) -> &int { &self.x }
}
*/

fn main() {
}

Compiles fine. However, once impl A for B is uncommented, it properly errors with:

foo.rs:2:28: 2:40 error: cannot infer an appropriate lifetime due to conflicting requirements
foo.rs:2     fn foo(&self) -> &int { self.bar() }
                                     ^~~~~~~~~~~~
foo.rs:2:26: 2:40 note: first, the lifetime cannot outlive the anonymous lifetime #1 defined on the block at 2:26...
foo.rs:2     fn foo(&self) -> &int { self.bar() }
                                   ^~~~~~~~~~~~~~
foo.rs:2:28: 2:33 note: ...due to the following expression
foo.rs:2     fn foo(&self) -> &int { self.bar() }
                                     ^~~~~
foo.rs:2:26: 2:40 note: but, the lifetime must be valid for the anonymous lifetime #2 defined on the block at 2:26...
foo.rs:2     fn foo(&self) -> &int { self.bar() }
                                   ^~~~~~~~~~~~~~
foo.rs:2:28: 2:40 note: ...due to the following expression
foo.rs:2     fn foo(&self) -> &int { self.bar() }
                                     ^~~~~~~~~~~~

What's interesting is that the impl cannot correct for the error in the trait. Changing it to this:

impl A for B {
    fn foo<'a>(&'a self) -> &'a int { &self.x }
}

Results in this error:

foo.rs:10:4: 10:47 error: method `foo` has an incompatible type: expected concrete lifetime, but found bound lifetime parameter &
foo.rs:10     fn foo<'a>(&'a self) -> &'a int { &self.x }
              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I feel we should error out on the trait method or else we risk someone creating an unimplementable trait.

@bluss
Copy link
Member

bluss commented Aug 29, 2013

Compare with issue #8309; &int is effectively the same as &'static int here, which makes it implementable, but relatively useless.

@nikomatsakis
Copy link
Contributor

Also a dup of #2478 I suspect

@flaper87
Copy link
Contributor

Triage bump

Still an issue and the example in the report is still valid.

@alexcrichton
Copy link
Member

This is now an error or expected with lifetime elision, so closing.

xFrednet pushed a commit to xFrednet/rust that referenced this issue May 21, 2022
Remove code block from `pub_enum_variant_names`

changelog: none

Just noticed this empty code block that no other lint has 😄.
![old](https://user-images.githubusercontent.com/69764315/169314573-098ce938-8a4c-4451-afd7-7b082823a0de.png)

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

No branches or pull requests

5 participants