2020-01-10 11:26:18 -06:00
|
|
|
//! `AstTransformer`s are functions that replace nodes in an AST and can be easily combined.
|
2020-01-10 11:59:57 -06:00
|
|
|
use rustc_hash::FxHashMap;
|
2020-01-10 11:26:18 -06:00
|
|
|
|
2020-05-14 01:56:20 -05:00
|
|
|
use hir::{HirDisplay, PathResolution, SemanticsScope};
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2020-03-24 11:03:05 -05:00
|
|
|
algo::SyntaxRewriter,
|
|
|
|
ast::{self, AstNode},
|
|
|
|
};
|
2020-01-10 11:26:18 -06:00
|
|
|
|
|
|
|
pub trait AstTransform<'a> {
|
2020-08-12 11:26:51 -05:00
|
|
|
fn get_substitution(&self, node: &syntax::SyntaxNode) -> Option<syntax::SyntaxNode>;
|
2020-01-10 11:26:18 -06:00
|
|
|
|
|
|
|
fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a>;
|
|
|
|
fn or<T: AstTransform<'a> + 'a>(self, other: T) -> Box<dyn AstTransform<'a> + 'a>
|
|
|
|
where
|
|
|
|
Self: Sized + 'a,
|
|
|
|
{
|
|
|
|
self.chain_before(Box::new(other))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct NullTransformer;
|
|
|
|
|
|
|
|
impl<'a> AstTransform<'a> for NullTransformer {
|
2020-08-12 11:26:51 -05:00
|
|
|
fn get_substitution(&self, _node: &syntax::SyntaxNode) -> Option<syntax::SyntaxNode> {
|
2020-01-10 11:26:18 -06:00
|
|
|
None
|
|
|
|
}
|
|
|
|
fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a> {
|
|
|
|
other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 05:42:44 -06:00
|
|
|
pub struct SubstituteTypeParams<'a> {
|
2020-07-01 01:34:45 -05:00
|
|
|
source_scope: &'a SemanticsScope<'a>,
|
2020-07-31 05:06:38 -05:00
|
|
|
substs: FxHashMap<hir::TypeParam, ast::Type>,
|
2020-01-10 11:26:18 -06:00
|
|
|
previous: Box<dyn AstTransform<'a> + 'a>,
|
|
|
|
}
|
|
|
|
|
2020-02-25 05:42:44 -06:00
|
|
|
impl<'a> SubstituteTypeParams<'a> {
|
2020-01-10 11:26:18 -06:00
|
|
|
pub fn for_trait_impl(
|
2020-07-01 01:34:45 -05:00
|
|
|
source_scope: &'a SemanticsScope<'a>,
|
2020-02-18 11:35:10 -06:00
|
|
|
// FIXME: there's implicit invariant that `trait_` and `source_scope` match...
|
2020-01-10 11:26:18 -06:00
|
|
|
trait_: hir::Trait,
|
2020-07-30 11:28:28 -05:00
|
|
|
impl_def: ast::Impl,
|
2020-02-25 05:42:44 -06:00
|
|
|
) -> SubstituteTypeParams<'a> {
|
2020-02-29 14:24:40 -06:00
|
|
|
let substs = get_syntactic_substs(impl_def).unwrap_or_default();
|
2020-01-10 11:26:18 -06:00
|
|
|
let generic_def: hir::GenericDef = trait_.into();
|
2020-01-10 11:59:57 -06:00
|
|
|
let substs_by_param: FxHashMap<_, _> = generic_def
|
2020-03-13 12:02:04 -05:00
|
|
|
.params(source_scope.db)
|
2020-01-10 11:26:18 -06:00
|
|
|
.into_iter()
|
|
|
|
// this is a trait impl, so we need to skip the first type parameter -- this is a bit hacky
|
|
|
|
.skip(1)
|
2020-05-13 08:06:42 -05:00
|
|
|
// The actual list of trait type parameters may be longer than the one
|
2020-08-10 21:55:26 -05:00
|
|
|
// used in the `impl` block due to trailing default type parameters.
|
2020-05-13 08:06:42 -05:00
|
|
|
// For that case we extend the `substs` with an empty iterator so we
|
|
|
|
// can still hit those trailing values and check if they actually have
|
|
|
|
// a default type. If they do, go for that type from `hir` to `ast` so
|
|
|
|
// the resulting change can be applied correctly.
|
|
|
|
.zip(substs.into_iter().map(Some).chain(std::iter::repeat(None)))
|
|
|
|
.filter_map(|(k, v)| match v {
|
|
|
|
Some(v) => Some((k, v)),
|
2020-05-14 01:56:20 -05:00
|
|
|
None => {
|
|
|
|
let default = k.default(source_scope.db)?;
|
|
|
|
Some((
|
2020-05-13 08:06:42 -05:00
|
|
|
k,
|
2020-08-05 12:29:24 -05:00
|
|
|
ast::make::ty(
|
2020-05-14 01:56:20 -05:00
|
|
|
&default
|
|
|
|
.display_source_code(source_scope.db, source_scope.module()?.into())
|
|
|
|
.ok()?,
|
2020-05-14 02:31:34 -05:00
|
|
|
),
|
2020-05-14 01:56:20 -05:00
|
|
|
))
|
|
|
|
}
|
2020-05-13 08:06:42 -05:00
|
|
|
})
|
2020-01-10 11:26:18 -06:00
|
|
|
.collect();
|
|
|
|
return SubstituteTypeParams {
|
2020-02-18 11:35:10 -06:00
|
|
|
source_scope,
|
2020-01-10 11:26:18 -06:00
|
|
|
substs: substs_by_param,
|
|
|
|
previous: Box::new(NullTransformer),
|
|
|
|
};
|
|
|
|
|
2020-01-10 11:40:45 -06:00
|
|
|
// FIXME: It would probably be nicer if we could get this via HIR (i.e. get the
|
2020-07-31 11:41:37 -05:00
|
|
|
// trait ref, and then go from the types in the substs back to the syntax).
|
2020-07-31 05:06:38 -05:00
|
|
|
fn get_syntactic_substs(impl_def: ast::Impl) -> Option<Vec<ast::Type>> {
|
2020-07-31 13:23:52 -05:00
|
|
|
let target_trait = impl_def.trait_()?;
|
2020-01-10 11:26:18 -06:00
|
|
|
let path_type = match target_trait {
|
2020-07-31 05:06:38 -05:00
|
|
|
ast::Type::PathType(path) => path,
|
2020-01-10 11:26:18 -06:00
|
|
|
_ => return None,
|
|
|
|
};
|
2020-07-31 11:41:37 -05:00
|
|
|
let generic_arg_list = path_type.path()?.segment()?.generic_arg_list()?;
|
|
|
|
|
2020-01-10 11:26:18 -06:00
|
|
|
let mut result = Vec::new();
|
2020-07-31 11:41:37 -05:00
|
|
|
for generic_arg in generic_arg_list.generic_args() {
|
|
|
|
match generic_arg {
|
|
|
|
ast::GenericArg::TypeArg(type_arg) => result.push(type_arg.ty()?),
|
|
|
|
ast::GenericArg::AssocTypeArg(_)
|
|
|
|
| ast::GenericArg::LifetimeArg(_)
|
|
|
|
| ast::GenericArg::ConstArg(_) => (),
|
|
|
|
}
|
2020-01-10 11:26:18 -06:00
|
|
|
}
|
2020-07-31 11:41:37 -05:00
|
|
|
|
2020-01-10 11:26:18 -06:00
|
|
|
Some(result)
|
|
|
|
}
|
|
|
|
}
|
2020-08-12 11:26:51 -05:00
|
|
|
fn get_substitution_inner(&self, node: &syntax::SyntaxNode) -> Option<syntax::SyntaxNode> {
|
2020-07-31 05:06:38 -05:00
|
|
|
let type_ref = ast::Type::cast(node.clone())?;
|
2020-01-10 11:26:18 -06:00
|
|
|
let path = match &type_ref {
|
2020-07-31 05:06:38 -05:00
|
|
|
ast::Type::PathType(path_type) => path_type.path()?,
|
2020-01-10 11:26:18 -06:00
|
|
|
_ => return None,
|
|
|
|
};
|
2020-08-11 04:19:02 -05:00
|
|
|
// FIXME: use `hir::Path::from_src` instead.
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let path = hir::Path::from_ast(path)?;
|
2020-02-18 11:35:10 -06:00
|
|
|
let resolution = self.source_scope.resolve_hir_path(&path)?;
|
2020-01-10 11:26:18 -06:00
|
|
|
match resolution {
|
|
|
|
hir::PathResolution::TypeParam(tp) => Some(self.substs.get(&tp)?.syntax().clone()),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 05:42:44 -06:00
|
|
|
impl<'a> AstTransform<'a> for SubstituteTypeParams<'a> {
|
2020-08-12 11:26:51 -05:00
|
|
|
fn get_substitution(&self, node: &syntax::SyntaxNode) -> Option<syntax::SyntaxNode> {
|
2020-01-10 11:26:18 -06:00
|
|
|
self.get_substitution_inner(node).or_else(|| self.previous.get_substitution(node))
|
|
|
|
}
|
|
|
|
fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a> {
|
|
|
|
Box::new(SubstituteTypeParams { previous: other, ..self })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 05:42:44 -06:00
|
|
|
pub struct QualifyPaths<'a> {
|
2020-07-01 01:34:45 -05:00
|
|
|
target_scope: &'a SemanticsScope<'a>,
|
|
|
|
source_scope: &'a SemanticsScope<'a>,
|
2020-01-10 11:26:18 -06:00
|
|
|
previous: Box<dyn AstTransform<'a> + 'a>,
|
|
|
|
}
|
|
|
|
|
2020-02-25 05:42:44 -06:00
|
|
|
impl<'a> QualifyPaths<'a> {
|
2020-07-01 01:34:45 -05:00
|
|
|
pub fn new(target_scope: &'a SemanticsScope<'a>, source_scope: &'a SemanticsScope<'a>) -> Self {
|
2020-03-13 12:02:04 -05:00
|
|
|
Self { target_scope, source_scope, previous: Box::new(NullTransformer) }
|
2020-01-10 11:26:18 -06:00
|
|
|
}
|
|
|
|
|
2020-08-12 11:26:51 -05:00
|
|
|
fn get_substitution_inner(&self, node: &syntax::SyntaxNode) -> Option<syntax::SyntaxNode> {
|
2020-01-10 11:26:18 -06:00
|
|
|
// FIXME handle value ns?
|
2020-02-18 11:35:10 -06:00
|
|
|
let from = self.target_scope.module()?;
|
|
|
|
let p = ast::Path::cast(node.clone())?;
|
2020-01-10 11:26:18 -06:00
|
|
|
if p.segment().and_then(|s| s.param_list()).is_some() {
|
|
|
|
// don't try to qualify `Fn(Foo) -> Bar` paths, they are in prelude anyway
|
|
|
|
return None;
|
|
|
|
}
|
2020-08-11 04:19:02 -05:00
|
|
|
// FIXME: use `hir::Path::from_src` instead.
|
|
|
|
#[allow(deprecated)]
|
|
|
|
let hir_path = hir::Path::from_ast(p.clone());
|
|
|
|
let resolution = self.source_scope.resolve_hir_path(&hir_path?)?;
|
2020-01-10 11:26:18 -06:00
|
|
|
match resolution {
|
|
|
|
PathResolution::Def(def) => {
|
2020-07-01 01:34:45 -05:00
|
|
|
let found_path = from.find_use_path(self.source_scope.db.upcast(), def)?;
|
2020-01-15 11:48:28 -06:00
|
|
|
let mut path = path_to_ast(found_path);
|
|
|
|
|
|
|
|
let type_args = p
|
2020-01-10 11:26:18 -06:00
|
|
|
.segment()
|
2020-07-31 11:29:29 -05:00
|
|
|
.and_then(|s| s.generic_arg_list())
|
2020-02-18 11:35:10 -06:00
|
|
|
.map(|arg_list| apply(self, arg_list));
|
2020-01-15 11:48:28 -06:00
|
|
|
if let Some(type_args) = type_args {
|
|
|
|
let last_segment = path.segment().unwrap();
|
|
|
|
path = path.with_segment(last_segment.with_type_args(type_args))
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(path.syntax().clone())
|
2020-01-10 11:26:18 -06:00
|
|
|
}
|
|
|
|
PathResolution::Local(_)
|
|
|
|
| PathResolution::TypeParam(_)
|
|
|
|
| PathResolution::SelfType(_) => None,
|
|
|
|
PathResolution::Macro(_) => None,
|
|
|
|
PathResolution::AssocItem(_) => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-18 11:35:10 -06:00
|
|
|
pub fn apply<'a, N: AstNode>(transformer: &dyn AstTransform<'a>, node: N) -> N {
|
2020-03-24 11:03:05 -05:00
|
|
|
SyntaxRewriter::from_fn(|element| match element {
|
2020-08-12 11:26:51 -05:00
|
|
|
syntax::SyntaxElement::Node(n) => {
|
2020-02-18 11:35:10 -06:00
|
|
|
let replacement = transformer.get_substitution(&n)?;
|
2020-01-10 11:26:18 -06:00
|
|
|
Some(replacement.into())
|
|
|
|
}
|
|
|
|
_ => None,
|
2020-03-24 11:03:05 -05:00
|
|
|
})
|
|
|
|
.rewrite_ast(&node)
|
2020-01-10 11:26:18 -06:00
|
|
|
}
|
|
|
|
|
2020-02-25 05:42:44 -06:00
|
|
|
impl<'a> AstTransform<'a> for QualifyPaths<'a> {
|
2020-08-12 11:26:51 -05:00
|
|
|
fn get_substitution(&self, node: &syntax::SyntaxNode) -> Option<syntax::SyntaxNode> {
|
2020-01-10 11:26:18 -06:00
|
|
|
self.get_substitution_inner(node).or_else(|| self.previous.get_substitution(node))
|
|
|
|
}
|
|
|
|
fn chain_before(self, other: Box<dyn AstTransform<'a> + 'a>) -> Box<dyn AstTransform<'a> + 'a> {
|
|
|
|
Box::new(QualifyPaths { previous: other, ..self })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 14:42:04 -06:00
|
|
|
pub(crate) fn path_to_ast(path: hir::ModPath) -> ast::Path {
|
2020-01-10 11:40:45 -06:00
|
|
|
let parse = ast::SourceFile::parse(&path.to_string());
|
2020-03-02 06:59:35 -06:00
|
|
|
parse
|
|
|
|
.tree()
|
|
|
|
.syntax()
|
|
|
|
.descendants()
|
|
|
|
.find_map(ast::Path::cast)
|
|
|
|
.unwrap_or_else(|| panic!("failed to parse path {:?}, `{}`", path, path))
|
2020-01-10 11:40:45 -06:00
|
|
|
}
|