Skip to content

Commit 13dd9af

Browse files
committed
ast: Compress AttrId from usize to u32
Also stop encoding/decoding it entirely
1 parent 5f13820 commit 13dd9af

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

src/librustc_ast/ast.rs

+8-14
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ use crate::tokenstream::{DelimSpan, TokenStream, TokenTree};
3131
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
3232
use rustc_data_structures::sync::Lrc;
3333
use rustc_data_structures::thin_vec::ThinVec;
34-
use rustc_index::vec::Idx;
3534
use rustc_macros::HashStable_Generic;
3635
use rustc_serialize::{self, Decoder, Encoder};
3736
use rustc_span::source_map::{respan, Spanned};
@@ -2251,27 +2250,22 @@ pub enum AttrStyle {
22512250
Inner,
22522251
}
22532252

2254-
#[derive(Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord, Copy)]
2255-
pub struct AttrId(pub usize);
2256-
2257-
impl Idx for AttrId {
2258-
fn new(idx: usize) -> Self {
2259-
AttrId(idx)
2260-
}
2261-
fn index(self) -> usize {
2262-
self.0
2253+
rustc_index::newtype_index! {
2254+
pub struct AttrId {
2255+
ENCODABLE = custom
2256+
DEBUG_FORMAT = "AttrId({})"
22632257
}
22642258
}
22652259

22662260
impl rustc_serialize::Encodable for AttrId {
2267-
fn encode<S: Encoder>(&self, s: &mut S) -> Result<(), S::Error> {
2268-
s.emit_unit()
2261+
fn encode<S: Encoder>(&self, _: &mut S) -> Result<(), S::Error> {
2262+
Ok(())
22692263
}
22702264
}
22712265

22722266
impl rustc_serialize::Decodable for AttrId {
2273-
fn decode<D: Decoder>(d: &mut D) -> Result<AttrId, D::Error> {
2274-
d.read_nil().map(|_| crate::attr::mk_attr_id())
2267+
fn decode<D: Decoder>(_: &mut D) -> Result<AttrId, D::Error> {
2268+
Ok(crate::attr::mk_attr_id())
22752269
}
22762270
}
22772271

src/librustc_ast/attr/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,14 @@ pub fn mk_nested_word_item(ident: Ident) -> NestedMetaItem {
366366
}
367367

368368
crate fn mk_attr_id() -> AttrId {
369-
use std::sync::atomic::AtomicUsize;
369+
use std::sync::atomic::AtomicU32;
370370
use std::sync::atomic::Ordering;
371371

372-
static NEXT_ATTR_ID: AtomicUsize = AtomicUsize::new(0);
372+
static NEXT_ATTR_ID: AtomicU32 = AtomicU32::new(0);
373373

374374
let id = NEXT_ATTR_ID.fetch_add(1, Ordering::SeqCst);
375-
assert!(id != ::std::usize::MAX);
376-
AttrId(id)
375+
assert!(id != u32::MAX);
376+
AttrId::from_u32(id)
377377
}
378378

379379
pub fn mk_attr(style: AttrStyle, path: Path, args: MacArgs, span: Span) -> Attribute {

0 commit comments

Comments
 (0)