Lifetime bound not satisfied on trait impl whose signature is same as trait definition
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
I'm trying to make a trait for objects supporting std::thread::scope-like functionality. The trait definition itself compiles, but the implementation reports an error if I'm using Self::Scope. (playground link)
trait HasScoped {
type Scope<'scope, 'env: 'scope>: Scope<'scope, 'env>;
fn scope<'env, F>(&self, f: F)
where
F: for<'scope> FnOnce(&'scope Self::Scope<'scope, 'env>);
}
struct Thread {}
impl HasScoped for Thread {
type Scope<'scope, 'env: 'scope> = std::thread::Scope<'scope, 'env>;
fn scope<'env, F>(&self, f: F)
where
// lifetime bound not satisfied:
F: for<'scope> FnOnce(&'scope Self::Scope<'scope, 'env>),
// But this compiles:
// F: for<'scope> FnOnce(&'scope std::thread::Scope<'scope, 'env>),
{
std::thread::scope(f);
}
}
trait Scope<'scope, 'env: 'scope> {
fn spawn<T: FnOnce() + Send + 'scope>(&'scope self, obj: T);
}
impl<'scope, 'env: 'scope> Scope<'scope, 'env> for std::thread::Scope<'scope, 'env> {
fn spawn<T: FnOnce() + Send + 'scope>(&'scope self, obj: T) {
self.spawn(obj);
}
}
I expected to see this happen: Compiles without errors. The HasScoped::scope impl signature is an exact copied of its definition in the trait.
Instead, this happened:
error[E0478]: lifetime bound not satisfied
--> src/lib.rs:17:12
|
17 | F: for<'scope> FnOnce(&'scope Self::Scope<'scope, 'env>),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime `'env` as defined here
--> src/lib.rs:14:14
|
14 | fn scope<'env, F>(&self, f: F)
| ^^^^
error[E0478]: lifetime bound not satisfied
--> src/lib.rs:17:24
|
17 | F: for<'scope> FnOnce(&'scope Self::Scope<'scope, 'env>),
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
note: lifetime parameter instantiated with the lifetime `'env` as defined here
--> src/lib.rs:14:14
|
14 | fn scope<'env, F>(&self, f: F)
| ^^^^
For more information about this error, try `rustc --explain E0478`.
If I write std::thread::Scope directly instead of Self::Scope in the impl block, then it compiles. But as defined, Self::Scope<..> should be the same type as std::thread::Scope<..> thus should result in the same behavior.
Meta
Reproduced on stable rustc (1.93.1) and nightly (1.95.0-nightly 2026-02-23 b3869b94cd1ed4bfa2eb) on playground.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the linked Rust Playground reproducer and compare the trait definition and impl using Self::Scope with the version using std:🧵:Scope. Investigate the E0478 diagnostics on stable 1.93.1 and nightly 1.95.0, then confirm whether the impl accepts the same signature and lifetime bounds without errors.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100