]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_expand/src/eager.rs
internal: Split unresolve proc-macro error out of mbe
[rust.git] / crates / hir_expand / src / eager.rs
index f12132f84bfb4a92a75343a86cec8598c4b6d730..1de0d5a77d62f36b0c86e16712df6fc900c96773 100644 (file)
 //! > and we need to live with it because it's available on stable and widely relied upon.
 //!
 //!
-//! See the full discussion : https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros
+//! See the full discussion : <https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/Eager.20expansion.20of.20built-in.20macros>
+use std::sync::Arc;
+
+use base_db::CrateId;
+use syntax::{ted, SyntaxNode};
 
 use crate::{
     ast::{self, AstNode},
     db::AstDatabase,
-    EagerCallLoc, EagerMacroId, InFile, MacroCallId, MacroCallKind, MacroDefId, MacroDefKind,
+    hygiene::Hygiene,
+    mod_path::ModPath,
+    EagerCallInfo, ExpandError, ExpandResult, ExpandTo, InFile, MacroCallId, MacroCallKind,
+    MacroCallLoc, MacroDefId, MacroDefKind, UnresolvedMacro,
 };
 
-use base_db::CrateId;
-use mbe::ExpandResult;
-use parser::FragmentKind;
-use std::sync::Arc;
-use syntax::{ted, SyntaxNode};
-
 #[derive(Debug)]
 pub struct ErrorEmitted {
     _private: (),
 }
 
 pub trait ErrorSink {
-    fn emit(&mut self, err: mbe::ExpandError);
+    fn emit(&mut self, err: ExpandError);
 
     fn option<T>(
         &mut self,
         opt: Option<T>,
-        error: impl FnOnce() -> mbe::ExpandError,
+        error: impl FnOnce() -> ExpandError,
     ) -> Result<T, ErrorEmitted> {
         match opt {
             Some(it) => Ok(it),
@@ -56,12 +57,12 @@ fn option<T>(
     fn option_with<T>(
         &mut self,
         opt: impl FnOnce() -> Option<T>,
-        error: impl FnOnce() -> mbe::ExpandError,
+        error: impl FnOnce() -> ExpandError,
     ) -> Result<T, ErrorEmitted> {
         self.option(opt(), error)
     }
 
-    fn result<T>(&mut self, res: Result<T, mbe::ExpandError>) -> Result<T, ErrorEmitted> {
+    fn result<T>(&mut self, res: Result<T, ExpandError>) -> Result<T, ErrorEmitted> {
         match res {
             Ok(it) => Ok(it),
             Err(e) => {
@@ -88,83 +89,86 @@ fn expand_result_option<T>(&mut self, res: ExpandResult<Option<T>>) -> Result<T,
     }
 }
 
-impl ErrorSink for &'_ mut dyn FnMut(mbe::ExpandError) {
-    fn emit(&mut self, err: mbe::ExpandError) {
+impl ErrorSink for &'_ mut dyn FnMut(ExpandError) {
+    fn emit(&mut self, err: ExpandError) {
         self(err);
     }
 }
 
-fn err(msg: impl Into<String>) -> mbe::ExpandError {
-    mbe::ExpandError::Other(msg.into())
-}
-
 pub fn expand_eager_macro(
     db: &dyn AstDatabase,
     krate: CrateId,
     macro_call: InFile<ast::MacroCall>,
     def: MacroDefId,
-    resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
-    mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError),
-) -> Result<EagerMacroId, ErrorEmitted> {
-    let parsed_args = diagnostic_sink.option_with(
-        || Some(mbe::ast_to_token_tree(&macro_call.value.token_tree()?).0),
-        || err("malformed macro invocation"),
-    )?;
+    resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
+    diagnostic_sink: &mut dyn FnMut(ExpandError),
+) -> Result<Result<MacroCallId, ErrorEmitted>, UnresolvedMacro> {
+    let hygiene = Hygiene::new(db, macro_call.file_id);
+    let parsed_args = macro_call
+        .value
+        .token_tree()
+        .map(|tt| mbe::syntax_node_to_token_tree(tt.syntax()).0)
+        .unwrap_or_default();
 
     let ast_map = db.ast_id_map(macro_call.file_id);
     let call_id = InFile::new(macro_call.file_id, ast_map.ast_id(&macro_call.value));
+    let expand_to = ExpandTo::from_call_site(&macro_call.value);
 
     // Note:
     // When `lazy_expand` is called, its *parent* file must be already exists.
     // Here we store an eager macro id for the argument expanded subtree here
     // for that purpose.
-    let arg_id = db.intern_eager_expansion({
-        EagerCallLoc {
-            def,
-            fragment: FragmentKind::Expr,
-            subtree: Arc::new(parsed_args.clone()),
-            krate,
-            call: call_id,
+    let arg_id = db.intern_macro_call(MacroCallLoc {
+        def,
+        krate,
+        eager: Some(EagerCallInfo {
+            arg_or_expansion: Arc::new(parsed_args.clone()),
             included_file: None,
-        }
+        }),
+        kind: MacroCallKind::FnLike { ast_id: call_id, expand_to: ExpandTo::Expr },
     });
-    let arg_file_id: MacroCallId = arg_id.into();
 
-    let parsed_args =
-        diagnostic_sink.result(mbe::token_tree_to_syntax_node(&parsed_args, FragmentKind::Expr))?.0;
-    let result = eager_macro_recur(
+    let parsed_args = mbe::token_tree_to_syntax_node(&parsed_args, mbe::TopEntryPoint::Expr).0;
+    let result = match eager_macro_recur(
         db,
-        InFile::new(arg_file_id.as_file(), parsed_args.syntax_node()),
+        &hygiene,
+        InFile::new(arg_id.as_file(), parsed_args.syntax_node()),
         krate,
         resolver,
         diagnostic_sink,
-    )?;
-    let subtree =
-        diagnostic_sink.option(to_subtree(&result), || err("failed to parse macro result"))?;
+    ) {
+        Ok(Ok(it)) => it,
+        Ok(Err(err)) => return Ok(Err(err)),
+        Err(err) => return Err(err),
+    };
+    let subtree = to_subtree(&result);
 
     if let MacroDefKind::BuiltInEager(eager, _) = def.kind {
         let res = eager.expand(db, arg_id, &subtree);
+        if let Some(err) = res.err {
+            diagnostic_sink(err.into());
+        }
 
-        let expanded = diagnostic_sink.expand_result_option(res)?;
-        let eager = EagerCallLoc {
+        let loc = MacroCallLoc {
             def,
-            fragment: expanded.fragment,
-            subtree: Arc::new(expanded.subtree),
             krate,
-            call: call_id,
-            included_file: expanded.included_file,
+            eager: Some(EagerCallInfo {
+                arg_or_expansion: Arc::new(res.value.subtree),
+                included_file: res.value.included_file,
+            }),
+            kind: MacroCallKind::FnLike { ast_id: call_id, expand_to },
         };
 
-        Ok(db.intern_eager_expansion(eager))
+        Ok(Ok(db.intern_macro_call(loc)))
     } else {
         panic!("called `expand_eager_macro` on non-eager macro def {:?}", def);
     }
 }
 
-fn to_subtree(node: &SyntaxNode) -> Option<tt::Subtree> {
+fn to_subtree(node: &SyntaxNode) -> tt::Subtree {
     let mut subtree = mbe::syntax_node_to_token_tree(node).0;
     subtree.delimiter = None;
-    Some(subtree)
+    subtree
 }
 
 fn lazy_expand(
@@ -175,9 +179,12 @@ fn lazy_expand(
 ) -> ExpandResult<Option<InFile<SyntaxNode>>> {
     let ast_id = db.ast_id_map(macro_call.file_id).ast_id(&macro_call.value);
 
-    let id: MacroCallId = def
-        .as_lazy_macro(db, krate, MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id) })
-        .into();
+    let expand_to = ExpandTo::from_call_site(&macro_call.value);
+    let id = def.as_lazy_macro(
+        db,
+        krate,
+        MacroCallKind::FnLike { ast_id: macro_call.with_value(ast_id), expand_to },
+    );
 
     let err = db.macro_expand_error(id);
     let value = db.parse_or_expand(id.as_file()).map(|node| InFile::new(id.as_file(), node));
@@ -187,55 +194,73 @@ fn lazy_expand(
 
 fn eager_macro_recur(
     db: &dyn AstDatabase,
+    hygiene: &Hygiene,
     curr: InFile<SyntaxNode>,
     krate: CrateId,
-    macro_resolver: &dyn Fn(ast::Path) -> Option<MacroDefId>,
-    mut diagnostic_sink: &mut dyn FnMut(mbe::ExpandError),
-) -> Result<SyntaxNode, ErrorEmitted> {
-    let original = curr.value.clone().clone_for_update();
+    macro_resolver: &dyn Fn(ModPath) -> Option<MacroDefId>,
+    mut diagnostic_sink: &mut dyn FnMut(ExpandError),
+) -> Result<Result<SyntaxNode, ErrorEmitted>, UnresolvedMacro> {
+    let original = curr.value.clone_for_update();
 
     let children = original.descendants().filter_map(ast::MacroCall::cast);
     let mut replacements = Vec::new();
 
     // Collect replacement
     for child in children {
-        let def = diagnostic_sink
-            .option_with(|| macro_resolver(child.path()?), || err("failed to resolve macro"))?;
+        let def = match child.path().and_then(|path| ModPath::from_src(db, path, &hygiene)) {
+            Some(path) => macro_resolver(path.clone()).ok_or_else(|| UnresolvedMacro { path })?,
+            None => {
+                diagnostic_sink(ExpandError::Other("malformed macro invocation".into()));
+                continue;
+            }
+        };
         let insert = match def.kind {
             MacroDefKind::BuiltInEager(..) => {
-                let id: MacroCallId = expand_eager_macro(
+                let id = match expand_eager_macro(
                     db,
                     krate,
                     curr.with_value(child.clone()),
                     def,
                     macro_resolver,
                     diagnostic_sink,
-                )?
-                .into();
+                ) {
+                    Ok(Ok(it)) => it,
+                    Ok(Err(err)) => return Ok(Err(err)),
+                    Err(err) => return Err(err),
+                };
                 db.parse_or_expand(id.as_file())
                     .expect("successful macro expansion should be parseable")
                     .clone_for_update()
             }
             MacroDefKind::Declarative(_)
             | MacroDefKind::BuiltIn(..)
+            | MacroDefKind::BuiltInAttr(..)
             | MacroDefKind::BuiltInDerive(..)
             | MacroDefKind::ProcMacro(..) => {
                 let res = lazy_expand(db, &def, curr.with_value(child.clone()), krate);
-                let val = diagnostic_sink.expand_result_option(res)?;
+                let val = match diagnostic_sink.expand_result_option(res) {
+                    Ok(it) => it,
+                    Err(err) => return Ok(Err(err)),
+                };
 
                 // replace macro inside
-                eager_macro_recur(db, val, krate, macro_resolver, diagnostic_sink)?
+                let hygiene = Hygiene::new(db, val.file_id);
+                match eager_macro_recur(db, &hygiene, val, krate, macro_resolver, diagnostic_sink) {
+                    Ok(Ok(it)) => it,
+                    Ok(Err(err)) => return Ok(Err(err)),
+                    Err(err) => return Err(err),
+                }
             }
         };
 
         // check if the whole original syntax is replaced
         if child.syntax() == &original {
-            return Ok(insert);
+            return Ok(Ok(insert));
         }
 
         replacements.push((child, insert));
     }
 
     replacements.into_iter().rev().for_each(|(old, new)| ted::replace(old.syntax(), new));
-    Ok(original)
+    Ok(Ok(original))
 }