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

Associated consts in traits interact weirdly with type inference #100282

Open
GoldsteinE opened this issue Aug 8, 2022 · 3 comments
Open

Associated consts in traits interact weirdly with type inference #100282

GoldsteinE opened this issue Aug 8, 2022 · 3 comments
Labels
A-associated-items Area: Associated items such as associated types and consts. A-inference Area: Type inference A-traits Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@GoldsteinE
Copy link
Contributor

GoldsteinE commented Aug 8, 2022

I tried this code (playground):

use std::marker::PhantomData;

struct IsSend<T>(PhantomData<T>);
struct TaggedBool<T>(bool, PhantomData<T>);

trait Helper<T> {
    const VALUE: TaggedBool<T>;
}

impl<T, U> Helper<T> for U {
    const VALUE: TaggedBool<T> = TaggedBool(false, PhantomData);
}

impl<T: Send> IsSend<T> {
    const VALUE: TaggedBool<T> = TaggedBool(true, PhantomData);
}

impl<T> IsSend<T> {
    const fn new(_: &T) -> Self {
        Self(PhantomData)
    }
}

const IS_SEND_PTR: bool = {
    // Some value we want to check
    let to_check: *const () = &();
    let mut _checker = IsSend::new(&to_check);
    let tb = IsSend::VALUE;
    // this works:
    // let tb = IsSend::<*const ()>::VALUE;
    _checker.0 = tb.1;
    tb.0
};

fn main() {
    assert!(!IS_SEND_PTR);
}

I expected this to compile and pass the assertion.
Instead, this happened: compiler successfuly inferred type for tb, but for some reason didn’t use the associated constant from trait:

error[[E0277]](https://doc.rust-lang.org/stable/error-index.html#E0277): `*const ()` cannot be sent between threads safely
  --> src/main.rs:28:14
   |
28 |     let tb = IsSend::VALUE;
   |              ^^^^^^^^^^^^^ `*const ()` cannot be sent between threads safely
   |
   = help: the trait `Send` is not implemented for `*const ()`
note: required by a bound in `IsSend::<T>::VALUE`
  --> src/main.rs:14:9
   |
14 | impl<T: Send> IsSend<T> {
   |         ^^^^ required by this bound in `IsSend::<T>::VALUE`
15 |     const VALUE: TaggedBool<T> = TaggedBool(true, PhantomData);
   |           ----- required by a bound in this

Meta

Replicates on stable, beta and nightly on playground (v1.62.1, v1.63.0-beta9 and 1.65.0-nightly (2022-08-07 d394408) respectively).

@GoldsteinE
Copy link
Contributor Author

GoldsteinE commented Aug 8, 2022

Actually, I think it’s any associated items. It looks for me like the bug is somewhere in resolve_ty_and_res_fully_qualified_call, but I’m hugely underqualified for further debug.

I also failed to find any workaround. Even if I supply the correct answer, inference uses inherent impl and insists on incorrect answer:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=c5fae9258adb2687d94f0c817cd70a5e

@GoldsteinE
Copy link
Contributor Author

In the unlikely case someone faces the same problem, I found a workaround:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=2a5e77fa0621dc77a77da278fc23d9d7

@ChrisDenton ChrisDenton added the needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. label Jul 16, 2023
@AstralSorcerer
Copy link
Contributor

triage: still an issue on stable and nightly. I also wonder if it may be related to #102939.

@fmease fmease added A-traits Area: Trait system A-associated-items Area: Associated items such as associated types and consts. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-inference Area: Type inference T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage-legacy Old issue that were never triaged. Remove this label once the issue has been sufficiently triaged. labels Jan 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items such as associated types and consts. A-inference Area: Type inference A-traits Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants