Skip to content

[derivable_impls]: don't lint if default() call expr unsize-coerces to trait object #10954

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

Merged
merged 4 commits into from
Jun 14, 2023

Conversation

y21
Copy link
Member

@y21 y21 commented Jun 14, 2023

Fixes #10158.

This fixes a FP where the derive-generated Default impl would have different behavior because of unsize coercion from Box<T> to Box<dyn Trait>:

struct S {
  x: Box<dyn std::fmt::Debug>
}
impl Default for S {
  fn default() -> Self {
    Self {
      x: Box::<()>::default()
     // ^~ Box<()> coerces to Box<dyn Debug>
     // #[derive(Default)] would call Box::<dyn Debug>::default()
    }
  }
}

(this intentionally only looks for trait objects dyn specifically, and not any unsize coercion, e.g. &[i32; 5] to &[i32], because that breaks existing tests and isn't actually problematic, as far as I can tell)

changelog: [derivable_impls]: don't lint if default() call expression unsize-coerces to trait object

@rustbot
Copy link
Collaborator

rustbot commented Jun 14, 2023

r? @llogiq

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties label Jun 14, 2023
@llogiq
Copy link
Contributor

llogiq commented Jun 14, 2023

Thank you!

@bors r+

@bors
Copy link
Contributor

bors commented Jun 14, 2023

📌 Commit 4795c91 has been approved by llogiq

It is now in the queue for this repository.

@bors
Copy link
Contributor

bors commented Jun 14, 2023

⌛ Testing commit 4795c91 with merge ffe9525...

@bors
Copy link
Contributor

bors commented Jun 14, 2023

☀️ Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test
Approved by: llogiq
Pushing ffe9525 to master...

@bors bors merged commit ffe9525 into rust-lang:master Jun 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties
Projects
None yet
Development

Successfully merging this pull request may close these issues.

derivable_impl false positive when Default is implemented manually to structs with a dyn field
4 participants