Auto merge of #14056 - Veykril:allow_internal_unsafe, r=Veykril

Parse macros `allow_internal_unsafe` attribute

We don't use it for anything yet but it might become part of hygiene
This commit is contained in:
bors 2023-01-31 10:50:47 +00:00
commit b4832b3d00
4 changed files with 20 additions and 5 deletions

View file

@ -290,6 +290,7 @@ pub struct Macro2Loc {
pub container: ModuleId, pub container: ModuleId,
pub id: ItemTreeId<MacroDef>, pub id: ItemTreeId<MacroDef>,
pub expander: MacroExpander, pub expander: MacroExpander,
pub allow_internal_unsafe: bool,
} }
impl_intern!(Macro2Id, Macro2Loc, intern_macro2, lookup_intern_macro2); impl_intern!(Macro2Id, Macro2Loc, intern_macro2, lookup_intern_macro2);
@ -299,8 +300,9 @@ pub struct MacroRulesId(salsa::InternId);
pub struct MacroRulesLoc { pub struct MacroRulesLoc {
pub container: ModuleId, pub container: ModuleId,
pub id: ItemTreeId<MacroRules>, pub id: ItemTreeId<MacroRules>,
pub local_inner: bool,
pub expander: MacroExpander, pub expander: MacroExpander,
pub allow_internal_unsafe: bool,
pub local_inner: bool,
} }
impl_intern!(MacroRulesId, MacroRulesLoc, intern_macro_rules, lookup_intern_macro_rules); impl_intern!(MacroRulesId, MacroRulesLoc, intern_macro_rules, lookup_intern_macro_rules);
@ -894,6 +896,7 @@ pub fn macro_id_to_def_id(db: &dyn db::DefDatabase, id: MacroId) -> MacroDefId {
} }
}, },
local_inner: false, local_inner: false,
allow_internal_unsafe: loc.allow_internal_unsafe,
} }
} }
MacroId::MacroRulesId(it) => { MacroId::MacroRulesId(it) => {
@ -918,6 +921,7 @@ pub fn macro_id_to_def_id(db: &dyn db::DefDatabase, id: MacroId) -> MacroDefId {
} }
}, },
local_inner: loc.local_inner, local_inner: loc.local_inner,
allow_internal_unsafe: loc.allow_internal_unsafe,
} }
} }
MacroId::ProcMacroId(it) => { MacroId::ProcMacroId(it) => {
@ -933,6 +937,7 @@ pub fn macro_id_to_def_id(db: &dyn db::DefDatabase, id: MacroId) -> MacroDefId {
InFile::new(loc.id.file_id(), makro.ast_id), InFile::new(loc.id.file_id(), makro.ast_id),
), ),
local_inner: false, local_inner: false,
allow_internal_unsafe: false,
} }
} }
} }

View file

@ -97,7 +97,9 @@ pub fn identity_when_valid(_attr: TokenStream, item: TokenStream) -> TokenStream
let ast_id = AstId::new(source.file_id, file_ast_id.upcast()); let ast_id = AstId::new(source.file_id, file_ast_id.upcast());
let kind = MacroDefKind::Declarative(ast_id); let kind = MacroDefKind::Declarative(ast_id);
let macro_def = db.macro_def(MacroDefId { krate, kind, local_inner: false }).unwrap(); let macro_def = db
.macro_def(MacroDefId { krate, kind, local_inner: false, allow_internal_unsafe: false })
.unwrap();
if let TokenExpander::DeclarativeMacro { mac, def_site_token_map } = &*macro_def { if let TokenExpander::DeclarativeMacro { mac, def_site_token_map } = &*macro_def {
let tt = match &macro_ { let tt = match &macro_ {
ast::Macro::MacroRules(mac) => mac.token_tree().unwrap(), ast::Macro::MacroRules(mac) => mac.token_tree().unwrap(),

View file

@ -1982,11 +1982,13 @@ impl ModCollector<'_, '_> {
// Case 2: normal `macro_rules!` macro // Case 2: normal `macro_rules!` macro
MacroExpander::Declarative MacroExpander::Declarative
}; };
let allow_internal_unsafe = attrs.by_key("allow_internal_unsafe").exists();
let macro_id = MacroRulesLoc { let macro_id = MacroRulesLoc {
container: module, container: module,
id: ItemTreeId::new(self.tree_id, id), id: ItemTreeId::new(self.tree_id, id),
local_inner, local_inner,
allow_internal_unsafe,
expander, expander,
} }
.intern(self.def_collector.db); .intern(self.def_collector.db);
@ -2046,9 +2048,14 @@ impl ModCollector<'_, '_> {
// Case 2: normal `macro` // Case 2: normal `macro`
MacroExpander::Declarative MacroExpander::Declarative
}; };
let allow_internal_unsafe = attrs.by_key("allow_internal_unsafe").exists();
let macro_id = let macro_id = Macro2Loc {
Macro2Loc { container: module, id: ItemTreeId::new(self.tree_id, id), expander } container: module,
id: ItemTreeId::new(self.tree_id, id),
expander,
allow_internal_unsafe,
}
.intern(self.def_collector.db); .intern(self.def_collector.db);
self.def_collector.define_macro_def( self.def_collector.define_macro_def(
self.module_id, self.module_id,

View file

@ -116,6 +116,7 @@ pub struct MacroDefId {
pub krate: CrateId, pub krate: CrateId,
pub kind: MacroDefKind, pub kind: MacroDefKind,
pub local_inner: bool, pub local_inner: bool,
pub allow_internal_unsafe: bool,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]