Skip to content

Commit cb86c38

Browse files
Fix #[derive(Default)] on a generic #[default] enum adding unnecessary Default bounds
1 parent 5b4bd15 commit cb86c38

File tree

12 files changed

+35
-1
lines changed

12 files changed

+35
-1
lines changed

compiler/rustc_builtin_macros/src/deriving/bounds.rs

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn expand_deriving_copy(
1616
let trait_def = TraitDef {
1717
span,
1818
path: path_std!(marker::Copy),
19+
skip_path_as_bound: false,
1920
additional_bounds: Vec::new(),
2021
generics: Bounds::empty(),
2122
supports_unions: true,

compiler/rustc_builtin_macros/src/deriving/clone.rs

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ pub fn expand_deriving_clone(
7272
let trait_def = TraitDef {
7373
span,
7474
path: path_std!(clone::Clone),
75+
skip_path_as_bound: false,
7576
additional_bounds: bounds,
7677
generics: Bounds::empty(),
7778
supports_unions: true,

compiler/rustc_builtin_macros/src/deriving/cmp/eq.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ pub fn expand_deriving_eq(
2525
let trait_def = TraitDef {
2626
span,
2727
path: path_std!(cmp::Eq),
28+
skip_path_as_bound: false,
2829
additional_bounds: Vec::new(),
2930
generics: Bounds::empty(),
3031
supports_unions: true,

compiler/rustc_builtin_macros/src/deriving/cmp/ord.rs

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub fn expand_deriving_ord(
1919
let trait_def = TraitDef {
2020
span,
2121
path: path_std!(cmp::Ord),
22+
skip_path_as_bound: false,
2223
additional_bounds: Vec::new(),
2324
generics: Bounds::empty(),
2425
supports_unions: false,

compiler/rustc_builtin_macros/src/deriving/cmp/partial_eq.rs

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ pub fn expand_deriving_partial_eq(
8383
let trait_def = TraitDef {
8484
span,
8585
path: path_std!(cmp::PartialEq),
86+
skip_path_as_bound: false,
8687
additional_bounds: Vec::new(),
8788
generics: Bounds::empty(),
8889
supports_unions: false,

compiler/rustc_builtin_macros/src/deriving/cmp/partial_ord.rs

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ pub fn expand_deriving_partial_ord(
3737
let trait_def = TraitDef {
3838
span,
3939
path: path_std!(cmp::PartialOrd),
40+
skip_path_as_bound: false,
4041
additional_bounds: vec![],
4142
generics: Bounds::empty(),
4243
supports_unions: false,

compiler/rustc_builtin_macros/src/deriving/debug.rs

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub fn expand_deriving_debug(
2020
let trait_def = TraitDef {
2121
span,
2222
path: path_std!(fmt::Debug),
23+
skip_path_as_bound: false,
2324
additional_bounds: Vec::new(),
2425
generics: Bounds::empty(),
2526
supports_unions: false,

compiler/rustc_builtin_macros/src/deriving/decodable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ pub fn expand_deriving_rustc_decodable(
2323
let trait_def = TraitDef {
2424
span,
2525
path: Path::new_(vec![krate, sym::Decodable], vec![], PathKind::Global),
26+
skip_path_as_bound: false,
2627
additional_bounds: Vec::new(),
2728
generics: Bounds::empty(),
2829
supports_unions: false,

compiler/rustc_builtin_macros/src/deriving/default.rs

+20
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub fn expand_deriving_default(
2424
let trait_def = TraitDef {
2525
span,
2626
path: Path::new(vec![kw::Default, sym::Default]),
27+
skip_path_as_bound: has_a_default_variant(item),
2728
additional_bounds: Vec::new(),
2829
generics: Bounds::empty(),
2930
supports_unions: false,
@@ -262,3 +263,22 @@ impl<'a, 'b> rustc_ast::visit::Visitor<'a> for DetectNonVariantDefaultAttr<'a, '
262263
}
263264
}
264265
}
266+
267+
fn has_a_default_variant(item: &Annotatable) -> bool {
268+
struct HasDefaultAttrOnVariant {
269+
found: bool,
270+
}
271+
272+
impl<'ast> rustc_ast::visit::Visitor<'ast> for HasDefaultAttrOnVariant {
273+
fn visit_variant(&mut self, v: &'ast rustc_ast::Variant) {
274+
if v.attrs.iter().any(|attr| attr.has_name(kw::Default)) {
275+
self.found = true;
276+
}
277+
// no need to subrecurse.
278+
}
279+
}
280+
281+
let mut visitor = HasDefaultAttrOnVariant { found: false };
282+
item.visit_with(&mut visitor);
283+
visitor.found
284+
}

compiler/rustc_builtin_macros/src/deriving/encodable.rs

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ pub fn expand_deriving_rustc_encodable(
107107
let trait_def = TraitDef {
108108
span,
109109
path: Path::new_(vec![krate, sym::Encodable], vec![], PathKind::Global),
110+
skip_path_as_bound: false,
110111
additional_bounds: Vec::new(),
111112
generics: Bounds::empty(),
112113
supports_unions: false,

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
172172
use rustc_span::Span;
173173
use std::cell::RefCell;
174174
use std::iter;
175+
use std::ops::Not;
175176
use std::vec;
176177
use thin_vec::thin_vec;
177178
use ty::{Bounds, Path, Ref, Self_, Ty};
@@ -185,6 +186,9 @@ pub struct TraitDef<'a> {
185186
/// Path of the trait, including any type parameters
186187
pub path: Path,
187188

189+
/// Whether to skip adding the current trait as a bound to the type parameters of the type.
190+
pub skip_path_as_bound: bool,
191+
188192
/// Additional bounds required of any type parameters of the type,
189193
/// other than the current trait
190194
pub additional_bounds: Vec<Ty>,
@@ -594,7 +598,7 @@ impl<'a> TraitDef<'a> {
594598
cx.trait_bound(p.to_path(cx, self.span, type_ident, generics))
595599
}).chain(
596600
// require the current trait
597-
iter::once(cx.trait_bound(trait_path.clone()))
601+
self.skip_path_as_bound.not().then(|| cx.trait_bound(trait_path.clone()))
598602
).chain(
599603
// also add in any bounds from the declaration
600604
param.bounds.iter().cloned()

compiler/rustc_builtin_macros/src/deriving/hash.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub fn expand_deriving_hash(
2222
let hash_trait_def = TraitDef {
2323
span,
2424
path,
25+
skip_path_as_bound: false,
2526
additional_bounds: Vec::new(),
2627
generics: Bounds::empty(),
2728
supports_unions: false,

0 commit comments

Comments
 (0)