From 436dcd9656b89b10a9719828a3421b4586ac331f Mon Sep 17 00:00:00 2001 From: adamrk Date: Sat, 20 Jun 2020 11:32:01 +0200 Subject: move tests to ra_hir_ty --- crates/ra_hir_ty/src/tests/traits.rs | 155 +++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) (limited to 'crates/ra_hir_ty/src/tests') diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index e81193a3c..8efe05877 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 + "### + ); +} -- cgit v1.2.3 From f07338bae2e4209d5e47e69f131bfa9efc56c890 Mon Sep 17 00:00:00 2001 From: adamrk Date: Sat, 20 Jun 2020 11:43:40 +0200 Subject: Add test for dyn Fn Output --- crates/ra_hir_ty/src/tests/traits.rs | 68 ++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'crates/ra_hir_ty/src/tests') diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index 8efe05877..dd5bdd2d0 100644 --- a/crates/ra_hir_ty/src/tests/traits.rs +++ b/crates/ra_hir_ty/src/tests/traits.rs @@ -3043,3 +3043,71 @@ fn infer_box_fn_arg() { "### ); } + +#[test] +fn infer_dyn_fn_output() { + 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; + } + + #[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 + } + } + + fn foo() { + let f: Box i32> = box(|| 5); + let x = f(); + } + "# + ), + @r###" + 182..186 'self': Self + 188..192 'args': Args + 349..353 'self': &Self + 355..359 'args': Args + 523..527 'self': &Self + 789..793 'self': &Box + 801..852 '{ ... }': &T + 823..834 '&self.inner': &*mut T + 824..828 'self': &Box + 824..834 'self.inner': *mut T + 889..990 '{ ... }': () + 911..912 'f': Box> + 937..946 'box(|| 5)': Box<|| -> i32> + 941..945 '|| 5': || -> i32 + 944..945 '5': i32 + 968..969 'x': i32 + 972..973 'f': Box> + 972..975 'f()': i32 + "### + ); +} -- cgit v1.2.3 From 1629fb770e21c7e7fd4c478f5074590d5c9e6829 Mon Sep 17 00:00:00 2001 From: adamrk Date: Sat, 20 Jun 2020 17:00:57 +0200 Subject: Push obligation instead of matching on solution --- crates/ra_hir_ty/src/tests/traits.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'crates/ra_hir_ty/src/tests') diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index dd5bdd2d0..961be4abd 100644 --- a/crates/ra_hir_ty/src/tests/traits.rs +++ b/crates/ra_hir_ty/src/tests/traits.rs @@ -3028,7 +3028,7 @@ fn infer_box_fn_arg() { 656..667 '&self.inner': &*mut T 657..661 'self': &Box 657..667 'self.inner': *mut T - 812..957 '{ ... }': FnOnce::Output,)>, ({unknown},)> + 812..957 '{ ... }': FnOnce::Output,)>, (&Option,)> 834..835 's': Option 838..850 'Option::None': Option 872..873 'f': Box,)>> @@ -3037,7 +3037,7 @@ fn infer_box_fn_arg() { 913..915 'ps': {unknown} 917..919 '{}': () 938..939 'f': Box,)>> - 938..943 'f(&s)': FnOnce::Output,)>, ({unknown},)> + 938..943 'f(&s)': FnOnce::Output,)>, (&Option,)> 940..942 '&s': &Option 941..942 's': Option "### @@ -3105,9 +3105,9 @@ fn infer_dyn_fn_output() { 937..946 'box(|| 5)': Box<|| -> i32> 941..945 '|| 5': || -> i32 944..945 '5': i32 - 968..969 'x': i32 + 968..969 'x': FnOnce::Output, ()> 972..973 'f': Box> - 972..975 'f()': i32 + 972..975 'f()': FnOnce::Output, ()> "### ); } -- cgit v1.2.3