Skip to content

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

+1
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

+5-4
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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

+2
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)

compiler/rustc_builtin_macros/src/deriving/default.rs

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ pub fn expand_deriving_default(
1616
mitem: &ast::MetaItem,
1717
item: &Annotatable,
1818
push: &mut dyn FnMut(Annotatable),
19+
is_const: bool,
1920
) {
2021
item.visit_with(&mut DetectNonVariantDefaultAttr { cx });
2122

@@ -46,6 +47,7 @@ pub fn expand_deriving_default(
4647
})),
4748
}],
4849
associated_types: Vec::new(),
50+
is_const,
4951
};
5052
trait_def.expand(cx, mitem, item, push)
5153
}

compiler/rustc_builtin_macros/src/deriving/encodable.rs

+2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ pub fn expand_deriving_rustc_encodable(
100100
mitem: &MetaItem,
101101
item: &Annotatable,
102102
push: &mut dyn FnMut(Annotatable),
103+
is_const: bool,
103104
) {
104105
let krate = sym::rustc_serialize;
105106
let typaram = sym::__S;
@@ -138,6 +139,7 @@ pub fn expand_deriving_rustc_encodable(
138139
})),
139140
}],
140141
associated_types: Vec::new(),
142+
is_const,
141143
};
142144

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

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ use rustc_ast::{GenericArg, GenericParamKind, VariantData};
171171
use rustc_attr as attr;
172172
use rustc_expand::base::{Annotatable, ExtCtxt};
173173
use rustc_span::symbol::{kw, sym, Ident, Symbol};
174-
use rustc_span::Span;
174+
use rustc_span::{Span, DUMMY_SP};
175175
use std::cell::RefCell;
176176
use std::iter;
177177
use std::vec;
@@ -200,6 +200,8 @@ pub struct TraitDef<'a> {
200200
pub methods: Vec<MethodDef<'a>>,
201201

202202
pub associated_types: Vec<(Ident, Ty)>,
203+
204+
pub is_const: bool,
203205
}
204206

205207
pub struct MethodDef<'a> {
@@ -726,7 +728,7 @@ impl<'a> TraitDef<'a> {
726728
unsafety: ast::Unsafe::No,
727729
polarity: ast::ImplPolarity::Positive,
728730
defaultness: ast::Defaultness::Final,
729-
constness: ast::Const::No,
731+
constness: if self.is_const { ast::Const::Yes(DUMMY_SP) } else { ast::Const::No },
730732
generics: trait_generics,
731733
of_trait: opt_trait_ref,
732734
self_ty: self_type,

compiler/rustc_builtin_macros/src/deriving/hash.rs

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub fn expand_deriving_hash(
1313
mitem: &MetaItem,
1414
item: &Annotatable,
1515
push: &mut dyn FnMut(Annotatable),
16+
is_const: bool,
1617
) {
1718
let path = Path::new_(pathvec_std!(hash::Hash), vec![], PathKind::Std);
1819

@@ -38,6 +39,7 @@ pub fn expand_deriving_hash(
3839
})),
3940
}],
4041
associated_types: Vec::new(),
42+
is_const,
4143
};
4244

4345
hash_trait_def.expand(cx, mitem, item, push);

compiler/rustc_builtin_macros/src/deriving/mod.rs

+22-13
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ pub mod partial_ord;
3838

3939
pub mod generic;
4040

41-
pub(crate) struct BuiltinDerive(
42-
pub(crate) fn(&mut ExtCtxt<'_>, Span, &MetaItem, &Annotatable, &mut dyn FnMut(Annotatable)),
43-
);
41+
pub(crate) type BuiltinDeriveFn =
42+
fn(&mut ExtCtxt<'_>, Span, &MetaItem, &Annotatable, &mut dyn FnMut(Annotatable), bool);
43+
44+
pub(crate) struct BuiltinDerive(pub(crate) BuiltinDeriveFn);
4445

4546
impl MultiItemModifier for BuiltinDerive {
4647
fn expand(
@@ -49,6 +50,7 @@ impl MultiItemModifier for BuiltinDerive {
4950
span: Span,
5051
meta_item: &MetaItem,
5152
item: Annotatable,
53+
is_derive_const: bool,
5254
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
5355
// FIXME: Built-in derives often forget to give spans contexts,
5456
// so we are doing it here in a centralized way.
@@ -57,21 +59,28 @@ impl MultiItemModifier for BuiltinDerive {
5759
match item {
5860
Annotatable::Stmt(stmt) => {
5961
if let ast::StmtKind::Item(item) = stmt.into_inner().kind {
60-
(self.0)(ecx, span, meta_item, &Annotatable::Item(item), &mut |a| {
61-
// Cannot use 'ecx.stmt_item' here, because we need to pass 'ecx'
62-
// to the function
63-
items.push(Annotatable::Stmt(P(ast::Stmt {
64-
id: ast::DUMMY_NODE_ID,
65-
kind: ast::StmtKind::Item(a.expect_item()),
66-
span,
67-
})));
68-
});
62+
(self.0)(
63+
ecx,
64+
span,
65+
meta_item,
66+
&Annotatable::Item(item),
67+
&mut |a| {
68+
// Cannot use 'ecx.stmt_item' here, because we need to pass 'ecx'
69+
// to the function
70+
items.push(Annotatable::Stmt(P(ast::Stmt {
71+
id: ast::DUMMY_NODE_ID,
72+
kind: ast::StmtKind::Item(a.expect_item()),
73+
span,
74+
})));
75+
},
76+
is_derive_const,
77+
);
6978
} else {
7079
unreachable!("should have already errored on non-item statement")
7180
}
7281
}
7382
_ => {
74-
(self.0)(ecx, span, meta_item, &item, &mut |a| items.push(a));
83+
(self.0)(ecx, span, meta_item, &item, &mut |a| items.push(a), is_derive_const);
7584
}
7685
}
7786
ExpandResult::Ready(items)

compiler/rustc_builtin_macros/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
9797
bench: test::expand_bench,
9898
cfg_accessible: cfg_accessible::Expander,
9999
cfg_eval: cfg_eval::expand,
100-
derive: derive::Expander,
100+
derive: derive::Expander(false),
101+
derive_const: derive::Expander(true),
101102
global_allocator: global_allocator::expand,
102103
test: test::expand_test,
103104
test_case: test::expand_test_case,

compiler/rustc_expand/src/base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ pub trait MultiItemModifier {
248248
span: Span,
249249
meta_item: &ast::MetaItem,
250250
item: Annotatable,
251+
is_derive_const: bool,
251252
) -> ExpandResult<Vec<Annotatable>, Annotatable>;
252253
}
253254

@@ -261,6 +262,7 @@ where
261262
span: Span,
262263
meta_item: &ast::MetaItem,
263264
item: Annotatable,
265+
_is_derive_const: bool,
264266
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
265267
ExpandResult::Ready(self(ecx, span, meta_item, item))
266268
}
@@ -871,7 +873,7 @@ impl SyntaxExtension {
871873
/// Error type that denotes indeterminacy.
872874
pub struct Indeterminate;
873875

874-
pub type DeriveResolutions = Vec<(ast::Path, Annotatable, Option<Lrc<SyntaxExtension>>)>;
876+
pub type DeriveResolutions = Vec<(ast::Path, Annotatable, Option<Lrc<SyntaxExtension>>, bool)>;
875877

876878
pub trait ResolverExpand {
877879
fn next_node_id(&mut self) -> NodeId;

0 commit comments

Comments
 (0)