Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Lifetime bound error when using the indexing operator with an associated type containing a lifetime #32382

Closed

Description

@koute

Please consider the following code:

use std::marker::PhantomData;
use std::ops::Index;

pub trait Context: Clone {
    type Container: ?Sized;
    fn foobar_1( container: &Self::Container ) -> &str;
    fn foobar_2( container: &Self::Container ) -> &str;
    fn foobar_3( container: &Self::Container ) -> &str;
}

#[derive(Clone)]
struct Foobar<'a> {
    phantom: PhantomData<&'a ()>
}

impl<'a> Context for Foobar<'a> {
    type Container = [&'a str];

    fn foobar_1<'r>( container: &'r [&'a str] ) -> &'r str {
        // This compiles fine.
        container[0]
    }

    fn foobar_2<'r>( container: &'r Self::Container ) -> &'r str {
        // This also compiles fine.
        container.index( 0 )
    }

    fn foobar_3<'r>( container: &'r Self::Container ) -> &'r str {
        // error: `*container[..]` does not live long enough
        container[0]
    }
}

fn main() {}

which fails with the following error:

test.rs:31:9: 31:21 error: `*container[..]` does not live long enough
test.rs:31         container[0]
                   ^~~~~~~~~~~~
test.rs:29:66: 32:6 note: reference must be valid for the lifetime 'r as defined on the block at 29:65...
test.rs:29     fn foobar_3<'r>( container: &'r Self::Container ) -> &'r str {
test.rs:30         // error: `*container[..]` does not live long enough
test.rs:31         container[0]
test.rs:32     }
test.rs:29:66: 32:6 note: ...but borrowed value is only valid for the lifetime 'a as defined on the block at 29:65
test.rs:29     fn foobar_3<'r>( container: &'r Self::Container ) -> &'r str {
test.rs:30         // error: `*container[..]` does not live long enough
test.rs:31         container[0]
test.rs:32     }
error: aborting due to previous error

So if I use .index() and use an associated type it compiles fine. If I use [] and replace the associated type with the underlying type it also compiles fine. Logic would have it that if I use [] and I'll leave the associated type as-is it should also compile fine, but alas, I get an error, which doesn't really make any sense.

Rust version: rustc 1.9.0-nightly (b12b4e4 2016-03-17)

This is a regression; this code used to compile on at least 1.4, 1.5 and 1.6; it broke at 1.7.

Activity

  1. alexcrichton commented on Mar 21, 2016

    @alexcrichton
    Member
  2. alexcrichton commented on Mar 21, 2016

    @alexcrichton
    Member

    also @rust-lang/compiler

  3. added
    regression-from-stable-to-stable
    on Mar 21, 2016
  4. added
    T-compiler
    T-lang
    on Jun 23, 2016
  5. self-assigned this
    on Jun 23, 2016
  6. pnkfelix commented on Jun 23, 2016

    @pnkfelix
    Contributor

    I will look at this.

  7. pnkfelix commented on Jun 23, 2016

    @pnkfelix
    Contributor

    marking as P-medium nonetheless, since I am not convinced this needs to high priority...

  8. added
    P-medium
    and removed on Jun 23, 2016
  9. added
    A-type-system
    A-borrow-checker
    and removed
    A-borrow-checker
    on Sep 13, 2016
  10. added
    A-lifetimes
    on Sep 13, 2016
  11. 2 remaining items

  12. Mark-Simulacrum commented on May 13, 2017

    @Mark-Simulacrum
    Member

    @pnkfelix You are currently assigned to this, are you still investigating?

  13. eddyb commented on May 13, 2017

    @eddyb
    Contributor

    cc @nikomatsakis as well

  14. added
    C-bug
    on Jul 24, 2017
  15. JelteF commented on Apr 2, 2018

    @JelteF
    Contributor

    I'm running into this issue as well when trying to convert get().unwrap() to an index operation on this line:
    https://github.com/JelteF/derive_more/blob/2e37e7f8181ba333df9503088222772b77c83d91/src/from.rs#L103

    It indeed works fine when manually calling index().

  16. added
    fixed-by-NLL
    on Oct 4, 2018
  17. pnkfelix commented on Oct 4, 2018

    @pnkfelix
    Contributor

    I just tried this out with the 2018 edition and it works there. Or on the nightly compiler, one can add #![feature(nll)] to get the fix.

    We should add a regression test before closing though.

  18. added
    E-needs-test
    on Oct 4, 2018
  19. added a commit that references this issue on Oct 4, 2018
  20. added a commit that references this issue on Oct 5, 2018
  21. added a commit that references this issue on Oct 6, 2018
  22. pnkfelix commented on Nov 9, 2018

    @pnkfelix
    Contributor

    #54812 added the test.

  23. removed
    E-needs-test
    on Nov 9, 2018
  24. removed their assignment
    on Nov 9, 2018
  25. pnkfelix commented on Apr 29, 2019

    @pnkfelix
    Contributor

    NLL (migrate mode) is enabled in all editions as of PR #59114. Verified that test case compiled in Nightly 2015 edition; closing as fixed.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-lifetimesA-type-systemC-bugP-mediumT-compilerT-langfixed-by-NLLregression-from-stable-to-stable

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions