aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ide/src/hover.rs4
-rw-r--r--crates/ide_assists/src/handlers/convert_into_to_from.rs44
-rw-r--r--crates/ide_assists/src/handlers/generate_from_impl_for_enum.rs105
-rw-r--r--crates/ide_db/src/helpers/famous_defs_fixture.rs10
-rw-r--r--crates/test_utils/src/minicore.rs28
5 files changed, 129 insertions, 62 deletions
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs
index 592610eab..01dd0c0da 100644
--- a/crates/ide/src/hover.rs
+++ b/crates/ide/src/hover.rs
@@ -3016,8 +3016,8 @@ fn foo() {
3016 file_id: FileId( 3016 file_id: FileId(
3017 1, 3017 1,
3018 ), 3018 ),
3019 full_range: 246..428, 3019 full_range: 247..429,
3020 focus_range: 285..291, 3020 focus_range: 286..292,
3021 name: "Future", 3021 name: "Future",
3022 kind: Trait, 3022 kind: Trait,
3023 description: "pub trait Future", 3023 description: "pub trait Future",
diff --git a/crates/ide_assists/src/handlers/convert_into_to_from.rs b/crates/ide_assists/src/handlers/convert_into_to_from.rs
index 199e1ad5c..79a0c4879 100644
--- a/crates/ide_assists/src/handlers/convert_into_to_from.rs
+++ b/crates/ide_assists/src/handlers/convert_into_to_from.rs
@@ -6,6 +6,8 @@ use syntax::ast::{self, AstNode, NameOwner};
6 6
7use crate::{AssistContext, AssistId, AssistKind, Assists}; 7use crate::{AssistContext, AssistId, AssistKind, Assists};
8 8
9// FIXME: this should be a diagnostic
10
9// Assist: convert_into_to_from 11// Assist: convert_into_to_from
10// 12//
11// Converts an Into impl to an equivalent From impl. 13// Converts an Into impl to an equivalent From impl.
@@ -114,12 +116,14 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext) -> Op
114mod tests { 116mod tests {
115 use super::*; 117 use super::*;
116 118
117 use crate::tests::check_assist; 119 use crate::tests::{check_assist, check_assist_not_applicable};
118 120
119 #[test] 121 #[test]
120 fn convert_into_to_from_converts_a_struct() { 122 fn convert_into_to_from_converts_a_struct() {
121 check_convert_into_to_from( 123 check_assist(
124 convert_into_to_from,
122 r#" 125 r#"
126//- minicore: from
123struct Thing { 127struct Thing {
124 a: String, 128 a: String,
125 b: usize 129 b: usize
@@ -154,8 +158,10 @@ impl From<usize> for Thing {
154 158
155 #[test] 159 #[test]
156 fn convert_into_to_from_converts_enums() { 160 fn convert_into_to_from_converts_enums() {
157 check_convert_into_to_from( 161 check_assist(
162 convert_into_to_from,
158 r#" 163 r#"
164//- minicore: from
159enum Thing { 165enum Thing {
160 Foo(String), 166 Foo(String),
161 Bar(String) 167 Bar(String)
@@ -190,8 +196,10 @@ impl From<Thing> for String {
190 196
191 #[test] 197 #[test]
192 fn convert_into_to_from_on_enum_with_lifetimes() { 198 fn convert_into_to_from_on_enum_with_lifetimes() {
193 check_convert_into_to_from( 199 check_assist(
200 convert_into_to_from,
194 r#" 201 r#"
202//- minicore: from
195enum Thing<'a> { 203enum Thing<'a> {
196 Foo(&'a str), 204 Foo(&'a str),
197 Bar(&'a str) 205 Bar(&'a str)
@@ -226,8 +234,10 @@ impl<'a> From<Thing<'a>> for &'a str {
226 234
227 #[test] 235 #[test]
228 fn convert_into_to_from_works_on_references() { 236 fn convert_into_to_from_works_on_references() {
229 check_convert_into_to_from( 237 check_assist(
238 convert_into_to_from,
230 r#" 239 r#"
240//- minicore: from
231struct Thing(String); 241struct Thing(String);
232 242
233impl $0core::convert::Into<String> for &Thing { 243impl $0core::convert::Into<String> for &Thing {
@@ -250,8 +260,10 @@ impl From<&Thing> for String {
250 260
251 #[test] 261 #[test]
252 fn convert_into_to_from_works_on_qualified_structs() { 262 fn convert_into_to_from_works_on_qualified_structs() {
253 check_convert_into_to_from( 263 check_assist(
264 convert_into_to_from,
254 r#" 265 r#"
266//- minicore: from
255mod things { 267mod things {
256 pub struct Thing(String); 268 pub struct Thing(String);
257 pub struct BetterThing(String); 269 pub struct BetterThing(String);
@@ -280,8 +292,10 @@ impl From<&things::Thing> for things::BetterThing {
280 292
281 #[test] 293 #[test]
282 fn convert_into_to_from_works_on_qualified_enums() { 294 fn convert_into_to_from_works_on_qualified_enums() {
283 check_convert_into_to_from( 295 check_assist(
296 convert_into_to_from,
284 r#" 297 r#"
298//- minicore: from
285mod things { 299mod things {
286 pub enum Thing { 300 pub enum Thing {
287 A(String) 301 A(String)
@@ -323,10 +337,12 @@ impl From<&things::Thing> for things::BetterThing {
323 #[test] 337 #[test]
324 fn convert_into_to_from_not_applicable_on_any_trait_named_into() { 338 fn convert_into_to_from_not_applicable_on_any_trait_named_into() {
325 check_assist_not_applicable( 339 check_assist_not_applicable(
340 convert_into_to_from,
326 r#" 341 r#"
327pub trait Into<T> {{ 342//- minicore: from
343pub trait Into<T> {
328 pub fn into(self) -> T; 344 pub fn into(self) -> T;
329}} 345}
330 346
331struct Thing { 347struct Thing {
332 a: String, 348 a: String,
@@ -342,14 +358,4 @@ impl $0Into<Thing> for String {
342"#, 358"#,
343 ); 359 );
344 } 360 }
345
346 fn check_convert_into_to_from(before: &str, after: &str) {
347 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
348 check_assist(convert_into_to_from, before, after);
349 }
350
351 fn check_assist_not_applicable(before: &str) {
352 let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
353 crate::tests::check_assist_not_applicable(convert_into_to_from, before);
354 }
355} 361}
diff --git a/crates/ide_assists/src/handlers/generate_from_impl_for_enum.rs b/crates/ide_assists/src/handlers/generate_from_impl_for_enum.rs
index ce6998d82..8727be07d 100644
--- a/crates/ide_assists/src/handlers/generate_from_impl_for_enum.rs
+++ b/crates/ide_assists/src/handlers/generate_from_impl_for_enum.rs
@@ -110,14 +110,19 @@ mod tests {
110 fn test_generate_from_impl_for_enum() { 110 fn test_generate_from_impl_for_enum() {
111 check_assist( 111 check_assist(
112 generate_from_impl_for_enum, 112 generate_from_impl_for_enum,
113 "enum A { $0One(u32) }", 113 r#"
114 r#"enum A { One(u32) } 114//- minicore: from
115enum A { $0One(u32) }
116"#,
117 r#"
118enum A { One(u32) }
115 119
116impl From<u32> for A { 120impl From<u32> for A {
117 fn from(v: u32) -> Self { 121 fn from(v: u32) -> Self {
118 Self::One(v) 122 Self::One(v)
119 } 123 }
120}"#, 124}
125"#,
121 ); 126 );
122 } 127 }
123 128
@@ -125,53 +130,71 @@ impl From<u32> for A {
125 fn test_generate_from_impl_for_enum_complicated_path() { 130 fn test_generate_from_impl_for_enum_complicated_path() {
126 check_assist( 131 check_assist(
127 generate_from_impl_for_enum, 132 generate_from_impl_for_enum,
128 r#"enum A { $0One(foo::bar::baz::Boo) }"#, 133 r#"
129 r#"enum A { One(foo::bar::baz::Boo) } 134//- minicore: from
135enum A { $0One(foo::bar::baz::Boo) }
136"#,
137 r#"
138enum A { One(foo::bar::baz::Boo) }
130 139
131impl From<foo::bar::baz::Boo> for A { 140impl From<foo::bar::baz::Boo> for A {
132 fn from(v: foo::bar::baz::Boo) -> Self { 141 fn from(v: foo::bar::baz::Boo) -> Self {
133 Self::One(v) 142 Self::One(v)
134 } 143 }
135}"#, 144}
145"#,
136 ); 146 );
137 } 147 }
138 148
139 fn check_not_applicable(ra_fixture: &str) {
140 let fixture =
141 format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE);
142 check_assist_not_applicable(generate_from_impl_for_enum, &fixture)
143 }
144
145 #[test] 149 #[test]
146 fn test_add_from_impl_no_element() { 150 fn test_add_from_impl_no_element() {
147 check_not_applicable("enum A { $0One }"); 151 check_assist_not_applicable(
152 generate_from_impl_for_enum,
153 r#"
154//- minicore: from
155enum A { $0One }
156"#,
157 );
148 } 158 }
149 159
150 #[test] 160 #[test]
151 fn test_add_from_impl_more_than_one_element_in_tuple() { 161 fn test_add_from_impl_more_than_one_element_in_tuple() {
152 check_not_applicable("enum A { $0One(u32, String) }"); 162 check_assist_not_applicable(
163 generate_from_impl_for_enum,
164 r#"
165//- minicore: from
166enum A { $0One(u32, String) }
167"#,
168 );
153 } 169 }
154 170
155 #[test] 171 #[test]
156 fn test_add_from_impl_struct_variant() { 172 fn test_add_from_impl_struct_variant() {
157 check_assist( 173 check_assist(
158 generate_from_impl_for_enum, 174 generate_from_impl_for_enum,
159 "enum A { $0One { x: u32 } }", 175 r#"
160 r#"enum A { One { x: u32 } } 176//- minicore: from
177enum A { $0One { x: u32 } }
178"#,
179 r#"
180enum A { One { x: u32 } }
161 181
162impl From<u32> for A { 182impl From<u32> for A {
163 fn from(x: u32) -> Self { 183 fn from(x: u32) -> Self {
164 Self::One { x } 184 Self::One { x }
165 } 185 }
166}"#, 186}
187"#,
167 ); 188 );
168 } 189 }
169 190
170 #[test] 191 #[test]
171 fn test_add_from_impl_already_exists() { 192 fn test_add_from_impl_already_exists() {
172 cov_mark::check!(test_add_from_impl_already_exists); 193 cov_mark::check!(test_add_from_impl_already_exists);
173 check_not_applicable( 194 check_assist_not_applicable(
195 generate_from_impl_for_enum,
174 r#" 196 r#"
197//- minicore: from
175enum A { $0One(u32), } 198enum A { $0One(u32), }
176 199
177impl From<u32> for A { 200impl From<u32> for A {
@@ -187,7 +210,9 @@ impl From<u32> for A {
187 fn test_add_from_impl_different_variant_impl_exists() { 210 fn test_add_from_impl_different_variant_impl_exists() {
188 check_assist( 211 check_assist(
189 generate_from_impl_for_enum, 212 generate_from_impl_for_enum,
190 r#"enum A { $0One(u32), Two(String), } 213 r#"
214//- minicore: from
215enum A { $0One(u32), Two(String), }
191 216
192impl From<String> for A { 217impl From<String> for A {
193 fn from(v: String) -> Self { 218 fn from(v: String) -> Self {
@@ -197,8 +222,10 @@ impl From<String> for A {
197 222
198pub trait From<T> { 223pub trait From<T> {
199 fn from(T) -> Self; 224 fn from(T) -> Self;
200}"#, 225}
201 r#"enum A { One(u32), Two(String), } 226"#,
227 r#"
228enum A { One(u32), Two(String), }
202 229
203impl From<u32> for A { 230impl From<u32> for A {
204 fn from(v: u32) -> Self { 231 fn from(v: u32) -> Self {
@@ -214,7 +241,8 @@ impl From<String> for A {
214 241
215pub trait From<T> { 242pub trait From<T> {
216 fn from(T) -> Self; 243 fn from(T) -> Self;
217}"#, 244}
245"#,
218 ); 246 );
219 } 247 }
220 248
@@ -222,14 +250,19 @@ pub trait From<T> {
222 fn test_add_from_impl_static_str() { 250 fn test_add_from_impl_static_str() {
223 check_assist( 251 check_assist(
224 generate_from_impl_for_enum, 252 generate_from_impl_for_enum,
225 "enum A { $0One(&'static str) }", 253 r#"
226 r#"enum A { One(&'static str) } 254//- minicore: from
255enum A { $0One(&'static str) }
256"#,
257 r#"
258enum A { One(&'static str) }
227 259
228impl From<&'static str> for A { 260impl From<&'static str> for A {
229 fn from(v: &'static str) -> Self { 261 fn from(v: &'static str) -> Self {
230 Self::One(v) 262 Self::One(v)
231 } 263 }
232}"#, 264}
265"#,
233 ); 266 );
234 } 267 }
235 268
@@ -237,14 +270,19 @@ impl From<&'static str> for A {
237 fn test_add_from_impl_generic_enum() { 270 fn test_add_from_impl_generic_enum() {
238 check_assist( 271 check_assist(
239 generate_from_impl_for_enum, 272 generate_from_impl_for_enum,
240 "enum Generic<T, U: Clone> { $0One(T), Two(U) }", 273 r#"
241 r#"enum Generic<T, U: Clone> { One(T), Two(U) } 274//- minicore: from
275enum Generic<T, U: Clone> { $0One(T), Two(U) }
276"#,
277 r#"
278enum Generic<T, U: Clone> { One(T), Two(U) }
242 279
243impl<T, U: Clone> From<T> for Generic<T, U> { 280impl<T, U: Clone> From<T> for Generic<T, U> {
244 fn from(v: T) -> Self { 281 fn from(v: T) -> Self {
245 Self::One(v) 282 Self::One(v)
246 } 283 }
247}"#, 284}
285"#,
248 ); 286 );
249 } 287 }
250 288
@@ -252,14 +290,19 @@ impl<T, U: Clone> From<T> for Generic<T, U> {
252 fn test_add_from_impl_with_lifetime() { 290 fn test_add_from_impl_with_lifetime() {
253 check_assist( 291 check_assist(
254 generate_from_impl_for_enum, 292 generate_from_impl_for_enum,
255 "enum Generic<'a> { $0One(&'a i32) }", 293 r#"
256 r#"enum Generic<'a> { One(&'a i32) } 294//- minicore: from
295enum Generic<'a> { $0One(&'a i32) }
296"#,
297 r#"
298enum Generic<'a> { One(&'a i32) }
257 299
258impl<'a> From<&'a i32> for Generic<'a> { 300impl<'a> From<&'a i32> for Generic<'a> {
259 fn from(v: &'a i32) -> Self { 301 fn from(v: &'a i32) -> Self {
260 Self::One(v) 302 Self::One(v)
261 } 303 }
262}"#, 304}
305"#,
263 ); 306 );
264 } 307 }
265} 308}
diff --git a/crates/ide_db/src/helpers/famous_defs_fixture.rs b/crates/ide_db/src/helpers/famous_defs_fixture.rs
index d26b7162e..fa4fc5307 100644
--- a/crates/ide_db/src/helpers/famous_defs_fixture.rs
+++ b/crates/ide_db/src/helpers/famous_defs_fixture.rs
@@ -10,16 +10,6 @@ pub mod cmp {
10 } 10 }
11} 11}
12 12
13pub mod convert {
14 pub trait From<T> {
15 fn from(t: T) -> Self;
16 }
17
18 pub trait Into<T> {
19 pub fn into(self) -> T;
20 }
21}
22
23pub mod prelude { 13pub mod prelude {
24 pub mod rust_2018 { 14 pub mod rust_2018 {
25 pub use crate::{ 15 pub use crate::{
diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs
index 4093a04bc..a5a7c2f7d 100644
--- a/crates/test_utils/src/minicore.rs
+++ b/crates/test_utils/src/minicore.rs
@@ -23,6 +23,7 @@
23//! iterator: option 23//! iterator: option
24//! iterators: iterator 24//! iterators: iterator
25//! default: sized 25//! default: sized
26//! from: sized
26 27
27pub mod marker { 28pub mod marker {
28 // region:sized 29 // region:sized
@@ -46,6 +47,32 @@ pub mod default {
46} 47}
47// endregion:default 48// endregion:default
48 49
50// region:from
51pub mod convert {
52 pub trait From<T>: Sized {
53 fn from(_: T) -> Self;
54 }
55 pub trait Into<T>: Sized {
56 fn into(self) -> T;
57 }
58
59 impl<T, U> Into<U> for T
60 where
61 U: From<T>,
62 {
63 fn into(self) -> U {
64 U::from(self)
65 }
66 }
67
68 impl<T> From<T> for T {
69 fn from(t: T) -> T {
70 t
71 }
72 }
73}
74// endregion:from
75
49pub mod ops { 76pub mod ops {
50 // region:coerce_unsized 77 // region:coerce_unsized
51 mod unsize { 78 mod unsize {
@@ -324,6 +351,7 @@ pub mod prelude {
324 ops::{Fn, FnMut, FnOnce}, // :fn 351 ops::{Fn, FnMut, FnOnce}, // :fn
325 option::Option::{self, None, Some}, // :option 352 option::Option::{self, None, Some}, // :option
326 result::Result::{self, Err, Ok}, // :result 353 result::Result::{self, Err, Ok}, // :result
354 convert::{From, Into}, // :from
327 }; 355 };
328 } 356 }
329 357