diff options
author | Aleksey Kladov <[email protected]> | 2021-06-17 22:30:22 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2021-06-17 22:30:22 +0100 |
commit | ebb591a5704f767d783d57af7022272f665318ac (patch) | |
tree | 76a5268851cf7b79447d5de79514c0d0b579e823 /crates | |
parent | 9b013ffcae672595aeb1aee9de907e05fb7ca065 (diff) |
internal: add derive and ord support to minicore
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide/src/hover.rs | 4 | ||||
-rw-r--r-- | crates/ide_assists/src/handlers/apply_demorgan.rs | 114 | ||||
-rw-r--r-- | crates/ide_assists/src/tests.rs | 1 | ||||
-rw-r--r-- | crates/ide_db/src/helpers/famous_defs_fixture.rs | 10 | ||||
-rw-r--r-- | crates/test_utils/src/minicore.rs | 62 |
5 files changed, 130 insertions, 61 deletions
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 01dd0c0da..e81bcf73e 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: 247..429, | 3019 | full_range: 248..430, |
3020 | focus_range: 286..292, | 3020 | focus_range: 287..293, |
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/apply_demorgan.rs b/crates/ide_assists/src/handlers/apply_demorgan.rs index c93959e66..e2bd6e456 100644 --- a/crates/ide_assists/src/handlers/apply_demorgan.rs +++ b/crates/ide_assists/src/handlers/apply_demorgan.rs | |||
@@ -147,74 +147,92 @@ fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> { | |||
147 | 147 | ||
148 | #[cfg(test)] | 148 | #[cfg(test)] |
149 | mod tests { | 149 | mod tests { |
150 | use ide_db::helpers::FamousDefs; | ||
151 | |||
152 | use super::*; | ||
153 | |||
154 | use crate::tests::{check_assist, check_assist_not_applicable}; | 150 | use crate::tests::{check_assist, check_assist_not_applicable}; |
155 | 151 | ||
156 | const ORDABLE_FIXTURE: &'static str = r" | 152 | use super::*; |
157 | //- /lib.rs deps:core crate:ordable | ||
158 | struct NonOrderable; | ||
159 | struct Orderable; | ||
160 | impl core::cmp::Ord for Orderable {} | ||
161 | "; | ||
162 | |||
163 | fn check(ra_fixture_before: &str, ra_fixture_after: &str) { | ||
164 | let before = &format!( | ||
165 | "//- /main.rs crate:main deps:core,ordable\n{}\n{}{}", | ||
166 | ra_fixture_before, | ||
167 | FamousDefs::FIXTURE, | ||
168 | ORDABLE_FIXTURE | ||
169 | ); | ||
170 | check_assist(apply_demorgan, before, &format!("{}\n", ra_fixture_after)); | ||
171 | } | ||
172 | 153 | ||
173 | #[test] | 154 | #[test] |
174 | fn demorgan_handles_leq() { | 155 | fn demorgan_handles_leq() { |
175 | check( | 156 | check_assist( |
176 | r"use ordable::Orderable; | 157 | apply_demorgan, |
158 | r#" | ||
159 | //- minicore: ord, derive | ||
160 | #[derive(PartialEq, Eq, PartialOrd, Ord)] | ||
161 | struct S; | ||
162 | |||
177 | fn f() { | 163 | fn f() { |
178 | Orderable < Orderable &&$0 Orderable <= Orderable | 164 | S < S &&$0 S <= S |
179 | }", | 165 | } |
180 | r"use ordable::Orderable; | 166 | "#, |
167 | r#" | ||
168 | #[derive(PartialEq, Eq, PartialOrd, Ord)] | ||
169 | struct S; | ||
170 | |||
181 | fn f() { | 171 | fn f() { |
182 | !(Orderable >= Orderable || Orderable > Orderable) | 172 | !(S >= S || S > S) |
183 | }", | 173 | } |
174 | "#, | ||
184 | ); | 175 | ); |
185 | check( | 176 | |
186 | r"use ordable::NonOrderable; | 177 | check_assist( |
178 | apply_demorgan, | ||
179 | r#" | ||
180 | //- minicore: ord, derive | ||
181 | struct S; | ||
182 | |||
187 | fn f() { | 183 | fn f() { |
188 | NonOrderable < NonOrderable &&$0 NonOrderable <= NonOrderable | 184 | S < S &&$0 S <= S |
189 | }", | 185 | } |
190 | r"use ordable::NonOrderable; | 186 | "#, |
187 | r#" | ||
188 | struct S; | ||
189 | |||
191 | fn f() { | 190 | fn f() { |
192 | !(!(NonOrderable < NonOrderable) || !(NonOrderable <= NonOrderable)) | 191 | !(!(S < S) || !(S <= S)) |
193 | }", | 192 | } |
193 | "#, | ||
194 | ); | 194 | ); |
195 | } | 195 | } |
196 | 196 | ||
197 | #[test] | 197 | #[test] |
198 | fn demorgan_handles_geq() { | 198 | fn demorgan_handles_geq() { |
199 | check( | 199 | check_assist( |
200 | r"use ordable::Orderable; | 200 | apply_demorgan, |
201 | r#" | ||
202 | //- minicore: ord, derive | ||
203 | #[derive(PartialEq, Eq, PartialOrd, Ord)] | ||
204 | struct S; | ||
205 | |||
201 | fn f() { | 206 | fn f() { |
202 | Orderable > Orderable &&$0 Orderable >= Orderable | 207 | S > S &&$0 S >= S |
203 | }", | 208 | } |
204 | r"use ordable::Orderable; | 209 | "#, |
210 | r#" | ||
211 | #[derive(PartialEq, Eq, PartialOrd, Ord)] | ||
212 | struct S; | ||
213 | |||
205 | fn f() { | 214 | fn f() { |
206 | !(Orderable <= Orderable || Orderable < Orderable) | 215 | !(S <= S || S < S) |
207 | }", | 216 | } |
217 | "#, | ||
208 | ); | 218 | ); |
209 | check( | 219 | check_assist( |
210 | r"use ordable::NonOrderable; | 220 | apply_demorgan, |
221 | r#" | ||
222 | //- minicore: ord, derive | ||
223 | struct S; | ||
224 | |||
211 | fn f() { | 225 | fn f() { |
212 | Orderable > Orderable &&$0 Orderable >= Orderable | 226 | S > S &&$0 S >= S |
213 | }", | 227 | } |
214 | r"use ordable::NonOrderable; | 228 | "#, |
229 | r#" | ||
230 | struct S; | ||
231 | |||
215 | fn f() { | 232 | fn f() { |
216 | !(!(Orderable > Orderable) || !(Orderable >= Orderable)) | 233 | !(!(S > S) || !(S >= S)) |
217 | }", | 234 | } |
235 | "#, | ||
218 | ); | 236 | ); |
219 | } | 237 | } |
220 | 238 | ||
diff --git a/crates/ide_assists/src/tests.rs b/crates/ide_assists/src/tests.rs index 29bd4a563..4e96ff1ec 100644 --- a/crates/ide_assists/src/tests.rs +++ b/crates/ide_assists/src/tests.rs | |||
@@ -35,6 +35,7 @@ pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { | |||
35 | RootDatabase::with_single_file(text) | 35 | RootDatabase::with_single_file(text) |
36 | } | 36 | } |
37 | 37 | ||
38 | #[track_caller] | ||
38 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { | 39 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { |
39 | let ra_fixture_after = trim_indent(ra_fixture_after); | 40 | let ra_fixture_after = trim_indent(ra_fixture_after); |
40 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None); | 41 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None); |
diff --git a/crates/ide_db/src/helpers/famous_defs_fixture.rs b/crates/ide_db/src/helpers/famous_defs_fixture.rs index fa4fc5307..6310fc0e1 100644 --- a/crates/ide_db/src/helpers/famous_defs_fixture.rs +++ b/crates/ide_db/src/helpers/famous_defs_fixture.rs | |||
@@ -1,15 +1,5 @@ | |||
1 | //- /libcore.rs crate:core | 1 | //- /libcore.rs crate:core |
2 | //! Signatures of traits, types and functions from the core lib for use in tests. | 2 | //! Signatures of traits, types and functions from the core lib for use in tests. |
3 | pub mod cmp { | ||
4 | |||
5 | pub trait Ord { | ||
6 | fn cmp(&self, other: &Self) -> Ordering; | ||
7 | fn max(self, other: Self) -> Self; | ||
8 | fn min(self, other: Self) -> Self; | ||
9 | fn clamp(self, min: Self, max: Self) -> Self; | ||
10 | } | ||
11 | } | ||
12 | |||
13 | pub mod prelude { | 3 | pub mod prelude { |
14 | pub mod rust_2018 { | 4 | pub mod rust_2018 { |
15 | pub use crate::{ | 5 | pub use crate::{ |
diff --git a/crates/test_utils/src/minicore.rs b/crates/test_utils/src/minicore.rs index a5a7c2f7d..9ec541c57 100644 --- a/crates/test_utils/src/minicore.rs +++ b/crates/test_utils/src/minicore.rs | |||
@@ -24,6 +24,9 @@ | |||
24 | //! iterators: iterator | 24 | //! iterators: iterator |
25 | //! default: sized | 25 | //! default: sized |
26 | //! from: sized | 26 | //! from: sized |
27 | //! eq: sized | ||
28 | //! ord: eq, option | ||
29 | //! derive: | ||
27 | 30 | ||
28 | pub mod marker { | 31 | pub mod marker { |
29 | // region:sized | 32 | // region:sized |
@@ -173,6 +176,49 @@ pub mod ops { | |||
173 | // endregion:fn | 176 | // endregion:fn |
174 | } | 177 | } |
175 | 178 | ||
179 | // region:eq | ||
180 | pub mod cmp { | ||
181 | #[lang = "eq"] | ||
182 | pub trait PartialEq<Rhs: ?Sized = Self> { | ||
183 | fn eq(&self, other: &Rhs) -> bool; | ||
184 | } | ||
185 | |||
186 | pub trait Eq: PartialEq<Self> {} | ||
187 | |||
188 | // region:derive | ||
189 | #[rustc_builtin_macro] | ||
190 | pub macro PartialEq($item:item) {} | ||
191 | #[rustc_builtin_macro] | ||
192 | pub macro Eq($item:item) {} | ||
193 | // endregion:derive | ||
194 | |||
195 | // region:ord | ||
196 | #[lang = "partial_ord"] | ||
197 | pub trait PartialOrd<Rhs: ?Sized = Self>: PartialEq<Rhs> { | ||
198 | fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>; | ||
199 | } | ||
200 | |||
201 | pub trait Ord: Eq + PartialOrd<Self> { | ||
202 | fn cmp(&self, other: &Self) -> Ordering; | ||
203 | } | ||
204 | |||
205 | pub enum Ordering { | ||
206 | Less = -1, | ||
207 | Equal = 0, | ||
208 | Greater = 1, | ||
209 | } | ||
210 | |||
211 | // region:derive | ||
212 | #[rustc_builtin_macro] | ||
213 | pub macro PartialOrd($item:item) {} | ||
214 | #[rustc_builtin_macro] | ||
215 | pub macro Ord($item:item) {} | ||
216 | // endregion:derive | ||
217 | |||
218 | // endregion:ord | ||
219 | } | ||
220 | // endregion:eq | ||
221 | |||
176 | // region:slice | 222 | // region:slice |
177 | pub mod slice { | 223 | pub mod slice { |
178 | #[lang = "slice"] | 224 | #[lang = "slice"] |
@@ -342,16 +388,30 @@ pub mod iter { | |||
342 | } | 388 | } |
343 | // endregion:iterator | 389 | // endregion:iterator |
344 | 390 | ||
391 | // region:derive | ||
392 | mod macros { | ||
393 | pub(crate) mod builtin { | ||
394 | #[rustc_builtin_macro] | ||
395 | pub macro derive($item:item) { | ||
396 | /* compiler built-in */ | ||
397 | } | ||
398 | } | ||
399 | } | ||
400 | // endregion:derive | ||
401 | |||
345 | pub mod prelude { | 402 | pub mod prelude { |
346 | pub mod v1 { | 403 | pub mod v1 { |
347 | pub use crate::{ | 404 | pub use crate::{ |
405 | cmp::{Eq, PartialEq}, // :eq | ||
406 | cmp::{Ord, PartialOrd}, // :ord | ||
407 | convert::{From, Into}, // :from | ||
348 | default::Default, // :default | 408 | default::Default, // :default |
349 | iter::{IntoIterator, Iterator}, // :iterator | 409 | iter::{IntoIterator, Iterator}, // :iterator |
410 | macros::builtin::derive, // :derive | ||
350 | marker::Sized, // :sized | 411 | marker::Sized, // :sized |
351 | ops::{Fn, FnMut, FnOnce}, // :fn | 412 | ops::{Fn, FnMut, FnOnce}, // :fn |
352 | option::Option::{self, None, Some}, // :option | 413 | option::Option::{self, None, Some}, // :option |
353 | result::Result::{self, Err, Ok}, // :result | 414 | result::Result::{self, Err, Ok}, // :result |
354 | convert::{From, Into}, // :from | ||
355 | }; | 415 | }; |
356 | } | 416 | } |
357 | 417 | ||