Skip to content

Regression in required trait bounds in Rust 1.78.0  #124984

Closed
@alexcrichton

Description

@alexcrichton

Code

This code

pub trait Host {}

impl<T: ?Sized + Host> Host for &mut T {}

pub trait GetHost<T>: Send + Sync + Copy + 'static {
    fn get_host<'a>(&self, data: &'a mut T) -> impl Host;
}

impl<T, U, F> GetHost<T> for F
where
    U: Host,
    F: Fn(&mut T) -> &mut U + Send + Sync + Copy + 'static,
{
    fn get_host<'a>(&self, data: &'a mut T) -> impl Host {
        self(data)
    }
}

fn main() {}

Compiles successfully with Rust 1.77.0 but fails to compile with Rust 1.78.0:

error[E0309]: the parameter type `U` may not live long enough
  --> foo.rs:15:9
   |
14 |     fn get_host<'a>(&self, data: &'a mut T) -> impl Host {
   |                 -- the parameter type `U` must be valid for the lifetime `'a` as defined here...
15 |         self(data)
   |         ^^^^^^^^^^ ...so that the type `U` will meet its required lifetime bounds
   |
help: consider adding an explicit lifetime bound
   |
14 |     fn get_host<'a>(&self, data: &'a mut T) -> impl Host where U: 'a {
   |                                                          +++++++++++

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0309`.

Version it worked on

It most recently worked on: 1.77.0

Version with regression

rustc --version --verbose:

rustc 1.78.0 (9b00956e5 2024-04-29)
binary: rustc
commit-hash: 9b00956e56009bab2aa15d7bff10916599e3d6d6
commit-date: 2024-04-29
host: x86_64-unknown-linux-gnu
release: 1.78.0
LLVM version: 18.1.2

Information from cargo bisect-rustc

Regression in rust-lang-ci@68a543d (#118882)

searched nightlies: from nightly-2024-02-01 to nightly-2024-03-16
regressed nightly: nightly-2024-02-15
searched commit range: a84bb95...ee9c7c9
regressed commit: 7508c3e

bisected with cargo-bisect-rustc v0.6.8

Host triple: x86_64-unknown-linux-gnu
Reproduce with:

cargo bisect-rustc --start 1.77.0 --end 1.78.0 --script rustc --preserve -- foo.rs --crate-type lib

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.regression-untriagedUntriaged performance or correctness regression.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions