Skip to content

Commit 916384a

Browse files
bors[bot]matklad
andauthored
9319: internal: add derive and ord support to minicore r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 9b013ff + ebb591a commit 916384a

File tree

5 files changed

+130
-61
lines changed

5 files changed

+130
-61
lines changed

crates/ide/src/hover.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3016,8 +3016,8 @@ fn foo() {
30163016
file_id: FileId(
30173017
1,
30183018
),
3019-
full_range: 247..429,
3020-
focus_range: 286..292,
3019+
full_range: 248..430,
3020+
focus_range: 287..293,
30213021
name: "Future",
30223022
kind: Trait,
30233023
description: "pub trait Future",

crates/ide_assists/src/handlers/apply_demorgan.rs

Lines changed: 66 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -147,74 +147,92 @@ fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> {
147147

148148
#[cfg(test)]
149149
mod tests {
150-
use ide_db::helpers::FamousDefs;
151-
152-
use super::*;
153-
154150
use crate::tests::{check_assist, check_assist_not_applicable};
155151

156-
const ORDABLE_FIXTURE: &'static str = r"
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-
}
152+
use super::*;
172153

173154
#[test]
174155
fn demorgan_handles_leq() {
175-
check(
176-
r"use ordable::Orderable;
156+
check_assist(
157+
apply_demorgan,
158+
r#"
159+
//- minicore: ord, derive
160+
#[derive(PartialEq, Eq, PartialOrd, Ord)]
161+
struct S;
162+
177163
fn f() {
178-
Orderable < Orderable &&$0 Orderable <= Orderable
179-
}",
180-
r"use ordable::Orderable;
164+
S < S &&$0 S <= S
165+
}
166+
"#,
167+
r#"
168+
#[derive(PartialEq, Eq, PartialOrd, Ord)]
169+
struct S;
170+
181171
fn f() {
182-
!(Orderable >= Orderable || Orderable > Orderable)
183-
}",
172+
!(S >= S || S > S)
173+
}
174+
"#,
184175
);
185-
check(
186-
r"use ordable::NonOrderable;
176+
177+
check_assist(
178+
apply_demorgan,
179+
r#"
180+
//- minicore: ord, derive
181+
struct S;
182+
187183
fn f() {
188-
NonOrderable < NonOrderable &&$0 NonOrderable <= NonOrderable
189-
}",
190-
r"use ordable::NonOrderable;
184+
S < S &&$0 S <= S
185+
}
186+
"#,
187+
r#"
188+
struct S;
189+
191190
fn f() {
192-
!(!(NonOrderable < NonOrderable) || !(NonOrderable <= NonOrderable))
193-
}",
191+
!(!(S < S) || !(S <= S))
192+
}
193+
"#,
194194
);
195195
}
196196

197197
#[test]
198198
fn demorgan_handles_geq() {
199-
check(
200-
r"use ordable::Orderable;
199+
check_assist(
200+
apply_demorgan,
201+
r#"
202+
//- minicore: ord, derive
203+
#[derive(PartialEq, Eq, PartialOrd, Ord)]
204+
struct S;
205+
201206
fn f() {
202-
Orderable > Orderable &&$0 Orderable >= Orderable
203-
}",
204-
r"use ordable::Orderable;
207+
S > S &&$0 S >= S
208+
}
209+
"#,
210+
r#"
211+
#[derive(PartialEq, Eq, PartialOrd, Ord)]
212+
struct S;
213+
205214
fn f() {
206-
!(Orderable <= Orderable || Orderable < Orderable)
207-
}",
215+
!(S <= S || S < S)
216+
}
217+
"#,
208218
);
209-
check(
210-
r"use ordable::NonOrderable;
219+
check_assist(
220+
apply_demorgan,
221+
r#"
222+
//- minicore: ord, derive
223+
struct S;
224+
211225
fn f() {
212-
Orderable > Orderable &&$0 Orderable >= Orderable
213-
}",
214-
r"use ordable::NonOrderable;
226+
S > S &&$0 S >= S
227+
}
228+
"#,
229+
r#"
230+
struct S;
231+
215232
fn f() {
216-
!(!(Orderable > Orderable) || !(Orderable >= Orderable))
217-
}",
233+
!(!(S > S) || !(S >= S))
234+
}
235+
"#,
218236
);
219237
}
220238

crates/ide_assists/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
3535
RootDatabase::with_single_file(text)
3636
}
3737

38+
#[track_caller]
3839
pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
3940
let ra_fixture_after = trim_indent(ra_fixture_after);
4041
check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None);

crates/ide_db/src/helpers/famous_defs_fixture.rs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,5 @@
11
//- /libcore.rs crate:core
22
//! 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-
133
pub mod prelude {
144
pub mod rust_2018 {
155
pub use crate::{

crates/test_utils/src/minicore.rs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
//! iterators: iterator
2525
//! default: sized
2626
//! from: sized
27+
//! eq: sized
28+
//! ord: eq, option
29+
//! derive:
2730
2831
pub mod marker {
2932
// region:sized
@@ -173,6 +176,49 @@ pub mod ops {
173176
// endregion:fn
174177
}
175178

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+
176222
// region:slice
177223
pub mod slice {
178224
#[lang = "slice"]
@@ -342,16 +388,30 @@ pub mod iter {
342388
}
343389
// endregion:iterator
344390

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+
345402
pub mod prelude {
346403
pub mod v1 {
347404
pub use crate::{
405+
cmp::{Eq, PartialEq}, // :eq
406+
cmp::{Ord, PartialOrd}, // :ord
407+
convert::{From, Into}, // :from
348408
default::Default, // :default
349409
iter::{IntoIterator, Iterator}, // :iterator
410+
macros::builtin::derive, // :derive
350411
marker::Sized, // :sized
351412
ops::{Fn, FnMut, FnOnce}, // :fn
352413
option::Option::{self, None, Some}, // :option
353414
result::Result::{self, Err, Ok}, // :result
354-
convert::{From, Into}, // :from
355415
};
356416
}
357417

0 commit comments

Comments
 (0)