Skip to content

Commit

Permalink
Rollup merge of rust-lang#84243 - Soveu:fix-derive-macro-const-defaul…
Browse files Browse the repository at this point in the history
…t, r=petrochenkov

Builtin derive macros: fix error with const generics default

This fixes a bug where builtin derive macros (like Clone, Debug) would basically copy-paste the default from a const generic, causing a compile error with very confusing message - it would say defaults are not allowed in impl blocks, while pointing at struct/enum/union definition.
  • Loading branch information
Joshua Nelson committed Apr 16, 2021
2 parents a793e8b + 9ecfae4 commit 1d83b1b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
15 changes: 13 additions & 2 deletions compiler/rustc_builtin_macros/src/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ impl<'a> TraitDef<'a> {
self.generics.to_generics(cx, self.span, type_ident, generics);

// Create the generic parameters
params.extend(generics.params.iter().map(|param| match param.kind {
params.extend(generics.params.iter().map(|param| match &param.kind {
GenericParamKind::Lifetime { .. } => param.clone(),
GenericParamKind::Type { .. } => {
// I don't think this can be moved out of the loop, since
Expand All @@ -561,7 +561,18 @@ impl<'a> TraitDef<'a> {

cx.typaram(self.span, param.ident, vec![], bounds, None)
}
GenericParamKind::Const { .. } => param.clone(),
GenericParamKind::Const { ty, kw_span, .. } => {
let const_nodefault_kind = GenericParamKind::Const {
ty: ty.clone(),
kw_span: kw_span.clone(),

// We can't have default values inside impl block
default: None,
};
let mut param_clone = param.clone();
param_clone.kind = const_nodefault_kind;
param_clone
}
}));

// and similarly for where clauses
Expand Down
14 changes: 14 additions & 0 deletions src/test/ui/derives/derive-macro-const-default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// check-pass
#![allow(incomplete_features)]
#![feature(const_generics_defaults)]

#[derive(Clone, PartialEq, Debug)]
struct Example<T, const N: usize = 1usize>([T; N]);

fn main() {
let a = Example([(); 16]);
let b = a.clone();
if a != b {
let _c = format!("{:?}", a);
}
}

0 comments on commit 1d83b1b

Please sign in to comment.