Skip to content

Commit 8b779e8

Browse files
bors[bot]Jonas Schievink
andauthored
Merge #11327
11327: internal: Remove redundant `Option` from eager macro fns r=jonas-schievink a=jonas-schievink This isn't needed since `tt::Subtree` already implements `Default`, and an empty expansion is the appropriate default here. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents df53403 + e5ed43b commit 8b779e8

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

crates/hir_expand/src/builtin_fn_macro.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ macro_rules! register_builtin {
4242
db: &dyn AstDatabase,
4343
arg_id: MacroCallId,
4444
tt: &tt::Subtree,
45-
) -> ExpandResult<Option<ExpandedEager>> {
45+
) -> ExpandResult<ExpandedEager> {
4646
let expander = match *self {
4747
$( EagerExpander::$e_kind => $e_expand, )*
4848
};
@@ -60,7 +60,7 @@ macro_rules! register_builtin {
6060
};
6161
}
6262

63-
#[derive(Debug)]
63+
#[derive(Debug, Default)]
6464
pub struct ExpandedEager {
6565
pub(crate) subtree: tt::Subtree,
6666
/// The included file ID of the include macro.
@@ -362,7 +362,7 @@ fn compile_error_expand(
362362
_db: &dyn AstDatabase,
363363
_id: MacroCallId,
364364
tt: &tt::Subtree,
365-
) -> ExpandResult<Option<ExpandedEager>> {
365+
) -> ExpandResult<ExpandedEager> {
366366
let err = match &*tt.token_trees {
367367
[tt::TokenTree::Leaf(tt::Leaf::Literal(it))] => {
368368
let text = it.text.as_str();
@@ -376,14 +376,14 @@ fn compile_error_expand(
376376
_ => mbe::ExpandError::BindingError("`compile_error!` argument must be a string".into()),
377377
};
378378

379-
ExpandResult { value: Some(ExpandedEager::new(quote! {})), err: Some(err) }
379+
ExpandResult { value: ExpandedEager::new(quote! {}), err: Some(err) }
380380
}
381381

382382
fn concat_expand(
383383
_db: &dyn AstDatabase,
384384
_arg_id: MacroCallId,
385385
tt: &tt::Subtree,
386-
) -> ExpandResult<Option<ExpandedEager>> {
386+
) -> ExpandResult<ExpandedEager> {
387387
let mut err = None;
388388
let mut text = String::new();
389389
for (i, mut t) in tt.token_trees.iter().enumerate() {
@@ -418,14 +418,14 @@ fn concat_expand(
418418
}
419419
}
420420
}
421-
ExpandResult { value: Some(ExpandedEager::new(quote!(#text))), err }
421+
ExpandResult { value: ExpandedEager::new(quote!(#text)), err }
422422
}
423423

424424
fn concat_idents_expand(
425425
_db: &dyn AstDatabase,
426426
_arg_id: MacroCallId,
427427
tt: &tt::Subtree,
428-
) -> ExpandResult<Option<ExpandedEager>> {
428+
) -> ExpandResult<ExpandedEager> {
429429
let mut err = None;
430430
let mut ident = String::new();
431431
for (i, t) in tt.token_trees.iter().enumerate() {
@@ -440,7 +440,7 @@ fn concat_idents_expand(
440440
}
441441
}
442442
let ident = tt::Ident { text: ident.into(), id: tt::TokenId::unspecified() };
443-
ExpandResult { value: Some(ExpandedEager::new(quote!(#ident))), err }
443+
ExpandResult { value: ExpandedEager::new(quote!(#ident)), err }
444444
}
445445

446446
fn relative_file(
@@ -476,7 +476,7 @@ fn include_expand(
476476
db: &dyn AstDatabase,
477477
arg_id: MacroCallId,
478478
tt: &tt::Subtree,
479-
) -> ExpandResult<Option<ExpandedEager>> {
479+
) -> ExpandResult<ExpandedEager> {
480480
let res = (|| {
481481
let path = parse_string(tt)?;
482482
let file_id = relative_file(db, arg_id, &path, false)?;
@@ -488,7 +488,7 @@ fn include_expand(
488488

489489
match res {
490490
Ok((subtree, file_id)) => {
491-
ExpandResult::ok(Some(ExpandedEager { subtree, included_file: Some(file_id) }))
491+
ExpandResult::ok(ExpandedEager { subtree, included_file: Some(file_id) })
492492
}
493493
Err(e) => ExpandResult::only_err(e),
494494
}
@@ -498,7 +498,7 @@ fn include_bytes_expand(
498498
_db: &dyn AstDatabase,
499499
_arg_id: MacroCallId,
500500
tt: &tt::Subtree,
501-
) -> ExpandResult<Option<ExpandedEager>> {
501+
) -> ExpandResult<ExpandedEager> {
502502
if let Err(e) = parse_string(tt) {
503503
return ExpandResult::only_err(e);
504504
}
@@ -511,14 +511,14 @@ fn include_bytes_expand(
511511
id: tt::TokenId::unspecified(),
512512
}))],
513513
};
514-
ExpandResult::ok(Some(ExpandedEager::new(res)))
514+
ExpandResult::ok(ExpandedEager::new(res))
515515
}
516516

517517
fn include_str_expand(
518518
db: &dyn AstDatabase,
519519
arg_id: MacroCallId,
520520
tt: &tt::Subtree,
521-
) -> ExpandResult<Option<ExpandedEager>> {
521+
) -> ExpandResult<ExpandedEager> {
522522
let path = match parse_string(tt) {
523523
Ok(it) => it,
524524
Err(e) => return ExpandResult::only_err(e),
@@ -531,14 +531,14 @@ fn include_str_expand(
531531
let file_id = match relative_file(db, arg_id, &path, true) {
532532
Ok(file_id) => file_id,
533533
Err(_) => {
534-
return ExpandResult::ok(Some(ExpandedEager::new(quote!(""))));
534+
return ExpandResult::ok(ExpandedEager::new(quote!("")));
535535
}
536536
};
537537

538538
let text = db.file_text(file_id);
539539
let text = &*text;
540540

541-
ExpandResult::ok(Some(ExpandedEager::new(quote!(#text))))
541+
ExpandResult::ok(ExpandedEager::new(quote!(#text)))
542542
}
543543

544544
fn get_env_inner(db: &dyn AstDatabase, arg_id: MacroCallId, key: &str) -> Option<String> {
@@ -550,7 +550,7 @@ fn env_expand(
550550
db: &dyn AstDatabase,
551551
arg_id: MacroCallId,
552552
tt: &tt::Subtree,
553-
) -> ExpandResult<Option<ExpandedEager>> {
553+
) -> ExpandResult<ExpandedEager> {
554554
let key = match parse_string(tt) {
555555
Ok(it) => it,
556556
Err(e) => return ExpandResult::only_err(e),
@@ -574,14 +574,14 @@ fn env_expand(
574574
});
575575
let expanded = quote! { #s };
576576

577-
ExpandResult { value: Some(ExpandedEager::new(expanded)), err }
577+
ExpandResult { value: ExpandedEager::new(expanded), err }
578578
}
579579

580580
fn option_env_expand(
581581
db: &dyn AstDatabase,
582582
arg_id: MacroCallId,
583583
tt: &tt::Subtree,
584-
) -> ExpandResult<Option<ExpandedEager>> {
584+
) -> ExpandResult<ExpandedEager> {
585585
let key = match parse_string(tt) {
586586
Ok(it) => it,
587587
Err(e) => return ExpandResult::only_err(e),
@@ -592,5 +592,5 @@ fn option_env_expand(
592592
Some(s) => quote! { std::option::Some(#s) },
593593
};
594594

595-
ExpandResult::ok(Some(ExpandedEager::new(expanded)))
595+
ExpandResult::ok(ExpandedEager::new(expanded))
596596
}

crates/hir_expand/src/eager.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,16 @@ pub fn expand_eager_macro(
145145

146146
if let MacroDefKind::BuiltInEager(eager, _) = def.kind {
147147
let res = eager.expand(db, arg_id, &subtree);
148+
if let Some(err) = res.err {
149+
diagnostic_sink(err);
150+
}
148151

149-
let expanded = diagnostic_sink.expand_result_option(res)?;
150152
let loc = MacroCallLoc {
151153
def,
152154
krate,
153155
eager: Some(EagerCallInfo {
154-
arg_or_expansion: Arc::new(expanded.subtree),
155-
included_file: expanded.included_file,
156+
arg_or_expansion: Arc::new(res.value.subtree),
157+
included_file: res.value.included_file,
156158
}),
157159
kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
158160
};

0 commit comments

Comments
 (0)