mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
internal: add From to minicore
This commit is contained in:
parent
82c7afc703
commit
ca99aaa053
5 changed files with 129 additions and 62 deletions
|
@ -3016,8 +3016,8 @@ fn foo() {
|
||||||
file_id: FileId(
|
file_id: FileId(
|
||||||
1,
|
1,
|
||||||
),
|
),
|
||||||
full_range: 246..428,
|
full_range: 247..429,
|
||||||
focus_range: 285..291,
|
focus_range: 286..292,
|
||||||
name: "Future",
|
name: "Future",
|
||||||
kind: Trait,
|
kind: Trait,
|
||||||
description: "pub trait Future",
|
description: "pub trait Future",
|
||||||
|
|
|
@ -6,6 +6,8 @@ use syntax::ast::{self, AstNode, NameOwner};
|
||||||
|
|
||||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
|
||||||
|
// FIXME: this should be a diagnostic
|
||||||
|
|
||||||
// Assist: convert_into_to_from
|
// Assist: convert_into_to_from
|
||||||
//
|
//
|
||||||
// Converts an Into impl to an equivalent From impl.
|
// 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
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use crate::tests::check_assist;
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_into_to_from_converts_a_struct() {
|
fn convert_into_to_from_converts_a_struct() {
|
||||||
check_convert_into_to_from(
|
check_assist(
|
||||||
|
convert_into_to_from,
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: from
|
||||||
struct Thing {
|
struct Thing {
|
||||||
a: String,
|
a: String,
|
||||||
b: usize
|
b: usize
|
||||||
|
@ -154,8 +158,10 @@ impl From<usize> for Thing {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_into_to_from_converts_enums() {
|
fn convert_into_to_from_converts_enums() {
|
||||||
check_convert_into_to_from(
|
check_assist(
|
||||||
|
convert_into_to_from,
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: from
|
||||||
enum Thing {
|
enum Thing {
|
||||||
Foo(String),
|
Foo(String),
|
||||||
Bar(String)
|
Bar(String)
|
||||||
|
@ -190,8 +196,10 @@ impl From<Thing> for String {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_into_to_from_on_enum_with_lifetimes() {
|
fn convert_into_to_from_on_enum_with_lifetimes() {
|
||||||
check_convert_into_to_from(
|
check_assist(
|
||||||
|
convert_into_to_from,
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: from
|
||||||
enum Thing<'a> {
|
enum Thing<'a> {
|
||||||
Foo(&'a str),
|
Foo(&'a str),
|
||||||
Bar(&'a str)
|
Bar(&'a str)
|
||||||
|
@ -226,8 +234,10 @@ impl<'a> From<Thing<'a>> for &'a str {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_into_to_from_works_on_references() {
|
fn convert_into_to_from_works_on_references() {
|
||||||
check_convert_into_to_from(
|
check_assist(
|
||||||
|
convert_into_to_from,
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: from
|
||||||
struct Thing(String);
|
struct Thing(String);
|
||||||
|
|
||||||
impl $0core::convert::Into<String> for &Thing {
|
impl $0core::convert::Into<String> for &Thing {
|
||||||
|
@ -250,8 +260,10 @@ impl From<&Thing> for String {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_into_to_from_works_on_qualified_structs() {
|
fn convert_into_to_from_works_on_qualified_structs() {
|
||||||
check_convert_into_to_from(
|
check_assist(
|
||||||
|
convert_into_to_from,
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: from
|
||||||
mod things {
|
mod things {
|
||||||
pub struct Thing(String);
|
pub struct Thing(String);
|
||||||
pub struct BetterThing(String);
|
pub struct BetterThing(String);
|
||||||
|
@ -280,8 +292,10 @@ impl From<&things::Thing> for things::BetterThing {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_into_to_from_works_on_qualified_enums() {
|
fn convert_into_to_from_works_on_qualified_enums() {
|
||||||
check_convert_into_to_from(
|
check_assist(
|
||||||
|
convert_into_to_from,
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: from
|
||||||
mod things {
|
mod things {
|
||||||
pub enum Thing {
|
pub enum Thing {
|
||||||
A(String)
|
A(String)
|
||||||
|
@ -323,10 +337,12 @@ impl From<&things::Thing> for things::BetterThing {
|
||||||
#[test]
|
#[test]
|
||||||
fn convert_into_to_from_not_applicable_on_any_trait_named_into() {
|
fn convert_into_to_from_not_applicable_on_any_trait_named_into() {
|
||||||
check_assist_not_applicable(
|
check_assist_not_applicable(
|
||||||
|
convert_into_to_from,
|
||||||
r#"
|
r#"
|
||||||
pub trait Into<T> {{
|
//- minicore: from
|
||||||
|
pub trait Into<T> {
|
||||||
pub fn into(self) -> T;
|
pub fn into(self) -> T;
|
||||||
}}
|
}
|
||||||
|
|
||||||
struct Thing {
|
struct Thing {
|
||||||
a: String,
|
a: String,
|
||||||
|
@ -342,14 +358,4 @@ impl $0Into<Thing> for String {
|
||||||
"#,
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_convert_into_to_from(before: &str, after: &str) {
|
|
||||||
let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
|
|
||||||
check_assist(convert_into_to_from, before, after);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn check_assist_not_applicable(before: &str) {
|
|
||||||
let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
|
|
||||||
crate::tests::check_assist_not_applicable(convert_into_to_from, before);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,14 +110,19 @@ mod tests {
|
||||||
fn test_generate_from_impl_for_enum() {
|
fn test_generate_from_impl_for_enum() {
|
||||||
check_assist(
|
check_assist(
|
||||||
generate_from_impl_for_enum,
|
generate_from_impl_for_enum,
|
||||||
"enum A { $0One(u32) }",
|
r#"
|
||||||
r#"enum A { One(u32) }
|
//- minicore: from
|
||||||
|
enum A { $0One(u32) }
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
enum A { One(u32) }
|
||||||
|
|
||||||
impl From<u32> for A {
|
impl From<u32> for A {
|
||||||
fn from(v: u32) -> Self {
|
fn from(v: u32) -> Self {
|
||||||
Self::One(v)
|
Self::One(v)
|
||||||
}
|
}
|
||||||
}"#,
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -125,53 +130,71 @@ impl From<u32> for A {
|
||||||
fn test_generate_from_impl_for_enum_complicated_path() {
|
fn test_generate_from_impl_for_enum_complicated_path() {
|
||||||
check_assist(
|
check_assist(
|
||||||
generate_from_impl_for_enum,
|
generate_from_impl_for_enum,
|
||||||
r#"enum A { $0One(foo::bar::baz::Boo) }"#,
|
r#"
|
||||||
r#"enum A { One(foo::bar::baz::Boo) }
|
//- minicore: from
|
||||||
|
enum A { $0One(foo::bar::baz::Boo) }
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
enum A { One(foo::bar::baz::Boo) }
|
||||||
|
|
||||||
impl From<foo::bar::baz::Boo> for A {
|
impl From<foo::bar::baz::Boo> for A {
|
||||||
fn from(v: foo::bar::baz::Boo) -> Self {
|
fn from(v: foo::bar::baz::Boo) -> Self {
|
||||||
Self::One(v)
|
Self::One(v)
|
||||||
}
|
}
|
||||||
}"#,
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_not_applicable(ra_fixture: &str) {
|
|
||||||
let fixture =
|
|
||||||
format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE);
|
|
||||||
check_assist_not_applicable(generate_from_impl_for_enum, &fixture)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add_from_impl_no_element() {
|
fn test_add_from_impl_no_element() {
|
||||||
check_not_applicable("enum A { $0One }");
|
check_assist_not_applicable(
|
||||||
|
generate_from_impl_for_enum,
|
||||||
|
r#"
|
||||||
|
//- minicore: from
|
||||||
|
enum A { $0One }
|
||||||
|
"#,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add_from_impl_more_than_one_element_in_tuple() {
|
fn test_add_from_impl_more_than_one_element_in_tuple() {
|
||||||
check_not_applicable("enum A { $0One(u32, String) }");
|
check_assist_not_applicable(
|
||||||
|
generate_from_impl_for_enum,
|
||||||
|
r#"
|
||||||
|
//- minicore: from
|
||||||
|
enum A { $0One(u32, String) }
|
||||||
|
"#,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add_from_impl_struct_variant() {
|
fn test_add_from_impl_struct_variant() {
|
||||||
check_assist(
|
check_assist(
|
||||||
generate_from_impl_for_enum,
|
generate_from_impl_for_enum,
|
||||||
"enum A { $0One { x: u32 } }",
|
r#"
|
||||||
r#"enum A { One { x: u32 } }
|
//- minicore: from
|
||||||
|
enum A { $0One { x: u32 } }
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
enum A { One { x: u32 } }
|
||||||
|
|
||||||
impl From<u32> for A {
|
impl From<u32> for A {
|
||||||
fn from(x: u32) -> Self {
|
fn from(x: u32) -> Self {
|
||||||
Self::One { x }
|
Self::One { x }
|
||||||
}
|
}
|
||||||
}"#,
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_add_from_impl_already_exists() {
|
fn test_add_from_impl_already_exists() {
|
||||||
cov_mark::check!(test_add_from_impl_already_exists);
|
cov_mark::check!(test_add_from_impl_already_exists);
|
||||||
check_not_applicable(
|
check_assist_not_applicable(
|
||||||
|
generate_from_impl_for_enum,
|
||||||
r#"
|
r#"
|
||||||
|
//- minicore: from
|
||||||
enum A { $0One(u32), }
|
enum A { $0One(u32), }
|
||||||
|
|
||||||
impl From<u32> for A {
|
impl From<u32> for A {
|
||||||
|
@ -187,7 +210,9 @@ impl From<u32> for A {
|
||||||
fn test_add_from_impl_different_variant_impl_exists() {
|
fn test_add_from_impl_different_variant_impl_exists() {
|
||||||
check_assist(
|
check_assist(
|
||||||
generate_from_impl_for_enum,
|
generate_from_impl_for_enum,
|
||||||
r#"enum A { $0One(u32), Two(String), }
|
r#"
|
||||||
|
//- minicore: from
|
||||||
|
enum A { $0One(u32), Two(String), }
|
||||||
|
|
||||||
impl From<String> for A {
|
impl From<String> for A {
|
||||||
fn from(v: String) -> Self {
|
fn from(v: String) -> Self {
|
||||||
|
@ -197,8 +222,10 @@ impl From<String> for A {
|
||||||
|
|
||||||
pub trait From<T> {
|
pub trait From<T> {
|
||||||
fn from(T) -> Self;
|
fn from(T) -> Self;
|
||||||
}"#,
|
}
|
||||||
r#"enum A { One(u32), Two(String), }
|
"#,
|
||||||
|
r#"
|
||||||
|
enum A { One(u32), Two(String), }
|
||||||
|
|
||||||
impl From<u32> for A {
|
impl From<u32> for A {
|
||||||
fn from(v: u32) -> Self {
|
fn from(v: u32) -> Self {
|
||||||
|
@ -214,7 +241,8 @@ impl From<String> for A {
|
||||||
|
|
||||||
pub trait From<T> {
|
pub trait From<T> {
|
||||||
fn from(T) -> Self;
|
fn from(T) -> Self;
|
||||||
}"#,
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,14 +250,19 @@ pub trait From<T> {
|
||||||
fn test_add_from_impl_static_str() {
|
fn test_add_from_impl_static_str() {
|
||||||
check_assist(
|
check_assist(
|
||||||
generate_from_impl_for_enum,
|
generate_from_impl_for_enum,
|
||||||
"enum A { $0One(&'static str) }",
|
r#"
|
||||||
r#"enum A { One(&'static str) }
|
//- minicore: from
|
||||||
|
enum A { $0One(&'static str) }
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
enum A { One(&'static str) }
|
||||||
|
|
||||||
impl From<&'static str> for A {
|
impl From<&'static str> for A {
|
||||||
fn from(v: &'static str) -> Self {
|
fn from(v: &'static str) -> Self {
|
||||||
Self::One(v)
|
Self::One(v)
|
||||||
}
|
}
|
||||||
}"#,
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,14 +270,19 @@ impl From<&'static str> for A {
|
||||||
fn test_add_from_impl_generic_enum() {
|
fn test_add_from_impl_generic_enum() {
|
||||||
check_assist(
|
check_assist(
|
||||||
generate_from_impl_for_enum,
|
generate_from_impl_for_enum,
|
||||||
"enum Generic<T, U: Clone> { $0One(T), Two(U) }",
|
r#"
|
||||||
r#"enum Generic<T, U: Clone> { One(T), Two(U) }
|
//- minicore: from
|
||||||
|
enum Generic<T, U: Clone> { $0One(T), Two(U) }
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
enum Generic<T, U: Clone> { One(T), Two(U) }
|
||||||
|
|
||||||
impl<T, U: Clone> From<T> for Generic<T, U> {
|
impl<T, U: Clone> From<T> for Generic<T, U> {
|
||||||
fn from(v: T) -> Self {
|
fn from(v: T) -> Self {
|
||||||
Self::One(v)
|
Self::One(v)
|
||||||
}
|
}
|
||||||
}"#,
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,14 +290,19 @@ impl<T, U: Clone> From<T> for Generic<T, U> {
|
||||||
fn test_add_from_impl_with_lifetime() {
|
fn test_add_from_impl_with_lifetime() {
|
||||||
check_assist(
|
check_assist(
|
||||||
generate_from_impl_for_enum,
|
generate_from_impl_for_enum,
|
||||||
"enum Generic<'a> { $0One(&'a i32) }",
|
r#"
|
||||||
r#"enum Generic<'a> { One(&'a i32) }
|
//- minicore: from
|
||||||
|
enum Generic<'a> { $0One(&'a i32) }
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
enum Generic<'a> { One(&'a i32) }
|
||||||
|
|
||||||
impl<'a> From<&'a i32> for Generic<'a> {
|
impl<'a> From<&'a i32> for Generic<'a> {
|
||||||
fn from(v: &'a i32) -> Self {
|
fn from(v: &'a i32) -> Self {
|
||||||
Self::One(v)
|
Self::One(v)
|
||||||
}
|
}
|
||||||
}"#,
|
}
|
||||||
|
"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,16 +10,6 @@ pub mod cmp {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod convert {
|
|
||||||
pub trait From<T> {
|
|
||||||
fn from(t: T) -> Self;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait Into<T> {
|
|
||||||
pub fn into(self) -> T;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub mod rust_2018 {
|
pub mod rust_2018 {
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
|
|
|
@ -23,6 +23,7 @@
|
||||||
//! iterator: option
|
//! iterator: option
|
||||||
//! iterators: iterator
|
//! iterators: iterator
|
||||||
//! default: sized
|
//! default: sized
|
||||||
|
//! from: sized
|
||||||
|
|
||||||
pub mod marker {
|
pub mod marker {
|
||||||
// region:sized
|
// region:sized
|
||||||
|
@ -46,6 +47,32 @@ pub mod default {
|
||||||
}
|
}
|
||||||
// endregion:default
|
// endregion:default
|
||||||
|
|
||||||
|
// region:from
|
||||||
|
pub mod convert {
|
||||||
|
pub trait From<T>: Sized {
|
||||||
|
fn from(_: T) -> Self;
|
||||||
|
}
|
||||||
|
pub trait Into<T>: Sized {
|
||||||
|
fn into(self) -> T;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T, U> Into<U> for T
|
||||||
|
where
|
||||||
|
U: From<T>,
|
||||||
|
{
|
||||||
|
fn into(self) -> U {
|
||||||
|
U::from(self)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> From<T> for T {
|
||||||
|
fn from(t: T) -> T {
|
||||||
|
t
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// endregion:from
|
||||||
|
|
||||||
pub mod ops {
|
pub mod ops {
|
||||||
// region:coerce_unsized
|
// region:coerce_unsized
|
||||||
mod unsize {
|
mod unsize {
|
||||||
|
@ -324,6 +351,7 @@ pub mod prelude {
|
||||||
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