Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7db40dd

Browse files
committed
Add test cases
1 parent 1f5a88a commit 7db40dd

File tree

2 files changed

+130
-2
lines changed

2 files changed

+130
-2
lines changed

src/tools/rust-analyzer/crates/ide/src/goto_definition.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3102,6 +3102,105 @@ impl From<A> for B {
31023102
fn f() {
31033103
let a = A;
31043104
let b: B = a.into$0();
3105+
}
3106+
"#,
3107+
);
3108+
}
3109+
3110+
#[test]
3111+
fn goto_into_definition_if_exists() {
3112+
check(
3113+
r#"
3114+
//- minicore: from
3115+
struct A;
3116+
3117+
struct B;
3118+
3119+
impl Into<B> for A {
3120+
fn into(self) -> B {
3121+
//^^^^
3122+
B
3123+
}
3124+
}
3125+
3126+
fn f() {
3127+
let a = A;
3128+
let b: B = a.into$0();
3129+
}
3130+
"#,
3131+
);
3132+
}
3133+
3134+
#[test]
3135+
fn try_into_call_to_try_from_definition() {
3136+
check(
3137+
r#"
3138+
//- minicore: from
3139+
struct A;
3140+
3141+
struct B;
3142+
3143+
impl TryFrom<A> for B {
3144+
type Error = String;
3145+
3146+
fn try_from(value: A) -> Result<Self, Self::Error> {
3147+
//^^^^^^^^
3148+
Ok(B)
3149+
}
3150+
}
3151+
3152+
fn f() {
3153+
let a = A;
3154+
let b: Result<B, _> = a.try_into$0();
3155+
}
3156+
"#,
3157+
);
3158+
}
3159+
3160+
#[test]
3161+
fn goto_try_into_definition_if_exists() {
3162+
check(
3163+
r#"
3164+
//- minicore: from
3165+
struct A;
3166+
3167+
struct B;
3168+
3169+
impl TryInto<B> for A {
3170+
type Error = String;
3171+
3172+
fn try_into(self) -> Result<B, Self::Error> {
3173+
//^^^^^^^^
3174+
Ok(B)
3175+
}
3176+
}
3177+
3178+
fn f() {
3179+
let a = A;
3180+
let b: Result<B, _> = a.try_into$0();
3181+
}
3182+
"#,
3183+
);
3184+
}
3185+
3186+
#[test]
3187+
fn parse_call_to_from_str_definition() {
3188+
check(
3189+
r#"
3190+
//- minicore: from, str
3191+
struct A;
3192+
3193+
impl FromStr for A {
3194+
type Error = String;
3195+
3196+
fn from_str(value: &str) -> Result<Self, Self::Error> {
3197+
//^^^^^^^^
3198+
Ok(A)
3199+
}
3200+
}
3201+
3202+
fn f() {
3203+
let a: Result<A, _> = "aaaaaa".parse$0();
31053204
}
31063205
"#,
31073206
);

src/tools/rust-analyzer/crates/test-utils/src/minicore.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
//! error: fmt
3333
//! fmt: option, result, transmute, coerce_unsized, copy, clone, derive
3434
//! fn: tuple
35-
//! from: sized
35+
//! from: sized, result
3636
//! future: pin
3737
//! coroutine: pin
3838
//! dispatch_from_dyn: unsize, pin
@@ -332,6 +332,25 @@ pub mod convert {
332332
t
333333
}
334334
}
335+
336+
pub trait TryFrom<T>: Sized {
337+
type Error;
338+
fn try_from(value: T) -> Result<Self, Self::Error>;
339+
}
340+
pub trait TryInto<T>: Sized {
341+
type Error;
342+
fn try_into(self) -> Result<T, Self::Error>;
343+
}
344+
345+
impl<T, U> TryInto<U> for T
346+
where
347+
U: TryFrom<T>,
348+
{
349+
type Error = U::Error;
350+
fn try_into(self) -> Result<U, U::Error> {
351+
U::try_from(self)
352+
}
353+
}
335354
// endregion:from
336355

337356
// region:as_ref
@@ -1532,6 +1551,15 @@ pub mod str {
15321551
pub const unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
15331552
""
15341553
}
1554+
pub trait FromStr: Sized {
1555+
type Err;
1556+
fn from_str(s: &str) -> Result<Self, Self::Err>;
1557+
}
1558+
impl str {
1559+
pub fn parse<F: FromStr>(&self) -> Result<F, F::Err> {
1560+
FromStr::from_str(self)
1561+
}
1562+
}
15351563
}
15361564
// endregion:str
15371565

@@ -1791,7 +1819,7 @@ pub mod prelude {
17911819
cmp::{Eq, PartialEq}, // :eq
17921820
cmp::{Ord, PartialOrd}, // :ord
17931821
convert::AsRef, // :as_ref
1794-
convert::{From, Into}, // :from
1822+
convert::{From, Into, TryFrom, TryInto}, // :from
17951823
default::Default, // :default
17961824
iter::{IntoIterator, Iterator}, // :iterator
17971825
macros::builtin::{derive, derive_const}, // :derive
@@ -1806,6 +1834,7 @@ pub mod prelude {
18061834
option::Option::{self, None, Some}, // :option
18071835
panic, // :panic
18081836
result::Result::{self, Err, Ok}, // :result
1837+
str::FromStr, // :from
18091838
};
18101839
}
18111840

0 commit comments

Comments
 (0)