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.
This commit is contained in:
Aleksey Kladov 2021-08-31 19:14:33 +03:00
parent d8a3d6f378
commit 81602f8a5d
6 changed files with 25 additions and 32 deletions

View File

@ -11,7 +11,7 @@
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 @@ fn expand(
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)]

View File

@ -12,7 +12,8 @@
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};

View File

@ -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 @@ pub fn expand(
// 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),
}

View File

@ -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 @@ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
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),
}

View File

@ -5,7 +5,8 @@
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 @@ fn expand(
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())),
}
}
}

View File

@ -274,18 +274,3 @@ pub fn as_debug_string(&self) -> String {
}
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),
}
}
}