Skip to content

impl From<T> for U triggers infinite recursion via .into() — not detected by unconditional_recursion #148742

@HackingRepo

Description

@HackingRepo

Safe Rust code using impl From for String recursively calls .into() on a fresh E {} instance, leading to a stack overflow at runtime. The compiler does not emit a warning or lint, even though the recursion is unconditional and deterministic. This bypasses the compiler recursion check.

Example:

struct E;
impl E {
    fn new() -> Self {
        E {

        }
    }
}
impl From<E> for String {
    fn from(value: E) -> Self {
        E {}.into()
    }
}

fn main() {    
    let v = String::from(E {});
}

When i run this i get

thread 'main' (6844) has overflowed its stack

I suggest to Emit a warning when From calls .into() on a fresh T without branching.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.L-unconditional_recursionLint: unconditional_recursionT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.needs-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions