]> git.lizzy.rs Git - rust.git/blobdiff - crates/hir_ty/src/tests/patterns.rs
Merge #9007
[rust.git] / crates / hir_ty / src / tests / patterns.rs
index aeb191c79a77f826987652677dce6d6784bf3e9d..cd08b5c7a5723c5637bab7c2b29c97ef64718d95 100644 (file)
@@ -1,7 +1,6 @@
 use expect_test::expect;
-use test_utils::mark;
 
-use super::{check_infer, check_infer_with_mismatches};
+use super::{check_infer, check_infer_with_mismatches, check_types};
 
 #[test]
 fn infer_pattern() {
@@ -197,7 +196,7 @@ fn test() {
 
 #[test]
 fn infer_pattern_match_ergonomics_ref() {
-    mark::check!(match_ergonomics_ref);
+    cov_mark::check!(match_ergonomics_ref);
     check_infer(
         r#"
         fn test() {
@@ -244,8 +243,8 @@ fn test() {
         expect![[r#"
             10..209 '{     ...   } }': ()
             20..25 'slice': &[f64]
-            36..42 '&[0.0]': &[f64; _]
-            37..42 '[0.0]': [f64; _]
+            36..42 '&[0.0]': &[f64; 1]
+            37..42 '[0.0]': [f64; 1]
             38..41 '0.0': f64
             48..207 'match ...     }': ()
             54..59 'slice': &[f64]
@@ -346,19 +345,19 @@ fn test() {
         "#,
         expect![[r#"
             10..179 '{     ...   } }': ()
-            20..23 'arr': [f64; _]
-            36..46 '[0.0, 1.0]': [f64; _]
+            20..23 'arr': [f64; 2]
+            36..46 '[0.0, 1.0]': [f64; 2]
             37..40 '0.0': f64
             42..45 '1.0': f64
             52..177 'match ...     }': ()
-            58..61 'arr': [f64; _]
-            72..80 '[1.0, a]': [f64; _]
+            58..61 'arr': [f64; 2]
+            72..80 '[1.0, a]': [f64; 2]
             73..76 '1.0': f64
             73..76 '1.0': f64
             78..79 'a': f64
             84..110 '{     ...     }': ()
             98..99 'a': f64
-            120..126 '[b, c]': [f64; _]
+            120..126 '[b, c]': [f64; 2]
             121..122 'b': f64
             124..125 'c': f64
             130..171 '{     ...     }': ()
@@ -547,7 +546,9 @@ fn test() {
             273..276 'Bar': usize
             280..283 'Bar': usize
             200..223: expected (), got Foo
+            211..214: expected (), got Foo
             262..285: expected (), got usize
+            273..276: expected (), got usize
         "#]],
     );
 }
@@ -654,3 +655,255 @@ fn foo(params: &[i32]) {
         "#]],
     );
 }
+
+#[test]
+fn box_pattern() {
+    check_infer(
+        r#"
+        pub struct Global;
+        #[lang = "owned_box"]
+        pub struct Box<T, A = Global>(T);
+
+        fn foo(params: Box<i32>) {
+            match params {
+                box integer => {}
+            }
+        }
+        "#,
+        expect![[r#"
+            83..89 'params': Box<i32, Global>
+            101..155 '{     ...   } }': ()
+            107..153 'match ...     }': ()
+            113..119 'params': Box<i32, Global>
+            130..141 'box integer': Box<i32, Global>
+            134..141 'integer': i32
+            145..147 '{}': ()
+        "#]],
+    );
+    check_infer(
+        r#"
+        #[lang = "owned_box"]
+        pub struct Box<T>(T);
+
+        fn foo(params: Box<i32>) {
+            match params {
+                box integer => {}
+            }
+        }
+        "#,
+        expect![[r#"
+            52..58 'params': Box<i32>
+            70..124 '{     ...   } }': ()
+            76..122 'match ...     }': ()
+            82..88 'params': Box<i32>
+            99..110 'box integer': Box<i32>
+            103..110 'integer': i32
+            114..116 '{}': ()
+        "#]],
+    );
+}
+
+#[test]
+fn tuple_ellipsis_pattern() {
+    check_infer_with_mismatches(
+        r#"
+fn foo(tuple: (u8, i16, f32)) {
+    match tuple {
+        (.., b, c) => {},
+        (a, .., c) => {},
+        (a, b, ..) => {},
+        (a, b) => {/*too short*/}
+        (a, b, c, d) => {/*too long*/}
+        _ => {}
+    }
+}"#,
+        expect![[r#"
+            7..12 'tuple': (u8, i16, f32)
+            30..224 '{     ...   } }': ()
+            36..222 'match ...     }': ()
+            42..47 'tuple': (u8, i16, f32)
+            58..68 '(.., b, c)': (u8, i16, f32)
+            63..64 'b': i16
+            66..67 'c': f32
+            72..74 '{}': ()
+            84..94 '(a, .., c)': (u8, i16, f32)
+            85..86 'a': u8
+            92..93 'c': f32
+            98..100 '{}': ()
+            110..120 '(a, b, ..)': (u8, i16, f32)
+            111..112 'a': u8
+            114..115 'b': i16
+            124..126 '{}': ()
+            136..142 '(a, b)': (u8, i16)
+            137..138 'a': u8
+            140..141 'b': i16
+            146..161 '{/*too short*/}': ()
+            170..182 '(a, b, c, d)': (u8, i16, f32, {unknown})
+            171..172 'a': u8
+            174..175 'b': i16
+            177..178 'c': f32
+            180..181 'd': {unknown}
+            186..200 '{/*too long*/}': ()
+            209..210 '_': (u8, i16, f32)
+            214..216 '{}': ()
+            136..142: expected (u8, i16, f32), got (u8, i16)
+            170..182: expected (u8, i16, f32), got (u8, i16, f32, {unknown})
+        "#]],
+    );
+}
+
+#[test]
+fn tuple_struct_ellipsis_pattern() {
+    check_infer(
+        r#"
+struct Tuple(u8, i16, f32);
+fn foo(tuple: Tuple) {
+    match tuple {
+        Tuple(.., b, c) => {},
+        Tuple(a, .., c) => {},
+        Tuple(a, b, ..) => {},
+        Tuple(a, b) => {/*too short*/}
+        Tuple(a, b, c, d) => {/*too long*/}
+        _ => {}
+    }
+}"#,
+        expect![[r#"
+            35..40 'tuple': Tuple
+            49..268 '{     ...   } }': ()
+            55..266 'match ...     }': ()
+            61..66 'tuple': Tuple
+            77..92 'Tuple(.., b, c)': Tuple
+            87..88 'b': i16
+            90..91 'c': f32
+            96..98 '{}': ()
+            108..123 'Tuple(a, .., c)': Tuple
+            114..115 'a': u8
+            121..122 'c': f32
+            127..129 '{}': ()
+            139..154 'Tuple(a, b, ..)': Tuple
+            145..146 'a': u8
+            148..149 'b': i16
+            158..160 '{}': ()
+            170..181 'Tuple(a, b)': Tuple
+            176..177 'a': u8
+            179..180 'b': i16
+            185..200 '{/*too short*/}': ()
+            209..226 'Tuple(... c, d)': Tuple
+            215..216 'a': u8
+            218..219 'b': i16
+            221..222 'c': f32
+            224..225 'd': {unknown}
+            230..244 '{/*too long*/}': ()
+            253..254 '_': Tuple
+            258..260 '{}': ()
+        "#]],
+    );
+}
+
+#[test]
+fn const_block_pattern() {
+    check_infer(
+        r#"
+struct Foo(usize);
+fn foo(foo: Foo) {
+    match foo {
+        const { Foo(15 + 32) } => {},
+        _ => {}
+    }
+}"#,
+        expect![[r#"
+            26..29 'foo': Foo
+            36..115 '{     ...   } }': ()
+            42..113 'match ...     }': ()
+            48..51 'foo': Foo
+            62..84 'const ... 32) }': Foo
+            68..84 '{ Foo(... 32) }': Foo
+            70..73 'Foo': Foo(usize) -> Foo
+            70..82 'Foo(15 + 32)': Foo
+            74..76 '15': usize
+            74..81 '15 + 32': usize
+            79..81 '32': usize
+            88..90 '{}': ()
+            100..101 '_': Foo
+            105..107 '{}': ()
+        "#]],
+    );
+}
+
+#[test]
+fn macro_pat() {
+    check_types(
+        r#"
+macro_rules! pat {
+    ($name:ident) => { Enum::Variant1($name) }
+}
+
+enum Enum {
+    Variant1(u8),
+    Variant2,
+}
+
+fn f(e: Enum) {
+    match e {
+        pat!(bind) => {
+            bind;
+          //^^^^ u8
+        }
+        Enum::Variant2 => {}
+    }
+}
+    "#,
+    )
+}
+
+#[test]
+fn type_mismatch_in_or_pattern() {
+    check_infer_with_mismatches(
+        r#"
+fn main() {
+    match (false,) {
+        (true | (),) => {}
+        (() | true,) => {}
+        (_ | (),) => {}
+        (() | _,) => {}
+    }
+}
+"#,
+        expect![[r#"
+            10..142 '{     ...   } }': ()
+            16..140 'match ...     }': ()
+            22..30 '(false,)': (bool,)
+            23..28 'false': bool
+            41..53 '(true | (),)': (bool,)
+            42..46 'true': bool
+            42..46 'true': bool
+            42..51 'true | ()': bool
+            49..51 '()': ()
+            57..59 '{}': ()
+            68..80 '(() | true,)': ((),)
+            69..71 '()': ()
+            69..78 '() | true': ()
+            74..78 'true': bool
+            74..78 'true': bool
+            84..86 '{}': ()
+            95..104 '(_ | (),)': (bool,)
+            96..97 '_': bool
+            96..102 '_ | ()': bool
+            100..102 '()': ()
+            108..110 '{}': ()
+            119..128 '(() | _,)': ((),)
+            120..122 '()': ()
+            120..126 '() | _': ()
+            125..126 '_': bool
+            132..134 '{}': ()
+            49..51: expected bool, got ()
+            68..80: expected (bool,), got ((),)
+            69..71: expected bool, got ()
+            69..78: expected bool, got ()
+            100..102: expected bool, got ()
+            119..128: expected (bool,), got ((),)
+            120..122: expected bool, got ()
+            120..126: expected bool, got ()
+        "#]],
+    );
+}