diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index e81193a3c30..8efe0587717 100644 --- a/crates/ra_hir_ty/src/tests/traits.rs +++ b/crates/ra_hir_ty/src/tests/traits.rs @@ -2888,3 +2888,158 @@ impl iter::Iterator for ops::Range { ); assert_eq!(t, "i32"); } + +#[test] +fn infer_closure_arg() { + assert_snapshot!( + infer( + r#" + //- /lib.rs + + enum Option { + None, + Some(T) + } + + fn foo() { + let s = Option::None; + let f = |x: Option| {}; + (&f)(s) + } + "# + ), + @r###" + 137..259 '{ ... }': () + 159..160 's': Option + 163..175 'Option::None': Option + 197..198 'f': |Option| -> () + 201..220 '|x: Op...2>| {}': |Option| -> () + 202..203 'x': Option + 218..220 '{}': () + 238..245 '(&f)(s)': () + 239..241 '&f': &|Option| -> () + 240..241 'f': |Option| -> () + 243..244 's': Option + "### + ); +} + +#[test] +fn infer_fn_trait_arg() { + assert_snapshot!( + infer( + r#" + //- /lib.rs deps:std + + #[lang = "fn_once"] + pub trait FnOnce { + type Output; + + extern "rust-call" fn call_once(&self, args: Args) -> Self::Output; + } + + #[lang = "fn"] + pub trait Fn:FnOnce { + extern "rust-call" fn call(&self, args: Args) -> Self::Output; + } + + enum Option { + None, + Some(T) + } + + fn foo(f: F) -> T + where + F: Fn(Option) -> T, + { + let s = None; + f(s) + } + "# + ), + @r###" + 183..187 'self': &Self + 189..193 'args': Args + 350..354 'self': &Self + 356..360 'args': Args + 515..516 'f': F + 597..663 '{ ... }': T + 619..620 's': Option + 623..627 'None': Option + 645..646 'f': F + 645..649 'f(s)': T + 647..648 's': Option + "### + ); +} + +#[test] +fn infer_box_fn_arg() { + assert_snapshot!( + infer( + r#" + //- /lib.rs deps:std + + #[lang = "fn_once"] + pub trait FnOnce { + type Output; + + extern "rust-call" fn call_once(self, args: Args) -> Self::Output; + } + + #[lang = "deref"] + pub trait Deref { + type Target: ?Sized; + + fn deref(&self) -> &Self::Target; + } + + #[lang = "owned_box"] + pub struct Box { + inner: *mut T, + } + + impl Deref for Box { + type Target = T; + + fn deref(&self) -> &T { + &self.inner + } + } + + enum Option { + None, + Some(T) + } + + fn foo() { + let s = Option::None; + let f: Box)> = box (|ps| {}); + f(&s) + } + "# + ), + @r###" + 182..186 'self': Self + 188..192 'args': Args + 356..360 'self': &Self + 622..626 'self': &Box + 634..685 '{ ... }': &T + 656..667 '&self.inner': &*mut T + 657..661 'self': &Box + 657..667 'self.inner': *mut T + 812..957 '{ ... }': FnOnce::Output,)>, ({unknown},)> + 834..835 's': Option + 838..850 'Option::None': Option + 872..873 'f': Box,)>> + 907..920 'box (|ps| {})': Box<|{unknown}| -> ()> + 912..919 '|ps| {}': |{unknown}| -> () + 913..915 'ps': {unknown} + 917..919 '{}': () + 938..939 'f': Box,)>> + 938..943 'f(&s)': FnOnce::Output,)>, ({unknown},)> + 940..942 '&s': &Option + 941..942 's': Option + "### + ); +} diff --git a/crates/ra_ide/src/hover.rs b/crates/ra_ide/src/hover.rs index a03024d090b..d870e4cbce6 100644 --- a/crates/ra_ide/src/hover.rs +++ b/crates/ra_ide/src/hover.rs @@ -2410,108 +2410,4 @@ fn func(foo: i32) { if true { <|>foo; }; } ] "###); } - - #[test] - fn infer_closure_arg() { - check_hover_result( - r#" - //- /lib.rs - - enum Option { - None, - Some(T) - } - - fn foo() { - let s<|> = Option::None; - let f = |x: Option| {}; - (&f)(s) - } - "#, - &["Option"], - ); - } - - #[test] - fn infer_fn_trait_arg() { - check_hover_result( - r#" - //- /lib.rs deps:std - - #[lang = "fn_once"] - pub trait FnOnce { - type Output; - - extern "rust-call" fn call_once(&self, args: Args) -> Self::Output; - } - - #[lang = "fn"] - pub trait Fn:FnOnce { - extern "rust-call" fn call(&self, args: Args) -> Self::Output; - } - - enum Option { - None, - Some(T) - } - - fn foo(f: F) -> T - where - F: Fn(Option) -> T, - { - let s<|> = None; - f(s) - } - "#, - &["Option"], - ); - } - - #[test] - fn infer_box_fn_arg() { - check_hover_result( - r#" - //- /lib.rs deps:std - - #[lang = "fn_once"] - pub trait FnOnce { - type Output; - - extern "rust-call" fn call_once(self, args: Args) -> Self::Output; - } - - #[lang = "deref"] - pub trait Deref { - type Target: ?Sized; - - fn deref(&self) -> &Self::Target; - } - - #[lang = "owned_box"] - pub struct Box { - inner: *mut T, - } - - impl Deref for Box { - type Target = T; - - fn deref(&self) -> &T { - &self.inner - } - } - - enum Option { - None, - Some(T) - } - - fn foo() { - let s<|> = Option::None; - let f: Box)> = box (|ps| {}); - f(&s) - } - "#, - &["Option"], - ); - } }