Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compile time error, for structs with lifetime and trait functions that have a where clause #133387

Open
mematthias opened this issue Nov 23, 2024 · 2 comments
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.

Comments

@mematthias
Copy link

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
@mematthias mematthias added the C-bug Category: This is a bug. label Nov 23, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 23, 2024
@ChayimFriedman2
Copy link
Contributor

This happens because there are considered to be two implementations of TFoo, the real one and one from the where, and the compiler doesn't unify them. Where bounds are preferred to real impls - and the associated type in the bound is unknown.

@jieyouxu jieyouxu added C-discussion Category: Discussion or questions that doesn't represent real issues. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Nov 24, 2024
@mematthias
Copy link
Author

  1. Does this mean that the correct solution here is to simply remove the where clause in the impl block? That is not intuitive :(
  2. If there is no lifetime defined in the struct (like DSuccessBecauseNoLifetime), there are not two implementations of TFoo and therefore the compilation will not fail. Do I understand that correctly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-discussion Category: Discussion or questions that doesn't represent real issues.
Projects
None yet
Development

No branches or pull requests

4 participants