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 cfg::CfgOptions;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use syntax::SmolStr; use syntax::SmolStr;
use tt::{ExpansionError, Subtree}; use tt::Subtree;
use vfs::{file_set::FileSet, FileId, VfsPath}; use vfs::{file_set::FileSet, FileId, VfsPath};
/// Files are grouped into source roots. A source root is a directory on the /// 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, subtree: &Subtree,
attrs: Option<&Subtree>, attrs: Option<&Subtree>,
env: &Env, 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)] #[derive(Debug, Clone)]

View file

@ -12,7 +12,8 @@ pub use crate::{
change::Change, change::Change,
input::{ input::{
CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, 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}; pub use salsa::{self, Cancelled};

View file

@ -1,7 +1,7 @@
//! Proc Macro Expander stub //! Proc Macro Expander stub
use crate::db::AstDatabase; use crate::db::AstDatabase;
use base_db::{CrateId, ProcMacroId}; use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId};
use mbe::ExpandResult; use mbe::ExpandResult;
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] #[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. // Proc macros have access to the environment variables of the invoking crate.
let env = &krate_graph[calling_crate].env; 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), None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro),
} }

View file

@ -39,17 +39,11 @@ pub enum ExpandError {
UnexpectedToken, UnexpectedToken,
BindingError(String), BindingError(String),
ConversionError, ConversionError,
ProcMacroError(tt::ExpansionError), // FXME: no way mbe should know about proc macros.
UnresolvedProcMacro, UnresolvedProcMacro,
Other(String), Other(String),
} }
impl From<tt::ExpansionError> for ExpandError {
fn from(it: tt::ExpansionError) -> Self {
ExpandError::ProcMacroError(it)
}
}
impl fmt::Display for ExpandError { impl fmt::Display for ExpandError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
@ -57,7 +51,6 @@ impl fmt::Display for ExpandError {
ExpandError::UnexpectedToken => f.write_str("unexpected token in input"), ExpandError::UnexpectedToken => f.write_str("unexpected token in input"),
ExpandError::BindingError(e) => f.write_str(e), ExpandError::BindingError(e) => f.write_str(e),
ExpandError::ConversionError => f.write_str("could not convert tokens"), ExpandError::ConversionError => f.write_str("could not convert tokens"),
ExpandError::ProcMacroError(e) => e.fmt(f),
ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc macro"), ExpandError::UnresolvedProcMacro => f.write_str("unresolved proc macro"),
ExpandError::Other(e) => f.write_str(e), ExpandError::Other(e) => f.write_str(e),
} }

View file

@ -5,7 +5,8 @@ use flycheck::{FlycheckConfig, FlycheckHandle};
use hir::db::DefDatabase; use hir::db::DefDatabase;
use ide::Change; use ide::Change;
use ide_db::base_db::{ 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 proc_macro_api::{MacroDylib, ProcMacroServer};
use project_model::{ProjectWorkspace, WorkspaceBuildScripts}; use project_model::{ProjectWorkspace, WorkspaceBuildScripts};
@ -606,12 +607,12 @@ pub(crate) fn load_proc_macro(client: Option<&ProcMacroServer>, path: &AbsPath)
subtree: &tt::Subtree, subtree: &tt::Subtree,
attrs: Option<&tt::Subtree>, attrs: Option<&tt::Subtree>,
env: &Env, 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(); let env = env.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect();
match self.0.expand(subtree, attrs, env) { match self.0.expand(subtree, attrs, env) {
Ok(Ok(subtree)) => Ok(subtree), Ok(Ok(subtree)) => Ok(subtree),
Ok(Err(err)) => Err(tt::ExpansionError::ExpansionError(err.0)), Ok(Err(err)) => Err(ProcMacroExpansionError::Panic(err.0)),
Err(err) => Err(tt::ExpansionError::Unknown(err.to_string())), Err(err) => Err(ProcMacroExpansionError::System(err.to_string())),
} }
} }
} }

View file

@ -274,18 +274,3 @@ impl Subtree {
} }
pub mod buffer; 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),
}
}
}