4234: Support local_inner_macros r=jonas-schievink a=edwin0cheng

This PR implements `#[macro_export(local_inner_macros)]` support. 

Note that the rustc implementation is quite [hacky][1] too. :)

[1]: 614f273e93/src/librustc_resolve/macros.rs (L456)

Co-authored-by: Edwin Cheng <edwin0cheng@gmail.com>
This commit is contained in:
bors[bot]
2020-05-02 10:30:49 +00:00
committed by GitHub
13 changed files with 139 additions and 17 deletions

View File

@@ -38,7 +38,7 @@ macro_rules! register_builtin {
_ => return None,
};
Some(MacroDefId { krate: None, ast_id: None, kind: MacroDefKind::BuiltInDerive(kind) })
Some(MacroDefId { krate: None, ast_id: None, kind: MacroDefKind::BuiltInDerive(kind), local_inner: false })
}
};
}

View File

@@ -73,11 +73,13 @@ pub fn find_builtin_macro(
krate: Some(krate),
ast_id: Some(ast_id),
kind: MacroDefKind::BuiltIn(kind),
local_inner: false,
}),
Either::Right(kind) => Some(MacroDefId {
krate: Some(krate),
ast_id: Some(ast_id),
kind: MacroDefKind::BuiltInEager(kind),
local_inner: false,
}),
}
}
@@ -406,6 +408,7 @@ mod tests {
krate: Some(CrateId(0)),
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))),
kind: MacroDefKind::BuiltIn(expander),
local_inner: false,
};
let loc = MacroCallLoc {
@@ -425,6 +428,7 @@ mod tests {
krate: Some(CrateId(0)),
ast_id: Some(AstId::new(file_id.into(), ast_id_map.ast_id(&macro_calls[0]))),
kind: MacroDefKind::BuiltInEager(expander),
local_inner: false,
};
let args = macro_calls[1].token_tree().unwrap();

View File

@@ -16,31 +16,34 @@ use crate::{
pub struct Hygiene {
// This is what `$crate` expands to
def_crate: Option<CrateId>,
// Indiciate this is a local inner macro
local_inner: bool,
}
impl Hygiene {
pub fn new(db: &dyn AstDatabase, file_id: HirFileId) -> Hygiene {
let def_crate = match file_id.0 {
HirFileIdRepr::FileId(_) => None,
let (def_crate, local_inner) = match file_id.0 {
HirFileIdRepr::FileId(_) => (None, false),
HirFileIdRepr::MacroFile(macro_file) => match macro_file.macro_call_id {
MacroCallId::LazyMacro(id) => {
let loc = db.lookup_intern_macro(id);
match loc.def.kind {
MacroDefKind::Declarative => loc.def.krate,
MacroDefKind::BuiltIn(_) => None,
MacroDefKind::BuiltInDerive(_) => None,
MacroDefKind::BuiltInEager(_) => None,
MacroDefKind::CustomDerive(_) => None,
MacroDefKind::Declarative => (loc.def.krate, loc.def.local_inner),
MacroDefKind::BuiltIn(_) => (None, false),
MacroDefKind::BuiltInDerive(_) => (None, false),
MacroDefKind::BuiltInEager(_) => (None, false),
MacroDefKind::CustomDerive(_) => (None, false),
}
}
MacroCallId::EagerMacro(_id) => None,
MacroCallId::EagerMacro(_id) => (None, false),
},
};
Hygiene { def_crate }
Hygiene { def_crate, local_inner }
}
pub fn new_unhygienic() -> Hygiene {
Hygiene { def_crate: None }
Hygiene { def_crate: None, local_inner: false }
}
// FIXME: this should just return name
@@ -52,4 +55,12 @@ impl Hygiene {
}
Either::Left(name_ref.as_name())
}
pub fn local_inner_macros(&self) -> Option<CrateId> {
if self.local_inner {
self.def_crate
} else {
None
}
}
}

View File

@@ -204,6 +204,8 @@ pub struct MacroDefId {
pub krate: Option<CrateId>,
pub ast_id: Option<AstId<ast::MacroCall>>,
pub kind: MacroDefKind,
pub local_inner: bool,
}
impl MacroDefId {