rust-lang / rust-lang/rust

Function bodies affect compilation of other functions

Open
#146,680 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-trait-system C-bug fixed-by-next-solver T-compiler
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_stuff doesn't use random_unrelated_code in any way, so I wouldn't expect the body of random_unrelated_code to impact whether print_stuff compiles.
  • I wouldn't expect privately implementing Wrapper to be a breaking change, yet it causes previously working consumer code to fail to compile. This is doubly surprising because print_stuff and Wrapper each compile in isolation; it's not until used together that you discover that Wrapper is problematic.
  • I'm also surprised that Wrapper hits recursion limits because its IntoIterator impl just hands things off to the wrapped implementation, and Rust has no difficulty compiling a nearly-identical IntoIterator implementation 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

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 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.