Skip to content

Commit 82728eb

Browse files
committed
Switch AstDatabase::exapnd_proc_macro to ExpandResult
1 parent 269de9a commit 82728eb

File tree

3 files changed

+18
-35
lines changed

3 files changed

+18
-35
lines changed

crates/hir_expand/src/builtin_attr.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Builtin attributes.
22
3+
use mbe::ExpandResult;
34
use syntax::ast;
45

56
use crate::{db::AstDatabase, name, AstId, CrateId, MacroCallId, MacroDefId, MacroDefKind};
@@ -18,7 +19,7 @@ macro_rules! register_builtin {
1819
id: MacroCallId,
1920
tt: &tt::Subtree,
2021
item: &tt::Subtree,
21-
) -> Result<tt::Subtree, mbe::ExpandError> {
22+
) -> ExpandResult<tt::Subtree> {
2223
let expander = match *self {
2324
$( BuiltinAttrExpander::$variant => $expand, )*
2425
};
@@ -64,6 +65,6 @@ fn dummy_attr_expand(
6465
_id: MacroCallId,
6566
_tt: &tt::Subtree,
6667
item: &tt::Subtree,
67-
) -> Result<tt::Subtree, mbe::ExpandError> {
68-
Ok(item.clone())
68+
) -> ExpandResult<tt::Subtree> {
69+
ExpandResult::ok(item.clone())
6970
}

crates/hir_expand/src/db.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,16 @@ impl TokenExpander {
5353
TokenExpander::MacroRules { mac, .. } => mac.expand(tt),
5454
TokenExpander::MacroDef { mac, .. } => mac.expand(tt),
5555
TokenExpander::Builtin(it) => it.expand(db, id, tt),
56-
// FIXME switch these to ExpandResult as well
5756
TokenExpander::BuiltinAttr(it) => match db.macro_arg(id) {
58-
Some(macro_arg) => it.expand(db, id, tt, &macro_arg.0).into(),
59-
None => mbe::ExpandResult::only_err(
60-
mbe::ExpandError::Other("No item argument for attribute".to_string()).into(),
61-
),
57+
Some(macro_arg) => it.expand(db, id, tt, &macro_arg.0),
58+
None => mbe::ExpandResult::str_err("No item argument for attribute".to_string()),
6259
},
6360
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt),
6461
TokenExpander::ProcMacro(_) => {
6562
// We store the result in salsa db to prevent non-deterministic behavior in
6663
// some proc-macro implementation
6764
// See #4315 for details
68-
db.expand_proc_macro(id).into()
65+
db.expand_proc_macro(id)
6966
}
7067
}
7168
}
@@ -133,7 +130,7 @@ pub trait AstDatabase: SourceDatabase {
133130
/// proc macros, since they are not deterministic in general, and
134131
/// non-determinism breaks salsa in a very, very, very bad way. @edwin0cheng
135132
/// heroically debugged this once!
136-
fn expand_proc_macro(&self, call: MacroCallId) -> Result<tt::Subtree, mbe::ExpandError>;
133+
fn expand_proc_macro(&self, call: MacroCallId) -> ExpandResult<tt::Subtree>;
137134
/// Firewall query that returns the error from the `macro_expand` query.
138135
fn macro_expand_error(&self, macro_call: MacroCallId) -> Option<ExpandError>;
139136

@@ -379,18 +376,11 @@ fn macro_expand_error(db: &dyn AstDatabase, macro_call: MacroCallId) -> Option<E
379376
db.macro_expand(macro_call).err
380377
}
381378

382-
fn expand_proc_macro(
383-
db: &dyn AstDatabase,
384-
id: MacroCallId,
385-
) -> Result<tt::Subtree, mbe::ExpandError> {
379+
fn expand_proc_macro(db: &dyn AstDatabase, id: MacroCallId) -> ExpandResult<tt::Subtree> {
386380
let loc: MacroCallLoc = db.lookup_intern_macro(id);
387381
let macro_arg = match db.macro_arg(id) {
388382
Some(it) => it,
389-
None => {
390-
return Err(
391-
tt::ExpansionError::Unknown("No arguments for proc-macro".to_string()).into()
392-
)
393-
}
383+
None => return ExpandResult::str_err("No arguments for proc-macro".to_string()),
394384
};
395385

396386
let expander = match loc.def.kind {

crates/hir_expand/src/proc_macro.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,14 @@
22
33
use crate::db::AstDatabase;
44
use base_db::{CrateId, ProcMacroId};
5+
use mbe::ExpandResult;
56

67
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
78
pub struct ProcMacroExpander {
89
krate: CrateId,
910
proc_macro_id: Option<ProcMacroId>,
1011
}
1112

12-
macro_rules! err {
13-
($fmt:literal, $($tt:tt),*) => {
14-
mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown(format!($fmt, $($tt),*)))
15-
};
16-
($fmt:literal) => {
17-
mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown($fmt.to_string()))
18-
}
19-
}
20-
2113
impl ProcMacroExpander {
2214
pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> Self {
2315
Self { krate, proc_macro_id: Some(proc_macro_id) }
@@ -38,21 +30,21 @@ impl ProcMacroExpander {
3830
calling_crate: CrateId,
3931
tt: &tt::Subtree,
4032
attr_arg: Option<&tt::Subtree>,
41-
) -> Result<tt::Subtree, mbe::ExpandError> {
33+
) -> ExpandResult<tt::Subtree> {
4234
match self.proc_macro_id {
4335
Some(id) => {
4436
let krate_graph = db.crate_graph();
45-
let proc_macro = krate_graph[self.krate]
46-
.proc_macro
47-
.get(id.0 as usize)
48-
.ok_or_else(|| err!("No derive macro found."))?;
37+
let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) {
38+
Some(proc_macro) => proc_macro,
39+
None => return ExpandResult::str_err("No derive macro found.".to_string()),
40+
};
4941

5042
// Proc macros have access to the environment variables of the invoking crate.
5143
let env = &krate_graph[calling_crate].env;
5244

53-
proc_macro.expander.expand(tt, attr_arg, env).map_err(mbe::ExpandError::from)
45+
proc_macro.expander.expand(tt, attr_arg, env).map_err(mbe::ExpandError::from).into()
5446
}
55-
None => Err(mbe::ExpandError::UnresolvedProcMacro),
47+
None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro),
5648
}
5749
}
5850
}

0 commit comments

Comments
 (0)