Skip to content

Regression in interaction between lifetime parameter and defaulted type parameter #49344

Closed

Description

@bsteinb

The mpi crate contains (approximately) the following code that used to work before nightly-2018-03-02:

use std::cell::Cell;
use std::marker::PhantomData;

pub struct Request<'a, S: Scope<'a> = StaticScope> {
    scope: S,
    phantom: PhantomData<Cell<&'a ()>>,
}

pub unsafe trait Scope<'a> {
    /// Registers a request with the scope.
    fn register(&self);

    /// Unregisters a request from the scope.
    unsafe fn unregister(&self);
}

pub struct StaticScope;

unsafe impl Scope<'static> for StaticScope {
    fn register(&self) {}

    unsafe fn unregister(&self) {}
}

The newest nightly compiler that successfully compiles this is:

rustc 1.26.0-nightly (0ff9872b2 2018-02-28)
binary: rustc
commit-hash: 0ff9872b2280009f094af0df3dcdc542cc46a5fd
commit-date: 2018-02-28
host: x86_64-apple-darwin
release: 1.26.0-nightly
LLVM version: 6.0

It no longer compiles on:

rustc 1.26.0-nightly (3eeb5a665 2018-03-01)
binary: rustc
commit-hash: 3eeb5a665e313c5b281820099e04d4c6c8188b46
commit-date: 2018-03-01
host: x86_64-apple-darwin
release: 1.26.0-nightly
LLVM version: 6.0

Was this not supposed to ever work or is this a regression?

Activity

  1. added
    A-lifetimesArea: Lifetimes / regions
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    C-bugCategory: This is a bug.
    on Mar 27, 2018
  2. bsteinb commented on Mar 28, 2018

    @bsteinb
    Author

    Oops, it seems I completely forgot to paste the resulting error message:

    error[E0308]: mismatched types
     --> src/lib.rs:4:1
      |
    4 | / pub struct Request<'a, S: Scope<'a> = StaticScope> {
    5 | |     scope: S,
    6 | |     phantom: PhantomData<Cell<&'a ()>>,
    7 | | }
      | |_^ lifetime mismatch
      |
      = note: expected type `Scope<'a>`
                 found type `Scope<'static>`
    note: the lifetime 'a as defined on the struct at 4:1...
     --> src/lib.rs:4:1
      |
    4 | pub struct Request<'a, S: Scope<'a> = StaticScope> {
      | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      = note: ...does not necessarily outlive the static lifetime
    
    error: aborting due to previous error
    
    For more information about this error, try `rustc --explain E0308`.
    error: Could not compile `rsmpi-request-lifetime-issue`.
    
    To learn more, run the command again with --verbose.
    

    Sorry about that. As mentioned in the original issue the problem can be worked around by not having the type parameter S default to StaticScope and instead always specify both parameters explicitly, e.g. Request<'static, StaticScope>

  3. nikomatsakis commented on Apr 5, 2018

    @nikomatsakis
    Contributor

    I'm guessing this is caused by #46785 but I'm not sure yet

  4. nikomatsakis commented on Apr 5, 2018

    @nikomatsakis
    Contributor

    cc @leodasvacas -- I think was something of an unexpected interaction, do you agree? I think we are probably checking that the default value (StaticScope) meets the bounds Scope<'a>, but we shouldn't be, because it talks about 'a.

  5. nikomatsakis commented on Apr 5, 2018

    @nikomatsakis
    Contributor

    triage: P-high

  6. self-assigned this
    on Apr 5, 2018
  7. leoyvens commented on Apr 5, 2018

    @leoyvens
    Contributor

    Yes it's a bug in that PR, we should not be doing this check in predicates with lifetimes, I'll work on a fix.

  8. added a commit that references this issue on Apr 5, 2018
    933f9eb
  9. added a commit that references this issue on Apr 8, 2018
  10. added a commit that references this issue on Apr 10, 2018
    be673e8
  11. added a commit that references this issue on Apr 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-lifetimesArea: Lifetimes / regionsC-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.regression-from-stable-to-nightlyPerformance or correctness regression from stable to nightly.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions