Compiler cannot detect recursive Default::default() #87482
Labels
A-lints
Area: Lints (warnings about flaws in source code) such as unused_mut.
C-bug
Category: This is a bug.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
I tried this code:
#[derive(Debug)]
struct Foo {
flag: bool,
name: String,
id: i32,
}
impl Default for Foo {
fn default() -> Self {
Foo {
flag: true,
name: "Tom".to_string(),
..Default::default()
}
}
}
fn main() {
let foo = Foo::default();
println!("Foo - {:?}", foo);
}
I expected to see this happen: warning: function cannot return without recursing
--> src/main.rs:9:5
|
9 | fn default() -> Self {
| ^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
...
13 | ..Default::default()
| ------------------ recursive call site
|
Instead, this happened: no warnings
If I comment out the following line:
//name: "Tom".to_string(),
the warning is populated
Also you can check online
https://play.rust-lang.org/?version=stable&mode=release&edition=2018&gist=8e13af07d962a2956955fc3d9f4869b0
Meta
rustc 1.53.0 (53cb7b0 2021-06-17)
1.53.0binary: rustc
commit-hash: 53cb7b0
commit-date: 2021-06-17
host: x86_64-apple-darwin
release: 1.53.0
LLVM version: 12.0.1
Backtrace
thread 'main' has overflowed its stack
Nonefatal runtime error: stack overflow
Abort trap: 6
The text was updated successfully, but these errors were encountered: