Skip to content

Commit a68172c

Browse files
committed
rustc: remove the explicit count from the lang_item macro.
We can use a secondary macro to calculate the count from the information we're already having to pass to the lang items macro.
1 parent d0f6ef0 commit a68172c

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

Diff for: src/librustc/middle/lang_items.rs

+17-5
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,27 @@ use std::hashmap::HashMap;
3333
use std::iter::Enumerate;
3434
use std::vec;
3535

36+
37+
// Get the last "argument" (has to be done recursively to avoid phoney local ambiguity error)
38+
macro_rules! last {
39+
( $first:expr, $( $remainder:expr, )+ ) => ( last!( $( $remainder, )+ ) );
40+
( $first:expr, ) => ( $first )
41+
}
42+
3643
// The actual lang items defined come at the end of this file in one handy table.
3744
// So you probably just want to nip down to the end.
3845
macro_rules! lets_do_this {
46+
// secondary rule to allow us to use `$num` as both an expression
47+
// and a pattern.
48+
(
49+
$( $num:tt, $variant:ident, $name:expr, $method:ident; )*
50+
) => {
51+
lets_do_this!(count = 1 + last!($($num,)*),
52+
$($num, $variant, $name, $method; )*)
53+
};
54+
3955
(
40-
There are $num_lang_items:expr lang items.
41-
$( $num:pat, $variant:ident, $name:expr, $method:ident; )*
56+
count = $num_lang_items:expr, $( $num:pat, $variant:ident, $name:expr, $method:ident; )*
4257
) => {
4358

4459
pub enum LangItem {
@@ -207,8 +222,6 @@ pub fn collect_language_items(crate: &ast::Crate,
207222
}
208223

209224
lets_do_this! {
210-
There are 40 lang items.
211-
212225
// ID, Variant name, Name, Method name;
213226
0, FreezeTraitLangItem, "freeze", freeze_trait;
214227
1, SendTraitLangItem, "send", send_trait;
@@ -261,4 +274,3 @@ lets_do_this! {
261274
38, ExchangeHeapLangItem, "exchange_heap", exchange_heap;
262275
39, GcLangItem, "gc", gc;
263276
}
264-

0 commit comments

Comments
 (0)