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

Anonymous lifetime inference regression #10396

Closed
sfackler opened this issue Nov 10, 2013 · 7 comments · Fixed by #18555
Closed

Anonymous lifetime inference regression #10396

sfackler opened this issue Nov 10, 2013 · 7 comments · Fixed by #18555
Labels
A-lifetimes Area: lifetime related E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added.

Comments

@sfackler
Copy link
Member

The following function compiled up to today:

    fn write_messages(&mut self, messages: &[&FrontendMessage]) {
         for &message in messages.iter() {
             self.stream.write_message(message);
         }
         self.stream.flush();
     }

After updating to the from yesterday's Rust nightly to today's, it now fails to compile with the following error:

/home/sfackler/rust/src/rust-postgres/lib.rs:540:13: 540:20 error: in type `&lib::message::FrontendMessage<>`, pointer has a longer lifetime than the data it references
/home/sfackler/rust/src/rust-postgres/lib.rs:540         for &message in messages.iter() {
                                                              ^~~~~~~
/home/sfackler/rust/src/rust-postgres/lib.rs:539:64: 544:5 note: the pointer is valid for the anonymous lifetime #3 defined on the block at 539:64
/home/sfackler/rust/src/rust-postgres/lib.rs:539     fn write_messages(&mut self, messages: &[&FrontendMessage]) {
/home/sfackler/rust/src/rust-postgres/lib.rs:540         for &message in messages.iter() {
/home/sfackler/rust/src/rust-postgres/lib.rs:541             self.stream.write_message(message);
/home/sfackler/rust/src/rust-postgres/lib.rs:542         }
/home/sfackler/rust/src/rust-postgres/lib.rs:543         self.stream.flush();
/home/sfackler/rust/src/rust-postgres/lib.rs:544     }
/home/sfackler/rust/src/rust-postgres/lib.rs:539:64: 544:5 note: but the referenced data is only valid for the anonymous lifetime #4 defined on the block at 539:64
/home/sfackler/rust/src/rust-postgres/lib.rs:539     fn write_messages(&mut self, messages: &[&FrontendMessage]) {
/home/sfackler/rust/src/rust-postgres/lib.rs:540         for &message in messages.iter() {
/home/sfackler/rust/src/rust-postgres/lib.rs:541             self.stream.write_message(message);
/home/sfackler/rust/src/rust-postgres/lib.rs:542         }
/home/sfackler/rust/src/rust-postgres/lib.rs:543         self.stream.flush();
/home/sfackler/rust/src/rust-postgres/lib.rs:544     }
error: aborting due to previous error
task '<unnamed>' failed at 'explicit failure', /build/rust-git/src/rust/src/libsyntax/diagnostic.rs:101
task '<unnamed>' failed at 'receiving on closed channel', /build/rust-git/src/rust/src/libstd/rt/comm.rs:198

Possibly related to #10153?

@jdm
Copy link
Contributor

jdm commented Nov 10, 2013

cc @nikomatsakis

@nikomatsakis
Copy link
Contributor

Interesting. I'm probably going to need more context to diagnose what's going on. @sfackler is it possible to create something self-contained or, failing that, point me at the repo / revision where this occurs?

@sfackler
Copy link
Member Author

Here's a minimal reproduction:

enum Foo<'self> {
    V(&'self str)
}

fn f(arr: &[&Foo]) {
    for &f in arr.iter() {
        println!("{:?}", f);
    }
}

It looks like the key part is that Foo contains borrowed pointers. If I change the &'self str to ~str, it compiles.

@flaper87
Copy link
Contributor

@nikomatsakis updated example:

enum Foo<'s> {
    V(&'s str)
}

fn f(arr: &[&Foo]) {
    for &f in arr.iter() {
        println!("{:?}", f);
    }
}

fn main() {}

@eddyb
Copy link
Member

eddyb commented Apr 23, 2014

This small testcase I've found (while working on #12809) seems to indicate that we don't infer 'a <= 'b from the existence of &'a &'b T (which may be implied in the original title/description):

struct Foo<'a, 'b> {
    foo: &'a &'b int
}
fn foo<'a, 'b>(x: Foo<'a, 'b>) {
    let _y = x.foo;
}

@pnkfelix
Copy link
Member

I spoke with @eddyb and pointed him at regions-variance-covariant-use-contravariant.rs to illustrate that we do infer 'a <= 'b from the existence of &'a &'b T.

The fact that the code above does not compile as-is is interesting. I cannot tell if that is actually related to the original bug filed here or not. I have now filed #13703 for it.

(Moved rest of comment to the issue description for #13703.)

@sfackler
Copy link
Member Author

This now compiles.

@sfackler sfackler added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Oct 26, 2014
flip1995 pushed a commit to flip1995/rust that referenced this issue Mar 10, 2023
Do not suggest to derive `Default` on generics with implicit arguments

Fixes rust-lang#10396

changelog: FP: [`derivable_impls`]: do not suggest to derive `Default` on generics with implicit arguments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: lifetime related 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

Successfully merging a pull request may close this issue.

6 participants