the "add missing members" assists: implemented the transformation of const param default values

This commit is contained in:
ponyii 2023-07-11 18:46:39 +04:00
parent 4ebdc6f052
commit 4e2be8e959
3 changed files with 49 additions and 11 deletions

View file

@ -21,6 +21,7 @@ enum TypeOrConst {
}
type LifetimeName = String;
type DefaultedParam = Either<hir::TypeParam, hir::ConstParam>;
/// `PathTransform` substitutes path in SyntaxNodes in bulk.
///
@ -115,7 +116,7 @@ impl<'a> PathTransform<'a> {
};
let mut type_substs: FxHashMap<hir::TypeParam, ast::Type> = Default::default();
let mut const_substs: FxHashMap<hir::ConstParam, SyntaxNode> = Default::default();
let mut default_types: Vec<hir::TypeParam> = Default::default();
let mut defaulted_params: Vec<DefaultedParam> = Default::default();
self.generic_def
.into_iter()
.flat_map(|it| it.type_params(db))
@ -139,7 +140,7 @@ impl<'a> PathTransform<'a> {
&default.display_source_code(db, source_module.into(), false).ok()
{
type_substs.insert(k, ast::make::ty(default).clone_for_update());
default_types.push(k);
defaulted_params.push(Either::Left(k));
}
}
}
@ -162,7 +163,7 @@ impl<'a> PathTransform<'a> {
if let Some(default) = k.default(db) {
if let Some(default) = ast::make::expr_const_value(&default).expr() {
const_substs.insert(k, default.syntax().clone_for_update());
// FIXME: transform the default value
defaulted_params.push(Either::Right(k));
}
}
}
@ -182,7 +183,7 @@ impl<'a> PathTransform<'a> {
target_module,
source_scope: self.source_scope,
};
ctx.transform_default_type_substs(default_types);
ctx.transform_default_values(defaulted_params);
ctx
}
}
@ -219,13 +220,19 @@ impl Ctx<'_> {
});
}
fn transform_default_type_substs(&self, default_types: Vec<hir::TypeParam>) {
for k in default_types {
let v = self.type_substs.get(&k).unwrap();
fn transform_default_values(&self, defaulted_params: Vec<DefaultedParam>) {
// By now the default values are simply copied from where they are declared
// and should be transformed. As any value is allowed to refer to previous
// generic (both type and const) parameters, they should be all iterated left-to-right.
for param in defaulted_params {
let value = match param {
Either::Left(k) => self.type_substs.get(&k).unwrap().syntax(),
Either::Right(k) => self.const_substs.get(&k).unwrap(),
};
// `transform_path` may update a node's parent and that would break the
// tree traversal. Thus all paths in the tree are collected into a vec
// so that such operation is safe.
let paths = postorder(&v.syntax()).filter_map(ast::Path::cast).collect::<Vec<_>>();
let paths = postorder(value).filter_map(ast::Path::cast).collect::<Vec<_>>();
for path in paths {
self.transform_path(path);
}