Merge #9316
9316: internal: add default to minicore r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
commit
53d26164c6
@ -3016,8 +3016,8 @@ fn foo() {
|
||||
file_id: FileId(
|
||||
1,
|
||||
),
|
||||
full_range: 245..427,
|
||||
focus_range: 284..290,
|
||||
full_range: 246..428,
|
||||
focus_range: 285..291,
|
||||
name: "Future",
|
||||
kind: Trait,
|
||||
description: "pub trait Future",
|
||||
|
@ -1,5 +1,4 @@
|
||||
use ide_db::helpers::FamousDefs;
|
||||
use ide_db::RootDatabase;
|
||||
use ide_db::{helpers::FamousDefs, RootDatabase};
|
||||
use syntax::ast::{self, AstNode, NameOwner};
|
||||
|
||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||
@ -92,23 +91,20 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
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_default_from_enum_variant, &fixture)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_default_from_variant() {
|
||||
check_assist(
|
||||
generate_default_from_enum_variant,
|
||||
r#"
|
||||
//- minicore: default
|
||||
enum Variant {
|
||||
Undefined,
|
||||
Minor$0,
|
||||
Major,
|
||||
}"#,
|
||||
r#"enum Variant {
|
||||
}
|
||||
"#,
|
||||
r#"
|
||||
enum Variant {
|
||||
Undefined,
|
||||
Minor,
|
||||
Major,
|
||||
@ -118,15 +114,18 @@ impl Default for Variant {
|
||||
fn default() -> Self {
|
||||
Self::Minor
|
||||
}
|
||||
}"#,
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_generate_default_already_implemented() {
|
||||
cov_mark::check!(test_gen_default_impl_already_exists);
|
||||
check_not_applicable(
|
||||
check_assist_not_applicable(
|
||||
generate_default_from_enum_variant,
|
||||
r#"
|
||||
//- minicore: default
|
||||
enum Variant {
|
||||
Undefined,
|
||||
Minor$0,
|
||||
@ -137,20 +136,24 @@ impl Default for Variant {
|
||||
fn default() -> Self {
|
||||
Self::Minor
|
||||
}
|
||||
}"#,
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_from_impl_no_element() {
|
||||
cov_mark::check!(test_gen_default_on_non_unit_variant_not_implemented);
|
||||
check_not_applicable(
|
||||
check_assist_not_applicable(
|
||||
generate_default_from_enum_variant,
|
||||
r#"
|
||||
//- minicore: default
|
||||
enum Variant {
|
||||
Undefined,
|
||||
Minor(u32)$0,
|
||||
Major,
|
||||
}"#,
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
@ -158,7 +161,10 @@ enum Variant {
|
||||
fn test_generate_default_from_variant_with_one_variant() {
|
||||
check_assist(
|
||||
generate_default_from_enum_variant,
|
||||
r#"enum Variant { Undefi$0ned }"#,
|
||||
r#"
|
||||
//- minicore: default
|
||||
enum Variant { Undefi$0ned }
|
||||
"#,
|
||||
r#"
|
||||
enum Variant { Undefined }
|
||||
|
||||
@ -166,7 +172,8 @@ impl Default for Variant {
|
||||
fn default() -> Self {
|
||||
Self::Undefined
|
||||
}
|
||||
}"#,
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,3 @@
|
||||
use crate::{
|
||||
assist_context::{AssistContext, Assists},
|
||||
AssistId,
|
||||
};
|
||||
use ide_db::helpers::FamousDefs;
|
||||
use itertools::Itertools;
|
||||
use stdx::format_to;
|
||||
@ -10,6 +6,11 @@
|
||||
AstNode,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
assist_context::{AssistContext, Assists},
|
||||
AssistId,
|
||||
};
|
||||
|
||||
// Assist: generate_default_from_new
|
||||
//
|
||||
// Generates default implementation from new method.
|
||||
@ -140,16 +141,16 @@ fn is_default_implemented(ctx: &AssistContext, impl_: &Impl) -> bool {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use ide_db::helpers::FamousDefs;
|
||||
|
||||
use crate::tests::{check_assist, check_assist_not_applicable};
|
||||
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn generate_default() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
struct Example { _inner: () }
|
||||
|
||||
impl Example {
|
||||
@ -182,8 +183,10 @@ fn main() {}
|
||||
|
||||
#[test]
|
||||
fn generate_default2() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
struct Test { value: u32 }
|
||||
|
||||
impl Test {
|
||||
@ -212,8 +215,10 @@ fn default() -> Self {
|
||||
|
||||
#[test]
|
||||
fn new_function_with_generic() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
pub struct Foo<T> {
|
||||
_bar: *mut T,
|
||||
}
|
||||
@ -246,8 +251,10 @@ fn default() -> Self {
|
||||
|
||||
#[test]
|
||||
fn new_function_with_generics() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
pub struct Foo<T, B> {
|
||||
_tars: *mut T,
|
||||
_bar: *mut B,
|
||||
@ -282,8 +289,10 @@ fn default() -> Self {
|
||||
|
||||
#[test]
|
||||
fn new_function_with_generic_and_bound() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
pub struct Foo<T> {
|
||||
t: T,
|
||||
}
|
||||
@ -316,8 +325,10 @@ fn default() -> Self {
|
||||
|
||||
#[test]
|
||||
fn new_function_with_generics_and_bounds() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
pub struct Foo<T, B> {
|
||||
_tars: T,
|
||||
_bar: B,
|
||||
@ -352,8 +363,10 @@ fn default() -> Self {
|
||||
|
||||
#[test]
|
||||
fn new_function_with_generic_and_where() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
pub struct Foo<T> {
|
||||
t: T,
|
||||
}
|
||||
@ -395,8 +408,10 @@ fn default() -> Self {
|
||||
|
||||
#[test]
|
||||
fn new_function_with_generics_and_wheres() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
pub struct Foo<T, B> {
|
||||
_tars: T,
|
||||
_bar: B,
|
||||
@ -441,8 +456,10 @@ fn default() -> Self {
|
||||
#[test]
|
||||
fn new_function_with_parameters() {
|
||||
cov_mark::check!(new_function_with_parameters);
|
||||
check_not_applicable(
|
||||
check_assist_not_applicable(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
struct Example { _inner: () }
|
||||
|
||||
impl Example {
|
||||
@ -457,7 +474,8 @@ pub fn $0new(value: ()) -> Self {
|
||||
#[test]
|
||||
fn other_function_than_new() {
|
||||
cov_mark::check!(other_function_than_new);
|
||||
check_not_applicable(
|
||||
check_assist_not_applicable(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
struct Example { _inner: () }
|
||||
|
||||
@ -474,8 +492,10 @@ pub fn a$0dd() -> Self {
|
||||
#[test]
|
||||
fn default_block_is_already_present() {
|
||||
cov_mark::check!(default_block_is_already_present);
|
||||
check_not_applicable(
|
||||
check_assist_not_applicable(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
struct Example { _inner: () }
|
||||
|
||||
impl Example {
|
||||
@ -495,7 +515,8 @@ fn default() -> Self {
|
||||
|
||||
#[test]
|
||||
fn standalone_new_function() {
|
||||
check_not_applicable(
|
||||
check_assist_not_applicable(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
fn n$0ew() -> u32 {
|
||||
0
|
||||
@ -506,8 +527,10 @@ fn n$0ew() -> u32 {
|
||||
|
||||
#[test]
|
||||
fn multiple_struct_blocks() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
struct Example { _inner: () }
|
||||
struct Test { value: u32 }
|
||||
|
||||
@ -538,8 +561,10 @@ fn default() -> Self {
|
||||
|
||||
#[test]
|
||||
fn when_struct_is_after_impl() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
impl Example {
|
||||
pub fn $0new() -> Self {
|
||||
Self { _inner: () }
|
||||
@ -568,8 +593,10 @@ struct Example { _inner: () }
|
||||
|
||||
#[test]
|
||||
fn struct_in_module() {
|
||||
check_pass(
|
||||
check_assist(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
mod test {
|
||||
struct Example { _inner: () }
|
||||
|
||||
@ -603,8 +630,10 @@ fn default() -> Self {
|
||||
#[test]
|
||||
fn struct_in_module_with_default() {
|
||||
cov_mark::check!(struct_in_module_with_default);
|
||||
check_not_applicable(
|
||||
check_assist_not_applicable(
|
||||
generate_default_from_new,
|
||||
r#"
|
||||
//- minicore: default
|
||||
mod test {
|
||||
struct Example { _inner: () }
|
||||
|
||||
@ -623,14 +652,4 @@ fn default() -> Self {
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
fn check_pass(before: &str, after: &str) {
|
||||
let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
|
||||
check_assist(generate_default_from_new, before, after);
|
||||
}
|
||||
|
||||
fn check_not_applicable(before: &str) {
|
||||
let before = &format!("//- /main.rs crate:main deps:core{}{}", before, FamousDefs::FIXTURE);
|
||||
check_assist_not_applicable(generate_default_from_new, before);
|
||||
}
|
||||
}
|
||||
|
@ -48,10 +48,9 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use expect_test::{expect, Expect};
|
||||
use ide_db::helpers::FamousDefs;
|
||||
|
||||
use crate::{
|
||||
tests::{self, filtered_completion_list},
|
||||
tests::{check_edit, filtered_completion_list},
|
||||
CompletionKind,
|
||||
};
|
||||
|
||||
@ -61,31 +60,17 @@ fn check(ra_fixture: &str, expect: Expect) {
|
||||
}
|
||||
|
||||
fn check_snippet(ra_fixture: &str, expect: Expect) {
|
||||
let actual = filtered_completion_list(
|
||||
&format!("//- /main.rs crate:main deps:core\n{}\n{}", ra_fixture, FamousDefs::FIXTURE),
|
||||
CompletionKind::Snippet,
|
||||
);
|
||||
let actual = filtered_completion_list(ra_fixture, CompletionKind::Snippet);
|
||||
expect.assert_eq(&actual);
|
||||
}
|
||||
|
||||
fn check_edit(what: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||
tests::check_edit(
|
||||
what,
|
||||
&format!(
|
||||
"//- /main.rs crate:main deps:core{}\n{}",
|
||||
ra_fixture_before,
|
||||
FamousDefs::FIXTURE,
|
||||
),
|
||||
&(ra_fixture_after.to_owned() + "\n"),
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_record_literal_field_default() {
|
||||
let test_code = r#"
|
||||
//- minicore: default
|
||||
struct S { foo: u32, bar: usize }
|
||||
|
||||
impl core::default::Default for S {
|
||||
impl Default for S {
|
||||
fn default() -> Self {
|
||||
S {
|
||||
foo: 0,
|
||||
@ -121,9 +106,10 @@ fn test_record_literal_field_default_completion() {
|
||||
check_edit(
|
||||
"..Default::default()",
|
||||
r#"
|
||||
//- minicore: default
|
||||
struct S { foo: u32, bar: usize }
|
||||
|
||||
impl core::default::Default for S {
|
||||
impl Default for S {
|
||||
fn default() -> Self {
|
||||
S {
|
||||
foo: 0,
|
||||
@ -142,7 +128,7 @@ fn process(f: S) {
|
||||
r#"
|
||||
struct S { foo: u32, bar: usize }
|
||||
|
||||
impl core::default::Default for S {
|
||||
impl Default for S {
|
||||
fn default() -> Self {
|
||||
S {
|
||||
foo: 0,
|
||||
|
@ -20,12 +20,6 @@ pub trait Into<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod default {
|
||||
pub trait Default {
|
||||
fn default() -> Self;
|
||||
}
|
||||
}
|
||||
|
||||
pub mod option {
|
||||
pub enum Option<T> {
|
||||
None,
|
||||
|
@ -22,6 +22,7 @@
|
||||
//! result:
|
||||
//! iterator: option
|
||||
//! iterators: iterator
|
||||
//! default: sized
|
||||
|
||||
pub mod marker {
|
||||
// region:sized
|
||||
@ -37,6 +38,14 @@ pub trait Unsize<T: ?Sized> {}
|
||||
// endregion:unsize
|
||||
}
|
||||
|
||||
// region:default
|
||||
pub mod default {
|
||||
pub trait Default: Sized {
|
||||
fn default() -> Self;
|
||||
}
|
||||
}
|
||||
// endregion:default
|
||||
|
||||
pub mod ops {
|
||||
// region:coerce_unsized
|
||||
mod unsize {
|
||||
@ -309,6 +318,7 @@ fn into_iter(self) -> I {
|
||||
pub mod prelude {
|
||||
pub mod v1 {
|
||||
pub use crate::{
|
||||
default::Default, // :default
|
||||
iter::{IntoIterator, Iterator}, // :iterator
|
||||
marker::Sized, // :sized
|
||||
ops::{Fn, FnMut, FnOnce}, // :fn
|
||||
|
Loading…
Reference in New Issue
Block a user