Skip to content

Detect heterogeneous generic recursion usage pre-monomorphization #90289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
scottmcm opened this issue Oct 26, 2021 · 2 comments
Open

Detect heterogeneous generic recursion usage pre-monomorphization #90289

scottmcm opened this issue Oct 26, 2021 · 2 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@scottmcm
Copy link
Member

scottmcm commented Oct 26, 2021

(Request inspired by #90063 (comment), after I wrote compiles-but-never-works code in https://rust-lang.github.io/rfcs/3058-try-trait-v2.html#the-opscontrolflow-type )

Given the following (minimal example) code: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=584d7b447db0f7890d2f94c3b945640e

pub fn foo<T>(x: T) {
    if rand() {
        foo(&x);
    }
}

The current output is: no errors nor warnings

However, if it's ever called, it'll give an error like

error: reached the recursion limit while instantiating `foo::<&&&&&&&&&&&&&&&&&&&&&&&&&&...&&&&&&&&&&&&&&&&&&&&&&&&&&&&i32>`

It would be nice for the compiler to detect, pre-monomorphization, that this will never actually work because any possible monomorphization will end up requiring instantiating another additional monomorphization.

(Not catching all cases would be fine -- I suspect indirect recursion would be particularly annoying to detect well. Maybe a deny-by-default lint for this? It'd technically be a breaking change to make it a hard error, and I don't think it really needs to be hard error. The lint is enough.)

@scottmcm scottmcm added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 26, 2021
@yanok
Copy link
Contributor

yanok commented Oct 26, 2021

@rustbot claim

@yanok
Copy link
Contributor

yanok commented Oct 26, 2021

Thanks for filing the bug! I think I have an idea how to handle this. Just need to get more familiar with rustc internal representations to evaluate if it's feasible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

2 participants