mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Switch AstDatabase::exapnd_proc_macro to ExpandResult
This commit is contained in:
parent
269de9abe3
commit
82728eb757
3 changed files with 18 additions and 35 deletions
|
@ -1,5 +1,6 @@
|
||||||
//! Builtin attributes.
|
//! Builtin attributes.
|
||||||
|
|
||||||
|
use mbe::ExpandResult;
|
||||||
use syntax::ast;
|
use syntax::ast;
|
||||||
|
|
||||||
use crate::{db::AstDatabase, name, AstId, CrateId, MacroCallId, MacroDefId, MacroDefKind};
|
use crate::{db::AstDatabase, name, AstId, CrateId, MacroCallId, MacroDefId, MacroDefKind};
|
||||||
|
@ -18,7 +19,7 @@ macro_rules! register_builtin {
|
||||||
id: MacroCallId,
|
id: MacroCallId,
|
||||||
tt: &tt::Subtree,
|
tt: &tt::Subtree,
|
||||||
item: &tt::Subtree,
|
item: &tt::Subtree,
|
||||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
let expander = match *self {
|
let expander = match *self {
|
||||||
$( BuiltinAttrExpander::$variant => $expand, )*
|
$( BuiltinAttrExpander::$variant => $expand, )*
|
||||||
};
|
};
|
||||||
|
@ -64,6 +65,6 @@ fn dummy_attr_expand(
|
||||||
_id: MacroCallId,
|
_id: MacroCallId,
|
||||||
_tt: &tt::Subtree,
|
_tt: &tt::Subtree,
|
||||||
item: &tt::Subtree,
|
item: &tt::Subtree,
|
||||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
Ok(item.clone())
|
ExpandResult::ok(item.clone())
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,19 +53,16 @@ impl TokenExpander {
|
||||||
TokenExpander::MacroRules { mac, .. } => mac.expand(tt),
|
TokenExpander::MacroRules { mac, .. } => mac.expand(tt),
|
||||||
TokenExpander::MacroDef { mac, .. } => mac.expand(tt),
|
TokenExpander::MacroDef { mac, .. } => mac.expand(tt),
|
||||||
TokenExpander::Builtin(it) => it.expand(db, id, tt),
|
TokenExpander::Builtin(it) => it.expand(db, id, tt),
|
||||||
// FIXME switch these to ExpandResult as well
|
|
||||||
TokenExpander::BuiltinAttr(it) => match db.macro_arg(id) {
|
TokenExpander::BuiltinAttr(it) => match db.macro_arg(id) {
|
||||||
Some(macro_arg) => it.expand(db, id, tt, ¯o_arg.0).into(),
|
Some(macro_arg) => it.expand(db, id, tt, ¯o_arg.0),
|
||||||
None => mbe::ExpandResult::only_err(
|
None => mbe::ExpandResult::str_err("No item argument for attribute".to_string()),
|
||||||
mbe::ExpandError::Other("No item argument for attribute".to_string()).into(),
|
|
||||||
),
|
|
||||||
},
|
},
|
||||||
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt),
|
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt),
|
||||||
TokenExpander::ProcMacro(_) => {
|
TokenExpander::ProcMacro(_) => {
|
||||||
// We store the result in salsa db to prevent non-deterministic behavior in
|
// We store the result in salsa db to prevent non-deterministic behavior in
|
||||||
// some proc-macro implementation
|
// some proc-macro implementation
|
||||||
// See #4315 for details
|
// See #4315 for details
|
||||||
db.expand_proc_macro(id).into()
|
db.expand_proc_macro(id)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -133,7 +130,7 @@ pub trait AstDatabase: SourceDatabase {
|
||||||
/// proc macros, since they are not deterministic in general, and
|
/// proc macros, since they are not deterministic in general, and
|
||||||
/// non-determinism breaks salsa in a very, very, very bad way. @edwin0cheng
|
/// non-determinism breaks salsa in a very, very, very bad way. @edwin0cheng
|
||||||
/// heroically debugged this once!
|
/// heroically debugged this once!
|
||||||
fn expand_proc_macro(&self, call: MacroCallId) -> Result<tt::Subtree, mbe::ExpandError>;
|
fn expand_proc_macro(&self, call: MacroCallId) -> ExpandResult<tt::Subtree>;
|
||||||
/// Firewall query that returns the error from the `macro_expand` query.
|
/// Firewall query that returns the error from the `macro_expand` query.
|
||||||
fn macro_expand_error(&self, macro_call: MacroCallId) -> Option<ExpandError>;
|
fn macro_expand_error(&self, macro_call: MacroCallId) -> Option<ExpandError>;
|
||||||
|
|
||||||
|
@ -379,18 +376,11 @@ fn macro_expand_error(db: &dyn AstDatabase, macro_call: MacroCallId) -> Option<E
|
||||||
db.macro_expand(macro_call).err
|
db.macro_expand(macro_call).err
|
||||||
}
|
}
|
||||||
|
|
||||||
fn expand_proc_macro(
|
fn expand_proc_macro(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<tt::Subtree> {
|
||||||
db: &dyn AstDatabase,
|
|
||||||
id: MacroCallId,
|
|
||||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
||||||
let loc: MacroCallLoc = db.lookup_intern_macro(id);
|
let loc: MacroCallLoc = db.lookup_intern_macro(id);
|
||||||
let macro_arg = match db.macro_arg(id) {
|
let macro_arg = match db.macro_arg(id) {
|
||||||
Some(it) => it,
|
Some(it) => it,
|
||||||
None => {
|
None => return ExpandResult::str_err("No arguments for proc-macro".to_string()),
|
||||||
return Err(
|
|
||||||
tt::ExpansionError::Unknown("No arguments for proc-macro".to_string()).into()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let expander = match loc.def.kind {
|
let expander = match loc.def.kind {
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use crate::db::AstDatabase;
|
use crate::db::AstDatabase;
|
||||||
use base_db::{CrateId, ProcMacroId};
|
use base_db::{CrateId, ProcMacroId};
|
||||||
|
use mbe::ExpandResult;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
|
||||||
pub struct ProcMacroExpander {
|
pub struct ProcMacroExpander {
|
||||||
|
@ -9,15 +10,6 @@ pub struct ProcMacroExpander {
|
||||||
proc_macro_id: Option<ProcMacroId>,
|
proc_macro_id: Option<ProcMacroId>,
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! err {
|
|
||||||
($fmt:literal, $($tt:tt),*) => {
|
|
||||||
mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown(format!($fmt, $($tt),*)))
|
|
||||||
};
|
|
||||||
($fmt:literal) => {
|
|
||||||
mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown($fmt.to_string()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ProcMacroExpander {
|
impl ProcMacroExpander {
|
||||||
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> Self {
|
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> Self {
|
||||||
Self { krate, proc_macro_id: Some(proc_macro_id) }
|
Self { krate, proc_macro_id: Some(proc_macro_id) }
|
||||||
|
@ -38,21 +30,21 @@ impl ProcMacroExpander {
|
||||||
calling_crate: CrateId,
|
calling_crate: CrateId,
|
||||||
tt: &tt::Subtree,
|
tt: &tt::Subtree,
|
||||||
attr_arg: Option<&tt::Subtree>,
|
attr_arg: Option<&tt::Subtree>,
|
||||||
) -> Result<tt::Subtree, mbe::ExpandError> {
|
) -> ExpandResult<tt::Subtree> {
|
||||||
match self.proc_macro_id {
|
match self.proc_macro_id {
|
||||||
Some(id) => {
|
Some(id) => {
|
||||||
let krate_graph = db.crate_graph();
|
let krate_graph = db.crate_graph();
|
||||||
let proc_macro = krate_graph[self.krate]
|
let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) {
|
||||||
.proc_macro
|
Some(proc_macro) => proc_macro,
|
||||||
.get(id.0 as usize)
|
None => return ExpandResult::str_err("No derive macro found.".to_string()),
|
||||||
.ok_or_else(|| err!("No derive macro found."))?;
|
};
|
||||||
|
|
||||||
// Proc macros have access to the environment variables of the invoking crate.
|
// Proc macros have access to the environment variables of the invoking crate.
|
||||||
let env = &krate_graph[calling_crate].env;
|
let env = &krate_graph[calling_crate].env;
|
||||||
|
|
||||||
proc_macro.expander.expand(tt, attr_arg, env).map_err(mbe::ExpandError::from)
|
proc_macro.expander.expand(tt, attr_arg, env).map_err(mbe::ExpandError::from).into()
|
||||||
}
|
}
|
||||||
None => Err(mbe::ExpandError::UnresolvedProcMacro),
|
None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue