From 944f71afc692733cbc151bb3113d45a8159de132 Mon Sep 17 00:00:00 2001 From: Unreal Hoang Date: Tue, 9 Jul 2019 00:02:15 +0900 Subject: projection over std::ops::Try::Ok to infer try/? --- crates/ra_hir/src/ty/tests.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'crates/ra_hir/src/ty/tests.rs') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index fe5e89f2d..d5c03c4bc 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -20,6 +20,42 @@ use crate::{ // against snapshots of the expected results using insta. Use cargo-insta to // update the snapshots. +#[test] +fn infer_try() { + let (mut db, pos) = MockDatabase::with_position( + r#" +//- /main.rs +enum Result { + Ok(O), + Err(E) +} + +impl ::std::ops::Try for Result { + type Ok = O; + type Error = E; +} +fn test() { + let r: Result = Result::Ok(1); + let v = r?; + v<|>; +} + +//- /lib.rs +mod ops { + trait Try { + type Ok; + type Error; + } +} +"#, + ); + db.set_crate_graph_from_fixture(crate_graph! { + "main": ("/main.rs", ["std"]), + "std": ("/lib.rs", []), + }); + assert_eq!("i32", type_at_pos(&db, pos)); +} + #[test] fn infer_for_loop() { let (mut db, pos) = MockDatabase::with_position( @@ -56,6 +92,7 @@ mod iter { }); assert_eq!("&str", type_at_pos(&db, pos)); } + #[test] fn infer_basics() { assert_snapshot_matches!( -- cgit v1.2.3 From 9a0d4b16b7d14530b77c96e2e18b80b3f5b526c8 Mon Sep 17 00:00:00 2001 From: Unreal Hoang Date: Tue, 9 Jul 2019 01:01:07 +0900 Subject: beautify tests --- crates/ra_hir/src/ty/tests.rs | 56 +++++++++++++++++++++++++++---------------- 1 file changed, 36 insertions(+), 20 deletions(-) (limited to 'crates/ra_hir/src/ty/tests.rs') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index d5c03c4bc..6aea1fb4a 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -25,33 +25,41 @@ fn infer_try() { let (mut db, pos) = MockDatabase::with_position( r#" //- /main.rs -enum Result { - Ok(O), - Err(E) -} -impl ::std::ops::Try for Result { - type Ok = O; - type Error = E; -} fn test() { let r: Result = Result::Ok(1); let v = r?; v<|>; } -//- /lib.rs +//- /std.rs + +#[prelude_import] use ops::*; mod ops { trait Try { type Ok; type Error; } } + +#[prelude_import] use result::*; +mod result { + enum Result { + Ok(O), + Err(E) + } + + impl crate::ops::Try for Result { + type Ok = O; + type Error = E; + } +} + "#, ); db.set_crate_graph_from_fixture(crate_graph! { "main": ("/main.rs", ["std"]), - "std": ("/lib.rs", []), + "std": ("/std.rs", []), }); assert_eq!("i32", type_at_pos(&db, pos)); } @@ -61,15 +69,9 @@ fn infer_for_loop() { let (mut db, pos) = MockDatabase::with_position( r#" //- /main.rs -struct Vec {} -impl Vec { - fn new() -> Self { Vec {} } - fn push(&mut self, t: T) { } -} -impl ::std::iter::IntoIterator for Vec { - type Item=T; -} +use std::collections::Vec; + fn test() { let v = Vec::new(); v.push("foo"); @@ -78,17 +80,31 @@ fn test() { } } -//- /lib.rs +//- /std.rs + +#[prelude_import] use iter::*; mod iter { trait IntoIterator { type Item; } } + +mod collections { + struct Vec {} + impl Vec { + fn new() -> Self { Vec {} } + fn push(&mut self, t: T) { } + } + + impl crate::iter::IntoIterator for Vec { + type Item=T; + } +} "#, ); db.set_crate_graph_from_fixture(crate_graph! { "main": ("/main.rs", ["std"]), - "std": ("/lib.rs", []), + "std": ("/std.rs", []), }); assert_eq!("&str", type_at_pos(&db, pos)); } -- cgit v1.2.3