rust-lang / rust-lang/rust

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

Open
#148,742 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug L-unconditional_recursion T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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.

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

Reproduce the Rust example and compare its behavior with the existing unconditional_recursion compiler check. Determine how From and .into() calls are analyzed, then define tests showing the warning is emitted for unconditional recursion without incorrectly flagging valid conversions; done means the regression is covered and the diagnostic behavior is agreed.

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
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.