Skip to content

Exponential type/trait-checking behavior from linearly nested iterator adapters. #54175

@eddyb

Description

@eddyb

The following test takes an exponential amount of time to type-check the body of huge (at the time of this writing, reported by -Z time-passes under "item-types checking"):

#![crate_type = "lib"]

pub fn unit() -> std::iter::Empty<()> {
    std::iter::empty()
}

macro_rules! nest {
    ($inner:expr) => (unit().flat_map(|_| {
        $inner
    }).flat_map(|_| unit()))
}

macro_rules! nests {
    () => (unit());
    ($_first:tt $($rest:tt)*) => (nest!(nests!($($rest)*)))
}

pub fn huge() -> impl Iterator<Item = ()> {
    nests! {
        // 1/x * 6/5 seconds.
        4096 2048 1024 512 256 128 64 32 16 8 4 2

        // x * 6/5 seconds.
        1 2 4 8 // 16 32 64
    }
}

This has been reduced from an ambiguity-aware parse tree visitor, and you can see a partial reduction here: https://gist.github.com/eddyb/5f20b8f48b68c92f7d4f022a18c374f4#file-repro-rs.

cc @nikomatsakis

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-enhancementCategory: An issue proposing an enhancement or a PR with one.I-compiletimeIssue: Problems and improvements with respect to compile times.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions