Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit cc8faa6

Browse files
committed
Auto merge of rust-lang#16034 - Veykril:ide-macro-improvements, r=Veykril
feat: Allow navigation targets to be duplicated when the focus range lies in the macro definition site ![Code_KI1EfbAHRZ](https://github.com/rust-lang/rust-analyzer/assets/3757771/2cc82e5c-320f-4de2-9d55-fe975d180f2a) Basically if a name of an item originates from the macro definition we now point to that as well as the creating macro call. Big diff because I also made `FileId`s field private due to some debugging I had to do (having a searchable constructor makes things easier).
2 parents 05df6c5 + 9cb13b6 commit cc8faa6

38 files changed

+851
-488
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/base-db/src/fixture.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ impl ChangeFixture {
135135

136136
let mut file_set = FileSet::default();
137137
let mut current_source_root_kind = SourceRootKind::Local;
138-
let mut file_id = FileId(0);
138+
let mut file_id = FileId::from_raw(0);
139139
let mut roots = Vec::new();
140140

141141
let mut file_position = None;
@@ -210,7 +210,7 @@ impl ChangeFixture {
210210
let path = VfsPath::new_virtual_path(meta.path);
211211
file_set.insert(file_id, path);
212212
files.push(file_id);
213-
file_id.0 += 1;
213+
file_id = FileId::from_raw(file_id.index() + 1);
214214
}
215215

216216
if crates.is_empty() {
@@ -255,7 +255,7 @@ impl ChangeFixture {
255255

256256
if let Some(mini_core) = mini_core {
257257
let core_file = file_id;
258-
file_id.0 += 1;
258+
file_id = FileId::from_raw(file_id.index() + 1);
259259

260260
let mut fs = FileSet::default();
261261
fs.insert(core_file, VfsPath::new_virtual_path("/sysroot/core/lib.rs".to_string()));
@@ -296,7 +296,6 @@ impl ChangeFixture {
296296
let mut proc_macros = ProcMacros::default();
297297
if !proc_macro_names.is_empty() {
298298
let proc_lib_file = file_id;
299-
file_id.0 += 1;
300299

301300
proc_macro_defs.extend(default_test_proc_macros());
302301
let (proc_macro, source) = filter_test_proc_macros(&proc_macro_names, proc_macro_defs);

crates/base-db/src/input.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ mod tests {
880880
fn detect_cyclic_dependency_indirect() {
881881
let mut graph = CrateGraph::default();
882882
let crate1 = graph.add_crate_root(
883-
FileId(1u32),
883+
FileId::from_raw(1u32),
884884
Edition2018,
885885
None,
886886
None,
@@ -893,7 +893,7 @@ mod tests {
893893
None,
894894
);
895895
let crate2 = graph.add_crate_root(
896-
FileId(2u32),
896+
FileId::from_raw(2u32),
897897
Edition2018,
898898
None,
899899
None,
@@ -906,7 +906,7 @@ mod tests {
906906
None,
907907
);
908908
let crate3 = graph.add_crate_root(
909-
FileId(3u32),
909+
FileId::from_raw(3u32),
910910
Edition2018,
911911
None,
912912
None,
@@ -942,7 +942,7 @@ mod tests {
942942
fn detect_cyclic_dependency_direct() {
943943
let mut graph = CrateGraph::default();
944944
let crate1 = graph.add_crate_root(
945-
FileId(1u32),
945+
FileId::from_raw(1u32),
946946
Edition2018,
947947
None,
948948
None,
@@ -955,7 +955,7 @@ mod tests {
955955
None,
956956
);
957957
let crate2 = graph.add_crate_root(
958-
FileId(2u32),
958+
FileId::from_raw(2u32),
959959
Edition2018,
960960
None,
961961
None,
@@ -985,7 +985,7 @@ mod tests {
985985
fn it_works() {
986986
let mut graph = CrateGraph::default();
987987
let crate1 = graph.add_crate_root(
988-
FileId(1u32),
988+
FileId::from_raw(1u32),
989989
Edition2018,
990990
None,
991991
None,
@@ -998,7 +998,7 @@ mod tests {
998998
None,
999999
);
10001000
let crate2 = graph.add_crate_root(
1001-
FileId(2u32),
1001+
FileId::from_raw(2u32),
10021002
Edition2018,
10031003
None,
10041004
None,
@@ -1011,7 +1011,7 @@ mod tests {
10111011
None,
10121012
);
10131013
let crate3 = graph.add_crate_root(
1014-
FileId(3u32),
1014+
FileId::from_raw(3u32),
10151015
Edition2018,
10161016
None,
10171017
None,
@@ -1041,7 +1041,7 @@ mod tests {
10411041
fn dashes_are_normalized() {
10421042
let mut graph = CrateGraph::default();
10431043
let crate1 = graph.add_crate_root(
1044-
FileId(1u32),
1044+
FileId::from_raw(1u32),
10451045
Edition2018,
10461046
None,
10471047
None,
@@ -1054,7 +1054,7 @@ mod tests {
10541054
None,
10551055
);
10561056
let crate2 = graph.add_crate_root(
1057-
FileId(2u32),
1057+
FileId::from_raw(2u32),
10581058
Edition2018,
10591059
None,
10601060
None,

crates/base-db/src/span.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ impl fmt::Debug for SpanAnchor {
7070
}
7171

7272
impl tt::SpanAnchor for SpanAnchor {
73-
const DUMMY: Self = SpanAnchor { file_id: FileId(0), ast_id: ROOT_ERASED_FILE_AST_ID };
73+
const DUMMY: Self = SpanAnchor { file_id: FileId::BOGUS, ast_id: ROOT_ERASED_FILE_AST_ID };
7474
}
7575

7676
/// Input to the analyzer is a set of files, where each file is identified by
@@ -99,12 +99,6 @@ impl From<HirFileId> for u32 {
9999
}
100100
}
101101

102-
impl From<u32> for HirFileId {
103-
fn from(value: u32) -> Self {
104-
HirFileId(value)
105-
}
106-
}
107-
108102
impl From<MacroCallId> for HirFileId {
109103
fn from(value: MacroCallId) -> Self {
110104
value.as_file()
@@ -147,7 +141,7 @@ pub enum HirFileIdRepr {
147141
impl fmt::Debug for HirFileIdRepr {
148142
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
149143
match self {
150-
Self::FileId(arg0) => f.debug_tuple("FileId").field(&arg0.0).finish(),
144+
Self::FileId(arg0) => f.debug_tuple("FileId").field(&arg0.index()).finish(),
151145
Self::MacroFile(arg0) => {
152146
f.debug_tuple("MacroFile").field(&arg0.macro_call_id.0).finish()
153147
}
@@ -156,9 +150,9 @@ impl fmt::Debug for HirFileIdRepr {
156150
}
157151

158152
impl From<FileId> for HirFileId {
159-
fn from(FileId(id): FileId) -> Self {
160-
assert!(id < Self::MAX_FILE_ID);
161-
HirFileId(id)
153+
fn from(id: FileId) -> Self {
154+
assert!(id.index() < Self::MAX_FILE_ID);
155+
HirFileId(id.index())
162156
}
163157
}
164158

@@ -192,15 +186,15 @@ impl HirFileId {
192186
#[inline]
193187
pub fn file_id(self) -> Option<FileId> {
194188
match self.0 & Self::MACRO_FILE_TAG_MASK {
195-
0 => Some(FileId(self.0)),
189+
0 => Some(FileId::from_raw(self.0)),
196190
_ => None,
197191
}
198192
}
199193

200194
#[inline]
201195
pub fn repr(self) -> HirFileIdRepr {
202196
match self.0 & Self::MACRO_FILE_TAG_MASK {
203-
0 => HirFileIdRepr::FileId(FileId(self.0)),
197+
0 => HirFileIdRepr::FileId(FileId::from_raw(self.0)),
204198
_ => HirFileIdRepr::MacroFile(MacroFileId {
205199
macro_call_id: MacroCallId(InternId::from(self.0 ^ Self::MACRO_FILE_TAG_MASK)),
206200
}),

crates/hir-def/src/attr/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ fn assert_parse_result(input: &str, expected: DocExpr) {
1313
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
1414
let tt = syntax_node_to_token_tree(
1515
tt.syntax(),
16-
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId(0))),
16+
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId::from_raw(0))),
1717
);
1818
let cfg = DocExpr::parse(&tt);
1919
assert_eq!(cfg, expected);

crates/hir-def/src/generics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ fn file_id_and_params_of(
524524
(src.file_id, src.value.generic_param_list())
525525
}
526526
// We won't be using this ID anyway
527-
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => (FileId(!0).into(), None),
527+
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => (FileId::BOGUS.into(), None),
528528
}
529529
}
530530

crates/hir-expand/src/files.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,40 @@ impl InFile<TextRange> {
307307
};
308308
range
309309
}
310+
311+
pub fn original_node_file_range(
312+
self,
313+
db: &dyn db::ExpandDatabase,
314+
) -> (FileRange, SyntaxContextId) {
315+
match self.file_id.repr() {
316+
HirFileIdRepr::FileId(file_id) => {
317+
(FileRange { file_id, range: self.value }, SyntaxContextId::ROOT)
318+
}
319+
HirFileIdRepr::MacroFile(mac_file) => {
320+
match ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value) {
321+
Some(it) => it,
322+
None => {
323+
let loc = db.lookup_intern_macro_call(mac_file.macro_call_id);
324+
(loc.kind.original_call_range(db), SyntaxContextId::ROOT)
325+
}
326+
}
327+
}
328+
}
329+
}
330+
331+
pub fn original_node_file_range_opt(
332+
self,
333+
db: &dyn db::ExpandDatabase,
334+
) -> Option<(FileRange, SyntaxContextId)> {
335+
match self.file_id.repr() {
336+
HirFileIdRepr::FileId(file_id) => {
337+
Some((FileRange { file_id, range: self.value }, SyntaxContextId::ROOT))
338+
}
339+
HirFileIdRepr::MacroFile(mac_file) => {
340+
ExpansionInfo::new(db, mac_file).map_node_range_up(db, self.value)
341+
}
342+
}
343+
}
310344
}
311345

312346
impl<N: AstNode> InFile<N> {

crates/hir-expand/src/fixup.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ pub(crate) fn fixup_syntax(span_map: SpanMapRef<'_>, node: &SyntaxNode) -> Synta
5454
let dummy_range = TextRange::empty(TextSize::new(0));
5555
// we use a file id of `FileId(!0)` to signal a fake node, and the text range's start offset as
5656
// the index into the replacement vec but only if the end points to !0
57-
let dummy_anchor =
58-
SpanAnchor { file_id: FileId(!0), ast_id: ErasedFileAstId::from_raw(RawIdx::from(!0)) };
57+
let dummy_anchor = SpanAnchor {
58+
file_id: FileId::from_raw(!0),
59+
ast_id: ErasedFileAstId::from_raw(RawIdx::from(!0)),
60+
};
5961
let fake_span = |range| SpanData {
6062
range: dummy_range,
6163
anchor: dummy_anchor,
@@ -308,7 +310,7 @@ fn reverse_fixups_(tt: &mut Subtree, undo_info: &[Subtree]) {
308310
.filter(|tt| match tt {
309311
tt::TokenTree::Leaf(leaf) => {
310312
let span = leaf.span();
311-
span.anchor.file_id != FileId(!0) || span.range.end() == TextSize::new(!0)
313+
span.anchor.file_id != FileId::from_raw(!0) || span.range.end() == TextSize::new(!0)
312314
}
313315
tt::TokenTree::Subtree(_) => true,
314316
})
@@ -318,7 +320,7 @@ fn reverse_fixups_(tt: &mut Subtree, undo_info: &[Subtree]) {
318320
SmallVec::from_const([tt.into()])
319321
}
320322
tt::TokenTree::Leaf(leaf) => {
321-
if leaf.span().anchor.file_id == FileId(!0) {
323+
if leaf.span().anchor.file_id == FileId::from_raw(!0) {
322324
let original = undo_info[u32::from(leaf.span().range.start()) as usize].clone();
323325
if original.delimiter.kind == tt::DelimiterKind::Invisible {
324326
original.token_trees.into()
@@ -373,7 +375,7 @@ mod tests {
373375
#[track_caller]
374376
fn check(ra_fixture: &str, mut expect: Expect) {
375377
let parsed = syntax::SourceFile::parse(ra_fixture);
376-
let span_map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(FileId(0))));
378+
let span_map = SpanMap::RealSpanMap(Arc::new(RealSpanMap::absolute(FileId::from_raw(0))));
377379
let fixups = super::fixup_syntax(span_map.as_ref(), &parsed.syntax_node());
378380
let mut tt = mbe::syntax_node_to_token_tree_modified(
379381
&parsed.syntax_node(),

crates/hir-expand/src/hygiene.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ pub trait SyntaxContextExt {
197197
fn normalize_to_macro_rules(self, db: &dyn ExpandDatabase) -> Self;
198198
fn normalize_to_macros_2_0(self, db: &dyn ExpandDatabase) -> Self;
199199
fn parent_ctxt(self, db: &dyn ExpandDatabase) -> Self;
200+
fn remove_mark(&mut self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparency);
200201
fn outer_mark(self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparency);
201202
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(Option<MacroCallId>, Transparency)>;
202203
}
@@ -223,6 +224,11 @@ impl SyntaxContextExt for SyntaxContextId {
223224
let data = db.lookup_intern_syntax_context(self);
224225
(data.outer_expn, data.outer_transparency)
225226
}
227+
fn remove_mark(&mut self, db: &dyn ExpandDatabase) -> (Option<MacroCallId>, Transparency) {
228+
let data = db.lookup_intern_syntax_context(*self);
229+
*self = data.parent;
230+
(data.outer_expn, data.outer_transparency)
231+
}
226232
fn marks(self, db: &dyn ExpandDatabase) -> Vec<(Option<MacroCallId>, Transparency)> {
227233
let mut marks = marks_rev(self, db).collect::<Vec<_>>();
228234
marks.reverse();

crates/hir-expand/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,21 @@ impl MacroDefId {
380380
db.intern_macro_call(MacroCallLoc { def: self, krate, eager: None, kind, call_site })
381381
}
382382

383+
pub fn definition_range(&self, db: &dyn db::ExpandDatabase) -> InFile<TextRange> {
384+
match self.kind {
385+
MacroDefKind::Declarative(id)
386+
| MacroDefKind::BuiltIn(_, id)
387+
| MacroDefKind::BuiltInAttr(_, id)
388+
| MacroDefKind::BuiltInDerive(_, id)
389+
| MacroDefKind::BuiltInEager(_, id) => {
390+
id.with_value(db.ast_id_map(id.file_id).get(id.value).text_range())
391+
}
392+
MacroDefKind::ProcMacro(_, _, id) => {
393+
id.with_value(db.ast_id_map(id.file_id).get(id.value).text_range())
394+
}
395+
}
396+
}
397+
383398
pub fn ast_id(&self) -> Either<AstId<ast::Macro>, AstId<ast::Fn>> {
384399
match self.kind {
385400
MacroDefKind::ProcMacro(.., id) => return Either::Right(id),

crates/hir-expand/src/quote.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ mod tests {
261261
assert_eq!(quoted.to_string(), "hello");
262262
let t = format!("{quoted:?}");
263263
expect![[r#"
264-
SUBTREE $$ SpanData { range: 0..0, anchor: SpanAnchor(FileId(4294967295), 0), ctx: SyntaxContextId(0) } SpanData { range: 0..0, anchor: SpanAnchor(FileId(4294967295), 0), ctx: SyntaxContextId(0) }
265-
IDENT hello SpanData { range: 0..0, anchor: SpanAnchor(FileId(4294967295), 0), ctx: SyntaxContextId(0) }"#]].assert_eq(&t);
264+
SUBTREE $$ SpanData { range: 0..0, anchor: SpanAnchor(FileId(937550), 0), ctx: SyntaxContextId(0) } SpanData { range: 0..0, anchor: SpanAnchor(FileId(937550), 0), ctx: SyntaxContextId(0) }
265+
IDENT hello SpanData { range: 0..0, anchor: SpanAnchor(FileId(937550), 0), ctx: SyntaxContextId(0) }"#]].assert_eq(&t);
266266
}
267267

268268
#[test]

crates/hir/src/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn modpath_from_str(db: &dyn HirDatabase, link: &str) -> Option<ModPath> {
241241
ModPath::from_src(
242242
db.upcast(),
243243
ast_path,
244-
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId(0))),
244+
SpanMapRef::RealSpanMap(&RealSpanMap::absolute(FileId::BOGUS)),
245245
)
246246
};
247247

crates/hir/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ pub use {
125125
},
126126
hir_expand::{
127127
attrs::{Attr, AttrId},
128+
hygiene::{marks_rev, SyntaxContextExt},
128129
name::{known, Name},
129130
tt, ExpandResult, HirFileId, HirFileIdExt, InFile, InMacroFile, InRealFile, MacroFileId,
130131
},

0 commit comments

Comments
 (0)