aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/tests.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir_ty/src/tests.rs')
-rw-r--r--crates/ra_hir_ty/src/tests.rs110
1 files changed, 98 insertions, 12 deletions
diff --git a/crates/ra_hir_ty/src/tests.rs b/crates/ra_hir_ty/src/tests.rs
index c8461b447..c856d6afd 100644
--- a/crates/ra_hir_ty/src/tests.rs
+++ b/crates/ra_hir_ty/src/tests.rs
@@ -8,7 +8,7 @@ use hir_def::{
8 body::BodySourceMap, db::DefDatabase, nameres::CrateDefMap, AssocItemId, DefWithBodyId, 8 body::BodySourceMap, db::DefDatabase, nameres::CrateDefMap, AssocItemId, DefWithBodyId,
9 LocalModuleId, Lookup, ModuleDefId, 9 LocalModuleId, Lookup, ModuleDefId,
10}; 10};
11use hir_expand::Source; 11use hir_expand::InFile;
12use insta::assert_snapshot; 12use insta::assert_snapshot;
13use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase}; 13use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase};
14use ra_syntax::{ 14use ra_syntax::{
@@ -222,6 +222,56 @@ mod collections {
222} 222}
223 223
224#[test] 224#[test]
225fn infer_ranges() {
226 let (db, pos) = TestDB::with_position(
227 r#"
228//- /main.rs crate:main deps:std
229fn test() {
230 let a = ..;
231 let b = 1..;
232 let c = ..2u32;
233 let d = 1..2usize;
234 let e = ..=10;
235 let f = 'a'..='z';
236
237 let t = (a, b, c, d, e, f);
238 t<|>;
239}
240
241//- /std.rs crate:std
242#[prelude_import] use prelude::*;
243mod prelude {}
244
245pub mod ops {
246 pub struct Range<Idx> {
247 pub start: Idx,
248 pub end: Idx,
249 }
250 pub struct RangeFrom<Idx> {
251 pub start: Idx,
252 }
253 struct RangeFull;
254 pub struct RangeInclusive<Idx> {
255 start: Idx,
256 end: Idx,
257 is_empty: u8,
258 }
259 pub struct RangeTo<Idx> {
260 pub end: Idx,
261 }
262 pub struct RangeToInclusive<Idx> {
263 pub end: Idx,
264 }
265}
266"#,
267 );
268 assert_eq!(
269 "(RangeFull, RangeFrom<i32>, RangeTo<u32>, Range<usize>, RangeToInclusive<i32>, RangeInclusive<char>)",
270 type_at_pos(&db, pos),
271 );
272}
273
274#[test]
225fn infer_while_let() { 275fn infer_while_let() {
226 let (db, pos) = TestDB::with_position( 276 let (db, pos) = TestDB::with_position(
227 r#" 277 r#"
@@ -2104,7 +2154,6 @@ fn test(x: Foo, y: Bar<&str>, z: Baz<i8, u8>) {
2104} 2154}
2105 2155
2106#[test] 2156#[test]
2107#[should_panic] // we currently can't handle this
2108fn recursive_type_alias() { 2157fn recursive_type_alias() {
2109 assert_snapshot!( 2158 assert_snapshot!(
2110 infer(r#" 2159 infer(r#"
@@ -2113,7 +2162,10 @@ type Foo = Foo;
2113type Bar = A<Bar>; 2162type Bar = A<Bar>;
2114fn test(x: Foo) {} 2163fn test(x: Foo) {}
2115"#), 2164"#),
2116 @"" 2165 @r###"
2166 [59; 60) 'x': {unknown}
2167 [67; 69) '{}': ()
2168 "###
2117 ) 2169 )
2118} 2170}
2119 2171
@@ -4626,10 +4678,48 @@ fn test<T, U>() where T::Item: Trait2, T: Trait<U::Item>, U: Trait<()> {
4626} 4678}
4627 4679
4628#[test] 4680#[test]
4629// FIXME this is currently a Salsa panic; it would be nicer if it just returned 4681fn trait_impl_self_ty() {
4630// in Unknown, and we should be able to do that once Salsa allows us to handle 4682 let t = type_at(
4631// the cycle. But at least it doesn't overflow for now. 4683 r#"
4632#[should_panic] 4684//- /main.rs
4685trait Trait<T> {
4686 fn foo(&self);
4687}
4688
4689struct S;
4690
4691impl Trait<Self> for S {}
4692
4693fn test() {
4694 S.foo()<|>;
4695}
4696"#,
4697 );
4698 assert_eq!(t, "()");
4699}
4700
4701#[test]
4702fn trait_impl_self_ty_cycle() {
4703 let t = type_at(
4704 r#"
4705//- /main.rs
4706trait Trait {
4707 fn foo(&self);
4708}
4709
4710struct S<T>;
4711
4712impl Trait for S<Self> {}
4713
4714fn test() {
4715 S.foo()<|>;
4716}
4717"#,
4718 );
4719 assert_eq!(t, "{unknown}");
4720}
4721
4722#[test]
4633fn unselected_projection_in_trait_env_cycle_1() { 4723fn unselected_projection_in_trait_env_cycle_1() {
4634 let t = type_at( 4724 let t = type_at(
4635 r#" 4725 r#"
@@ -4650,10 +4740,6 @@ fn test<T: Trait>() where T: Trait2<T::Item> {
4650} 4740}
4651 4741
4652#[test] 4742#[test]
4653// FIXME this is currently a Salsa panic; it would be nicer if it just returned
4654// in Unknown, and we should be able to do that once Salsa allows us to handle
4655// the cycle. But at least it doesn't overflow for now.
4656#[should_panic]
4657fn unselected_projection_in_trait_env_cycle_2() { 4743fn unselected_projection_in_trait_env_cycle_2() {
4658 let t = type_at( 4744 let t = type_at(
4659 r#" 4745 r#"
@@ -4680,7 +4766,7 @@ fn type_at_pos(db: &TestDB, pos: FilePosition) -> String {
4680 for decl in crate_def_map[module.local_id].scope.declarations() { 4766 for decl in crate_def_map[module.local_id].scope.declarations() {
4681 if let ModuleDefId::FunctionId(func) = decl { 4767 if let ModuleDefId::FunctionId(func) = decl {
4682 let (_body, source_map) = db.body_with_source_map(func.into()); 4768 let (_body, source_map) = db.body_with_source_map(func.into());
4683 if let Some(expr_id) = source_map.node_expr(Source::new(pos.file_id.into(), &expr)) { 4769 if let Some(expr_id) = source_map.node_expr(InFile::new(pos.file_id.into(), &expr)) {
4684 let infer = db.infer(func.into()); 4770 let infer = db.infer(func.into());
4685 let ty = &infer[expr_id]; 4771 let ty = &infer[expr_id];
4686 return ty.display(db).to_string(); 4772 return ty.display(db).to_string();