Skip to content

Commit 269de9a

Browse files
committed
Switch BuiltinDeriveExpander::expand to ExpandResult
1 parent 557df6f commit 269de9a

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl TokenExpander {
6060
mbe::ExpandError::Other("No item argument for attribute".to_string()).into(),
6161
),
6262
},
63-
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(),
63+
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt),
6464
TokenExpander::ProcMacro(_) => {
6565
// We store the result in salsa db to prevent non-deterministic behavior in
6666
// some proc-macro implementation

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;

0 commit comments

Comments
 (0)