about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-01-21 11:59:10 +0000
committerGitHub <noreply@github.com>2022-01-21 11:59:10 +0000
commit8b779e8870c4600b67cc3d64f4a3b32a1ee6609b (patch)
tree164fddb546f67d8c4017ca0ef4792396186e4c26
parentdf5340386365b2b16c4e9bbae546504b97564c41 (diff)
parente5ed43b1dc730ef283736eaa6e3a12a1bed0e905 (diff)
downloadrust-8b779e8870c4600b67cc3d64f4a3b32a1ee6609b.tar.gz
rust-8b779e8870c4600b67cc3d64f4a3b32a1ee6609b.zip
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 <jonas.schievink@ferrous-systems.com>
-rw-r--r--crates/hir_expand/src/builtin_fn_macro.rs38
-rw-r--r--crates/hir_expand/src/eager.rs8
2 files changed, 24 insertions, 22 deletions
diff --git a/crates/hir_expand/src/builtin_fn_macro.rs b/crates/hir_expand/src/builtin_fn_macro.rs
index 57e66e5cc4c..f98bc0aba2c 100644
--- a/crates/hir_expand/src/builtin_fn_macro.rs
+++ b/crates/hir_expand/src/builtin_fn_macro.rs
@@ -42,7 +42,7 @@ macro_rules! register_builtin {
                 db: &dyn AstDatabase,
                 arg_id: MacroCallId,
                 tt: &tt::Subtree,
-            ) -> ExpandResult<Option<ExpandedEager>> {
+            ) -> ExpandResult<ExpandedEager> {
                 let expander = match *self {
                     $( EagerExpander::$e_kind => $e_expand, )*
                 };
@@ -60,7 +60,7 @@ macro_rules! register_builtin {
     };
 }
 
-#[derive(Debug)]
+#[derive(Debug, Default)]
 pub struct ExpandedEager {
     pub(crate) subtree: tt::Subtree,
     /// The included file ID of the include macro.
@@ -362,7 +362,7 @@ fn compile_error_expand(
     _db: &dyn AstDatabase,
     _id: MacroCallId,
     tt: &tt::Subtree,
-) -> ExpandResult<Option<ExpandedEager>> {
+) -> ExpandResult<ExpandedEager> {
     let err = match &*tt.token_trees {
         [tt::TokenTree::Leaf(tt::Leaf::Literal(it))] => {
             let text = it.text.as_str();
@@ -376,14 +376,14 @@ fn compile_error_expand(
         _ => mbe::ExpandError::BindingError("`compile_error!` argument must be a string".into()),
     };
 
-    ExpandResult { value: Some(ExpandedEager::new(quote! {})), err: Some(err) }
+    ExpandResult { value: ExpandedEager::new(quote! {}), err: Some(err) }
 }
 
 fn concat_expand(
     _db: &dyn AstDatabase,
     _arg_id: MacroCallId,
     tt: &tt::Subtree,
-) -> ExpandResult<Option<ExpandedEager>> {
+) -> ExpandResult<ExpandedEager> {
     let mut err = None;
     let mut text = String::new();
     for (i, mut t) in tt.token_trees.iter().enumerate() {
@@ -418,14 +418,14 @@ fn concat_expand(
             }
         }
     }
-    ExpandResult { value: Some(ExpandedEager::new(quote!(#text))), err }
+    ExpandResult { value: ExpandedEager::new(quote!(#text)), err }
 }
 
 fn concat_idents_expand(
     _db: &dyn AstDatabase,
     _arg_id: MacroCallId,
     tt: &tt::Subtree,
-) -> ExpandResult<Option<ExpandedEager>> {
+) -> ExpandResult<ExpandedEager> {
     let mut err = None;
     let mut ident = String::new();
     for (i, t) in tt.token_trees.iter().enumerate() {
@@ -440,7 +440,7 @@ fn concat_idents_expand(
         }
     }
     let ident = tt::Ident { text: ident.into(), id: tt::TokenId::unspecified() };
-    ExpandResult { value: Some(ExpandedEager::new(quote!(#ident))), err }
+    ExpandResult { value: ExpandedEager::new(quote!(#ident)), err }
 }
 
 fn relative_file(
@@ -476,7 +476,7 @@ fn include_expand(
     db: &dyn AstDatabase,
     arg_id: MacroCallId,
     tt: &tt::Subtree,
-) -> ExpandResult<Option<ExpandedEager>> {
+) -> ExpandResult<ExpandedEager> {
     let res = (|| {
         let path = parse_string(tt)?;
         let file_id = relative_file(db, arg_id, &path, false)?;
@@ -488,7 +488,7 @@ fn include_expand(
 
     match res {
         Ok((subtree, file_id)) => {
-            ExpandResult::ok(Some(ExpandedEager { subtree, included_file: Some(file_id) }))
+            ExpandResult::ok(ExpandedEager { subtree, included_file: Some(file_id) })
         }
         Err(e) => ExpandResult::only_err(e),
     }
@@ -498,7 +498,7 @@ fn include_bytes_expand(
     _db: &dyn AstDatabase,
     _arg_id: MacroCallId,
     tt: &tt::Subtree,
-) -> ExpandResult<Option<ExpandedEager>> {
+) -> ExpandResult<ExpandedEager> {
     if let Err(e) = parse_string(tt) {
         return ExpandResult::only_err(e);
     }
@@ -511,14 +511,14 @@ fn include_bytes_expand(
             id: tt::TokenId::unspecified(),
         }))],
     };
-    ExpandResult::ok(Some(ExpandedEager::new(res)))
+    ExpandResult::ok(ExpandedEager::new(res))
 }
 
 fn include_str_expand(
     db: &dyn AstDatabase,
     arg_id: MacroCallId,
     tt: &tt::Subtree,
-) -> ExpandResult<Option<ExpandedEager>> {
+) -> ExpandResult<ExpandedEager> {
     let path = match parse_string(tt) {
         Ok(it) => it,
         Err(e) => return ExpandResult::only_err(e),
@@ -531,14 +531,14 @@ fn include_str_expand(
     let file_id = match relative_file(db, arg_id, &path, true) {
         Ok(file_id) => file_id,
         Err(_) => {
-            return ExpandResult::ok(Some(ExpandedEager::new(quote!(""))));
+            return ExpandResult::ok(ExpandedEager::new(quote!("")));
         }
     };
 
     let text = db.file_text(file_id);
     let text = &*text;
 
-    ExpandResult::ok(Some(ExpandedEager::new(quote!(#text))))
+    ExpandResult::ok(ExpandedEager::new(quote!(#text)))
 }
 
 fn get_env_inner(db: &dyn AstDatabase, arg_id: MacroCallId, key: &str) -> Option<String> {
@@ -550,7 +550,7 @@ fn env_expand(
     db: &dyn AstDatabase,
     arg_id: MacroCallId,
     tt: &tt::Subtree,
-) -> ExpandResult<Option<ExpandedEager>> {
+) -> ExpandResult<ExpandedEager> {
     let key = match parse_string(tt) {
         Ok(it) => it,
         Err(e) => return ExpandResult::only_err(e),
@@ -574,14 +574,14 @@ fn env_expand(
     });
     let expanded = quote! { #s };
 
-    ExpandResult { value: Some(ExpandedEager::new(expanded)), err }
+    ExpandResult { value: ExpandedEager::new(expanded), err }
 }
 
 fn option_env_expand(
     db: &dyn AstDatabase,
     arg_id: MacroCallId,
     tt: &tt::Subtree,
-) -> ExpandResult<Option<ExpandedEager>> {
+) -> ExpandResult<ExpandedEager> {
     let key = match parse_string(tt) {
         Ok(it) => it,
         Err(e) => return ExpandResult::only_err(e),
@@ -592,5 +592,5 @@ fn option_env_expand(
         Some(s) => quote! { std::option::Some(#s) },
     };
 
-    ExpandResult::ok(Some(ExpandedEager::new(expanded)))
+    ExpandResult::ok(ExpandedEager::new(expanded))
 }
diff --git a/crates/hir_expand/src/eager.rs b/crates/hir_expand/src/eager.rs
index 1d29ad26307..3d683d0640e 100644
--- a/crates/hir_expand/src/eager.rs
+++ b/crates/hir_expand/src/eager.rs
@@ -145,14 +145,16 @@ pub fn expand_eager_macro(
 
     if let MacroDefKind::BuiltInEager(eager, _) = def.kind {
         let res = eager.expand(db, arg_id, &subtree);
+        if let Some(err) = res.err {
+            diagnostic_sink(err);
+        }
 
-        let expanded = diagnostic_sink.expand_result_option(res)?;
         let loc = MacroCallLoc {
             def,
             krate,
             eager: Some(EagerCallInfo {
-                arg_or_expansion: Arc::new(expanded.subtree),
-                included_file: expanded.included_file,
+                arg_or_expansion: Arc::new(res.value.subtree),
+                included_file: res.value.included_file,
             }),
             kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
         };