rust-lang / rust-lang/rust

rustc hangs / infinite loop on small example with generic_const_exprs and specialization

Open
#126,609 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-const-generics C-bug F-generic_const_exprs I-hang P-low T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

This code hangs on nightly

#![feature(specialization)]
#![feature(generic_const_exprs)]

use std::mem;
use std::marker::PhantomData;

trait Repr1 {
  type Elem;
}

struct TypeWithSize<T, const TSize: usize>(PhantomData<T>);

impl<T> Repr1 for TypeWithSize<T, 1> {
  type Elem = u8;
}

impl<T: Sized> Repr1 for T
where
  TypeWithSize<T, { mem::size_of::<T>() }>: Repr1,
{
  type Elem = <TypeWithSize<T, { mem::size_of::<T>() }> as Repr1>::Elem;
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=8e9e36d48923272ac7366e521d379170

It works fine if there is a Repr2 to apparently break the cycle

#![feature(specialization)]
#![feature(generic_const_exprs)]
use std::mem;
use std::marker::PhantomData;
trait Repr1 {
  type Elem;
}

struct TypeWithSize<T, const TSize: usize>(PhantomData<T>);

impl<T> Repr1 for TypeWithSize<T, 1> {
  type Elem = u8;
}

trait Repr2 {
  type Elem;
}

impl<T: Sized> Repr2 for T
where
  TypeWithSize<T, { mem::size_of::<T>() }>: Repr1,
{
  type Elem = <TypeWithSize<T, { mem::size_of::<T>() }> as Repr1>::Elem;
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=753784ec4df8b6cca783f3d7bbd935b7

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 minimal reproducer from the first Rust Playground link on nightly and compare it with the second example, which does not hang. Trace the compiler behavior around specialization and generic_const_exprs; done means the first example no longer loops indefinitely while the related example continues to work.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.