-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: resolve doc path from parent module if outer comments exist on module #19507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
4db0156
to
de69c73
Compare
crates/hir-expand/src/attrs.rs
Outdated
@@ -211,6 +215,15 @@ pub struct Attr { | |||
pub path: Interned<ModPath>, | |||
pub input: Option<Box<AttrInput>>, | |||
pub ctxt: SyntaxContext, | |||
pub place: AttrPlacement, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This grows attributes by 8
bytes which will be noticable, we'll need to avoid that somehow if we can. Note quite sure how yet. Probably by using one of the AttrId
bits (we already do something there for cfg_attr but that is not really used so we can replace that with this likely). Will write oiut better instructions later
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rust-analyzer/crates/hir-expand/src/attrs.rs
Lines 185 to 189 in 588948f
impl AttrId { | |
const CFG_ATTR_BITS: usize = 7; | |
const AST_INDEX_MASK: usize = 0x00FF_FFFF; | |
const AST_INDEX_BITS: usize = Self::AST_INDEX_MASK.count_ones() as usize; | |
const CFG_ATTR_SET_BITS: u32 = 1 << 31; |
Right, that means AttrId currently uses bits like this:
- Bit 31 → is_cfg_attr
- Bits 24–30 → cfg_attr_index (7 bits)
- Bits 0–23 → ast_index (24 bits)
So, we could either remove or reduce the size of cfg_attr_index
, and reuse the freed bit for placement information.
- Bit 31 → inner_or_outer
- Bits 24–30 → (unused)or (is_cfg_attr & cfg_attr_index(6bit))
- Bits 0–23 → ast_index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rust-analyzer/crates/hir-expand/src/attrs.rs
Lines 108 to 110 in 588948f
let mut it = it.clone(); | |
it.id.id = (it.id.ast_index() as u32 + last_ast_index) | |
| ((it.id.cfg_attr_index().unwrap_or(0) as u32) |
And cfg_attr
was accessed through cfg_attr_index()
but this function was only used to copy it when creating a new [Attr]
. So would we remove cfg_attr?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IF the cfg attr stuff isnt actually used then lets remove it yes. Then just use the most significant bit for encoding the "inner-ness"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. The inner-ness uses the most significant bit, and bits 24–30 are currently unused.
a3167ad
to
83000f5
Compare
crates/hir-expand/src/attrs.rs
Outdated
@@ -183,25 +180,21 @@ 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 CFG_ATTR_BITS: usize = 7; | |||
const AST_INDEX_MASK: usize = 0x00FF_FFFF; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need the mask anymore, we just need to make sure to strip the inner attribute bit when using the index as an actual index
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. We don't need to clear 24-30bit. AST_INDEX uses 0-30bit.
crates/hir/src/attrs.rs
Outdated
let is_inner = | ||
def.attrs(db).by_key(&intern::sym::doc).attrs().all(|attr| attr.id.is_inner_attr()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a parameter of resolve_doc_path_on
as it depends on the attribute the link was usually fetched from. A module for example can very likely have both inner and outer attributes mixed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix commit 778322e
I see. In old commits, if both inner and outer attributes mixed, those attributes was resolved as outer attributes.
Instead to resolve inner-ness in resolve_doc_path_on
, this resolves inner-ness each attributes.
pub fn map(&self, range: TextRange) -> Option<(InFile<TextRange>, AttrId)> { |
rust-analyzer/crates/ide/src/doc_links.rs
Lines 333 to 342 in 778322e
let (in_expansion_range, link, ns, is_inner) = | |
extract_definitions_from_docs(&docs).into_iter().find_map(|(range, link, ns)| { | |
let (mapped, idx) = doc_mapping.map(range)?; | |
(mapped.value.contains(abs_in_expansion_offset)).then_some((mapped.value, link, ns, idx.is_inner_attr())) | |
})?; | |
// get the relative range to the doc/attribute in the expansion | |
let in_expansion_relative_range = in_expansion_range - descended_prefix_len - token_start; | |
// Apply relative range to the original input comment | |
let absolute_range = in_expansion_relative_range + original_start + prefix_len; | |
let def = resolve_doc_path_for_def(sema.db, def, &link, ns, is_inner)?; |
…rom cfg_attr bit to doc_place bit Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
78cce17
to
cc0e3ac
Compare
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
e405943
to
0235ff8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One small thing, otherwise this looks good to me!
crates/hir-expand/src/attrs.rs
Outdated
Self { | ||
id: if is_inner { | ||
id | Self::INNER_ATTR_SET_BIT | ||
} else { | ||
id & !Self::INNER_ATTR_SET_BIT | ||
} as u32, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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); | |
let id = as u32; | |
Self { | |
id: if is_inner { | |
id | Self::INNER_ATTR_SET_BIT | |
} else { | |
id | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fix commit 7c7d440
rust-analyzer/crates/hir-expand/src/attrs.rs
Lines 180 to 184 in 7c7d440
pub fn new(id: usize, is_inner: bool) -> Self { | |
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 } } | |
} |
Signed-off-by: Hayashi Mikihiro <34ttrweoewiwe28@gmail.com>
Thanks! |
fix: #19506

#19471
I set placement information about inner or outer in attribute.
When I check link on module, I search from parent module if attribute of doc item have a outer comment.