Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit a052f2c

Browse files
committed
Add the #[derive_const] attribute
1 parent 4af79cc commit a052f2c

File tree

30 files changed

+163
-30
lines changed

30 files changed

+163
-30
lines changed

compiler/rustc_builtin_macros/src/cfg_accessible.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ impl MultiItemModifier for Expander {
3434
span: Span,
3535
meta_item: &ast::MetaItem,
3636
item: Annotatable,
37+
_is_derive_const: bool,
3738
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
3839
let template = AttributeTemplate { list: Some("path"), ..Default::default() };
3940
let attr = &ecx.attribute(meta_item.clone());

compiler/rustc_builtin_macros/src/derive.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_session::Session;
1010
use rustc_span::symbol::{sym, Ident};
1111
use rustc_span::Span;
1212

13-
pub(crate) struct Expander;
13+
pub(crate) struct Expander(pub bool);
1414

1515
impl MultiItemModifier for Expander {
1616
fn expand(
@@ -19,6 +19,7 @@ impl MultiItemModifier for Expander {
1919
span: Span,
2020
meta_item: &ast::MetaItem,
2121
item: Annotatable,
22+
_: bool,
2223
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
2324
let sess = ecx.sess;
2425
if report_bad_target(sess, &item, span) {
@@ -58,20 +59,20 @@ impl MultiItemModifier for Expander {
5859
report_path_args(sess, &meta);
5960
meta.path
6061
})
61-
.map(|path| (path, dummy_annotatable(), None))
62+
.map(|path| (path, dummy_annotatable(), None, self.0))
6263
.collect();
6364

6465
// Do not configure or clone items unless necessary.
6566
match &mut resolutions[..] {
6667
[] => {}
67-
[(_, first_item, _), others @ ..] => {
68+
[(_, first_item, ..), others @ ..] => {
6869
*first_item = cfg_eval(
6970
sess,
7071
features,
7172
item.clone(),
7273
ecx.current_expansion.lint_node_id,
7374
);
74-
for (_, item, _) in others {
75+
for (_, item, _, _) in others {
7576
*item = first_item.clone();
7677
}
7778
}

compiler/rustc_builtin_macros/src/deriving/bounds.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub fn expand_deriving_copy(
1212
mitem: &MetaItem,
1313
item: &Annotatable,
1414
push: &mut dyn FnMut(Annotatable),
15+
is_const: bool,
1516
) {
1617
let trait_def = TraitDef {
1718
span,
@@ -21,6 +22,7 @@ pub fn expand_deriving_copy(
2122
supports_unions: true,
2223
methods: Vec::new(),
2324
associated_types: Vec::new(),
25+
is_const,
2426
};
2527

2628
trait_def.expand(cx, mitem, item, push);

compiler/rustc_builtin_macros/src/deriving/clone.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub fn expand_deriving_clone(
1414
mitem: &MetaItem,
1515
item: &Annotatable,
1616
push: &mut dyn FnMut(Annotatable),
17+
is_const: bool,
1718
) {
1819
// The simple form is `fn clone(&self) -> Self { *self }`, possibly with
1920
// some additional `AssertParamIsClone` assertions.
@@ -86,6 +87,7 @@ pub fn expand_deriving_clone(
8687
combine_substructure: substructure,
8788
}],
8889
associated_types: Vec::new(),
90+
is_const,
8991
};
9092

9193
trait_def.expand_ext(cx, mitem, item, push, is_simple)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub fn expand_deriving_eq(
1515
mitem: &MetaItem,
1616
item: &Annotatable,
1717
push: &mut dyn FnMut(Annotatable),
18+
is_const: bool,
1819
) {
1920
let span = cx.with_def_site_ctxt(span);
2021
let inline = cx.meta_word(span, sym::inline);
@@ -41,6 +42,7 @@ pub fn expand_deriving_eq(
4142
})),
4243
}],
4344
associated_types: Vec::new(),
45+
is_const,
4446
};
4547

4648
super::inject_impl_of_structural_trait(cx, span, item, path_std!(marker::StructuralEq), push);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn expand_deriving_ord(
1313
mitem: &MetaItem,
1414
item: &Annotatable,
1515
push: &mut dyn FnMut(Annotatable),
16+
is_const: bool,
1617
) {
1718
let inline = cx.meta_word(span, sym::inline);
1819
let attrs = thin_vec![cx.attribute(inline)];
@@ -33,6 +34,7 @@ pub fn expand_deriving_ord(
3334
combine_substructure: combine_substructure(Box::new(|a, b, c| cs_cmp(a, b, c))),
3435
}],
3536
associated_types: Vec::new(),
37+
is_const,
3638
};
3739

3840
trait_def.expand(cx, mitem, item, push)

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pub fn expand_deriving_partial_eq(
1414
mitem: &MetaItem,
1515
item: &Annotatable,
1616
push: &mut dyn FnMut(Annotatable),
17+
is_const: bool,
1718
) {
1819
fn cs_eq(cx: &mut ExtCtxt<'_>, span: Span, substr: &Substructure<'_>) -> BlockOrExpr {
1920
let base = true;
@@ -88,6 +89,7 @@ pub fn expand_deriving_partial_eq(
8889
supports_unions: false,
8990
methods,
9091
associated_types: Vec::new(),
92+
is_const,
9193
};
9294
trait_def.expand(cx, mitem, item, push)
9395
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn expand_deriving_partial_ord(
1313
mitem: &MetaItem,
1414
item: &Annotatable,
1515
push: &mut dyn FnMut(Annotatable),
16+
is_const: bool,
1617
) {
1718
let ordering_ty = Path(path_std!(cmp::Ordering));
1819
let ret_ty =
@@ -42,6 +43,7 @@ pub fn expand_deriving_partial_ord(
4243
supports_unions: false,
4344
methods: vec![partial_cmp_def],
4445
associated_types: Vec::new(),
46+
is_const,
4547
};
4648
trait_def.expand(cx, mitem, item, push)
4749
}

compiler/rustc_builtin_macros/src/deriving/debug.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn expand_deriving_debug(
1313
mitem: &MetaItem,
1414
item: &Annotatable,
1515
push: &mut dyn FnMut(Annotatable),
16+
is_const: bool,
1617
) {
1718
// &mut ::std::fmt::Formatter
1819
let fmtr = Ref(Box::new(Path(path_std!(fmt::Formatter))), ast::Mutability::Mut);
@@ -36,6 +37,7 @@ pub fn expand_deriving_debug(
3637
})),
3738
}],
3839
associated_types: Vec::new(),
40+
is_const,
3941
};
4042
trait_def.expand(cx, mitem, item, push)
4143
}

compiler/rustc_builtin_macros/src/deriving/decodable.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn expand_deriving_rustc_decodable(
1616
mitem: &MetaItem,
1717
item: &Annotatable,
1818
push: &mut dyn FnMut(Annotatable),
19+
is_const: bool,
1920
) {
2021
let krate = sym::rustc_serialize;
2122
let typaram = sym::__D;
@@ -54,6 +55,7 @@ pub fn expand_deriving_rustc_decodable(
5455
})),
5556
}],
5657
associated_types: Vec::new(),
58+
is_const,
5759
};
5860

5961
trait_def.expand(cx, mitem, item, push)

0 commit comments

Comments
 (0)