1+ use ide_db:: famous_defs:: FamousDefs ;
12use syntax:: {
23 ast:: { self , make} ,
34 ted, AstNode ,
45} ;
56
67use crate :: { AssistContext , AssistId , AssistKind , Assists } ;
78
8- // Generate proper `index_mut` method body refer to `index` method body may impossible due to the unpredicable case [#15581].
9+ // FIXME: Generate proper `index_mut` method body refer to `index` method body may impossible due to the unpredicable case [#15581].
910// Here just leave the `index_mut` method body be same as `index` method body, user can modify it manually to meet their need.
1011
1112// Assist: generate_mut_trait_impl
1213//
1314// Adds a IndexMut impl from the `Index` trait.
1415//
1516// ```
17+ // # //- minicore: index
1618// pub enum Axis { X = 0, Y = 1, Z = 2 }
1719//
18- // impl<T> Index$0<Axis> for [T; 3] {
20+ // impl<T> core::ops:: Index$0<Axis> for [T; 3] {
1921// type Output = T;
2022//
2123// fn index(&self, index: Axis) -> &Self::Output {
@@ -27,13 +29,13 @@ use crate::{AssistContext, AssistId, AssistKind, Assists};
2729// ```
2830// pub enum Axis { X = 0, Y = 1, Z = 2 }
2931//
30- // $0impl<T> IndexMut<Axis> for [T; 3] {
32+ // $0impl<T> core::ops:: IndexMut<Axis> for [T; 3] {
3133// fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
3234// &self[index as usize]
3335// }
3436// }
3537//
36- // impl<T> Index<Axis> for [T; 3] {
38+ // impl<T> core::ops:: Index<Axis> for [T; 3] {
3739// type Output = T;
3840//
3941// fn index(&self, index: Axis) -> &Self::Output {
@@ -45,9 +47,10 @@ pub(crate) fn generate_mut_trait_impl(acc: &mut Assists, ctx: &AssistContext<'_>
4547 let impl_def = ctx. find_node_at_offset :: < ast:: Impl > ( ) ?. clone_for_update ( ) ;
4648
4749 let trait_ = impl_def. trait_ ( ) ?;
48- if let ast:: Type :: PathType ( trait_type) = trait_. clone ( ) {
49- let trait_name = trait_type. path ( ) ?. segment ( ) ?. name_ref ( ) ?. to_string ( ) ;
50- if trait_name != "Index" {
50+ if let ast:: Type :: PathType ( trait_path) = trait_. clone ( ) {
51+ let trait_type = ctx. sema . resolve_trait ( & trait_path. path ( ) ?) ?;
52+ let scope = ctx. sema . scope ( trait_path. syntax ( ) ) ?;
53+ if trait_type != FamousDefs ( & ctx. sema , scope. krate ( ) ) . core_convert_Index ( ) ? {
5154 return None ;
5255 }
5356 }
@@ -118,9 +121,10 @@ mod tests {
118121 check_assist (
119122 generate_mut_trait_impl,
120123 r#"
124+ //- minicore: index
121125pub enum Axis { X = 0, Y = 1, Z = 2 }
122126
123- impl<T> Index$0<Axis> for [T; 3] {
127+ impl<T> core::ops:: Index$0<Axis> for [T; 3] {
124128 type Output = T;
125129
126130 fn index(&self, index: Axis) -> &Self::Output {
@@ -131,13 +135,13 @@ impl<T> Index$0<Axis> for [T; 3] {
131135 r#"
132136pub enum Axis { X = 0, Y = 1, Z = 2 }
133137
134- $0impl<T> IndexMut<Axis> for [T; 3] {
138+ $0impl<T> core::ops:: IndexMut<Axis> for [T; 3] {
135139 fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
136140 &self[index as usize]
137141 }
138142}
139143
140- impl<T> Index<Axis> for [T; 3] {
144+ impl<T> core::ops:: Index<Axis> for [T; 3] {
141145 type Output = T;
142146
143147 fn index(&self, index: Axis) -> &Self::Output {
@@ -150,9 +154,10 @@ impl<T> Index<Axis> for [T; 3] {
150154 check_assist (
151155 generate_mut_trait_impl,
152156 r#"
157+ //- minicore: index
153158pub enum Axis { X = 0, Y = 1, Z = 2 }
154159
155- impl<T> Index$0<Axis> for [T; 3] where T: Copy {
160+ impl<T> core::ops:: Index$0<Axis> for [T; 3] where T: Copy {
156161 type Output = T;
157162
158163 fn index(&self, index: Axis) -> &Self::Output {
@@ -164,14 +169,14 @@ impl<T> Index$0<Axis> for [T; 3] where T: Copy {
164169 r#"
165170pub enum Axis { X = 0, Y = 1, Z = 2 }
166171
167- $0impl<T> IndexMut<Axis> for [T; 3] where T: Copy {
172+ $0impl<T> core::ops:: IndexMut<Axis> for [T; 3] where T: Copy {
168173 fn index_mut(&mut self, index: Axis) -> &mut Self::Output {
169174 let var_name = &self[index as usize];
170175 var_name
171176 }
172177}
173178
174- impl<T> Index<Axis> for [T; 3] where T: Copy {
179+ impl<T> core::ops:: Index<Axis> for [T; 3] where T: Copy {
175180 type Output = T;
176181
177182 fn index(&self, index: Axis) -> &Self::Output {
@@ -188,7 +193,9 @@ impl<T> Index<Axis> for [T; 3] where T: Copy {
188193 check_assist_not_applicable (
189194 generate_mut_trait_impl,
190195 r#"
191- impl<T> Add$0<i32> for [T; 3] {}
196+ pub trait Index<Idx: ?Sized> {}
197+
198+ impl<T> Index$0<i32> for [T; 3] {}
192199"# ,
193200 ) ;
194201 }
0 commit comments