Unnamed repository; edit this file 'description' to name the repository.
internal: reduce coupling
tt is a data structure, data structures cant' go wrong, they shouldn't have the knowledge that the world outside of them has all kinds of errors.
Aleksey Kladov 2021-08-31
parent d8a3d6f · commit 81602f8
-rw-r--r--crates/base_db/src/input.rs10
-rw-r--r--crates/base_db/src/lib.rs3
-rw-r--r--crates/hir_expand/src/proc_macro.rs11
-rw-r--r--crates/mbe/src/lib.rs9
-rw-r--r--crates/rust-analyzer/src/reload.rs9
-rw-r--r--crates/tt/src/lib.rs15
6 files changed, 25 insertions, 32 deletions
diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs
index 671039e46b..c3730d0bbd 100644
--- a/crates/base_db/src/input.rs
+++ b/crates/base_db/src/input.rs
@@ -11,7 +11,7 @@ use std::{fmt, iter::FromIterator, ops, panic::RefUnwindSafe, str::FromStr, sync
use cfg::CfgOptions;
use rustc_hash::{FxHashMap, FxHashSet};
use syntax::SmolStr;
-use tt::{ExpansionError, Subtree};
+use tt::Subtree;
use vfs::{file_set::FileSet, FileId, VfsPath};
/// Files are grouped into source roots. A source root is a directory on the
@@ -163,7 +163,13 @@ pub trait ProcMacroExpander: fmt::Debug + Send + Sync + RefUnwindSafe {
subtree: &Subtree,
attrs: Option<&Subtree>,
env: &Env,
- ) -> Result<Subtree, ExpansionError>;
+ ) -> Result<Subtree, ProcMacroExpansionError>;
+}
+
+pub enum ProcMacroExpansionError {
+ Panic(String),
+ /// Things like "proc macro server was killed by OOM".
+ System(String),
}
#[derive(Debug, Clone)]
diff --git a/crates/base_db/src/lib.rs b/crates/base_db/src/lib.rs
index d26f8f1808..c34a329540 100644
--- a/crates/base_db/src/lib.rs
+++ b/crates/base_db/src/lib.rs
@@ -12,7 +12,8 @@ pub use crate::{
change::Change,
input::{
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, Dependency, Edition, Env,
- ProcMacro, ProcMacroExpander, ProcMacroId, ProcMacroKind, SourceRoot, SourceRootId,
+ ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroId, ProcMacroKind,
+ SourceRoot, SourceRootId,
},
};
pub use salsa::{self, Cancelled};
diff --git a/crates/hir_expand/src/proc_macro.rs b/crates/hir_expand/src/proc_macro.rs
index 1e5fff07d5..e8e1d99e80 100644
--- a/crates/hir_expand/src/proc_macro.rs
+++ b/crates/hir_expand/src/proc_macro.rs
@@ -1,7 +1,7 @@
//! Proc Macro Expander stub
use crate::db::AstDatabase;
-use base_db::{CrateId, ProcMacroId};
+use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId};
use mbe::ExpandResult;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
@@ -42,7 +42,14 @@ impl ProcMacroExpander {
// Proc macros have access to the environment variables of the invoking crate.
let env = &krate_graph[calling_crate].env;
- proc_macro.expander.expand(tt, attr_arg, env).map_err(mbe::ExpandError::from).into()
+ proc_macro
+ .expander
+ .expand(tt, attr_arg, env)
+ .map_err(|err| match err {
+ ProcMacroExpansionError::Panic(text) => mbe::ExpandError::Other(text),
+ ProcMacroExpansionError::System(text) => mbe::ExpandError::Other(text),
+ })
+ .into()
}
None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro),
}
diff --git a/crates/mbe/src/lib.rs b/crates/mbe/src/lib.rs
index 242d07c9ec..f490d75c3f 100644
--- a/crates/mbe/src/lib.rs
+++ b/crates/mbe/src/lib.rs
@@ -39,17 +39,11 @@ pub enum ExpandError {
UnexpectedToken,
BindingError(String),
ConversionError,
- ProcMacroError(tt::ExpansionError),
+ // FXME: no way mbe should know about proc macros.
UnresolvedProcMacro,
Other(String),
}
-impl From<tt::ExpansionError> for ExpandError {
- fn from(it: tt::ExpansionError) -> Self {
- ExpandError::ProcMacroError(it)
- }
-}
-
impl fmt::Display for ExpandError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
@@ -57,7 +51,6 @@ impl fmt::Display for ExpandError {
ExpandError::UnexpectedToken => f.write_str("unexpected token in input"),
ExpandError::BindingError(e) => f.write_str(e),
ExpandError::ConversionError => f.write_str("could not convert tokens"),
- ExpandError::ProcMacroError(e) => e.fmt(f),
ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc macro"),
ExpandError::Other(e) => f.write_str(e),
}
diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs
index 0a1226af5e..2cb5eb46b2 100644
--- a/crates/rust-analyzer/src/reload.rs
+++ b/crates/rust-analyzer/src/reload.rs
@@ -5,7 +5,8 @@ use flycheck::{FlycheckConfig, FlycheckHandle};
use hir::db::DefDatabase;
use ide::Change;
use ide_db::base_db::{
- CrateGraph, Env, ProcMacro, ProcMacroExpander, ProcMacroKind, SourceRoot, VfsPath,
+ CrateGraph, Env, ProcMacro, ProcMacroExpander, ProcMacroExpansionError, ProcMacroKind,
+ SourceRoot, VfsPath,
};
use proc_macro_api::{MacroDylib, ProcMacroServer};
use project_model::{ProjectWorkspace, WorkspaceBuildScripts};
@@ -606,12 +607,12 @@ pub(crate) fn load_proc_macro(client: Option<&ProcMacroServer>, path: &AbsPath)
subtree: &tt::Subtree,
attrs: Option<&tt::Subtree>,
env: &Env,
- ) -> Result<tt::Subtree, tt::ExpansionError> {
+ ) -> Result<tt::Subtree, ProcMacroExpansionError> {
let env = env.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect();
match self.0.expand(subtree, attrs, env) {
Ok(Ok(subtree)) => Ok(subtree),
- Ok(Err(err)) => Err(tt::ExpansionError::ExpansionError(err.0)),
- Err(err) => Err(tt::ExpansionError::Unknown(err.to_string())),
+ Ok(Err(err)) => Err(ProcMacroExpansionError::Panic(err.0)),
+ Err(err) => Err(ProcMacroExpansionError::System(err.to_string())),
}
}
}
diff --git a/crates/tt/src/lib.rs b/crates/tt/src/lib.rs
index b2841370ec..f457881f2e 100644
--- a/crates/tt/src/lib.rs
+++ b/crates/tt/src/lib.rs
@@ -274,18 +274,3 @@ impl Subtree {
}
pub mod buffer;
-
-#[derive(Debug, PartialEq, Eq, Clone)]
-pub enum ExpansionError {
- Unknown(String),
- ExpansionError(String),
-}
-
-impl fmt::Display for ExpansionError {
- fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
- match self {
- ExpansionError::Unknown(e) => e.fmt(f),
- ExpansionError::ExpansionError(e) => write!(f, "proc macro returned error: {}", e),
- }
- }
-}