]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_ty/src/tests/simple.rs
Support `#[cfg]` on all associated items
[rust.git] / crates / hir_ty / src / tests / simple.rs
index a569223b4b25d4561639278f8e12a94dc5186b19..bcc43ed700e9376bc8dec6bc69fe356853804dc3 100644 (file)
@@ -1,5 +1,4 @@
 use expect_test::expect;
-use test_utils::mark;
 
 use super::{check_infer, check_types};
 
@@ -28,6 +27,30 @@ pub struct Box<T: ?Sized> {
     );
 }
 
+#[test]
+fn infer_box_with_allocator() {
+    check_types(
+        r#"
+//- /main.rs crate:main deps:std
+fn test() {
+    let x = box 1;
+    let t = (x, box x, box &1, box [1]);
+    t;
+} //^ (Box<i32, {unknown}>, Box<Box<i32, {unknown}>, {unknown}>, Box<&i32, {unknown}>, Box<[i32; _], {unknown}>)
+
+//- /std.rs crate:std
+#[prelude_import] use prelude::*;
+mod boxed {
+    #[lang = "owned_box"]
+    pub struct Box<T: ?Sized, A: Allocator> {
+        inner: *mut T,
+        allocator: A,
+    }
+}
+"#,
+    );
+}
+
 #[test]
 fn infer_adt_self() {
     check_types(
@@ -1894,6 +1917,7 @@ async fn main() {
             let x = unsafe { 92 };
             let y = async { async { () }.await };
             let z = try { () };
+            let w = const { 92 };
             let t = 'a: { 92 };
         }
 
@@ -1905,7 +1929,7 @@ mod future {
         }
         "#,
         expect![[r#"
-            16..136 '{     ...2 }; }': ()
+            16..162 '{     ...2 }; }': ()
             26..27 'x': i32
             30..43 'unsafe { 92 }': i32
             37..43 '{ 92 }': i32
@@ -1921,9 +1945,13 @@ mod future {
             99..109 'try { () }': {unknown}
             103..109 '{ () }': ()
             105..107 '()': ()
-            119..120 't': i32
-            127..133 '{ 92 }': i32
-            129..131 '92': i32
+            119..120 'w': i32
+            123..135 'const { 92 }': i32
+            129..135 '{ 92 }': i32
+            131..133 '92': i32
+            145..146 't': i32
+            153..159 '{ 92 }': i32
+            155..157 '92': i32
         "#]],
     )
 }
@@ -2285,7 +2313,7 @@ fn test(t1: Thing) {
 
 #[test]
 fn infer_operator_overload() {
-    mark::check!(infer_expr_inner_binary_operator_overload);
+    cov_mark::check!(infer_expr_inner_binary_operator_overload);
 
     check_infer(
         r#"
@@ -2370,3 +2398,169 @@ fn test() {
         "#]],
     );
 }
+
+#[test]
+fn infer_const_params() {
+    check_infer(
+        r#"
+        fn foo<const FOO: usize>() {
+            let bar = FOO;
+        }
+        "#,
+        expect![[r#"
+            27..49 '{     ...FOO; }': ()
+            37..40 'bar': usize
+            43..46 'FOO': usize
+        "#]],
+    );
+}
+
+#[test]
+fn infer_inner_type() {
+    check_infer(
+        r#"
+        fn foo() {
+            struct S { field: u32 }
+            let s = S { field: 0 };
+            let f = s.field;
+        }
+    "#,
+        expect![[r#"
+            9..89 '{     ...eld; }': ()
+            47..48 's': S
+            51..65 'S { field: 0 }': S
+            62..63 '0': u32
+            75..76 'f': u32
+            79..80 's': S
+            79..86 's.field': u32
+        "#]],
+    );
+}
+
+#[test]
+fn infer_nested_inner_type() {
+    check_infer(
+        r#"
+        fn foo() {
+            {
+                let s = S { field: 0 };
+                let f = s.field;
+            }
+            struct S { field: u32 }
+        }
+    "#,
+        expect![[r#"
+            9..109 '{     ...32 } }': ()
+            15..79 '{     ...     }': ()
+            29..30 's': S
+            33..47 'S { field: 0 }': S
+            44..45 '0': u32
+            61..62 'f': u32
+            65..66 's': S
+            65..72 's.field': u32
+        "#]],
+    );
+}
+
+#[test]
+fn inner_use_enum_rename() {
+    check_infer(
+        r#"
+        enum Request {
+            Info
+        }
+
+        fn f() {
+            use Request as R;
+
+            let r = R::Info;
+            match r {
+                R::Info => {}
+            }
+        }
+    "#,
+        expect![[r#"
+            34..123 '{     ...   } }': ()
+            67..68 'r': Request
+            71..78 'R::Info': Request
+            84..121 'match ...     }': ()
+            90..91 'r': Request
+            102..109 'R::Info': Request
+            113..115 '{}': ()
+        "#]],
+    )
+}
+
+#[test]
+fn box_into_vec() {
+    check_infer(
+        r#"
+#[lang = "sized"]
+pub trait Sized {}
+
+#[lang = "unsize"]
+pub trait Unsize<T: ?Sized> {}
+
+#[lang = "coerce_unsized"]
+pub trait CoerceUnsized<T> {}
+
+pub unsafe trait Allocator {}
+
+pub struct Global;
+unsafe impl Allocator for Global {}
+
+#[lang = "owned_box"]
+#[fundamental]
+pub struct Box<T: ?Sized, A: Allocator = Global>;
+
+impl<T: ?Sized + Unsize<U>, U: ?Sized, A: Allocator> CoerceUnsized<Box<U, A>> for Box<T, A> {}
+
+pub struct Vec<T, A: Allocator = Global> {}
+
+#[lang = "slice"]
+impl<T> [T] {}
+
+#[lang = "slice_alloc"]
+impl<T> [T] {
+    pub fn into_vec<A: Allocator>(self: Box<Self, A>) -> Vec<T, A> {
+        unimplemented!()
+    }
+}
+
+fn test() {
+    let vec = <[_]>::into_vec(box [1i32]);
+}
+"#,
+        expect![[r#"
+            569..573 'self': Box<[T], A>
+            602..634 '{     ...     }': Vec<T, A>
+            612..628 'unimpl...ted!()': Vec<T, A>
+            648..694 '{     ...2]); }': ()
+            658..661 'vec': Vec<i32, Global>
+            664..679 '<[_]>::into_vec': fn into_vec<i32, Global>(Box<[i32], Global>) -> Vec<i32, Global>
+            664..691 '<[_]>:...1i32])': Vec<i32, Global>
+            680..690 'box [1i32]': Box<[i32; _], Global>
+            684..690 '[1i32]': [i32; _]
+            685..689 '1i32': i32
+        "#]],
+    )
+}
+
+#[test]
+fn cfgd_out_assoc_items() {
+    check_types(
+        r#"
+struct S;
+
+impl S {
+    #[cfg(FALSE)]
+    const C: S = S;
+}
+
+fn f() {
+    S::C;
+  //^^^^ {unknown}
+}
+    "#,
+    )
+}