Skip to content

Commit f0bfd43

Browse files
committed
Auto merge of rust-lang#17972 - rust-lang:revert-17936-module_path, r=Veykril
Revert "feat: Implement `module_path` macro" Reverts rust-lang/rust-analyzer#17936 Fixes rust-lang/rust-analyzer#17968
2 parents 05e6fb6 + 65d25fe commit f0bfd43

File tree

20 files changed

+71
-278
lines changed

20 files changed

+71
-278
lines changed

src/tools/rust-analyzer/crates/base-db/src/input.rs

-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,6 @@ impl ReleaseChannel {
272272
}
273273
}
274274

275-
#[non_exhaustive]
276275
#[derive(Debug, Clone, PartialEq, Eq)]
277276
pub struct CrateData {
278277
pub root_file_id: FileId,

src/tools/rust-analyzer/crates/hir-def/src/data.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -748,9 +748,8 @@ impl<'a> AssocItemCollector<'a> {
748748
&AstIdWithPath::new(file_id, ast_id, Clone::clone(path)),
749749
ctxt,
750750
expand_to,
751-
self.expander.module,
751+
self.expander.krate(),
752752
resolver,
753-
|module| module.def_map(self.db).path_for_module(self.db, module),
754753
) {
755754
Ok(Some(call_id)) => {
756755
let res =

src/tools/rust-analyzer/crates/hir-def/src/expander.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,9 @@ impl Expander {
6969

7070
let result = self.within_limit(db, |this| {
7171
let macro_call = this.in_file(&macro_call);
72-
match macro_call.as_call_id(
73-
db.upcast(),
74-
this.module,
75-
|path| resolver(path).map(|it| db.macro_def(it)),
76-
|module| this.module.def_map(db).path_for_module(db, module),
77-
) {
72+
match macro_call.as_call_id_with_errors(db.upcast(), this.module.krate(), |path| {
73+
resolver(path).map(|it| db.macro_def(it))
74+
}) {
7875
Ok(call_id) => call_id,
7976
Err(resolve_err) => {
8077
unresolved_macro_err = Some(resolve_err);

src/tools/rust-analyzer/crates/hir-def/src/lib.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ use base_db::{
7777
use hir_expand::{
7878
builtin::{BuiltinAttrExpander, BuiltinDeriveExpander, BuiltinFnLikeExpander, EagerExpander},
7979
db::ExpandDatabase,
80-
eager::{expand_eager_macro_input, expand_module_path_as_eager},
80+
eager::expand_eager_macro_input,
8181
impl_intern_lookup,
8282
name::Name,
8383
proc_macro::{CustomProcMacroExpander, ProcMacroKind},
@@ -1400,19 +1400,26 @@ pub trait AsMacroCall {
14001400
fn as_call_id(
14011401
&self,
14021402
db: &dyn ExpandDatabase,
1403-
module: ModuleId,
1403+
krate: CrateId,
1404+
resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
1405+
) -> Option<MacroCallId> {
1406+
self.as_call_id_with_errors(db, krate, resolver).ok()?.value
1407+
}
1408+
1409+
fn as_call_id_with_errors(
1410+
&self,
1411+
db: &dyn ExpandDatabase,
1412+
krate: CrateId,
14041413
resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
1405-
mod_path: impl FnOnce(ModuleId) -> String,
14061414
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro>;
14071415
}
14081416

14091417
impl AsMacroCall for InFile<&ast::MacroCall> {
1410-
fn as_call_id(
1418+
fn as_call_id_with_errors(
14111419
&self,
14121420
db: &dyn ExpandDatabase,
1413-
module: ModuleId,
1421+
krate: CrateId,
14141422
resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
1415-
mod_path: impl FnOnce(ModuleId) -> String,
14161423
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
14171424
let expands_to = hir_expand::ExpandTo::from_call_site(self.value);
14181425
let ast_id = AstId::new(self.file_id, db.ast_id_map(self.file_id).ast_id(self.value));
@@ -1439,10 +1446,9 @@ impl AsMacroCall for InFile<&ast::MacroCall> {
14391446
&path,
14401447
call_site.ctx,
14411448
expands_to,
1442-
module,
1449+
krate,
14431450
resolver,
14441451
resolver,
1445-
mod_path,
14461452
)
14471453
}
14481454
}
@@ -1469,20 +1475,18 @@ fn macro_call_as_call_id(
14691475
call: &AstIdWithPath<ast::MacroCall>,
14701476
call_site: SyntaxContextId,
14711477
expand_to: ExpandTo,
1472-
module: ModuleId,
1478+
krate: CrateId,
14731479
resolver: impl Fn(&path::ModPath) -> Option<MacroDefId> + Copy,
1474-
mod_path: impl FnOnce(ModuleId) -> String,
14751480
) -> Result<Option<MacroCallId>, UnresolvedMacro> {
14761481
macro_call_as_call_id_with_eager(
14771482
db,
14781483
call.ast_id,
14791484
&call.path,
14801485
call_site,
14811486
expand_to,
1482-
module,
1487+
krate,
14831488
resolver,
14841489
resolver,
1485-
mod_path,
14861490
)
14871491
.map(|res| res.value)
14881492
}
@@ -1493,26 +1497,16 @@ fn macro_call_as_call_id_with_eager(
14931497
path: &path::ModPath,
14941498
call_site: SyntaxContextId,
14951499
expand_to: ExpandTo,
1496-
module: ModuleId,
1500+
krate: CrateId,
14971501
resolver: impl FnOnce(&path::ModPath) -> Option<MacroDefId>,
14981502
eager_resolver: impl Fn(&path::ModPath) -> Option<MacroDefId>,
1499-
mod_path: impl FnOnce(ModuleId) -> String,
15001503
) -> Result<ExpandResult<Option<MacroCallId>>, UnresolvedMacro> {
15011504
let def = resolver(path).ok_or_else(|| UnresolvedMacro { path: path.clone() })?;
15021505

15031506
let res = match def.kind {
1504-
MacroDefKind::BuiltInEager(_, EagerExpander::ModulePath) => expand_module_path_as_eager(
1505-
db,
1506-
module.krate,
1507-
mod_path(module),
1508-
&ast_id.to_node(db),
1509-
ast_id,
1510-
def,
1511-
call_site,
1512-
),
15131507
MacroDefKind::BuiltInEager(..) => expand_eager_macro_input(
15141508
db,
1515-
module.krate,
1509+
krate,
15161510
&ast_id.to_node(db),
15171511
ast_id,
15181512
def,
@@ -1522,7 +1516,7 @@ fn macro_call_as_call_id_with_eager(
15221516
_ if def.is_fn_like() => ExpandResult {
15231517
value: Some(def.make_call(
15241518
db,
1525-
module.krate,
1519+
krate,
15261520
MacroCallKind::FnLike { ast_id, expand_to, eager: None },
15271521
call_site,
15281522
)),

src/tools/rust-analyzer/crates/hir-def/src/macro_expansion_tests/mod.rs

+5-10
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,11 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
9595
for macro_call in source_file.syntax().descendants().filter_map(ast::MacroCall::cast) {
9696
let macro_call = InFile::new(source.file_id, &macro_call);
9797
let res = macro_call
98-
.as_call_id(
99-
&db,
100-
resolver.module(),
101-
|path| {
102-
resolver
103-
.resolve_path_as_macro(&db, path, Some(MacroSubNs::Bang))
104-
.map(|(it, _)| db.macro_def(it))
105-
},
106-
|module| def_map.path_for_module(&db, module),
107-
)
98+
.as_call_id_with_errors(&db, krate, |path| {
99+
resolver
100+
.resolve_path_as_macro(&db, path, Some(MacroSubNs::Bang))
101+
.map(|(it, _)| db.macro_def(it))
102+
})
108103
.unwrap();
109104
let macro_call_id = res.value.unwrap();
110105
let macro_file = MacroFileId { macro_call_id };

src/tools/rust-analyzer/crates/hir-def/src/nameres.rs

+1-26
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ use base_db::CrateId;
6363
use hir_expand::{
6464
name::Name, proc_macro::ProcMacroKind, ErasedAstId, HirFileId, InFile, MacroCallId, MacroDefId,
6565
};
66-
use intern::{sym, Symbol};
66+
use intern::Symbol;
6767
use itertools::Itertools;
6868
use la_arena::Arena;
6969
use rustc_hash::{FxHashMap, FxHashSet};
@@ -139,7 +139,6 @@ pub struct DefMap {
139139
/// Data that belongs to a crate which is shared between a crate's def map and all its block def maps.
140140
#[derive(Clone, Debug, PartialEq, Eq)]
141141
struct DefMapCrateData {
142-
crate_name: Option<Symbol>,
143142
/// The extern prelude which contains all root modules of external crates that are in scope.
144143
extern_prelude: FxIndexMap<Name, (CrateRootModuleId, Option<ExternCrateId>)>,
145144

@@ -165,7 +164,6 @@ struct DefMapCrateData {
165164
impl DefMapCrateData {
166165
fn new(edition: Edition) -> Self {
167166
Self {
168-
crate_name: None,
169167
extern_prelude: FxIndexMap::default(),
170168
exported_derives: FxHashMap::default(),
171169
fn_proc_macro_mapping: FxHashMap::default(),
@@ -188,7 +186,6 @@ impl DefMapCrateData {
188186
registered_attrs,
189187
registered_tools,
190188
unstable_features,
191-
crate_name: _,
192189
rustc_coherence_is_core: _,
193190
no_core: _,
194191
no_std: _,
@@ -446,28 +443,6 @@ impl DefMap {
446443
self.modules.iter()
447444
}
448445

449-
pub fn path_for_module(&self, db: &dyn DefDatabase, mut module: ModuleId) -> String {
450-
debug_assert!(module.krate == self.krate && module.block == self.block.map(|b| b.block));
451-
let mut parts = vec![];
452-
if let Some(name) = module.name(db) {
453-
parts.push(name.symbol().clone());
454-
}
455-
while let Some(parent) = module.def_map(db).containing_module(module.local_id) {
456-
module = parent;
457-
if let Some(name) = module.name(db) {
458-
parts.push(name.symbol().clone());
459-
}
460-
if parts.len() > 10 {
461-
break;
462-
}
463-
}
464-
parts.push(match &self.data.crate_name {
465-
Some(name) => name.clone(),
466-
None => sym::crate_.clone(),
467-
});
468-
parts.into_iter().rev().format("::").to_string()
469-
}
470-
471446
pub fn derive_helpers_in_scope(
472447
&self,
473448
id: AstId<ast::Adt>,

src/tools/rust-analyzer/crates/hir-def/src/nameres/collector.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -247,23 +247,18 @@ impl DefCollector<'_> {
247247
let _p = tracing::info_span!("seed_with_top_level").entered();
248248

249249
let crate_graph = self.db.crate_graph();
250-
let crate_data = Arc::get_mut(&mut self.def_map.data).unwrap();
251-
crate_data.crate_name = crate_graph[self.def_map.krate]
252-
.display_name
253-
.as_ref()
254-
.map(|it| it.crate_name().symbol().clone());
255-
256250
let file_id = crate_graph[self.def_map.krate].root_file_id();
257251
let item_tree = self.db.file_item_tree(file_id.into());
258252
let attrs = item_tree.top_level_attrs(self.db, self.def_map.krate);
253+
let crate_data = Arc::get_mut(&mut self.def_map.data).unwrap();
259254

260-
let mut crate_cged_out = false;
255+
let mut process = true;
261256

262257
// Process other crate-level attributes.
263258
for attr in &*attrs {
264259
if let Some(cfg) = attr.cfg() {
265260
if self.cfg_options.check(&cfg) == Some(false) {
266-
crate_cged_out = true;
261+
process = false;
267262
break;
268263
}
269264
}
@@ -277,11 +272,6 @@ impl DefCollector<'_> {
277272
}
278273
}
279274
}
280-
() if *attr_name == sym::crate_name.clone() => {
281-
if let Some(name) = attr.string_value().cloned() {
282-
crate_data.crate_name = Some(name);
283-
}
284-
}
285275
() if *attr_name == sym::crate_type.clone() => {
286276
if attr.string_value() == Some(&sym::proc_dash_macro) {
287277
self.is_proc_macro = true;
@@ -347,7 +337,7 @@ impl DefCollector<'_> {
347337

348338
self.inject_prelude();
349339

350-
if crate_cged_out {
340+
if !process {
351341
return;
352342
}
353343

@@ -1217,9 +1207,8 @@ impl DefCollector<'_> {
12171207
ast_id,
12181208
*call_site,
12191209
*expand_to,
1220-
self.def_map.module_id(directive.module_id),
1210+
self.def_map.krate,
12211211
resolver_def_id,
1222-
|module| self.def_map.path_for_module(self.db, module),
12231212
);
12241213
if let Ok(Some(call_id)) = call_id {
12251214
self.def_map.modules[directive.module_id]
@@ -1497,7 +1486,7 @@ impl DefCollector<'_> {
14971486
ast_id,
14981487
*call_site,
14991488
*expand_to,
1500-
self.def_map.module_id(directive.module_id),
1489+
self.def_map.krate,
15011490
|path| {
15021491
let resolved_res = self.def_map.resolve_path_fp_with_macro(
15031492
self.db,
@@ -1509,7 +1498,6 @@ impl DefCollector<'_> {
15091498
);
15101499
resolved_res.resolved_def.take_macros().map(|it| self.db.macro_def(it))
15111500
},
1512-
|module| self.def_map.path_for_module(self.db, module),
15131501
);
15141502
if let Err(UnresolvedMacro { path }) = macro_call_as_call_id {
15151503
self.def_map.diagnostics.push(DefDiagnostic::unresolved_macro_call(
@@ -2363,7 +2351,7 @@ impl ModCollector<'_, '_> {
23632351
&ast_id.path,
23642352
ctxt,
23652353
expand_to,
2366-
self.def_collector.def_map.module_id(self.module_id),
2354+
self.def_collector.def_map.krate,
23672355
|path| {
23682356
path.as_ident().and_then(|name| {
23692357
let def_map = &self.def_collector.def_map;
@@ -2393,7 +2381,6 @@ impl ModCollector<'_, '_> {
23932381
);
23942382
resolved_res.resolved_def.take_macros().map(|it| db.macro_def(it))
23952383
},
2396-
|module| self.def_collector.def_map.path_for_module(self.def_collector.db, module),
23972384
) {
23982385
// FIXME: if there were errors, this might've been in the eager expansion from an
23992386
// unresolved macro, so we need to push this into late macro resolution. see fixme above

src/tools/rust-analyzer/crates/hir-expand/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Builtin macros and attributes
22
#[macro_use]
3-
pub(crate) mod quote;
3+
mod quote;
44

55
mod attr_macro;
66
mod derive_macro;

src/tools/rust-analyzer/crates/hir-expand/src/builtin/fn_macro.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ register_builtin! {
116116
(column, Column) => line_expand,
117117
(file, File) => file_expand,
118118
(line, Line) => line_expand,
119+
(module_path, ModulePath) => module_path_expand,
119120
(assert, Assert) => assert_expand,
120121
(stringify, Stringify) => stringify_expand,
121122
(llvm_asm, LlvmAsm) => asm_expand,
@@ -141,10 +142,7 @@ register_builtin! {
141142
(include_bytes, IncludeBytes) => include_bytes_expand,
142143
(include_str, IncludeStr) => include_str_expand,
143144
(env, Env) => env_expand,
144-
(option_env, OptionEnv) => option_env_expand,
145-
// This isn't really eager, we have no inputs, but we abuse the fact how eager macros are
146-
// handled in r-a to be able to thread the module path through.
147-
(module_path, ModulePath) => module_path_expand
145+
(option_env, OptionEnv) => option_env_expand
148146
}
149147

150148
fn mk_pound(span: Span) -> tt::Subtree {
@@ -159,6 +157,18 @@ fn mk_pound(span: Span) -> tt::Subtree {
159157
)
160158
}
161159

160+
fn module_path_expand(
161+
_db: &dyn ExpandDatabase,
162+
_id: MacroCallId,
163+
_tt: &tt::Subtree,
164+
span: Span,
165+
) -> ExpandResult<tt::Subtree> {
166+
// Just return a dummy result.
167+
ExpandResult::ok(quote! {span =>
168+
"module::path"
169+
})
170+
}
171+
162172
fn line_expand(
163173
_db: &dyn ExpandDatabase,
164174
_id: MacroCallId,
@@ -894,18 +904,6 @@ fn option_env_expand(
894904
ExpandResult::ok(expanded)
895905
}
896906

897-
fn module_path_expand(
898-
_db: &dyn ExpandDatabase,
899-
_id: MacroCallId,
900-
tt: &tt::Subtree,
901-
span: Span,
902-
) -> ExpandResult<tt::Subtree> {
903-
// Note: The actual implementation of this is in crates\hir-expand\src\eager.rs
904-
ExpandResult::ok(quote! {span =>
905-
#tt
906-
})
907-
}
908-
909907
fn quote_expand(
910908
_db: &dyn ExpandDatabase,
911909
_arg_id: MacroCallId,

0 commit comments

Comments
 (0)