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 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)]

View file

@ -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};

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 @@ 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),
}

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 @@ 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),
}

View file

@ -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())),
}
}
}

View file

@ -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),
}
}
}