Auto merge of #17936 - Veykril:module_path, r=Veykril

feat: Implement `module_path` macro

Turns out this is a pain to implement because of our hir-def hir-expand split :)
This commit is contained in:
bors 2024-08-23 09:32:27 +00:00
commit 33c1f57a1a
20 changed files with 278 additions and 71 deletions

View file

@ -1,6 +1,6 @@
//! Builtin macros and attributes
#[macro_use]
mod quote;
pub(crate) mod quote;
mod attr_macro;
mod derive_macro;

View file

@ -116,7 +116,6 @@ register_builtin! {
(column, Column) => line_expand,
(file, File) => file_expand,
(line, Line) => line_expand,
(module_path, ModulePath) => module_path_expand,
(assert, Assert) => assert_expand,
(stringify, Stringify) => stringify_expand,
(llvm_asm, LlvmAsm) => asm_expand,
@ -142,7 +141,10 @@ register_builtin! {
(include_bytes, IncludeBytes) => include_bytes_expand,
(include_str, IncludeStr) => include_str_expand,
(env, Env) => env_expand,
(option_env, OptionEnv) => option_env_expand
(option_env, OptionEnv) => option_env_expand,
// This isn't really eager, we have no inputs, but we abuse the fact how eager macros are
// handled in r-a to be able to thread the module path through.
(module_path, ModulePath) => module_path_expand
}
fn mk_pound(span: Span) -> tt::Subtree {
@ -157,18 +159,6 @@ fn mk_pound(span: Span) -> tt::Subtree {
)
}
fn module_path_expand(
_db: &dyn ExpandDatabase,
_id: MacroCallId,
_tt: &tt::Subtree,
span: Span,
) -> ExpandResult<tt::Subtree> {
// Just return a dummy result.
ExpandResult::ok(quote! {span =>
"module::path"
})
}
fn line_expand(
_db: &dyn ExpandDatabase,
_id: MacroCallId,
@ -904,6 +894,18 @@ fn option_env_expand(
ExpandResult::ok(expanded)
}
fn module_path_expand(
_db: &dyn ExpandDatabase,
_id: MacroCallId,
tt: &tt::Subtree,
span: Span,
) -> ExpandResult<tt::Subtree> {
// Note: The actual implementation of this is in crates\hir-expand\src\eager.rs
ExpandResult::ok(quote! {span =>
#tt
})
}
fn quote_expand(
_db: &dyn ExpandDatabase,
_arg_id: MacroCallId,

View file

@ -128,7 +128,7 @@ macro_rules! quote_impl__ {
}
};
}
pub(super) use quote_impl__ as __quote;
pub(crate) use quote_impl__ as __quote;
/// FIXME:
/// It probably should implement in proc-macro
@ -137,7 +137,7 @@ macro_rules! quote_impl {
$crate::builtin::quote::IntoTt::to_subtree($crate::builtin::quote::__quote!($span $($tt)*), $span)
}
}
pub(super) use quote_impl as quote;
pub(crate) use quote_impl as quote;
pub(crate) trait IntoTt {
fn to_subtree(self, span: Span) -> crate::tt::Subtree;

View file

@ -32,6 +32,51 @@ use crate::{
MacroCallId, MacroCallKind, MacroCallLoc, MacroDefId, MacroDefKind,
};
pub fn expand_module_path_as_eager(
db: &dyn ExpandDatabase,
krate: CrateId,
mod_path: String,
macro_call: &ast::MacroCall,
ast_id: AstId<ast::MacroCall>,
def: MacroDefId,
call_site: SyntaxContextId,
) -> ExpandResult<Option<MacroCallId>> {
let expand_to = ExpandTo::from_call_site(macro_call);
// Note:
// When `lazy_expand` is called, its *parent* file must already exist.
// Here we store an eager macro id for the argument expanded subtree
// for that purpose.
let arg_id = MacroCallLoc {
def,
krate,
kind: MacroCallKind::FnLike { ast_id, expand_to: ExpandTo::Expr, eager: None },
ctxt: call_site,
}
.intern(db);
#[allow(deprecated)] // builtin eager macros are never derives
let (_, _, span) = db.macro_arg(arg_id);
let subtree = crate::builtin::quote::quote! {span => #mod_path};
let loc = MacroCallLoc {
def,
krate,
kind: MacroCallKind::FnLike {
ast_id,
expand_to,
eager: Some(Arc::new(EagerCallInfo {
arg: Arc::new(subtree),
arg_id,
error: None,
span,
})),
},
ctxt: call_site,
};
ExpandResult { value: Some(loc.intern(db)), err: None }
}
pub fn expand_eager_macro_input(
db: &dyn ExpandDatabase,
krate: CrateId,