Skip to content
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

default_free_fn feature does not have the same diagnostics as using Default::default in recursing situations #102825

Open
harudagondi opened this issue Oct 9, 2022 · 1 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.

Comments

@harudagondi
Copy link
Contributor

Given the following code: Rust playground link

#![feature(default_free_fn)]

use std::default::default;

struct Foo {
    a: i32,
    b: String,
}

impl Default for Foo {
    fn default() -> Self {
        Self { a: 1, ..default() }
    }
}

fn main() {
    let foo = Foo::default();
}

The current output is:

   Compiling playground v0.0.1 (/playground)
warning: unused variable: `foo`
  --> src/main.rs:17:9
   |
17 |     let foo = Foo::default();
   |         ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: fields `a` and `b` are never read
 --> src/main.rs:6:5
  |
5 | struct Foo {
  |        --- fields in this struct
6 |     a: i32,
  |     ^
7 |     b: String,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `playground` (bin "playground") generated 2 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.86s
     Running `target/debug/playground`

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
timeout: the monitored command dumped core
/playground/tools/entrypoint.sh: line 11:     9 Aborted                 timeout --signal=KILL ${timeout} "$@"

Ideally the output should look like: (Using Default::default())

   Compiling playground v0.0.1 (/playground)
warning: unused variable: `foo`
  --> src/main.rs:16:9
   |
16 |     let foo = Foo::default();
   |         ^^^ help: if this is intentional, prefix it with an underscore: `_foo`
   |
   = note: `#[warn(unused_variables)]` on by default

warning: function cannot return without recursing
  --> src/main.rs:7:5
   |
7  |     fn default() -> Self {
   |     ^^^^^^^^^^^^^^^^^^^^ cannot return without recursing
...
10 |             ..Default::default()
   |               ------------------ recursive call site
   |
   = note: `#[warn(unconditional_recursion)]` on by default
   = help: a `loop` may express intention better if this is on purpose

warning: fields `a` and `b` are never read
 --> src/main.rs:2:5
  |
1 | struct Foo {
  |        --- fields in this struct
2 |     a: i32,
  |     ^
3 |     b: String,
  |     ^
  |
  = note: `#[warn(dead_code)]` on by default

warning: `playground` (bin "playground") generated 3 warnings
    Finished dev [unoptimized + debuginfo] target(s) in 0.86s
     Running `target/debug/playground`

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
timeout: the monitored command dumped core
/playground/tools/entrypoint.sh: line 11:     8 Aborted                 timeout --signal=KILL ${timeout} "$@"

Using default() should also warn the user about unconditional_recursion.

@harudagondi harudagondi 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 9, 2022
@lukas-code
Copy link
Member

Another instance of #57965.

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