Compile time error, for structs with lifetime and trait functions that have a where clause
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
If two traits are dependent on each other and the implementing struct has lifetimes, the compiler cannot build the source code. The two traits work for all structs that do not have a lifetime.
However, you can avoid this error by commenting out the where clause from TPrivateFoo::execute().
I tried this code:
struct DData {
counter: u32,
}
pub trait TFoo: TPrivateFoo {
type NativeStruct;
}
pub trait TPrivateFoo {
fn execute(&self, root: &<Self as TFoo>::NativeStruct)
where
Self: TFoo;
}
struct DSuccessBecauseNoLifetime {
title: String,
}
impl TFoo for DSuccessBecauseNoLifetime {
type NativeStruct = DData;
}
impl TPrivateFoo for DSuccessBecauseNoLifetime {
fn execute(&self, root: &<Self as TFoo>::NativeStruct)
where
Self: TFoo,
{
println!("{}", root.counter);
}
}
struct DSuccessBecauseCommentedOutWhereClause<'a> {
title: &'a str,
}
impl TFoo for DSuccessBecauseCommentedOutWhereClause<'_> {
type NativeStruct = DData;
}
impl TPrivateFoo for DSuccessBecauseCommentedOutWhereClause<'_> {
fn execute(&self, root: &<Self as TFoo>::NativeStruct)
//where
// Self: TFoo,
{
println!("{}", root.counter);
}
}
struct DErrorBecauseOfLifetime<'a> {
title: &'a str,
}
impl TFoo for DErrorBecauseOfLifetime<'_> {
type NativeStruct = DData;
}
impl TPrivateFoo for DErrorBecauseOfLifetime<'_> {
fn execute(&self, root: &<Self as TFoo>::NativeStruct)
// ####### Compile error #######
where
Self: TFoo,
// #############################
{
println!("{}", root.counter);
}
}
I expected to see this happen:
Source code is successfully compiled
Instead, a compile time error occured:
error[E0609]: no field `counter` on type `&<DErrorBecauseOfLifetime<'_> as TFoo>::NativeStruct`
--> src\test.rs:57:25
|
57 | println!("{:?}", root.counter);
| ^^^^^^^ unknown field
Meta
Bug exists in both stable and nighlty build.
rustc --version --verbose:
rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: x86_64-pc-windows-msvc
release: 1.82.0
LLVM version: 19.1.1
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 by reducing and compiling the provided Rust example, comparing the lifetime-bearing struct with the versions that compile, using the reported rustc 1.82.0 environment. Trace the compiler's handling of the associated type and the where Self: TFoo clause in the failing method. Done means the lifetime-bearing example compiles and the regression is covered by an appropriate compiler test.
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
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100