Skip to content

Commit c784c8e

Browse files
Use named fields in ExpandResult
1 parent 0bcd814 commit c784c8e

File tree

5 files changed

+39
-29
lines changed

5 files changed

+39
-29
lines changed

crates/hir_expand/src/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ fn macro_expand_with_arg(
242242
Some(it) => it,
243243
None => return MacroResult::error("Fail to find macro definition".into()),
244244
};
245-
let ExpandResult(tt, err) = macro_rules.0.expand(db, lazy_id, &macro_arg.0);
245+
let ExpandResult { value: tt, err } = macro_rules.0.expand(db, lazy_id, &macro_arg.0);
246246
// Set a hard limit for the expanded tt
247247
let count = tt.count();
248248
if count > 262144 {

crates/mbe/src/lib.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,33 +246,35 @@ fn validate(pattern: &tt::Subtree) -> Result<(), ParseError> {
246246
Ok(())
247247
}
248248

249-
#[derive(Debug)]
250-
pub struct ExpandResult<T>(pub T, pub Option<ExpandError>);
249+
#[derive(Debug, Clone, Eq, PartialEq)]
250+
pub struct ExpandResult<T> {
251+
pub value: T,
252+
pub err: Option<ExpandError>,
253+
}
251254

252255
impl<T> ExpandResult<T> {
253-
pub fn ok(t: T) -> ExpandResult<T> {
254-
ExpandResult(t, None)
256+
pub fn ok(value: T) -> Self {
257+
Self { value, err: None }
255258
}
256259

257-
pub fn only_err(err: ExpandError) -> ExpandResult<T>
260+
pub fn only_err(err: ExpandError) -> Self
258261
where
259262
T: Default,
260263
{
261-
ExpandResult(Default::default(), Some(err))
264+
Self { value: Default::default(), err: Some(err) }
262265
}
263266

264267
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> ExpandResult<U> {
265-
ExpandResult(f(self.0), self.1)
268+
ExpandResult { value: f(self.value), err: self.err }
266269
}
267270

268271
pub fn result(self) -> Result<T, ExpandError> {
269-
self.1.map(Err).unwrap_or(Ok(self.0))
272+
self.err.map(Err).unwrap_or(Ok(self.value))
270273
}
271274
}
272275

273276
impl<T: Default> From<Result<T, ExpandError>> for ExpandResult<T> {
274-
fn from(result: Result<T, ExpandError>) -> ExpandResult<T> {
275-
result
276-
.map_or_else(|e| ExpandResult(Default::default(), Some(e)), |it| ExpandResult(it, None))
277+
fn from(result: Result<T, ExpandError>) -> Self {
278+
result.map_or_else(|e| Self::only_err(e), |it| Self::ok(it))
277279
}
278280
}

crates/mbe/src/mbe_expander.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ fn expand_rules(rules: &[crate::Rule], input: &tt::Subtree) -> ExpandResult<tt::
2828
// If we find a rule that applies without errors, we're done.
2929
// Unconditionally returning the transcription here makes the
3030
// `test_repeat_bad_var` test fail.
31-
let ExpandResult(res, transcribe_err) =
31+
let ExpandResult { value, err: transcribe_err } =
3232
transcriber::transcribe(&rule.rhs, &new_match.bindings);
3333
if transcribe_err.is_none() {
34-
return ExpandResult::ok(res);
34+
return ExpandResult::ok(value);
3535
}
3636
}
3737
// Use the rule if we matched more tokens, or had fewer errors
@@ -47,11 +47,11 @@ fn expand_rules(rules: &[crate::Rule], input: &tt::Subtree) -> ExpandResult<tt::
4747
}
4848
if let Some((match_, rule)) = match_ {
4949
// if we got here, there was no match without errors
50-
let ExpandResult(result, transcribe_err) =
50+
let ExpandResult { value, err: transcribe_err } =
5151
transcriber::transcribe(&rule.rhs, &match_.bindings);
52-
ExpandResult(result, match_.err.or(transcribe_err))
52+
ExpandResult { value, err: match_.err.or(transcribe_err) }
5353
} else {
54-
ExpandResult(tt::Subtree::default(), Some(ExpandError::NoMatchingRule))
54+
ExpandResult::only_err(ExpandError::NoMatchingRule)
5555
}
5656
}
5757

@@ -143,7 +143,10 @@ mod tests {
143143
}
144144

145145
fn assert_err(macro_body: &str, invocation: &str, err: ExpandError) {
146-
assert_eq!(expand_first(&create_rules(&format_macro(macro_body)), invocation).1, Some(err));
146+
assert_eq!(
147+
expand_first(&create_rules(&format_macro(macro_body)), invocation).err,
148+
Some(err)
149+
);
147150
}
148151

149152
fn format_macro(macro_body: &str) -> String {

crates/mbe/src/mbe_expander/matcher.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,8 @@ fn match_subtree(
158158
continue;
159159
}
160160
};
161-
let ExpandResult(matched, match_err) = match_meta_var(kind.as_str(), src);
161+
let ExpandResult { value: matched, err: match_err } =
162+
match_meta_var(kind.as_str(), src);
162163
match matched {
163164
Some(fragment) => {
164165
res.bindings.inner.insert(name.clone(), Binding::Fragment(fragment));
@@ -342,17 +343,17 @@ impl<'a> TtIter<'a> {
342343
token_trees: res.into_iter().cloned().collect(),
343344
})),
344345
};
345-
ExpandResult(res, err)
346+
ExpandResult { value: res, err }
346347
}
347348

348349
pub(crate) fn eat_vis(&mut self) -> Option<tt::TokenTree> {
349350
let mut fork = self.clone();
350351
match fork.expect_fragment(Visibility) {
351-
ExpandResult(tt, None) => {
352+
ExpandResult { value: tt, err: None } => {
352353
*self = fork;
353354
tt
354355
}
355-
ExpandResult(_, Some(_)) => None,
356+
ExpandResult { value: _, err: Some(_) } => None,
356357
}
357358
}
358359
}

crates/mbe/src/mbe_expander/transcriber.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,25 +93,26 @@ fn expand_subtree(
9393
match op {
9494
Op::TokenTree(tt @ tt::TokenTree::Leaf(..)) => arena.push(tt.clone()),
9595
Op::TokenTree(tt::TokenTree::Subtree(tt)) => {
96-
let ExpandResult(tt, e) = expand_subtree(ctx, tt, arena);
96+
let ExpandResult { value: tt, err: e } = expand_subtree(ctx, tt, arena);
9797
err = err.or(e);
9898
arena.push(tt.into());
9999
}
100100
Op::Var { name, kind: _ } => {
101-
let ExpandResult(fragment, e) = expand_var(ctx, name);
101+
let ExpandResult { value: fragment, err: e } = expand_var(ctx, name);
102102
err = err.or(e);
103103
push_fragment(arena, fragment);
104104
}
105105
Op::Repeat { subtree, kind, separator } => {
106-
let ExpandResult(fragment, e) = expand_repeat(ctx, subtree, kind, separator, arena);
106+
let ExpandResult { value: fragment, err: e } =
107+
expand_repeat(ctx, subtree, kind, separator, arena);
107108
err = err.or(e);
108109
push_fragment(arena, fragment)
109110
}
110111
}
111112
}
112113
// drain the elements added in this instance of expand_subtree
113114
let tts = arena.drain(start_elements..arena.len()).collect();
114-
ExpandResult(tt::Subtree { delimiter: template.delimiter, token_trees: tts }, err)
115+
ExpandResult { value: tt::Subtree { delimiter: template.delimiter, token_trees: tts }, err }
115116
}
116117

117118
fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult<Fragment> {
@@ -152,7 +153,7 @@ fn expand_var(ctx: &mut ExpandCtx, v: &SmolStr) -> ExpandResult<Fragment> {
152153
ExpandResult::ok(Fragment::Tokens(tt))
153154
} else {
154155
ctx.bindings.get(&v, &mut ctx.nesting).map_or_else(
155-
|e| ExpandResult(Fragment::Tokens(tt::TokenTree::empty()), Some(e)),
156+
|e| ExpandResult { value: Fragment::Tokens(tt::TokenTree::empty()), err: Some(e) },
156157
|b| ExpandResult::ok(b.clone()),
157158
)
158159
}
@@ -174,7 +175,7 @@ fn expand_repeat(
174175
let mut counter = 0;
175176

176177
loop {
177-
let ExpandResult(mut t, e) = expand_subtree(ctx, template, arena);
178+
let ExpandResult { value: mut t, err: e } = expand_subtree(ctx, template, arena);
178179
let nesting_state = ctx.nesting.last_mut().unwrap();
179180
if nesting_state.at_end || !nesting_state.hit {
180181
break;
@@ -234,7 +235,10 @@ fn expand_repeat(
234235
let tt = tt::Subtree { delimiter: None, token_trees: buf }.into();
235236

236237
if RepeatKind::OneOrMore == kind && counter == 0 {
237-
return ExpandResult(Fragment::Tokens(tt), Some(ExpandError::UnexpectedToken));
238+
return ExpandResult {
239+
value: Fragment::Tokens(tt),
240+
err: Some(ExpandError::UnexpectedToken),
241+
};
238242
}
239243
ExpandResult::ok(Fragment::Tokens(tt))
240244
}

0 commit comments

Comments
 (0)