From ebb591a5704f767d783d57af7022272f665318ac Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 18 Jun 2021 00:30:22 +0300 Subject: internal: add derive and ord support to minicore --- crates/ide/src/hover.rs | 4 +- crates/ide_assists/src/handlers/apply_demorgan.rs | 114 +++++++++++++--------- crates/ide_assists/src/tests.rs | 1 + crates/ide_db/src/helpers/famous_defs_fixture.rs | 10 -- 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() { file_id: FileId( 1, ), - full_range: 247..429, - focus_range: 286..292, + full_range: 248..430, + focus_range: 287..293, name: "Future", kind: Trait, 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> { #[cfg(test)] mod tests { - use ide_db::helpers::FamousDefs; - - use super::*; - use crate::tests::{check_assist, check_assist_not_applicable}; - const ORDABLE_FIXTURE: &'static str = r" -//- /lib.rs deps:core crate:ordable -struct NonOrderable; -struct Orderable; -impl core::cmp::Ord for Orderable {} -"; - - fn check(ra_fixture_before: &str, ra_fixture_after: &str) { - let before = &format!( - "//- /main.rs crate:main deps:core,ordable\n{}\n{}{}", - ra_fixture_before, - FamousDefs::FIXTURE, - ORDABLE_FIXTURE - ); - check_assist(apply_demorgan, before, &format!("{}\n", ra_fixture_after)); - } + use super::*; #[test] fn demorgan_handles_leq() { - check( - r"use ordable::Orderable; + check_assist( + apply_demorgan, + r#" +//- minicore: ord, derive +#[derive(PartialEq, Eq, PartialOrd, Ord)] +struct S; + fn f() { - Orderable < Orderable &&$0 Orderable <= Orderable -}", - r"use ordable::Orderable; + S < S &&$0 S <= S +} +"#, + r#" +#[derive(PartialEq, Eq, PartialOrd, Ord)] +struct S; + fn f() { - !(Orderable >= Orderable || Orderable > Orderable) -}", + !(S >= S || S > S) +} +"#, ); - check( - r"use ordable::NonOrderable; + + check_assist( + apply_demorgan, + r#" +//- minicore: ord, derive +struct S; + fn f() { - NonOrderable < NonOrderable &&$0 NonOrderable <= NonOrderable -}", - r"use ordable::NonOrderable; + S < S &&$0 S <= S +} +"#, + r#" +struct S; + fn f() { - !(!(NonOrderable < NonOrderable) || !(NonOrderable <= NonOrderable)) -}", + !(!(S < S) || !(S <= S)) +} +"#, ); } #[test] fn demorgan_handles_geq() { - check( - r"use ordable::Orderable; + check_assist( + apply_demorgan, + r#" +//- minicore: ord, derive +#[derive(PartialEq, Eq, PartialOrd, Ord)] +struct S; + fn f() { - Orderable > Orderable &&$0 Orderable >= Orderable -}", - r"use ordable::Orderable; + S > S &&$0 S >= S +} +"#, + r#" +#[derive(PartialEq, Eq, PartialOrd, Ord)] +struct S; + fn f() { - !(Orderable <= Orderable || Orderable < Orderable) -}", + !(S <= S || S < S) +} +"#, ); - check( - r"use ordable::NonOrderable; + check_assist( + apply_demorgan, + r#" +//- minicore: ord, derive +struct S; + fn f() { - Orderable > Orderable &&$0 Orderable >= Orderable -}", - r"use ordable::NonOrderable; + S > S &&$0 S >= S +} +"#, + r#" +struct S; + fn f() { - !(!(Orderable > Orderable) || !(Orderable >= Orderable)) -}", + !(!(S > S) || !(S >= S)) +} +"#, ); } 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) { RootDatabase::with_single_file(text) } +#[track_caller] pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { let ra_fixture_after = trim_indent(ra_fixture_after); 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 @@ //- /libcore.rs crate:core //! Signatures of traits, types and functions from the core lib for use in tests. -pub mod cmp { - - pub trait Ord { - fn cmp(&self, other: &Self) -> Ordering; - fn max(self, other: Self) -> Self; - fn min(self, other: Self) -> Self; - fn clamp(self, min: Self, max: Self) -> Self; - } -} - pub mod prelude { pub mod rust_2018 { 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 @@ //! iterators: iterator //! default: sized //! from: sized +//! eq: sized +//! ord: eq, option +//! derive: pub mod marker { // region:sized @@ -173,6 +176,49 @@ pub mod ops { // endregion:fn } +// region:eq +pub mod cmp { + #[lang = "eq"] + pub trait PartialEq { + fn eq(&self, other: &Rhs) -> bool; + } + + pub trait Eq: PartialEq {} + + // region:derive + #[rustc_builtin_macro] + pub macro PartialEq($item:item) {} + #[rustc_builtin_macro] + pub macro Eq($item:item) {} + // endregion:derive + + // region:ord + #[lang = "partial_ord"] + pub trait PartialOrd: PartialEq { + fn partial_cmp(&self, other: &Rhs) -> Option; + } + + pub trait Ord: Eq + PartialOrd { + fn cmp(&self, other: &Self) -> Ordering; + } + + pub enum Ordering { + Less = -1, + Equal = 0, + Greater = 1, + } + + // region:derive + #[rustc_builtin_macro] + pub macro PartialOrd($item:item) {} + #[rustc_builtin_macro] + pub macro Ord($item:item) {} + // endregion:derive + + // endregion:ord +} +// endregion:eq + // region:slice pub mod slice { #[lang = "slice"] @@ -342,16 +388,30 @@ pub mod iter { } // endregion:iterator +// region:derive +mod macros { + pub(crate) mod builtin { + #[rustc_builtin_macro] + pub macro derive($item:item) { + /* compiler built-in */ + } + } +} +// endregion:derive + pub mod prelude { pub mod v1 { pub use crate::{ + cmp::{Eq, PartialEq}, // :eq + cmp::{Ord, PartialOrd}, // :ord + convert::{From, Into}, // :from default::Default, // :default iter::{IntoIterator, Iterator}, // :iterator + macros::builtin::derive, // :derive marker::Sized, // :sized ops::{Fn, FnMut, FnOnce}, // :fn option::Option::{self, None, Some}, // :option result::Result::{self, Err, Ok}, // :result - convert::{From, Into}, // :from }; } -- cgit v1.2.3