rust-lang / rust-lang/rust

rustc hangs with gordian knot of trait bounds

Open
#133,354 6 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-trait-system C-bug fixed-by-next-solver I-compiletime T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Given the following code, rustc hangs:

mod asn1 {
    pub trait Asn1Writable: Sized {}
    pub trait SimpleAsn1Writable: Sized {}

    impl<T: SimpleAsn1Writable> Asn1Writable for T {}
    impl<T: SimpleAsn1Writable> SimpleAsn1Writable for &T {}
    impl<T: SimpleAsn1Writable> SimpleAsn1Writable for Box<T> {}

    impl<T: Asn1Writable> Asn1Writable for Option<T> {}

    pub trait Asn1DefinedByWritable: Sized {}
}

mod common {
    use crate::asn1;

    pub struct AlgorithmIdentifier<'a> {
        pub params: AlgorithmParameters<'a>,
    }

    impl<'a> asn1::SimpleAsn1Writable for AlgorithmIdentifier<'a> where
        AlgorithmParameters<'a>: asn1::Asn1DefinedByWritable
    {
    }

    pub enum AlgorithmParameters<'a> {
        Sha1,
        Pbkdf2(PBKDF2Params<'a>),
    }

    impl<'a> asn1::Asn1DefinedByWritable for AlgorithmParameters<'a>
    where
        PBES2Params<'a>: asn1::Asn1Writable,
        PBKDF2Params<'a>: asn1::Asn1Writable,
    {
    }

    pub const PSS_SHA1_HASH_ALG: AlgorithmIdentifier<'_> = AlgorithmIdentifier {
        params: AlgorithmParameters::Sha1,
    };

    pub struct RsaPssParameters<'a> {
        pub hash_algorithm: AlgorithmIdentifier<'a>,
    }

    impl<'a> asn1::SimpleAsn1Writable for RsaPssParameters<'a> {}

    pub struct PBES2Params<'a> {
        pub key_derivation_func: Box<AlgorithmIdentifier<'a>>,
    }

    impl<'a> asn1::SimpleAsn1Writable for PBES2Params<'a> where
        Box<AlgorithmIdentifier<'a>>: asn1::Asn1Writable
    {
    }

    pub struct PBKDF2Params<'a> {
        pub salt: &'a [u8],
    }

    impl<'a> asn1::SimpleAsn1Writable for PBKDF2Params<'a> where
        Box<AlgorithmIdentifier<'a>>: asn1::Asn1Writable
    {
    }
}

pub fn write_element<T: asn1::Asn1Writable>(val: &T) {
    todo!()
}

pub fn f(p: &common::RsaPssParameters<'_>) {
    write_element(&Some(&p.hash_algorithm));
}

fn main() {}
/t/q ❯❯❯ rustc src/main.rs
[no amount of patience is enough]

This is minimized from https://github.com/pyca/cryptography and https://github.com/alex/rust-asn1.

Meta

rustc --version --verbose:

rustc 1.82.0 (f6e511eec 2024-10-15)
binary: rustc
commit-hash: f6e511eec7342f59a25f7c0534f1dbea00d01b14
commit-date: 2024-10-15
host: aarch64-apple-darwin
release: 1.82.0
LLVM version: 19.1.1

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling the reproducer in src/main.rs with rustc 1.82.0 and confirm that trait-bound evaluation does not terminate. Trace the compiler's handling of the recursive trait bounds, and consider the issue fixed when this input terminates without hanging while preserving the expected compilation behavior.

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
Clearly specified
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.