mirror of
https://github.com/rust-lang/rust.git
synced 2026-05-15 20:45:45 +03:00
Auto merge of #10954 - y21:issue10158, r=llogiq
[`derivable_impls`]: don't lint if `default()` call expr unsize-coerces to trait object 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>`: ```rs 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
This commit is contained in:
@@ -268,4 +268,25 @@ impl Default for OtherGenericType {
|
||||
}
|
||||
}
|
||||
|
||||
mod issue10158 {
|
||||
pub trait T {}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct S {}
|
||||
impl T for S {}
|
||||
|
||||
pub struct Outer {
|
||||
pub inner: Box<dyn T>,
|
||||
}
|
||||
|
||||
impl Default for Outer {
|
||||
fn default() -> Self {
|
||||
Outer {
|
||||
// Box::<S>::default() adjusts to Box<dyn T>
|
||||
inner: Box::<S>::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
||||
@@ -304,4 +304,25 @@ fn default() -> Self {
|
||||
}
|
||||
}
|
||||
|
||||
mod issue10158 {
|
||||
pub trait T {}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct S {}
|
||||
impl T for S {}
|
||||
|
||||
pub struct Outer {
|
||||
pub inner: Box<dyn T>,
|
||||
}
|
||||
|
||||
impl Default for Outer {
|
||||
fn default() -> Self {
|
||||
Outer {
|
||||
// Box::<S>::default() adjusts to Box<dyn T>
|
||||
inner: Box::<S>::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
||||
Reference in New Issue
Block a user