mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
Merge #9319
9319: internal: add derive and ord support to minicore r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
916384a1ea
5 changed files with 130 additions and 61 deletions
|
@ -3016,8 +3016,8 @@ fn foo() {
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
full_range: 247..429,
|
full_range: 248..430,
|
||||||
focus_range: 286..292,
|
focus_range: 287..293,
|
||||||
name: "Future",
|
name: "Future",
|
||||||
kind: Trait,
|
kind: Trait,
|
||||||
description: "pub trait Future",
|
description: "pub trait Future",
|
||||||
|
|
|
@ -147,74 +147,92 @@ fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use ide_db::helpers::FamousDefs;
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
||||||
|
|
||||||
use super::*;
|
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));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn demorgan_handles_leq() {
|
fn demorgan_handles_leq() {
|
||||||
check(
|
check_assist(
|
||||||
r"use ordable::Orderable;
|
apply_demorgan,
|
||||||
|
r#"
|
||||||
|
//- minicore: ord, derive
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
struct S;
|
||||||
|
|
||||||
fn f() {
|
fn f() {
|
||||||
Orderable < Orderable &&$0 Orderable <= Orderable
|
S < S &&$0 S <= S
|
||||||
}",
|
}
|
||||||
r"use ordable::Orderable;
|
"#,
|
||||||
|
r#"
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
struct S;
|
||||||
|
|
||||||
fn f() {
|
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() {
|
fn f() {
|
||||||
NonOrderable < NonOrderable &&$0 NonOrderable <= NonOrderable
|
S < S &&$0 S <= S
|
||||||
}",
|
}
|
||||||
r"use ordable::NonOrderable;
|
"#,
|
||||||
|
r#"
|
||||||
|
struct S;
|
||||||
|
|
||||||
fn f() {
|
fn f() {
|
||||||
!(!(NonOrderable < NonOrderable) || !(NonOrderable <= NonOrderable))
|
!(!(S < S) || !(S <= S))
|
||||||
}",
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn demorgan_handles_geq() {
|
fn demorgan_handles_geq() {
|
||||||
check(
|
check_assist(
|
||||||
r"use ordable::Orderable;
|
apply_demorgan,
|
||||||
|
r#"
|
||||||
|
//- minicore: ord, derive
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
struct S;
|
||||||
|
|
||||||
fn f() {
|
fn f() {
|
||||||
Orderable > Orderable &&$0 Orderable >= Orderable
|
S > S &&$0 S >= S
|
||||||
}",
|
}
|
||||||
r"use ordable::Orderable;
|
"#,
|
||||||
|
r#"
|
||||||
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||||
|
struct S;
|
||||||
|
|
||||||
fn f() {
|
fn f() {
|
||||||
!(Orderable <= Orderable || Orderable < Orderable)
|
!(S <= S || S < S)
|
||||||
}",
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
check(
|
check_assist(
|
||||||
r"use ordable::NonOrderable;
|
apply_demorgan,
|
||||||
|
r#"
|
||||||
|
//- minicore: ord, derive
|
||||||
|
struct S;
|
||||||
|
|
||||||
fn f() {
|
fn f() {
|
||||||
Orderable > Orderable &&$0 Orderable >= Orderable
|
S > S &&$0 S >= S
|
||||||
}",
|
}
|
||||||
r"use ordable::NonOrderable;
|
"#,
|
||||||
|
r#"
|
||||||
|
struct S;
|
||||||
|
|
||||||
fn f() {
|
fn f() {
|
||||||
!(!(Orderable > Orderable) || !(Orderable >= Orderable))
|
!(!(S > S) || !(S >= S))
|
||||||
}",
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,7 @@ pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) {
|
||||||
RootDatabase::with_single_file(text)
|
RootDatabase::with_single_file(text)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[track_caller]
|
||||||
pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
|
pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||||
let ra_fixture_after = trim_indent(ra_fixture_after);
|
let ra_fixture_after = trim_indent(ra_fixture_after);
|
||||||
check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None);
|
check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None);
|
||||||
|
|
|
@ -1,15 +1,5 @@
|
||||||
//- /libcore.rs crate:core
|
//- /libcore.rs crate:core
|
||||||
//! Signatures of traits, types and functions from the core lib for use in tests.
|
//! 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 prelude {
|
||||||
pub mod rust_2018 {
|
pub mod rust_2018 {
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
|
|
|
@ -24,6 +24,9 @@
|
||||||
//! iterators: iterator
|
//! iterators: iterator
|
||||||
//! default: sized
|
//! default: sized
|
||||||
//! from: sized
|
//! from: sized
|
||||||
|
//! eq: sized
|
||||||
|
//! ord: eq, option
|
||||||
|
//! derive:
|
||||||
|
|
||||||
pub mod marker {
|
pub mod marker {
|
||||||
// region:sized
|
// region:sized
|
||||||
|
@ -173,6 +176,49 @@ pub mod ops {
|
||||||
// endregion:fn
|
// endregion:fn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// region:eq
|
||||||
|
pub mod cmp {
|
||||||
|
#[lang = "eq"]
|
||||||
|
pub trait PartialEq<Rhs: ?Sized = Self> {
|
||||||
|
fn eq(&self, other: &Rhs) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Eq: PartialEq<Self> {}
|
||||||
|
|
||||||
|
// 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<Rhs: ?Sized = Self>: PartialEq<Rhs> {
|
||||||
|
fn partial_cmp(&self, other: &Rhs) -> Option<Ordering>;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait Ord: Eq + PartialOrd<Self> {
|
||||||
|
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
|
// region:slice
|
||||||
pub mod slice {
|
pub mod slice {
|
||||||
#[lang = "slice"]
|
#[lang = "slice"]
|
||||||
|
@ -342,16 +388,30 @@ pub mod iter {
|
||||||
}
|
}
|
||||||
// endregion:iterator
|
// 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 prelude {
|
||||||
pub mod v1 {
|
pub mod v1 {
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
|
cmp::{Eq, PartialEq}, // :eq
|
||||||
|
cmp::{Ord, PartialOrd}, // :ord
|
||||||
|
convert::{From, Into}, // :from
|
||||||
default::Default, // :default
|
default::Default, // :default
|
||||||
iter::{IntoIterator, Iterator}, // :iterator
|
iter::{IntoIterator, Iterator}, // :iterator
|
||||||
|
macros::builtin::derive, // :derive
|
||||||
marker::Sized, // :sized
|
marker::Sized, // :sized
|
||||||
ops::{Fn, FnMut, FnOnce}, // :fn
|
ops::{Fn, FnMut, FnOnce}, // :fn
|
||||||
option::Option::{self, None, Some}, // :option
|
option::Option::{self, None, Some}, // :option
|
||||||
result::Result::{self, Err, Ok}, // :result
|
result::Result::{self, Err, Ok}, // :result
|
||||||
convert::{From, Into}, // :from
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue