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

Potential overflow in the calculation of complex trait bounds involving tuples #126401

Open
ThinkRedstone opened this issue Jun 13, 2024 · 1 comment
Labels
A-traits Area: Trait system C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@ThinkRedstone
Copy link

ThinkRedstone commented Jun 13, 2024

When using a trait which is constrained by some bounds, and is implanted automatically for types satisfying those bounds (seen often as the equivalent of type alias in traits), the compiler can run into overflow evaluating bound requirement, which only happen with the helper trait (and not the full bound),

I tried this code:

use std::marker::PhantomData;

trait Hello<'a> {
    type Value;
}

struct Wrapper<T>(PhantomData<T>);

impl<'a, T1, T2> Hello<'a> for Wrapper<(T1, T2)>
where
    Wrapper<T1>: Hello<'a, Value = T1>,
    Wrapper<T2>: Hello<'a, Value = T2>,
{
    type Value = (T1, T2);
}

impl<'a> Hello<'a> for Wrapper<u32> {
    type Value = u32;
}

/// The helper trait
trait Helloable: Sized
where
    Wrapper<Self>: for<'a> Hello<'a, Value = Self>,
{
}

impl<T> Helloable for T where Wrapper<T>: for<'a> Hello<'a, Value = Self> {}

// compiles
fn test1<T>()
where
    Wrapper<T>: for<'a> Hello<'a, Value = T>,
{
}

// overflow error
fn test2<T>()
where
    T: Helloable,
{
}

playground link

I expected to see this happen: This code should compile, with both test1 and test2 (or at the very least not compile with either one).

Instead, this happened: This code compiles with test1, but adding test2 causes an overflow in the tuple implementation and prevents the code from compiling (even though the two functions are practically with the same bounds).

Meta

This happens on all channels (as can be seen in the playground), but here is the local nightly compiler I tried debugging this with:

rustc --version --verbose:

rustc 1.80.0-nightly (032af18af 2024-06-02)                                                                                                                                                                                                                   
binary: rustc                                                                                                                                                                                                                                                 
commit-hash: 032af18af578f4283a2927fb43b90df2bbb72b67                                                                                                                                                                                                         
commit-date: 2024-06-02                                                                                                                                                                                                                                       
host: x86_64-unknown-linux-gnu                                                                                                                                                                                                                                
release: 1.80.0-nightly                                                                                                                                                                                                                                       
LLVM version: 18.1.6  

For the curious, this structure of code arose from trying to work with serde's DeserializeSeed, which in this example I replaced with the trait Hello.

@ThinkRedstone ThinkRedstone added the C-bug Category: This is a bug. label Jun 13, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jun 13, 2024
@veera-sivarajan
Copy link
Contributor

@rustbot label -needs-triage +A-traits +T-types +D-confusing

@rustbot rustbot added A-traits Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. T-types Relevant to the types team, which will review and decide on the PR/issue. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-traits Area: Trait system C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. 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

3 participants