Skip to content

Commit 7c7d440

Browse files
committed
add assert to check ast_index smaller than INNER_ATTR_SET_BIT
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
1 parent 0235ff8 commit 7c7d440

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

crates/hir-expand/src/attrs.rs

+9-13
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ impl RawAttrs {
123123
(None, entries @ Some(_)) => Self { entries },
124124
(Some(entries), None) => Self { entries: Some(entries.clone()) },
125125
(Some(a), Some(b)) => {
126-
let last_ast_index = a.slice.last().map_or(0, |it| it.id.ast_index() + 1) as u32;
126+
let last_ast_index = a.slice.last().map_or(0, |it| it.id.ast_index() + 1);
127127
let items = a
128128
.slice
129129
.iter()
130130
.cloned()
131131
.chain(b.slice.iter().map(|it| {
132132
let mut it = it.clone();
133-
let id = it.id.ast_index() as u32 + last_ast_index;
134-
it.id = AttrId::new(id as usize, it.id.is_inner_attr());
133+
let id = it.id.ast_index() + last_ast_index;
134+
it.id = AttrId::new(id, it.id.is_inner_attr());
135135
it
136136
}))
137137
.collect::<Vec<_>>();
@@ -175,24 +175,20 @@ pub struct AttrId {
175175
// FIXME: This only handles a single level of cfg_attr nesting
176176
// that is `#[cfg_attr(all(), cfg_attr(all(), cfg(any())))]` breaks again
177177
impl AttrId {
178-
const INNER_ATTR_SET_BIT: usize = 1 << 31;
178+
const INNER_ATTR_SET_BIT: u32 = 1 << 31;
179179

180180
pub fn new(id: usize, is_inner: bool) -> Self {
181-
Self {
182-
id: if is_inner {
183-
id | Self::INNER_ATTR_SET_BIT
184-
} else {
185-
id & !Self::INNER_ATTR_SET_BIT
186-
} as u32,
187-
}
181+
assert!(id <= !Self::INNER_ATTR_SET_BIT as usize);
182+
let id = id as u32;
183+
Self { id: if is_inner { id | Self::INNER_ATTR_SET_BIT } else { id } }
188184
}
189185

190186
pub fn ast_index(&self) -> usize {
191-
self.id as usize & !Self::INNER_ATTR_SET_BIT
187+
(self.id & !Self::INNER_ATTR_SET_BIT) as usize
192188
}
193189

194190
pub fn is_inner_attr(&self) -> bool {
195-
(self.id as usize) & Self::INNER_ATTR_SET_BIT != 0
191+
self.id & Self::INNER_ATTR_SET_BIT != 0
196192
}
197193
}
198194

0 commit comments

Comments
 (0)