- Notifications
You must be signed in to change notification settings - Fork 13.9k
Open
Labels
A-trait-systemArea: Trait systemArea: Trait systemC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.Fixed by the next-generation trait solver, `-Znext-solver`.
Description
I found a situation where the body of a function affects whether another function (which doesn't call or otherwise use it) compiles. Normally Rust can compile print_stuff below, unless the broken feature is enabled, in which case compiling print_stuff hits the recursion limit.
main.rs:
use std::fmt::Display; fn main() { print_stuff(&[2, 3, 5, 7, 11, 13, 17]); } pub fn print_stuff<'a, T>(vals: &'a T) where &'a T: IntoIterator<Item: Display>, { println!("Values:"); for val in vals { println!(" {val}"); } } #[allow(dead_code)] fn random_unrelated_code() { #[cfg(feature="broken")] rust_broken_local_reasoning::no_op(); }lib.rs:
pub fn no_op() {} // Note: The rest isn't pub... struct Wrapper<T>(T); impl<'a, T> IntoIterator for &'a Wrapper<T> where &'a T: IntoIterator, { type Item = <&'a T as IntoIterator>::Item; type IntoIter = <&'a T as IntoIterator>::IntoIter; fn into_iter(self) -> Self::IntoIter { (&self.0).into_iter() } }(for convenience I've made a tiny repo demonstrating this)
I expected to see the code compile even with the broken feature enabled.
Instead, it hits recursion limits.
This was surprising to me for a few reasons:
print_stuffdoesn't userandom_unrelated_codein any way, so I wouldn't expect the body ofrandom_unrelated_codeto impact whetherprint_stuffcompiles.- I wouldn't expect privately implementing
Wrapperto be a breaking change, yet it causes previously working consumer code to fail to compile. This is doubly surprising becauseprint_stuffandWrappereach compile in isolation; it's not until used together that you discover thatWrapperis problematic. - I'm also surprised that
Wrapperhits recursion limits because itsIntoIteratorimpl just hands things off to the wrapped implementation, and Rust has no difficulty compiling a nearly-identicalIntoIteratorimplementation with the references removed.
Meta
This is reproducible on stable, nightly and beta.
rustc --version --verbose:
rustc 1.89.0 (29483883e 2025-08-04) binary: rustc commit-hash: 29483883eed69d5fb4db01964cdf2af4d86e9cb2 commit-date: 2025-08-04 host: x86_64-unknown-linux-gnu release: 1.89.0 LLVM version: 20.1.7 Metadata
Metadata
Assignees
Labels
A-trait-systemArea: Trait systemArea: Trait systemC-bugCategory: This is a bug.Category: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.fixed-by-next-solverFixed by the next-generation trait solver, `-Znext-solver`.Fixed by the next-generation trait solver, `-Znext-solver`.