Skip to content

Commit 7e08933

Browse files
committed
the "implement missing members" assist's const transformation patched
1 parent 8a3c214 commit 7e08933

File tree

3 files changed

+70
-15
lines changed

3 files changed

+70
-15
lines changed

crates/ide-assists/src/handlers/add_missing_impl_members.rs

+35
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,13 @@ impl<'x, 'y, T, V, U: Default> Trait<'x, 'y, T, V, U> for () {
423423
check_assist(
424424
add_missing_default_members,
425425
r#"
426+
struct Bar<const: N: bool> {
427+
bar: [i32, N]
428+
}
429+
426430
trait Foo<const N: usize, T> {
427431
fn get_n_sq(&self, arg: &T) -> usize { N * N }
432+
fn get_array(&self, arg: Bar<N>) -> [i32; N] { [1; N] }
428433
}
429434
430435
struct S<T> {
@@ -435,8 +440,13 @@ impl<const X: usize, Y, Z> Foo<X, Z> for S<Y> {
435440
$0
436441
}"#,
437442
r#"
443+
struct Bar<const: N: bool> {
444+
bar: [i32, N]
445+
}
446+
438447
trait Foo<const N: usize, T> {
439448
fn get_n_sq(&self, arg: &T) -> usize { N * N }
449+
fn get_array(&self, arg: Bar<N>) -> [i32; N] { [1; N] }
440450
}
441451
442452
struct S<T> {
@@ -445,6 +455,31 @@ struct S<T> {
445455
446456
impl<const X: usize, Y, Z> Foo<X, Z> for S<Y> {
447457
$0fn get_n_sq(&self, arg: &Z) -> usize { X * X }
458+
459+
fn get_array(&self, arg: Bar<X>) -> [i32; X] { [1; X] }
460+
}"#,
461+
)
462+
}
463+
464+
#[test]
465+
fn test_const_substitution_2() {
466+
check_assist(
467+
add_missing_default_members,
468+
r#"
469+
trait Foo<const N: usize, const M: usize, T> {
470+
fn get_sum(&self, arg: &T) -> usize { N + M }
471+
}
472+
473+
impl<X> Foo<42, {20 + 22}, X> for () {
474+
$0
475+
}"#,
476+
r#"
477+
trait Foo<const N: usize, const M: usize, T> {
478+
fn get_sum(&self, arg: &T) -> usize { N + M }
479+
}
480+
481+
impl<X> Foo<42, {20 + 22}, X> for () {
482+
$0fn get_sum(&self, arg: &X) -> usize { 42 + {20 + 22} }
448483
}"#,
449484
)
450485
}

crates/ide-assists/src/handlers/inline_call.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -958,7 +958,6 @@ fn main() {
958958
);
959959
}
960960

961-
// FIXME: const generics aren't being substituted, this is blocked on better support for them
962961
#[test]
963962
fn inline_substitutes_generics() {
964963
check_assist(
@@ -982,7 +981,7 @@ fn foo<T, const N: usize>() {
982981
fn bar<U, const M: usize>() {}
983982
984983
fn main() {
985-
bar::<usize, N>();
984+
bar::<usize, {0}>();
986985
}
987986
"#,
988987
);

crates/ide-db/src/path_transform.rs

+34-13
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,15 @@ use syntax::{
1111

1212
#[derive(Default)]
1313
struct AstSubsts {
14-
// ast::TypeArgs stands in fact for both type and const params
15-
// as consts declared elsewhere look just like type params.
16-
types_and_consts: Vec<ast::TypeArg>,
14+
types_and_consts: Vec<TypeOrConst>,
1715
lifetimes: Vec<ast::LifetimeArg>,
1816
}
1917

18+
enum TypeOrConst {
19+
Either(ast::TypeArg), // indistinguishable type or const param
20+
Const(ast::ConstArg),
21+
}
22+
2023
type LifetimeName = String;
2124

2225
/// `PathTransform` substitutes path in SyntaxNodes in bulk.
@@ -125,27 +128,38 @@ impl<'a> PathTransform<'a> {
125128
// the resulting change can be applied correctly.
126129
.zip(self.substs.types_and_consts.iter().map(Some).chain(std::iter::repeat(None)))
127130
.for_each(|(k, v)| match (k.split(db), v) {
128-
(Either::Right(t), Some(v)) => {
131+
(Either::Right(k), Some(TypeOrConst::Either(v))) => {
129132
if let Some(ty) = v.ty() {
130-
type_substs.insert(t, ty.clone());
133+
type_substs.insert(k, ty.clone());
131134
}
132135
}
133-
(Either::Right(t), None) => {
134-
if let Some(default) = t.default(db) {
136+
(Either::Right(k), None) => {
137+
if let Some(default) = k.default(db) {
135138
if let Some(default) =
136139
&default.display_source_code(db, source_module.into(), false).ok()
137140
{
138-
type_substs.insert(t, ast::make::ty(default).clone_for_update());
139-
default_types.push(t);
141+
type_substs.insert(k, ast::make::ty(default).clone_for_update());
142+
default_types.push(k);
140143
}
141144
}
142145
}
143-
(Either::Left(c), Some(v)) => {
146+
(Either::Left(k), Some(TypeOrConst::Either(v))) => {
144147
if let Some(ty) = v.ty() {
145-
const_substs.insert(c, ty.syntax().clone());
148+
const_substs.insert(k, ty.syntax().clone());
149+
}
150+
}
151+
(Either::Left(k), Some(TypeOrConst::Const(v))) => {
152+
if let Some(expr) = v.expr() {
153+
// FIXME: expressions in curly brackets can cause ambiguity after insertion
154+
// (e.g. `N * 2` -> `{1 + 1} * 2`; it's unclear whether `{1 + 1}`
155+
// is a standalone statement or a part of another expresson)
156+
// and sometimes require slight modifications; see
157+
// https://doc.rust-lang.org/reference/statements.html#expression-statements
158+
const_substs.insert(k, expr.syntax().clone());
146159
}
147160
}
148161
(Either::Left(_), None) => (), // FIXME: get default const value
162+
_ => (), // ignore mismatching params
149163
});
150164
let lifetime_substs: FxHashMap<_, _> = self
151165
.generic_def
@@ -201,6 +215,9 @@ impl<'a> Ctx<'a> {
201215
fn transform_default_type_substs(&self, default_types: Vec<hir::TypeParam>) {
202216
for k in default_types {
203217
let v = self.type_substs.get(&k).unwrap();
218+
// `transform_path` may update a node's parent and that would break the
219+
// tree traversal. Thus all paths in the tree are collected into a vec
220+
// so that such operation is safe.
204221
let paths = postorder(&v.syntax()).filter_map(ast::Path::cast).collect::<Vec<_>>();
205222
for path in paths {
206223
self.transform_path(path);
@@ -326,8 +343,12 @@ fn get_type_args_from_arg_list(generic_arg_list: ast::GenericArgList) -> Option<
326343
// Const params are marked as consts on definition only,
327344
// being passed to the trait they are indistguishable from type params;
328345
// anyway, we don't really need to distinguish them here.
329-
ast::GenericArg::TypeArg(type_or_const_arg) => {
330-
result.types_and_consts.push(type_or_const_arg)
346+
ast::GenericArg::TypeArg(type_arg) => {
347+
result.types_and_consts.push(TypeOrConst::Either(type_arg))
348+
}
349+
// Some const values are recognized correctly.
350+
ast::GenericArg::ConstArg(const_arg) => {
351+
result.types_and_consts.push(TypeOrConst::Const(const_arg));
331352
}
332353
ast::GenericArg::LifetimeArg(l_arg) => result.lifetimes.push(l_arg),
333354
_ => (),

0 commit comments

Comments
 (0)