Skip to content

Commit 32157d4

Browse files
bors[bot]matklad
andauthored
Merge #4843
4843: Don't guess macro expansion crate r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents f320c38 + fac7b0e commit 32157d4

File tree

17 files changed

+116
-77
lines changed

17 files changed

+116
-77
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/ra_db/src/input.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ use std::{
1515

1616
use ra_cfg::CfgOptions;
1717
use ra_syntax::SmolStr;
18-
use rustc_hash::FxHashMap;
19-
use rustc_hash::FxHashSet;
18+
use ra_tt::TokenExpander;
19+
use rustc_hash::{FxHashMap, FxHashSet};
2020

2121
use crate::{RelativePath, RelativePathBuf};
22-
use fmt::Display;
23-
use ra_tt::TokenExpander;
2422

2523
/// `FileId` is an integer which uniquely identifies a file. File paths are
2624
/// messy and system-dependent, so most of the code should work directly with
@@ -111,7 +109,7 @@ impl CrateName {
111109
}
112110
}
113111

114-
impl Display for CrateName {
112+
impl fmt::Display for CrateName {
115113
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116114
write!(f, "{}", self.0)
117115
}

crates/ra_db/src/lib.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{panic, sync::Arc};
77

88
use ra_prof::profile;
99
use ra_syntax::{ast, Parse, SourceFile, TextRange, TextSize};
10+
use rustc_hash::FxHashSet;
1011

1112
pub use crate::{
1213
cancellation::Canceled,
@@ -95,7 +96,7 @@ pub trait FileLoader {
9596
/// `struct StrPath(str)` for clarity some day, but it's a bit messy, so we
9697
/// get by with a `&str` for the time being.
9798
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId>;
98-
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>>;
99+
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>>;
99100
}
100101

101102
/// Database which stores all significant input facts: source code and project
@@ -133,16 +134,21 @@ pub trait SourceDatabaseExt: SourceDatabase {
133134
#[salsa::input]
134135
fn source_root(&self, id: SourceRootId) -> Arc<SourceRoot>;
135136

136-
fn source_root_crates(&self, id: SourceRootId) -> Arc<Vec<CrateId>>;
137+
fn source_root_crates(&self, id: SourceRootId) -> Arc<FxHashSet<CrateId>>;
137138
}
138139

139140
fn source_root_crates(
140141
db: &(impl SourceDatabaseExt + SourceDatabase),
141142
id: SourceRootId,
142-
) -> Arc<Vec<CrateId>> {
143-
let root = db.source_root(id);
143+
) -> Arc<FxHashSet<CrateId>> {
144144
let graph = db.crate_graph();
145-
let res = root.walk().filter_map(|it| graph.crate_id_for_crate_root(it)).collect::<Vec<_>>();
145+
let res = graph
146+
.iter()
147+
.filter(|&krate| {
148+
let root_file = graph[krate].root_file_id;
149+
db.file_source_root(root_file) == id
150+
})
151+
.collect::<FxHashSet<_>>();
146152
Arc::new(res)
147153
}
148154

@@ -156,7 +162,7 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
156162
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
157163
// FIXME: this *somehow* should be platform agnostic...
158164
if std::path::Path::new(path).is_absolute() {
159-
let krate = *self.relevant_crates(anchor).get(0)?;
165+
let krate = *self.relevant_crates(anchor).iter().next()?;
160166
let (extern_source_id, relative_file) =
161167
self.0.crate_graph()[krate].extern_source.extern_path(path.as_ref())?;
162168

@@ -175,7 +181,7 @@ impl<T: SourceDatabaseExt> FileLoader for FileLoaderDelegate<&'_ T> {
175181
}
176182
}
177183

178-
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
184+
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
179185
let source_root = self.0.file_source_root(file_id);
180186
self.0.source_root_crates(source_root)
181187
}

crates/ra_hir/src/semantics.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,9 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
122122
let macro_call =
123123
self.find_file(actual_macro_call.syntax().clone()).with_value(actual_macro_call);
124124
let sa = self.analyze2(macro_call.map(|it| it.syntax()), None);
125+
let krate = sa.resolver.krate()?;
125126
let macro_call_id = macro_call
126-
.as_call_id(self.db, |path| sa.resolver.resolve_path_as_macro(self.db, &path))?;
127+
.as_call_id(self.db, krate, |path| sa.resolver.resolve_path_as_macro(self.db, &path))?;
127128
hir_expand::db::expand_hypothetical(self.db, macro_call_id, hypothetical_args, token_to_map)
128129
}
129130

crates/ra_hir/src/source_analyzer.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ impl SourceAnalyzer {
307307
db: &dyn HirDatabase,
308308
macro_call: InFile<&ast::MacroCall>,
309309
) -> Option<HirFileId> {
310-
let macro_call_id = macro_call.as_call_id(db.upcast(), |path| {
310+
let krate = self.resolver.krate()?;
311+
let macro_call_id = macro_call.as_call_id(db.upcast(), krate, |path| {
311312
self.resolver.resolve_path_as_macro(db.upcast(), &path)
312313
})?;
313314
Some(macro_call_id.as_file())

crates/ra_hir_def/src/body.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ impl Expander {
9797

9898
let macro_call = InFile::new(self.current_file_id, &macro_call);
9999

100-
if let Some(call_id) = macro_call.as_call_id(db, |path| {
100+
if let Some(call_id) = macro_call.as_call_id(db, self.crate_def_map.krate, |path| {
101101
if let Some(local_scope) = local_scope {
102102
if let Some(def) = path.as_ident().and_then(|n| local_scope.get_legacy_macro(n)) {
103103
return Some(def);

crates/ra_hir_def/src/lib.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ pub trait AsMacroCall {
417417
fn as_call_id(
418418
&self,
419419
db: &dyn db::DefDatabase,
420+
krate: CrateId,
420421
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
421422
) -> Option<MacroCallId>;
422423
}
@@ -425,13 +426,14 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
425426
fn as_call_id(
426427
&self,
427428
db: &dyn db::DefDatabase,
429+
krate: CrateId,
428430
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
429431
) -> Option<MacroCallId> {
430432
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
431433
let h = Hygiene::new(db.upcast(), self.file_id);
432434
let path = path::ModPath::from_src(self.value.path()?, &h)?;
433435

434-
AstIdWithPath::new(ast_id.file_id, ast_id.value, path).as_call_id(db, resolver)
436+
AstIdWithPath::new(ast_id.file_id, ast_id.value, path).as_call_id(db, krate, resolver)
435437
}
436438
}
437439

@@ -452,6 +454,7 @@ impl AsMacroCall for AstIdWithPath<ast::MacroCall> {
452454
fn as_call_id(
453455
&self,
454456
db: &dyn db::DefDatabase,
457+
krate: CrateId,
455458
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
456459
) -> Option<MacroCallId> {
457460
let def: MacroDefId = resolver(self.path.clone())?;
@@ -461,13 +464,13 @@ impl AsMacroCall for AstIdWithPath<ast::MacroCall> {
461464
let hygiene = Hygiene::new(db.upcast(), self.ast_id.file_id);
462465

463466
Some(
464-
expand_eager_macro(db.upcast(), macro_call, def, &|path: ast::Path| {
467+
expand_eager_macro(db.upcast(), krate, macro_call, def, &|path: ast::Path| {
465468
resolver(path::ModPath::from_src(path, &hygiene)?)
466469
})?
467470
.into(),
468471
)
469472
} else {
470-
Some(def.as_lazy_macro(db.upcast(), MacroCallKind::FnLike(self.ast_id)).into())
473+
Some(def.as_lazy_macro(db.upcast(), krate, MacroCallKind::FnLike(self.ast_id)).into())
471474
}
472475
}
473476
}
@@ -476,12 +479,14 @@ impl AsMacroCall for AstIdWithPath<ast::ModuleItem> {
476479
fn as_call_id(
477480
&self,
478481
db: &dyn db::DefDatabase,
482+
krate: CrateId,
479483
resolver: impl Fn(path::ModPath) -> Option<MacroDefId>,
480484
) -> Option<MacroCallId> {
481485
let def = resolver(self.path.clone())?;
482486
Some(
483487
def.as_lazy_macro(
484488
db.upcast(),
489+
krate,
485490
MacroCallKind::Attr(self.ast_id, self.path.segments.last()?.to_string()),
486491
)
487492
.into(),

crates/ra_hir_def/src/nameres/collector.rs

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -571,16 +571,18 @@ impl DefCollector<'_> {
571571
return false;
572572
}
573573

574-
if let Some(call_id) = directive.ast_id.as_call_id(self.db, |path| {
575-
let resolved_res = self.def_map.resolve_path_fp_with_macro(
576-
self.db,
577-
ResolveMode::Other,
578-
directive.module_id,
579-
&path,
580-
BuiltinShadowMode::Module,
581-
);
582-
resolved_res.resolved_def.take_macros()
583-
}) {
574+
if let Some(call_id) =
575+
directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| {
576+
let resolved_res = self.def_map.resolve_path_fp_with_macro(
577+
self.db,
578+
ResolveMode::Other,
579+
directive.module_id,
580+
&path,
581+
BuiltinShadowMode::Module,
582+
);
583+
resolved_res.resolved_def.take_macros()
584+
})
585+
{
584586
resolved.push((directive.module_id, call_id, directive.depth));
585587
res = ReachedFixedPoint::No;
586588
return false;
@@ -589,9 +591,10 @@ impl DefCollector<'_> {
589591
true
590592
});
591593
attribute_macros.retain(|directive| {
592-
if let Some(call_id) = directive
593-
.ast_id
594-
.as_call_id(self.db, |path| self.resolve_attribute_macro(&directive, &path))
594+
if let Some(call_id) =
595+
directive.ast_id.as_call_id(self.db, self.def_map.krate, |path| {
596+
self.resolve_attribute_macro(&directive, &path)
597+
})
595598
{
596599
resolved.push((directive.module_id, call_id, 0));
597600
res = ReachedFixedPoint::No;
@@ -957,11 +960,13 @@ impl ModCollector<'_, '_> {
957960
}
958961

959962
// Case 2: try to resolve in legacy scope and expand macro_rules
960-
if let Some(macro_call_id) = ast_id.as_call_id(self.def_collector.db, |path| {
961-
path.as_ident().and_then(|name| {
962-
self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name)
963+
if let Some(macro_call_id) =
964+
ast_id.as_call_id(self.def_collector.db, self.def_collector.def_map.krate, |path| {
965+
path.as_ident().and_then(|name| {
966+
self.def_collector.def_map[self.module_id].scope.get_legacy_macro(&name)
967+
})
963968
})
964-
}) {
969+
{
965970
self.def_collector.unexpanded_macros.push(MacroDirective {
966971
module_id: self.module_id,
967972
ast_id,

crates/ra_hir_def/src/test_db.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77

88
use hir_expand::db::AstDatabase;
99
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, Upcast};
10+
use rustc_hash::FxHashSet;
1011

1112
use crate::db::DefDatabase;
1213

@@ -59,7 +60,7 @@ impl FileLoader for TestDB {
5960
fn resolve_path(&self, anchor: FileId, path: &str) -> Option<FileId> {
6061
FileLoaderDelegate(self).resolve_path(anchor, path)
6162
}
62-
fn relevant_crates(&self, file_id: FileId) -> Arc<Vec<CrateId>> {
63+
fn relevant_crates(&self, file_id: FileId) -> Arc<FxHashSet<CrateId>> {
6364
FileLoaderDelegate(self).relevant_crates(file_id)
6465
}
6566
}

crates/ra_hir_expand/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ doctest = false
1010
[dependencies]
1111
log = "0.4.8"
1212
either = "1.5.3"
13+
rustc-hash = "1.0.0"
1314

1415
ra_arena = { path = "../ra_arena" }
1516
ra_db = { path = "../ra_db" }

0 commit comments

Comments
 (0)