about summary refs log tree commit diff
diff options
context:
space:
mode:
authorHayashi Mikihiro <34ttrweoewiwe28@gmail.com>2025-05-08 23:37:47 +0900
committerHayashi Mikihiro <34ttrweoewiwe28@gmail.com>2025-05-08 23:37:47 +0900
commit656a59e40f792233be25e310d5d533d1d12ed1fd (patch)
tree9d6c3d11b05945f0b767b37a5798302da8cf2745
parent627fedb423343d3a26147986fdda1d8b18474597 (diff)
downloadrust-656a59e40f792233be25e310d5d533d1d12ed1fd.tar.gz
rust-656a59e40f792233be25e310d5d533d1d12ed1fd.zip
add assert to check ast_index smaller than INNER_ATTR_SET_BIT
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
-rw-r--r--src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs22
1 files changed, 9 insertions, 13 deletions
diff --git a/src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs b/src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs
index 4e519452aa6..94c97713f06 100644
--- a/src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs
+++ b/src/tools/rust-analyzer/crates/hir-expand/src/attrs.rs
@@ -123,15 +123,15 @@ impl RawAttrs {
             (None, entries @ Some(_)) => Self { entries },
             (Some(entries), None) => Self { entries: Some(entries.clone()) },
             (Some(a), Some(b)) => {
-                let last_ast_index = a.slice.last().map_or(0, |it| it.id.ast_index() + 1) as u32;
+                let last_ast_index = a.slice.last().map_or(0, |it| it.id.ast_index() + 1);
                 let items = a
                     .slice
                     .iter()
                     .cloned()
                     .chain(b.slice.iter().map(|it| {
                         let mut it = it.clone();
-                        let id = it.id.ast_index() as u32 + last_ast_index;
-                        it.id = AttrId::new(id as usize, it.id.is_inner_attr());
+                        let id = it.id.ast_index() + last_ast_index;
+                        it.id = AttrId::new(id, it.id.is_inner_attr());
                         it
                     }))
                     .collect::<Vec<_>>();
@@ -175,24 +175,20 @@ pub struct AttrId {
 // FIXME: This only handles a single level of cfg_attr nesting
 // that is `#[cfg_attr(all(), cfg_attr(all(), cfg(any())))]` breaks again
 impl AttrId {
-    const INNER_ATTR_SET_BIT: usize = 1 << 31;
+    const INNER_ATTR_SET_BIT: u32 = 1 << 31;
 
     pub fn new(id: usize, is_inner: bool) -> Self {
-        Self {
-            id: if is_inner {
-                id | Self::INNER_ATTR_SET_BIT
-            } else {
-                id & !Self::INNER_ATTR_SET_BIT
-            } as u32,
-        }
+        assert!(id <= !Self::INNER_ATTR_SET_BIT as usize);
+        let id = id as u32;
+        Self { id: if is_inner { id | Self::INNER_ATTR_SET_BIT } else { id } }
     }
 
     pub fn ast_index(&self) -> usize {
-        self.id as usize & !Self::INNER_ATTR_SET_BIT
+        (self.id & !Self::INNER_ATTR_SET_BIT) as usize
     }
 
     pub fn is_inner_attr(&self) -> bool {
-        (self.id as usize) & Self::INNER_ATTR_SET_BIT != 0
+        self.id & Self::INNER_ATTR_SET_BIT != 0
     }
 }