Skip to content

Commit 4b0b898

Browse files
authored
Rollup merge of #102049 - fee1-dead-contrib:derive_const, r=oli-obk
Add the `#[derive_const]` attribute Closes #102371. This is a minimal patchset for the attribute to work. There are no restrictions on what traits this attribute applies to. r? `````@oli-obk`````
2 parents b0c6527 + a052f2c commit 4b0b898

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,
@@ -22,6 +23,7 @@ pub fn expand_deriving_copy(
2223
supports_unions: true,
2324
methods: Vec::new(),
2425
associated_types: Vec::new(),
26+
is_const,
2527
};
2628

2729
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.
@@ -87,6 +88,7 @@ pub fn expand_deriving_clone(
8788
combine_substructure: substructure,
8889
}],
8990
associated_types: Vec::new(),
91+
is_const,
9092
};
9193

9294
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);
@@ -42,6 +43,7 @@ pub fn expand_deriving_eq(
4243
})),
4344
}],
4445
associated_types: Vec::new(),
46+
is_const,
4547
};
4648

4749
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)];
@@ -34,6 +35,7 @@ pub fn expand_deriving_ord(
3435
combine_substructure: combine_substructure(Box::new(|a, b, c| cs_cmp(a, b, c))),
3536
}],
3637
associated_types: Vec::new(),
38+
is_const,
3739
};
3840

3941
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;
@@ -89,6 +90,7 @@ pub fn expand_deriving_partial_eq(
8990
supports_unions: false,
9091
methods,
9192
associated_types: Vec::new(),
93+
is_const,
9294
};
9395
trait_def.expand(cx, mitem, item, push)
9496
}

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 =
@@ -43,6 +44,7 @@ pub fn expand_deriving_partial_ord(
4344
supports_unions: false,
4445
methods: vec![partial_cmp_def],
4546
associated_types: Vec::new(),
47+
is_const,
4648
};
4749
trait_def.expand(cx, mitem, item, push)
4850
}

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);
@@ -37,6 +38,7 @@ pub fn expand_deriving_debug(
3738
})),
3839
}],
3940
associated_types: Vec::new(),
41+
is_const,
4042
};
4143
trait_def.expand(cx, mitem, item, push)
4244
}

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;
@@ -55,6 +56,7 @@ pub fn expand_deriving_rustc_decodable(
5556
})),
5657
}],
5758
associated_types: Vec::new(),
59+
is_const,
5860
};
5961

6062
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

@@ -47,6 +48,7 @@ pub fn expand_deriving_default(
4748
})),
4849
}],
4950
associated_types: Vec::new(),
51+
is_const,
5052
};
5153
trait_def.expand(cx, mitem, item, push)
5254
}

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;
@@ -139,6 +140,7 @@ pub fn expand_deriving_rustc_encodable(
139140
})),
140141
}],
141142
associated_types: Vec::new(),
143+
is_const,
142144
};
143145

144146
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::ops::Not;
@@ -204,6 +204,8 @@ pub struct TraitDef<'a> {
204204
pub methods: Vec<MethodDef<'a>>,
205205

206206
pub associated_types: Vec<(Ident, Ty)>,
207+
208+
pub is_const: bool,
207209
}
208210

209211
pub struct MethodDef<'a> {
@@ -730,7 +732,7 @@ impl<'a> TraitDef<'a> {
730732
unsafety: ast::Unsafe::No,
731733
polarity: ast::ImplPolarity::Positive,
732734
defaultness: ast::Defaultness::Final,
733-
constness: ast::Const::No,
735+
constness: if self.is_const { ast::Const::Yes(DUMMY_SP) } else { ast::Const::No },
734736
generics: trait_generics,
735737
of_trait: opt_trait_ref,
736738
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

@@ -39,6 +40,7 @@ pub fn expand_deriving_hash(
3940
})),
4041
}],
4142
associated_types: Vec::new(),
43+
is_const,
4244
};
4345

4446
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
@@ -99,7 +99,8 @@ pub fn register_builtin_macros(resolver: &mut dyn ResolverExpand) {
9999
bench: test::expand_bench,
100100
cfg_accessible: cfg_accessible::Expander,
101101
cfg_eval: cfg_eval::expand,
102-
derive: derive::Expander,
102+
derive: derive::Expander(false),
103+
derive_const: derive::Expander(true),
103104
global_allocator: global_allocator::expand,
104105
test: test::expand_test,
105106
test_case: test::expand_test_case,

compiler/rustc_expand/src/base.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ pub trait MultiItemModifier {
250250
span: Span,
251251
meta_item: &ast::MetaItem,
252252
item: Annotatable,
253+
is_derive_const: bool,
253254
) -> ExpandResult<Vec<Annotatable>, Annotatable>;
254255
}
255256

@@ -263,6 +264,7 @@ where
263264
span: Span,
264265
meta_item: &ast::MetaItem,
265266
item: Annotatable,
267+
_is_derive_const: bool,
266268
) -> ExpandResult<Vec<Annotatable>, Annotatable> {
267269
ExpandResult::Ready(self(ecx, span, meta_item, item))
268270
}
@@ -873,7 +875,7 @@ impl SyntaxExtension {
873875
/// Error type that denotes indeterminacy.
874876
pub struct Indeterminate;
875877

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

878880
pub trait ResolverExpand {
879881
fn next_node_id(&mut self) -> NodeId;

0 commit comments

Comments
 (0)