Function bodies affect compilation of other functions
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the minimal reproduction in main.rs and lib.rs, using the linked repository and the reported rustc version to compare builds with and without the broken feature. Trace why the unrelated function body and private IntoIterator implementation interact during compilation. Done means identifying the compiler behavior and defining a regression test or correction for the recursion-limit failure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100