Skip to content

Commit 3fcdd1b

Browse files
Add AttrId to track attribute sources
1 parent 99ed68a commit 3fcdd1b

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

crates/hir_def/src/attr.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::{
99
use base_db::CrateId;
1010
use cfg::{CfgExpr, CfgOptions};
1111
use either::Either;
12-
use hir_expand::{hygiene::Hygiene, name::AsName, AstId, InFile};
12+
use hir_expand::{hygiene::Hygiene, name::AsName, AstId, AttrId, InFile};
1313
use itertools::Itertools;
1414
use la_arena::ArenaMap;
1515
use mbe::ast_to_token_tree;
@@ -98,13 +98,16 @@ impl RawAttrs {
9898
pub(crate) fn new(owner: &dyn ast::AttrsOwner, hygiene: &Hygiene) -> Self {
9999
let entries = collect_attrs(owner)
100100
.enumerate()
101-
.flat_map(|(i, attr)| match attr {
102-
Either::Left(attr) => Attr::from_src(attr, hygiene, i as u32),
103-
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
104-
index: i as u32,
105-
input: Some(AttrInput::Literal(SmolStr::new(doc))),
106-
path: Interned::new(ModPath::from(hir_expand::name!(doc))),
107-
}),
101+
.flat_map(|(i, attr)| {
102+
let index = AttrId(i as u32);
103+
match attr {
104+
Either::Left(attr) => Attr::from_src(attr, hygiene, index),
105+
Either::Right(comment) => comment.doc_comment().map(|doc| Attr {
106+
index,
107+
input: Some(AttrInput::Literal(SmolStr::new(doc))),
108+
path: Interned::new(ModPath::from(hir_expand::name!(doc))),
109+
}),
110+
}
108111
})
109112
.collect::<Arc<_>>();
110113

@@ -560,8 +563,8 @@ impl AttrSourceMap {
560563
/// the attribute represented by `Attr`.
561564
pub fn source_of(&self, attr: &Attr) -> InFile<&Either<ast::Attr, ast::Comment>> {
562565
self.attrs
563-
.get(attr.index as usize)
564-
.unwrap_or_else(|| panic!("cannot find `Attr` at index {}", attr.index))
566+
.get(attr.index.0 as usize)
567+
.unwrap_or_else(|| panic!("cannot find `Attr` at index {:?}", attr.index))
565568
.as_ref()
566569
}
567570
}
@@ -572,7 +575,7 @@ pub struct DocsRangeMap {
572575
// (docstring-line-range, attr_index, attr-string-range)
573576
// a mapping from the text range of a line of the [`Documentation`] to the attribute index and
574577
// the original (untrimmed) syntax doc line
575-
mapping: Vec<(TextRange, u32, TextRange)>,
578+
mapping: Vec<(TextRange, AttrId, TextRange)>,
576579
}
577580

578581
impl DocsRangeMap {
@@ -585,7 +588,7 @@ impl DocsRangeMap {
585588

586589
let relative_range = range - line_docs_range.start();
587590

588-
let &InFile { file_id, value: ref source } = &self.source[idx as usize];
591+
let &InFile { file_id, value: ref source } = &self.source[idx.0 as usize];
589592
match source {
590593
Either::Left(_) => None, // FIXME, figure out a nice way to handle doc attributes here
591594
// as well as for whats done in syntax highlight doc injection
@@ -606,7 +609,7 @@ impl DocsRangeMap {
606609

607610
#[derive(Debug, Clone, PartialEq, Eq)]
608611
pub struct Attr {
609-
index: u32,
612+
pub(crate) index: AttrId,
610613
pub(crate) path: Interned<ModPath>,
611614
pub(crate) input: Option<AttrInput>,
612615
}
@@ -620,7 +623,7 @@ pub enum AttrInput {
620623
}
621624

622625
impl Attr {
623-
fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: u32) -> Option<Attr> {
626+
fn from_src(ast: ast::Attr, hygiene: &Hygiene, index: AttrId) -> Option<Attr> {
624627
let path = Interned::new(ModPath::from_src(ast.path()?, hygiene)?);
625628
let input = if let Some(ast::Expr::Literal(lit)) = ast.expr() {
626629
let value = match lit.kind() {

crates/hir_expand/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,9 @@ pub enum MacroCallKind {
294294
Derive { ast_id: AstId<ast::Item>, derive_name: String },
295295
}
296296

297+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
298+
pub struct AttrId(pub u32);
299+
297300
impl MacroCallKind {
298301
fn file_id(&self) -> HirFileId {
299302
match self {

0 commit comments

Comments
 (0)