Skip to content

Commit 2fbe1c9

Browse files
bors[bot]Veykril
andauthored
Merge #9960
9960: internal: Use ExpandResult in all TokenExpanders r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents da5a5ba + 82728eb commit 2fbe1c9

File tree

5 files changed

+37
-58
lines changed

5 files changed

+37
-58
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/builtin_derive.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use log::debug;
44

5+
use mbe::ExpandResult;
56
use parser::FragmentKind;
67
use syntax::{
78
ast::{self, AstNode, GenericParamsOwner, ModuleItemOwner, NameOwner},
@@ -23,7 +24,7 @@ macro_rules! register_builtin {
2324
db: &dyn AstDatabase,
2425
id: MacroCallId,
2526
tt: &tt::Subtree,
26-
) -> Result<tt::Subtree, mbe::ExpandError> {
27+
) -> ExpandResult<tt::Subtree> {
2728
let expander = match *self {
2829
$( BuiltinDeriveExpander::$trait => $expand, )*
2930
};
@@ -147,11 +148,11 @@ fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
147148
result
148149
}
149150

150-
fn expand_simple_derive(
151-
tt: &tt::Subtree,
152-
trait_path: tt::Subtree,
153-
) -> Result<tt::Subtree, mbe::ExpandError> {
154-
let info = parse_adt(tt)?;
151+
fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResult<tt::Subtree> {
152+
let info = match parse_adt(tt) {
153+
Ok(info) => info,
154+
Err(e) => return ExpandResult::only_err(e),
155+
};
155156
let name = info.name;
156157
let trait_path_clone = trait_path.token_trees.clone();
157158
let bound = (quote! { : ##trait_path_clone }).token_trees;
@@ -161,7 +162,7 @@ fn expand_simple_derive(
161162
let expanded = quote! {
162163
impl ##type_params ##trait_path for #name ##type_args {}
163164
};
164-
Ok(expanded)
165+
ExpandResult::ok(expanded)
165166
}
166167

167168
fn find_builtin_crate(db: &dyn AstDatabase, id: MacroCallId) -> tt::TokenTree {
@@ -186,7 +187,7 @@ fn copy_expand(
186187
db: &dyn AstDatabase,
187188
id: MacroCallId,
188189
tt: &tt::Subtree,
189-
) -> Result<tt::Subtree, mbe::ExpandError> {
190+
) -> ExpandResult<tt::Subtree> {
190191
let krate = find_builtin_crate(db, id);
191192
expand_simple_derive(tt, quote! { #krate::marker::Copy })
192193
}
@@ -195,7 +196,7 @@ fn clone_expand(
195196
db: &dyn AstDatabase,
196197
id: MacroCallId,
197198
tt: &tt::Subtree,
198-
) -> Result<tt::Subtree, mbe::ExpandError> {
199+
) -> ExpandResult<tt::Subtree> {
199200
let krate = find_builtin_crate(db, id);
200201
expand_simple_derive(tt, quote! { #krate::clone::Clone })
201202
}
@@ -204,7 +205,7 @@ fn default_expand(
204205
db: &dyn AstDatabase,
205206
id: MacroCallId,
206207
tt: &tt::Subtree,
207-
) -> Result<tt::Subtree, mbe::ExpandError> {
208+
) -> ExpandResult<tt::Subtree> {
208209
let krate = find_builtin_crate(db, id);
209210
expand_simple_derive(tt, quote! { #krate::default::Default })
210211
}
@@ -213,7 +214,7 @@ fn debug_expand(
213214
db: &dyn AstDatabase,
214215
id: MacroCallId,
215216
tt: &tt::Subtree,
216-
) -> Result<tt::Subtree, mbe::ExpandError> {
217+
) -> ExpandResult<tt::Subtree> {
217218
let krate = find_builtin_crate(db, id);
218219
expand_simple_derive(tt, quote! { #krate::fmt::Debug })
219220
}
@@ -222,16 +223,12 @@ fn hash_expand(
222223
db: &dyn AstDatabase,
223224
id: MacroCallId,
224225
tt: &tt::Subtree,
225-
) -> Result<tt::Subtree, mbe::ExpandError> {
226+
) -> ExpandResult<tt::Subtree> {
226227
let krate = find_builtin_crate(db, id);
227228
expand_simple_derive(tt, quote! { #krate::hash::Hash })
228229
}
229230

230-
fn eq_expand(
231-
db: &dyn AstDatabase,
232-
id: MacroCallId,
233-
tt: &tt::Subtree,
234-
) -> Result<tt::Subtree, mbe::ExpandError> {
231+
fn eq_expand(db: &dyn AstDatabase, id: MacroCallId, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
235232
let krate = find_builtin_crate(db, id);
236233
expand_simple_derive(tt, quote! { #krate::cmp::Eq })
237234
}
@@ -240,7 +237,7 @@ fn partial_eq_expand(
240237
db: &dyn AstDatabase,
241238
id: MacroCallId,
242239
tt: &tt::Subtree,
243-
) -> Result<tt::Subtree, mbe::ExpandError> {
240+
) -> ExpandResult<tt::Subtree> {
244241
let krate = find_builtin_crate(db, id);
245242
expand_simple_derive(tt, quote! { #krate::cmp::PartialEq })
246243
}
@@ -249,7 +246,7 @@ fn ord_expand(
249246
db: &dyn AstDatabase,
250247
id: MacroCallId,
251248
tt: &tt::Subtree,
252-
) -> Result<tt::Subtree, mbe::ExpandError> {
249+
) -> ExpandResult<tt::Subtree> {
253250
let krate = find_builtin_crate(db, id);
254251
expand_simple_derive(tt, quote! { #krate::cmp::Ord })
255252
}
@@ -258,7 +255,7 @@ fn partial_ord_expand(
258255
db: &dyn AstDatabase,
259256
id: MacroCallId,
260257
tt: &tt::Subtree,
261-
) -> Result<tt::Subtree, mbe::ExpandError> {
258+
) -> ExpandResult<tt::Subtree> {
262259
let krate = find_builtin_crate(db, id);
263260
expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd })
264261
}

crates/hir_expand/src/db.rs

Lines changed: 7 additions & 17 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
},
63-
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(),
60+
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/input.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ fn remove_attr_invoc(item: ast::Item, attr_index: usize) -> ast::Item {
6262

6363
#[cfg(test)]
6464
mod tests {
65-
use base_db::fixture::WithFixture;
66-
use base_db::SourceDatabase;
65+
use base_db::{fixture::WithFixture, SourceDatabase};
6766
use expect_test::{expect, Expect};
6867

6968
use crate::test_db::TestDB;

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)