Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: implement missing members doesn't transform const params and default types #15054

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
175 changes: 175 additions & 0 deletions crates/ide-assists/src/handlers/add_missing_impl_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,72 @@ impl<'x, 'y, T, V, U: Default> Trait<'x, 'y, T, V, U> for () {
);
}

#[test]
fn test_const_substitution() {
ponyii marked this conversation as resolved.
Show resolved Hide resolved
check_assist(
add_missing_default_members,
r#"
struct Bar<const: N: bool> {
bar: [i32, N]
}

trait Foo<const N: usize, T> {
fn get_n_sq(&self, arg: &T) -> usize { N * N }
fn get_array(&self, arg: Bar<N>) -> [i32; N] { [1; N] }
}

struct S<T> {
wrapped: T
}

impl<const X: usize, Y, Z> Foo<X, Z> for S<Y> {
$0
}"#,
r#"
struct Bar<const: N: bool> {
bar: [i32, N]
}

trait Foo<const N: usize, T> {
fn get_n_sq(&self, arg: &T) -> usize { N * N }
fn get_array(&self, arg: Bar<N>) -> [i32; N] { [1; N] }
}

struct S<T> {
wrapped: T
}

impl<const X: usize, Y, Z> Foo<X, Z> for S<Y> {
$0fn get_n_sq(&self, arg: &Z) -> usize { X * X }

fn get_array(&self, arg: Bar<X>) -> [i32; X] { [1; X] }
}"#,
)
}

#[test]
fn test_const_substitution_2() {
check_assist(
add_missing_default_members,
r#"
trait Foo<const N: usize, const M: usize, T> {
fn get_sum(&self, arg: &T) -> usize { N + M }
}

impl<X> Foo<42, {20 + 22}, X> for () {
$0
}"#,
r#"
trait Foo<const N: usize, const M: usize, T> {
fn get_sum(&self, arg: &T) -> usize { N + M }
}

impl<X> Foo<42, {20 + 22}, X> for () {
$0fn get_sum(&self, arg: &X) -> usize { 42 + {20 + 22} }
}"#,
)
}

#[test]
fn test_cursor_after_empty_impl_def() {
check_assist(
Expand Down Expand Up @@ -799,6 +865,115 @@ impl Foo<T> for S<T> {
)
}

#[test]
fn test_qualify_generic_default_parameter() {
check_assist(
add_missing_impl_members,
r#"
mod m {
pub struct S;
pub trait Foo<T = S> {
fn bar(&self, other: &T);
}
}

struct S;
impl m::Foo for S { $0 }"#,
r#"
mod m {
pub struct S;
pub trait Foo<T = S> {
fn bar(&self, other: &T);
}
}

struct S;
impl m::Foo for S {
fn bar(&self, other: &m::S) {
${0:todo!()}
}
}"#,
)
}

#[test]
fn test_qualify_generic_default_parameter_2() {
check_assist(
add_missing_impl_members,
r#"
mod m {
pub struct Wrapper<T, V> {
one: T,
another: V
};
pub struct S;
pub trait Foo<T = Wrapper<S, bool>> {
fn bar(&self, other: &T);
}
}

struct S;
impl m::Foo for S { $0 }"#,
r#"
mod m {
pub struct Wrapper<T, V> {
one: T,
another: V
};
pub struct S;
pub trait Foo<T = Wrapper<S, bool>> {
fn bar(&self, other: &T);
}
}

struct S;
impl m::Foo for S {
fn bar(&self, other: &m::Wrapper<m::S, bool>) {
${0:todo!()}
}
}"#,
);
}

#[test]
fn test_qualify_generic_default_parameter_3() {
check_assist(
add_missing_impl_members,
r#"
mod m {
pub struct Wrapper<T, V> {
one: T,
another: V
};
pub struct S;
pub trait Foo<T = S, V = Wrapper<T, S>> {
fn bar(&self, other: &V);
}
}

struct S;
impl m::Foo for S { $0 }"#,
r#"
mod m {
pub struct Wrapper<T, V> {
one: T,
another: V
};
pub struct S;
pub trait Foo<T = S, V = Wrapper<T, S>> {
fn bar(&self, other: &V);
}
}

struct S;
impl m::Foo for S {
fn bar(&self, other: &m::Wrapper<m::S, m::S>) {
${0:todo!()}
}
}"#,
);
}

#[test]
fn test_assoc_type_bounds_are_removed() {
check_assist(
Expand Down
3 changes: 1 addition & 2 deletions crates/ide-assists/src/handlers/inline_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,6 @@ fn main() {
);
}

// FIXME: const generics aren't being substituted, this is blocked on better support for them
#[test]
fn inline_substitutes_generics() {
check_assist(
Expand All @@ -982,7 +981,7 @@ fn foo<T, const N: usize>() {
fn bar<U, const M: usize>() {}

fn main() {
bar::<usize, N>();
bar::<usize, {0}>();
}
"#,
);
Expand Down