Skip to content

Commit f3403ef

Browse files
committed
spans always come from real file
1 parent 89adbae commit f3403ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

57 files changed

+1368
-1223
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/span.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,15 @@ impl SyntaxContextId {
3535
// FIXME: This is very much UB, salsa exposes no way to create an InternId in a const context
3636
// currently (which kind of makes sense but we need it here!)
3737
pub const SELF_REF: Self = SyntaxContextId(unsafe { core::mem::transmute(!0u32) });
38+
39+
pub fn is_root(self) -> bool {
40+
self == Self::ROOT
41+
}
3842
}
3943

4044
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
4145
pub struct SpanAnchor {
42-
pub file_id: HirFileId,
46+
pub file_id: FileId,
4347
pub ast_id: ErasedFileAstId,
4448
}
4549

@@ -50,7 +54,7 @@ impl fmt::Debug for SpanAnchor {
5054
}
5155

5256
impl tt::SpanAnchor for SpanAnchor {
53-
const DUMMY: Self = SpanAnchor { file_id: HirFileId(0), ast_id: ROOT_ERASED_FILE_AST_ID };
57+
const DUMMY: Self = SpanAnchor { file_id: FileId(0), ast_id: ROOT_ERASED_FILE_AST_ID };
5458
}
5559

5660
/// Input to the analyzer is a set of files, where each file is identified by
@@ -101,7 +105,6 @@ impl fmt::Debug for HirFileId {
101105
pub struct MacroFile {
102106
pub macro_call_id: MacroCallId,
103107
}
104-
105108
/// `MacroCallId` identifies a particular macro invocation, like
106109
/// `println!("Hello, {}", world)`.
107110
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]

crates/cfg/src/tests.rs

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use arbitrary::{Arbitrary, Unstructured};
22
use expect_test::{expect, Expect};
3-
use mbe::syntax_node_to_token_tree;
3+
use mbe::{syntax_node_to_token_tree, SpanMapper};
44
use syntax::{ast, AstNode};
55
use tt::{SpanAnchor, SyntaxContext};
66

77
use crate::{CfgAtom, CfgExpr, CfgOptions, DnfExpr};
88

9-
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
1010
struct DummyFile;
1111
impl SpanAnchor for DummyFile {
1212
const DUMMY: Self = DummyFile;
@@ -17,28 +17,26 @@ impl SyntaxContext for DummyCtx {
1717
const DUMMY: Self = DummyCtx;
1818
}
1919

20+
struct NoOpMap;
21+
22+
impl SpanMapper<tt::SpanData<DummyFile, DummyCtx>> for NoOpMap {
23+
fn span_for(&self, range: syntax::TextRange) -> tt::SpanData<DummyFile, DummyCtx> {
24+
tt::SpanData { range, anchor: DummyFile, ctx: DummyCtx }
25+
}
26+
}
27+
2028
fn assert_parse_result(input: &str, expected: CfgExpr) {
2129
let source_file = ast::SourceFile::parse(input).ok().unwrap();
2230
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
23-
let tt = syntax_node_to_token_tree::<_, DummyCtx>(
24-
tt.syntax(),
25-
DummyFile,
26-
0.into(),
27-
&Default::default(),
28-
);
31+
let tt = syntax_node_to_token_tree(tt.syntax(), NoOpMap);
2932
let cfg = CfgExpr::parse(&tt);
3033
assert_eq!(cfg, expected);
3134
}
3235

3336
fn check_dnf(input: &str, expect: Expect) {
3437
let source_file = ast::SourceFile::parse(input).ok().unwrap();
3538
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
36-
let tt = syntax_node_to_token_tree::<_, DummyCtx>(
37-
tt.syntax(),
38-
DummyFile,
39-
0.into(),
40-
&Default::default(),
41-
);
39+
let tt = syntax_node_to_token_tree(tt.syntax(), NoOpMap);
4240
let cfg = CfgExpr::parse(&tt);
4341
let actual = format!("#![cfg({})]", DnfExpr::new(cfg));
4442
expect.assert_eq(&actual);
@@ -47,12 +45,7 @@ fn check_dnf(input: &str, expect: Expect) {
4745
fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
4846
let source_file = ast::SourceFile::parse(input).ok().unwrap();
4947
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
50-
let tt = syntax_node_to_token_tree::<_, DummyCtx>(
51-
tt.syntax(),
52-
DummyFile,
53-
0.into(),
54-
&Default::default(),
55-
);
48+
let tt = syntax_node_to_token_tree(tt.syntax(), NoOpMap);
5649
let cfg = CfgExpr::parse(&tt);
5750
let dnf = DnfExpr::new(cfg);
5851
let why_inactive = dnf.why_inactive(opts).unwrap().to_string();
@@ -63,12 +56,7 @@ fn check_why_inactive(input: &str, opts: &CfgOptions, expect: Expect) {
6356
fn check_enable_hints(input: &str, opts: &CfgOptions, expected_hints: &[&str]) {
6457
let source_file = ast::SourceFile::parse(input).ok().unwrap();
6558
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
66-
let tt = syntax_node_to_token_tree::<_, DummyCtx>(
67-
tt.syntax(),
68-
DummyFile,
69-
0.into(),
70-
&Default::default(),
71-
);
59+
let tt = syntax_node_to_token_tree(tt.syntax(), NoOpMap);
7260
let cfg = CfgExpr::parse(&tt);
7361
let dnf = DnfExpr::new(cfg);
7462
let hints = dnf.compute_enable_hints(opts).map(|diff| diff.to_string()).collect::<Vec<_>>();

crates/hir-def/src/attr.rs

Lines changed: 7 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ mod tests;
77

88
use std::{hash::Hash, ops, slice::Iter as SliceIter};
99

10-
use base_db::{
11-
span::{ErasedFileAstId, SpanAnchor},
12-
CrateId,
13-
};
10+
use base_db::CrateId;
1411
use cfg::{CfgExpr, CfgOptions};
1512
use either::Either;
1613
use hir_expand::{
@@ -31,8 +28,8 @@ use crate::{
3128
lang_item::LangItem,
3229
nameres::{ModuleOrigin, ModuleSource},
3330
src::{HasChildSource, HasSource},
34-
AdtId, AssocItemLoc, AttrDefId, EnumId, GenericDefId, GenericParamId, ItemLoc,
35-
LocalEnumVariantId, LocalFieldId, Lookup, MacroId, VariantId,
31+
AdtId, AssocItemLoc, AttrDefId, EnumId, GenericParamId, ItemLoc, LocalEnumVariantId,
32+
LocalFieldId, Lookup, MacroId, VariantId,
3633
};
3734

3835
#[derive(Default, Debug, Clone, PartialEq, Eq)]
@@ -419,43 +416,30 @@ impl AttrsWithOwner {
419416
AttrDefId::FunctionId(it) => attrs_from_item_tree_assoc(db, it),
420417
AttrDefId::TypeAliasId(it) => attrs_from_item_tree_assoc(db, it),
421418
AttrDefId::GenericParamId(it) => {
422-
let ast_id = |p| match p {
423-
GenericDefId::AdtId(AdtId::StructId(it)) => {
424-
erased_ast_id_from_item_tree(db, it)
425-
}
426-
GenericDefId::AdtId(AdtId::EnumId(it)) => erased_ast_id_from_item_tree(db, it),
427-
GenericDefId::AdtId(AdtId::UnionId(it)) => erased_ast_id_from_item_tree(db, it),
428-
GenericDefId::TraitId(it) => erased_ast_id_from_item_tree(db, it),
429-
GenericDefId::TraitAliasId(it) => erased_ast_id_from_item_tree(db, it),
430-
GenericDefId::ImplId(it) => erased_ast_id_from_item_tree(db, it),
431-
GenericDefId::EnumVariantId(it) => erased_ast_id_from_item_tree(db, it.parent),
432-
GenericDefId::TypeAliasId(it) => erased_ast_id_from_item_tree_assoc(db, it),
433-
GenericDefId::FunctionId(it) => erased_ast_id_from_item_tree_assoc(db, it),
434-
GenericDefId::ConstId(it) => erased_ast_id_from_item_tree_assoc(db, it),
435-
};
419+
// FIXME: we could probably just make these relative to the params?
436420
match it {
437421
GenericParamId::ConstParamId(it) => {
438422
let src = it.parent().child_source(db);
439423
RawAttrs::from_attrs_owner(
440424
db.upcast(),
441-
SpanAnchor { file_id: src.file_id, ast_id: ast_id(it.parent()) },
442425
src.with_value(&src.value[it.local_id()]),
426+
db.span_map(src.file_id).as_ref(),
443427
)
444428
}
445429
GenericParamId::TypeParamId(it) => {
446430
let src = it.parent().child_source(db);
447431
RawAttrs::from_attrs_owner(
448432
db.upcast(),
449-
SpanAnchor { file_id: src.file_id, ast_id: ast_id(it.parent()) },
450433
src.with_value(&src.value[it.local_id()]),
434+
db.span_map(src.file_id).as_ref(),
451435
)
452436
}
453437
GenericParamId::LifetimeParamId(it) => {
454438
let src = it.parent.child_source(db);
455439
RawAttrs::from_attrs_owner(
456440
db.upcast(),
457-
SpanAnchor { file_id: src.file_id, ast_id: ast_id(it.parent) },
458441
src.with_value(&src.value[it.local_id]),
442+
db.span_map(src.file_id).as_ref(),
459443
)
460444
}
461445
}
@@ -663,26 +647,6 @@ fn any_has_attrs(
663647
id.lookup(db).source(db).map(ast::AnyHasAttrs::new)
664648
}
665649

666-
fn erased_ast_id_from_item_tree<N: ItemTreeNode>(
667-
db: &dyn DefDatabase,
668-
lookup: impl Lookup<Data = ItemLoc<N>>,
669-
) -> ErasedFileAstId {
670-
let id = lookup.lookup(db).id;
671-
let tree = id.item_tree(db);
672-
let mod_item = N::id_to_mod_item(id.value);
673-
mod_item.ast_id(&tree).erase()
674-
}
675-
676-
fn erased_ast_id_from_item_tree_assoc<N: ItemTreeNode>(
677-
db: &dyn DefDatabase,
678-
lookup: impl Lookup<Data = AssocItemLoc<N>>,
679-
) -> ErasedFileAstId {
680-
let id = lookup.lookup(db).id;
681-
let tree = id.item_tree(db);
682-
let mod_item = N::id_to_mod_item(id.value);
683-
mod_item.ast_id(&tree).erase()
684-
}
685-
686650
fn attrs_from_item_tree<N: ItemTreeNode>(db: &dyn DefDatabase, id: ItemTreeId<N>) -> RawAttrs {
687651
let tree = id.item_tree(db);
688652
let mod_item = N::id_to_mod_item(id.value);

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

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,19 @@
11
//! This module contains tests for doc-expression parsing.
22
//! Currently, it tests `#[doc(hidden)]` and `#[doc(alias)]`.
33
4-
use base_db::span::SpanAnchor;
4+
use base_db::FileId;
5+
use hir_expand::span::{RealSpanMap, SpanMapRef};
56
use mbe::syntax_node_to_token_tree;
67
use syntax::{ast, AstNode};
7-
use tt::{SpanAnchor as _, SyntaxContext};
88

99
use crate::attr::{DocAtom, DocExpr};
1010

11-
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
12-
struct DummyCtx;
13-
impl SyntaxContext for DummyCtx {
14-
const DUMMY: Self = DummyCtx;
15-
}
16-
1711
fn assert_parse_result(input: &str, expected: DocExpr) {
1812
let source_file = ast::SourceFile::parse(input).ok().unwrap();
1913
let tt = source_file.syntax().descendants().find_map(ast::TokenTree::cast).unwrap();
20-
let tt = syntax_node_to_token_tree::<_, DummyCtx>(
14+
let tt = syntax_node_to_token_tree(
2115
tt.syntax(),
22-
SpanAnchor::DUMMY,
23-
0.into(),
24-
&Default::default(),
16+
SpanMapRef::RealSpanMap(&RealSpanMap::empty(FileId(0))),
2517
);
2618
let cfg = DocExpr::parse(&tt);
2719
assert_eq!(cfg, expected);

crates/hir-def/src/expander.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
//! Macro expansion utilities.
22
3-
use base_db::{
4-
span::{SpanAnchor, ROOT_ERASED_FILE_AST_ID},
5-
CrateId,
6-
};
3+
use base_db::CrateId;
74
use cfg::CfgOptions;
85
use drop_bomb::DropBomb;
96
use hir_expand::{
10-
attrs::RawAttrs, mod_path::ModPath, ExpandError, ExpandResult, HirFileId, InFile, MacroCallId,
11-
SpanMap, UnresolvedMacro,
7+
attrs::RawAttrs, mod_path::ModPath, span::SpanMap, ExpandError, ExpandResult, HirFileId,
8+
InFile, MacroCallId,
129
};
1310
use limit::Limit;
1411
use syntax::{ast, Parse, SyntaxNode};
15-
use triomphe::Arc;
1612

1713
use crate::{
1814
attr::Attrs, db::DefDatabase, lower::LowerCtx, macro_id_to_def_id, path::Path, AsMacroCall,
19-
MacroId, ModuleId,
15+
MacroId, ModuleId, UnresolvedMacro,
2016
};
2117

2218
#[derive(Debug)]
2319
pub struct Expander {
2420
cfg_options: CfgOptions,
25-
hygiene: Arc<SpanMap>,
21+
hygiene: SpanMap,
2622
krate: CrateId,
2723
pub(crate) current_file_id: HirFileId,
2824
pub(crate) module: ModuleId,
@@ -122,17 +118,7 @@ impl Expander {
122118
}
123119

124120
pub(crate) fn parse_attrs(&self, db: &dyn DefDatabase, owner: &dyn ast::HasAttrs) -> Attrs {
125-
Attrs::filter(
126-
db,
127-
self.krate,
128-
RawAttrs::new(
129-
db.upcast(),
130-
// Usin `ROOT_ERASED_FILE_AST_ID` here is fine as this is only used for cfg checking
131-
SpanAnchor { file_id: self.current_file_id, ast_id: ROOT_ERASED_FILE_AST_ID },
132-
owner,
133-
&self.hygiene,
134-
),
135-
)
121+
Attrs::filter(db, self.krate, RawAttrs::new(db.upcast(), owner, self.hygiene.as_ref()))
136122
}
137123

138124
pub(crate) fn cfg_options(&self) -> &CfgOptions {

crates/hir-def/src/find_path.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,7 @@ fn find_local_import_locations(
586586
#[cfg(test)]
587587
mod tests {
588588
use base_db::fixture::WithFixture;
589-
use hir_expand::SpanMap;
589+
use hir_expand::db::ExpandDatabase;
590590
use syntax::ast::AstNode;
591591

592592
use crate::test_db::TestDB;
@@ -608,7 +608,8 @@ mod tests {
608608
let parsed_path_file = syntax::SourceFile::parse(&format!("use {path};"));
609609
let ast_path =
610610
parsed_path_file.syntax_node().descendants().find_map(syntax::ast::Path::cast).unwrap();
611-
let mod_path = ModPath::from_src(&db, ast_path, &SpanMap::default()).unwrap();
611+
let mod_path =
612+
ModPath::from_src(&db, ast_path, db.span_map(pos.file_id.into()).as_ref()).unwrap();
612613

613614
let def_map = module.def_map(&db);
614615
let resolved = def_map

crates/hir-def/src/item_scope.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ pub struct ItemScope {
112112
#[derive(Debug, PartialEq, Eq)]
113113
struct DeriveMacroInvocation {
114114
attr_id: AttrId,
115+
/// The `#[derive]` call
115116
attr_call_id: MacroCallId,
116117
derive_call_ids: SmallVec<[Option<MacroCallId>; 1]>,
117118
}
@@ -401,6 +402,14 @@ impl ItemScope {
401402
})
402403
}
403404

405+
pub fn derive_macro_invoc(
406+
&self,
407+
ast_id: AstId<ast::Adt>,
408+
attr_id: AttrId,
409+
) -> Option<MacroCallId> {
410+
Some(self.derive_macros.get(&ast_id)?.iter().find(|it| it.attr_id == attr_id)?.attr_call_id)
411+
}
412+
404413
// FIXME: This is only used in collection, we should move the relevant parts of it out of ItemScope
405414
pub(crate) fn unnamed_trait_vis(&self, tr: TraitId) -> Option<Visibility> {
406415
self.unnamed_trait_imports.get(&tr).copied().map(|(a, _)| a)

crates/hir-def/src/item_tree.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ use std::{
4343
};
4444

4545
use ast::{AstNode, HasName, StructKind};
46-
use base_db::{
47-
span::{SpanAnchor, SyntaxContextId, ROOT_ERASED_FILE_AST_ID},
48-
CrateId,
49-
};
46+
use base_db::{span::SyntaxContextId, CrateId};
5047
use either::Either;
5148
use hir_expand::{
5249
ast_id_map::{AstIdNode, FileAstId},
@@ -121,7 +118,7 @@ impl ItemTree {
121118
let mut item_tree = match_ast! {
122119
match syntax {
123120
ast::SourceFile(file) => {
124-
top_attrs = Some(RawAttrs::new(db.upcast(), SpanAnchor { file_id, ast_id: ROOT_ERASED_FILE_AST_ID }, &file, ctx.span_map()));
121+
top_attrs = Some(RawAttrs::new(db.upcast(), &file, ctx.span_map()));
125122
ctx.lower_module_items(&file)
126123
},
127124
ast::MacroItems(items) => {
@@ -780,8 +777,8 @@ impl Use {
780777
let ast = InFile::new(file_id, self.ast_id).to_node(db.upcast());
781778
let ast_use_tree = ast.use_tree().expect("missing `use_tree`");
782779
let hygiene = db.span_map(file_id);
783-
let (_, source_map) =
784-
lower::lower_use_tree(db, &hygiene, ast_use_tree).expect("failed to lower use tree");
780+
let (_, source_map) = lower::lower_use_tree(db, hygiene.as_ref(), ast_use_tree)
781+
.expect("failed to lower use tree");
785782
source_map[index].clone()
786783
}
787784
/// Maps a `UseTree` contained in this import back to its AST node.
@@ -795,7 +792,9 @@ impl Use {
795792
let ast = InFile::new(file_id, self.ast_id).to_node(db.upcast());
796793
let ast_use_tree = ast.use_tree().expect("missing `use_tree`");
797794
let hygiene = db.span_map(file_id);
798-
lower::lower_use_tree(db, &hygiene, ast_use_tree).expect("failed to lower use tree").1
795+
lower::lower_use_tree(db, hygiene.as_ref(), ast_use_tree)
796+
.expect("failed to lower use tree")
797+
.1
799798
}
800799
}
801800

0 commit comments

Comments
 (0)