8 body::BodySourceMap, db::DefDatabase, nameres::CrateDefMap, AssocItemId, DefWithBodyId,
9 LocalModuleId, Lookup, ModuleDefId,
11 use hir_expand::InFile;
12 use insta::assert_snapshot;
13 use ra_db::{fixture::WithFixture, salsa::Database, FilePosition, SourceDatabase};
18 use test_utils::covers;
20 use crate::{db::HirDatabase, display::HirDisplay, test_db::TestDB, InferenceResult};
22 // These tests compare the inference results for all expressions in a file
23 // against snapshots of the expected results using insta. Use cargo-insta to
24 // update the snapshots.
28 let (db, pos) = TestDB::with_position(
30 //- /main.rs crate:main deps:foo cfg:test
36 fn foo1(&self) -> i32 { 0 }
41 fn foo2(&self) -> i32 { 0 }
45 let t = (S.foo1(), S.foo2(), T.foo3(), T.foo4());
54 fn foo3(&self) -> i32 { 0 }
59 fn foo4(&self) -> i32 { 0 }
63 assert_eq!("(i32, {unknown}, i32, {unknown})", type_at_pos(&db, pos));
68 let (db, pos) = TestDB::with_position(
70 //- /main.rs crate:main deps:std
74 impl Future for IntFuture {
85 #[prelude_import] use future::*;
94 assert_eq!("u64", type_at_pos(&db, pos));
99 let (db, pos) = TestDB::with_position(
101 //- /main.rs crate:main deps:std
105 let t = (x, box x, box &1, box [1]);
109 //- /std.rs crate:std
110 #[prelude_import] use prelude::*;
114 pub struct Box<T: ?Sized> {
121 assert_eq!("(Box<i32>, Box<Box<i32>>, Box<&i32>, Box<[i32;_]>)", type_at_pos(&db, pos));
125 fn infer_adt_self() {
126 let (db, pos) = TestDB::with_position(
129 enum Nat { Succ(Self), Demo(Nat), Zero }
132 let foo: Nat = Nat::Zero;
133 if let Nat::Succ(x) = foo {
140 assert_eq!("Nat", type_at_pos(&db, pos));
145 let (db, pos) = TestDB::with_position(
147 //- /main.rs crate:main deps:std
150 let r: Result<i32, u64> = Result::Ok(1);
155 //- /std.rs crate:std
157 #[prelude_import] use ops::*;
165 #[prelude_import] use result::*;
172 impl<O, E> crate::ops::Try for Result<O, E> {
180 assert_eq!("i32", type_at_pos(&db, pos));
184 fn infer_for_loop() {
185 let (db, pos) = TestDB::with_position(
187 //- /main.rs crate:main deps:std
189 use std::collections::Vec;
199 //- /std.rs crate:std
201 #[prelude_import] use iter::*;
211 fn new() -> Self { Vec {} }
212 fn push(&mut self, t: T) { }
215 impl<T> crate::iter::IntoIterator for Vec<T> {
221 assert_eq!("&str", type_at_pos(&db, pos));
226 let (db, pos) = TestDB::with_position(
228 //- /main.rs crate:main deps:std
237 let t = (a, b, c, d, e, f);
241 //- /std.rs crate:std
242 #[prelude_import] use prelude::*;
246 pub struct Range<Idx> {
250 pub struct RangeFrom<Idx> {
254 pub struct RangeInclusive<Idx> {
259 pub struct RangeTo<Idx> {
262 pub struct RangeToInclusive<Idx> {
269 "(RangeFull, RangeFrom<i32>, RangeTo<u32>, Range<usize>, RangeToInclusive<i32>, RangeInclusive<char>)",
270 type_at_pos(&db, pos),
275 fn infer_while_let() {
276 let (db, pos) = TestDB::with_position(
279 enum Option<T> { Some(T), None }
282 let foo: Option<f32> = None;
283 while let Option::Some(x) = foo {
290 assert_eq!("f32", type_at_pos(&db, pos));
297 fn test(a: u32, b: isize, c: !, d: &str) {
312 [42; 121) '{ ...f32; }': !
317 [76; 82) '1usize': usize
318 [88; 94) '1isize': isize
319 [100; 106) '"test"': &str
320 [112; 118) '1.0f32': f32
339 [11; 118) '{ ...= e; }': ()
341 [25; 31) '1isize': isize
370 [15; 20) '{ 1 }': u32
372 [48; 53) '{ 1 }': u32
374 [67; 91) '{ ...c(); }': ()
375 [73; 74) 'a': fn a() -> u32
377 [82; 86) 'b::c': fn c() -> u32
378 [82; 88) 'b::c()': u32
384 fn infer_path_type() {
390 fn foo() -> i32 { 1 }
399 [41; 46) '{ 1 }': i32
401 [60; 93) '{ ...o(); }': ()
402 [66; 72) 'S::foo': fn foo() -> i32
403 [66; 74) 'S::foo()': i32
404 [80; 88) '<S>::foo': fn foo() -> i32
405 [80; 90) '<S>::foo()': i32
411 fn infer_slice_method() {
421 #[lang = "slice_alloc"]
429 [45; 49) 'self': &[T]
430 [56; 79) '{ ... }': T
431 [66; 73) 'loop {}': !
433 [133; 160) '{ ...o"); }': ()
434 [139; 149) '<[_]>::foo': fn foo<u8>(&[T]) -> T
435 [139; 157) '<[_]>:..."foo")': u8
436 [150; 156) 'b"foo"': &[u8]
455 let a: A = A { b: B, c: C(1) };
461 [72; 154) '{ ...a.c; }': ()
463 [86; 87) 'C': C(usize) -> C
468 [114; 133) 'A { b:...C(1) }': A
470 [127; 128) 'C': C(usize) -> C
472 [129; 130) '1': usize
494 [48; 82) '{ E:...:V2; }': ()
495 [52; 70) 'E::V1 ...d: 1 }': E
506 fn test(a: &u32, b: &mut u32, c: *const u32, d: *mut u32) {
522 [18; 19) 'b': &mut u32
523 [31; 32) 'c': *const u32
524 [46; 47) 'd': *mut u32
525 [59; 150) '{ ... *d; }': ()
531 [88; 94) '&mut a': &mut &u32
533 [100; 101) 'b': &mut u32
535 [108; 109) 'b': &mut u32
536 [115; 117) '&b': &&mut u32
537 [116; 117) 'b': &mut u32
538 [123; 124) 'c': *const u32
540 [131; 132) 'c': *const u32
541 [138; 139) 'd': *mut u32
543 [146; 147) 'd': *mut u32
549 fn infer_literals() {
573 [11; 221) '{ ...o"#; }': ()
577 [47; 54) '"hello"': &str
578 [60; 68) 'b"bytes"': &[u8]
582 [103; 107) '5000': i32
583 [113; 118) 'false': bool
584 [124; 128) 'true': bool
585 [134; 202) 'r#" ... "#': &str
586 [208; 218) 'br#"yolo"#': &[u8]
592 fn infer_unary_op() {
597 fn test(x: SomeType) {
616 [27; 28) 'x': SomeType
617 [40; 272) '{ ...lo"; }': ()
619 [54; 59) 'false': bool
626 [112; 114) '-a': i128
629 [128; 132) '-100': i32
630 [129; 132) '100': i32
632 [146; 153) '!!!true': bool
633 [147; 153) '!!true': bool
634 [148; 153) '!true': bool
635 [149; 153) 'true': bool
637 [167; 170) '!42': i32
640 [184; 190) '!10u32': u32
641 [185; 190) '10u32': u32
643 [204; 206) '!a': i128
645 [212; 217) '-3.14': f64
646 [213; 217) '3.14': f64
649 [231; 233) '-x': {unknown}
650 [232; 233) 'x': SomeType
651 [239; 241) '!x': {unknown}
652 [240; 241) 'x': SomeType
653 [247; 255) '-"hello"': {unknown}
654 [248; 255) '"hello"': &str
655 [261; 269) '!"hello"': {unknown}
656 [262; 269) '"hello"': &str
662 fn infer_backwards() {
665 fn takes_u32(x: u32) {}
667 struct S { i32_field: i32 }
669 fn test() -> &mut &f64 {
670 let a = unknown_function();
672 let b = unknown_function();
674 let c = unknown_function();
681 [78; 231) '{ ...t &c }': &mut &f64
683 [92; 108) 'unknow...nction': {unknown}
684 [92; 110) 'unknow...tion()': u32
685 [116; 125) 'takes_u32': fn takes_u32(u32) -> ()
686 [116; 128) 'takes_u32(a)': ()
689 [142; 158) 'unknow...nction': {unknown}
690 [142; 160) 'unknow...tion()': i32
691 [166; 184) 'S { i3...d: b }': S
694 [198; 214) 'unknow...nction': {unknown}
695 [198; 216) 'unknow...tion()': f64
696 [222; 229) '&mut &c': &mut &f64
697 [227; 229) '&c': &f64
713 fn test2(self: &Self) {
726 [40; 61) '{ ... }': ()
729 [88; 109) '{ ... }': ()
731 [133; 153) '{ ... }': S
733 [177; 200) '{ ... }': S
734 [187; 194) 'Self {}': S
740 fn infer_binary_op() {
743 fn f(x: bool) -> i32 {
749 let y = true || false;
752 let minus_forty: isize = -40isize;
753 let h = minus_forty <= CONST_2;
754 let c = f(z || y) + 5;
756 let g = minus_forty ^= i;
758 let ten_is_eleven = ten == some_num;
765 [22; 34) '{ 0i32 }': i32
767 [54; 370) '{ ... < 3 }': bool
770 [68; 74) 'a && b': bool
773 [88; 92) 'true': bool
774 [88; 101) 'true || false': bool
775 [96; 101) 'false': bool
778 [115; 121) 'x == y': bool
782 [135; 141) 'x != y': bool
784 [151; 162) 'minus_forty': isize
785 [172; 180) '-40isize': isize
786 [173; 180) '40isize': isize
788 [194; 205) 'minus_forty': isize
789 [194; 216) 'minus_...ONST_2': bool
790 [209; 216) 'CONST_2': isize
792 [230; 231) 'f': fn f(bool) -> i32
793 [230; 239) 'f(z || y)': i32
794 [230; 243) 'f(z || y) + 5': i32
796 [232; 238) 'z || y': bool
799 [253; 254) 'd': {unknown}
800 [257; 258) 'b': {unknown}
802 [272; 283) 'minus_forty': isize
803 [272; 288) 'minus_...y ^= i': ()
804 [287; 288) 'i': isize
805 [298; 301) 'ten': usize
806 [311; 313) '10': usize
807 [323; 336) 'ten_is_eleven': bool
808 [339; 342) 'ten': usize
809 [339; 354) 'ten == some_num': bool
810 [346; 354) 'some_num': usize
811 [361; 364) 'ten': usize
812 [361; 368) 'ten < 3': bool
813 [367; 368) '3': usize
819 fn infer_field_autoderef() {
836 let a5 = &mut &&mut &&mut a;
840 fn test2(a1: *const A, a2: *mut A) {
847 [50; 213) '{ ...5.b; }': ()
857 [113; 115) 'a3': &mut A
858 [118; 124) '&mut a': &mut A
860 [130; 132) 'a3': &mut A
862 [144; 146) 'a4': &&&&&&&A
863 [149; 157) '&&&&&&&a': &&&&&&&A
864 [150; 157) '&&&&&&a': &&&&&&A
865 [151; 157) '&&&&&a': &&&&&A
866 [152; 157) '&&&&a': &&&&A
867 [153; 157) '&&&a': &&&A
868 [154; 157) '&&a': &&A
871 [163; 165) 'a4': &&&&&&&A
873 [177; 179) 'a5': &mut &&mut &&mut A
874 [182; 200) '&mut &...&mut a': &mut &&mut &&mut A
875 [187; 200) '&&mut &&mut a': &&mut &&mut A
876 [188; 200) '&mut &&mut a': &mut &&mut A
877 [193; 200) '&&mut a': &&mut A
878 [194; 200) '&mut a': &mut A
880 [206; 208) 'a5': &mut &&mut &&mut A
882 [224; 226) 'a1': *const A
883 [238; 240) 'a2': *mut A
884 [250; 273) '{ ...2.b; }': ()
885 [256; 258) 'a1': *const A
887 [266; 268) 'a2': *mut A
894 fn infer_argument_autoderef() {
900 fn deref(&self) -> &Self::Target;
906 fn foo(&self) -> &T {
913 impl<T> Deref for B<T> {
915 fn deref(&self) -> &Self::Target {
921 let t = A::foo(&&B(B(A(42))));
925 [68; 72) 'self': &Self
926 [139; 143) 'self': &A<T>
927 [151; 174) '{ ... }': &T
928 [161; 168) '&self.0': &T
929 [162; 166) 'self': &A<T>
930 [162; 168) 'self.0': T
931 [255; 259) 'self': &B<T>
932 [278; 301) '{ ... }': &T
933 [288; 295) '&self.0': &T
934 [289; 293) 'self': &B<T>
935 [289; 295) 'self.0': T
936 [315; 353) '{ ...))); }': ()
938 [329; 335) 'A::foo': fn foo<i32>(&A<T>) -> &T
939 [329; 350) 'A::foo...42))))': &i32
940 [336; 349) '&&B(B(A(42)))': &&B<B<A<i32>>>
941 [337; 349) '&B(B(A(42)))': &B<B<A<i32>>>
942 [338; 339) 'B': B<B<A<i32>>>(T) -> B<T>
943 [338; 349) 'B(B(A(42)))': B<B<A<i32>>>
944 [340; 341) 'B': B<A<i32>>(T) -> B<T>
945 [340; 348) 'B(A(42))': B<A<i32>>
946 [342; 343) 'A': A<i32>(T) -> A<T>
947 [342; 347) 'A(42)': A<i32>
954 fn infer_method_argument_autoderef() {
960 fn deref(&self) -> &Self::Target;
966 fn foo(&self, x: &A<T>) -> &T {
973 impl<T> Deref for B<T> {
975 fn deref(&self) -> &Self::Target {
981 let t = A(0 as *mut _).foo(&&B(B(a)));
985 [68; 72) 'self': &Self
986 [144; 148) 'self': &A<T>
987 [150; 151) 'x': &A<T>
988 [166; 187) '{ ... }': &T
989 [176; 181) '&*x.0': &T
991 [178; 179) 'x': &A<T>
992 [178; 181) 'x.0': *mut T
993 [268; 272) 'self': &B<T>
994 [291; 314) '{ ... }': &T
995 [301; 308) '&self.0': &T
996 [302; 306) 'self': &B<T>
997 [302; 308) 'self.0': T
998 [326; 327) 'a': A<i32>
999 [337; 383) '{ ...))); }': ()
1000 [347; 348) 't': &i32
1001 [351; 352) 'A': A<i32>(*mut T) -> A<T>
1002 [351; 365) 'A(0 as *mut _)': A<i32>
1003 [351; 380) 'A(0 as...B(a)))': &i32
1005 [353; 364) '0 as *mut _': *mut i32
1006 [370; 379) '&&B(B(a))': &&B<B<A<i32>>>
1007 [371; 379) '&B(B(a))': &B<B<A<i32>>>
1008 [372; 373) 'B': B<B<A<i32>>>(T) -> B<T>
1009 [372; 379) 'B(B(a))': B<B<A<i32>>>
1010 [374; 375) 'B': B<A<i32>>(T) -> B<T>
1011 [374; 378) 'B(a)': B<A<i32>>
1012 [376; 377) 'a': A<i32>
1026 [11; 37) '{ l... {}; }': ()
1028 [24; 34) 'if true {}': ()
1029 [27; 31) 'true': bool
1036 fn infer_in_elseif() {
1039 struct Foo { field: i32 }
1050 [45; 109) '{ ... } }': ()
1051 [51; 107) 'if tru... }': ()
1052 [54; 58) 'true': bool
1054 [73; 107) 'if fal... }': ()
1055 [76; 81) 'false': bool
1056 [82; 107) '{ ... }': i32
1058 [92; 101) 'foo.field': i32
1064 fn infer_if_match_with_return() {
1078 let _x3 = match true {
1084 let _x4 = match true {
1090 [10; 323) '{ ... }; }': ()
1092 [26; 80) 'if tru... }': i32
1093 [29; 33) 'true': bool
1094 [34; 51) '{ ... }': i32
1096 [57; 80) '{ ... }': !
1097 [67; 73) 'return': !
1099 [96; 149) 'if tru... }': i32
1100 [99; 103) 'true': bool
1101 [104; 121) '{ ... }': i32
1103 [127; 149) '{ ... }': !
1104 [137; 143) 'return': !
1105 [159; 162) '_x3': i32
1106 [165; 247) 'match ... }': i32
1107 [171; 175) 'true': bool
1108 [186; 190) 'true': bool
1110 [205; 206) '_': bool
1111 [210; 241) '{ ... }': !
1112 [224; 230) 'return': !
1113 [257; 260) '_x4': i32
1114 [263; 320) 'match ... }': i32
1115 [269; 273) 'true': bool
1116 [284; 288) 'true': bool
1118 [303; 304) '_': bool
1119 [308; 314) 'return': !
1125 fn infer_inherent_method() {
1131 fn foo(self, x: u32) -> i32 {}
1136 fn bar(&self, x: u64) -> i64 {}
1150 [103; 107) 'self': &A
1154 [150; 198) '{ ...(1); }': ()
1156 [156; 164) 'a.foo(1)': i32
1158 [170; 181) '(&a).bar(1)': i64
1163 [187; 195) 'a.bar(1)': i64
1170 fn infer_inherent_method_str() {
1175 fn foo(&self) -> i32 {}
1183 [40; 44) 'self': &str
1185 [69; 89) '{ ...o(); }': ()
1186 [75; 80) '"foo"': &str
1187 [75; 86) '"foo".foo()': i32
1196 fn test(x: &str, y: isize) {
1197 let a: (u32, &str) = (1, "a");
1208 [28; 170) '{ ...d"); }': ()
1209 [38; 39) 'a': (u32, &str)
1210 [55; 63) '(1, "a")': (u32, &str)
1212 [59; 62) '"a"': &str
1213 [73; 74) 'b': ((u32, &str), &str)
1214 [77; 83) '(a, x)': ((u32, &str), &str)
1215 [78; 79) 'a': (u32, &str)
1217 [93; 94) 'c': (isize, &str)
1218 [97; 103) '(y, x)': (isize, &str)
1220 [101; 102) 'x': &str
1221 [113; 114) 'd': ((isize, &str), &str)
1222 [117; 123) '(c, x)': ((isize, &str), &str)
1223 [118; 119) 'c': (isize, &str)
1224 [121; 122) 'x': &str
1225 [133; 134) 'e': (i32, &str)
1226 [137; 145) '(1, "e")': (i32, &str)
1228 [141; 144) '"e"': &str
1229 [155; 156) 'f': ((i32, &str), &str)
1230 [159; 167) '(e, "d")': ((i32, &str), &str)
1231 [160; 161) 'e': (i32, &str)
1232 [163; 166) '"d"': &str
1241 fn test(x: &str, y: isize) {
1246 let d = [y, 1, 2, 3];
1247 let d = [1, y, 2, 3];
1256 let x: [u8; 0] = [];
1262 [28; 293) '{ ... []; }': ()
1263 [38; 39) 'a': [&str;_]
1264 [42; 45) '[x]': [&str;_]
1266 [55; 56) 'b': [[&str;_];_]
1267 [59; 65) '[a, a]': [[&str;_];_]
1268 [60; 61) 'a': [&str;_]
1269 [63; 64) 'a': [&str;_]
1270 [75; 76) 'c': [[[&str;_];_];_]
1271 [79; 85) '[b, b]': [[[&str;_];_];_]
1272 [80; 81) 'b': [[&str;_];_]
1273 [83; 84) 'b': [[&str;_];_]
1274 [96; 97) 'd': [isize;_]
1275 [100; 112) '[y, 1, 2, 3]': [isize;_]
1276 [101; 102) 'y': isize
1277 [104; 105) '1': isize
1278 [107; 108) '2': isize
1279 [110; 111) '3': isize
1280 [122; 123) 'd': [isize;_]
1281 [126; 138) '[1, y, 2, 3]': [isize;_]
1282 [127; 128) '1': isize
1283 [130; 131) 'y': isize
1284 [133; 134) '2': isize
1285 [136; 137) '3': isize
1286 [148; 149) 'e': [isize;_]
1287 [152; 155) '[y]': [isize;_]
1288 [153; 154) 'y': isize
1289 [165; 166) 'f': [[isize;_];_]
1290 [169; 175) '[d, d]': [[isize;_];_]
1291 [170; 171) 'd': [isize;_]
1292 [173; 174) 'd': [isize;_]
1293 [185; 186) 'g': [[isize;_];_]
1294 [189; 195) '[e, e]': [[isize;_];_]
1295 [190; 191) 'e': [isize;_]
1296 [193; 194) 'e': [isize;_]
1297 [206; 207) 'h': [i32;_]
1298 [210; 216) '[1, 2]': [i32;_]
1301 [226; 227) 'i': [&str;_]
1302 [230; 240) '["a", "b"]': [&str;_]
1303 [231; 234) '"a"': &str
1304 [236; 239) '"b"': &str
1305 [251; 252) 'b': [[&str;_];_]
1306 [255; 265) '[a, ["b"]]': [[&str;_];_]
1307 [256; 257) 'a': [&str;_]
1308 [259; 264) '["b"]': [&str;_]
1309 [260; 263) '"b"': &str
1310 [275; 276) 'x': [u8;_]
1311 [288; 290) '[]': [u8;_]
1317 fn infer_pattern() {
1324 let (c, d) = (1, "hello");
1326 for (e, f) in some_iter {
1330 if let [val] = opt {
1334 let lambda = |a: u64, b, c: i32| { a + b; c };
1336 let ref ref_to_x = x;
1338 let ref mut mut_ref_to_x = x;
1339 let k = mut_ref_to_x;
1344 [18; 369) '{ ...o_x; }': ()
1352 [74; 80) '(c, d)': (i32, &str)
1355 [83; 95) '(1, "hello")': (i32, &str)
1357 [87; 94) '"hello"': &str
1358 [102; 152) 'for (e... }': ()
1359 [106; 112) '(e, f)': ({unknown}, {unknown})
1360 [107; 108) 'e': {unknown}
1361 [110; 111) 'f': {unknown}
1362 [116; 125) 'some_iter': {unknown}
1363 [126; 152) '{ ... }': ()
1364 [140; 141) 'g': {unknown}
1365 [144; 145) 'e': {unknown}
1366 [158; 205) 'if let... }': ()
1367 [165; 170) '[val]': {unknown}
1368 [173; 176) 'opt': {unknown}
1369 [177; 205) '{ ... }': ()
1370 [191; 192) 'h': {unknown}
1371 [195; 198) 'val': {unknown}
1372 [215; 221) 'lambda': |u64, u64, i32| -> i32
1373 [224; 256) '|a: u6...b; c }': |u64, u64, i32| -> i32
1377 [244; 256) '{ a + b; c }': i32
1379 [246; 251) 'a + b': u64
1382 [267; 279) 'ref ref_to_x': &&i32
1383 [282; 283) 'x': &i32
1384 [293; 302) 'mut mut_x': &i32
1385 [305; 306) 'x': &i32
1386 [316; 336) 'ref mu...f_to_x': &mut &i32
1387 [339; 340) 'x': &i32
1388 [350; 351) 'k': &mut &i32
1389 [354; 366) 'mut_ref_to_x': &mut &i32
1395 fn infer_pattern_match_ergonomics() {
1402 let A(n) = &mut A(1);
1406 [28; 79) '{ ...(1); }': ()
1407 [38; 42) 'A(n)': A<i32>
1409 [45; 50) '&A(1)': &A<i32>
1410 [46; 47) 'A': A<i32>(T) -> A<T>
1411 [46; 50) 'A(1)': A<i32>
1413 [60; 64) 'A(n)': A<i32>
1414 [62; 63) 'n': &mut i32
1415 [67; 76) '&mut A(1)': &mut A<i32>
1416 [72; 73) 'A': A<i32>(T) -> A<T>
1417 [72; 76) 'A(1)': A<i32>
1424 fn infer_pattern_match_ergonomics_ref() {
1425 covers!(match_ergonomics_ref);
1434 [11; 57) '{ ...= v; }': ()
1435 [21; 22) 'v': &(i32, &i32)
1436 [25; 33) '&(1, &2)': &(i32, &i32)
1437 [26; 33) '(1, &2)': (i32, &i32)
1441 [43; 50) '(_, &w)': (i32, &i32)
1445 [53; 54) 'v': &(i32, &i32)
1451 fn infer_adt_pattern() {
1462 let e = E::A { x: 3 };
1465 let E::A { x: new_var } = e;
1473 let ref d @ E::A { .. } = e;
1478 [68; 289) '{ ... d; }': ()
1480 [82; 95) 'E::A { x: 3 }': E
1482 [106; 113) 'S(y, z)': S
1486 [129; 148) 'E::A {..._var }': E
1487 [139; 146) 'new_var': usize
1489 [159; 245) 'match ... }': usize
1491 [177; 187) 'E::A { x }': E
1492 [184; 185) 'x': usize
1493 [191; 192) 'x': usize
1494 [202; 206) 'E::B': E
1495 [210; 213) 'foo': bool
1496 [217; 218) '1': usize
1497 [228; 232) 'E::B': E
1498 [236; 238) '10': usize
1499 [256; 275) 'ref d ...{ .. }': &E
1500 [264; 275) 'E::A { .. }': E
1508 fn infer_struct_generics() {
1515 fn test(a1: A<u32>, i: i32) {
1517 let a2 = A { x: i };
1519 let a3 = A::<i128> { x: 1 };
1524 [36; 38) 'a1': A<u32>
1526 [56; 147) '{ ...3.x; }': ()
1527 [62; 64) 'a1': A<u32>
1528 [62; 66) 'a1.x': u32
1529 [76; 78) 'a2': A<i32>
1530 [81; 91) 'A { x: i }': A<i32>
1532 [97; 99) 'a2': A<i32>
1533 [97; 101) 'a2.x': i32
1534 [111; 113) 'a3': A<i128>
1535 [116; 134) 'A::<i1...x: 1 }': A<i128>
1536 [131; 132) '1': i128
1537 [140; 142) 'a3': A<i128>
1538 [140; 144) 'a3.x': i128
1544 fn infer_tuple_struct_generics() {
1548 enum Option<T> { Some(T), None }
1557 let x: Option<i64> = None;
1561 [76; 184) '{ ...one; }': ()
1562 [82; 83) 'A': A<i32>(T) -> A<T>
1563 [82; 87) 'A(42)': A<i32>
1565 [93; 94) 'A': A<u128>(T) -> A<T>
1566 [93; 102) 'A(42u128)': A<u128>
1567 [95; 101) '42u128': u128
1568 [108; 112) 'Some': Some<&str>(T) -> Option<T>
1569 [108; 117) 'Some("x")': Option<&str>
1570 [113; 116) '"x"': &str
1571 [123; 135) 'Option::Some': Some<&str>(T) -> Option<T>
1572 [123; 140) 'Option...e("x")': Option<&str>
1573 [136; 139) '"x"': &str
1574 [146; 150) 'None': Option<{unknown}>
1575 [160; 161) 'x': Option<i64>
1576 [177; 181) 'None': Option<i64>
1582 fn infer_generics_in_patterns() {
1594 fn test(a1: A<u32>, o: Option<u64>) {
1595 let A { x: x2 } = a1;
1596 let A::<i64> { x: x3 } = A { x: 1 };
1598 Option::Some(t) => t,
1604 [79; 81) 'a1': A<u32>
1605 [91; 92) 'o': Option<u64>
1606 [107; 244) '{ ... }; }': ()
1607 [117; 128) 'A { x: x2 }': A<u32>
1608 [124; 126) 'x2': u32
1609 [131; 133) 'a1': A<u32>
1610 [143; 161) 'A::<i6...: x3 }': A<i64>
1611 [157; 159) 'x3': i64
1612 [164; 174) 'A { x: 1 }': A<i64>
1614 [180; 241) 'match ... }': u64
1615 [186; 187) 'o': Option<u64>
1616 [198; 213) 'Option::Some(t)': Option<u64>
1619 [228; 229) '_': Option<u64>
1626 fn infer_function_generics() {
1629 fn id<T>(t: T) -> T { t }
1641 [38; 98) '{ ...(1); }': ()
1642 [44; 46) 'id': fn id<u32>(T) -> T
1643 [44; 52) 'id(1u32)': u32
1644 [47; 51) '1u32': u32
1645 [58; 68) 'id::<i128>': fn id<i128>(T) -> T
1646 [58; 71) 'id::<i128>(1)': i128
1649 [90; 92) 'id': fn id<u64>(T) -> T
1650 [90; 95) 'id(1)': u64
1657 fn infer_impl_generics() {
1664 impl<Y, X> A<X, Y> {
1671 fn z<T>(self, t: T) -> (X, Y, T) {
1677 let a = A { x: 1u64, y: 1i64 };
1685 [74; 78) 'self': A<X, Y>
1686 [85; 107) '{ ... }': X
1687 [95; 99) 'self': A<X, Y>
1688 [95; 101) 'self.x': X
1689 [117; 121) 'self': A<X, Y>
1690 [128; 150) '{ ... }': Y
1691 [138; 142) 'self': A<X, Y>
1692 [138; 144) 'self.y': Y
1693 [163; 167) 'self': A<X, Y>
1695 [188; 223) '{ ... }': (X, Y, T)
1696 [198; 217) '(self.....y, t)': (X, Y, T)
1697 [199; 203) 'self': A<X, Y>
1698 [199; 205) 'self.x': X
1699 [207; 211) 'self': A<X, Y>
1700 [207; 213) 'self.y': Y
1702 [245; 342) '{ ...(1); }': ()
1703 [255; 256) 'a': A<u64, i64>
1704 [259; 281) 'A { x:...1i64 }': A<u64, i64>
1705 [266; 270) '1u64': u64
1706 [275; 279) '1i64': i64
1707 [287; 288) 'a': A<u64, i64>
1708 [287; 292) 'a.x()': u64
1709 [298; 299) 'a': A<u64, i64>
1710 [298; 303) 'a.y()': i64
1711 [309; 310) 'a': A<u64, i64>
1712 [309; 319) 'a.z(1i128)': (u64, i64, i128)
1713 [313; 318) '1i128': i128
1714 [325; 326) 'a': A<u64, i64>
1715 [325; 339) 'a.z::<u128>(1)': (u64, i64, u128)
1716 [337; 338) '1': u128
1722 fn infer_impl_generics_with_autoderef() {
1730 fn as_ref(&self) -> Option<&T> {}
1732 fn test(o: Option<u32>) {
1738 [78; 82) 'self': &Option<T>
1740 [111; 112) 'o': Option<u32>
1741 [127; 165) '{ ...f(); }': ()
1742 [133; 146) '(&o).as_ref()': Option<&u32>
1743 [134; 136) '&o': &Option<u32>
1744 [135; 136) 'o': Option<u32>
1745 [152; 153) 'o': Option<u32>
1746 [152; 162) 'o.as_ref()': Option<&u32>
1752 fn infer_generic_chain() {
1763 fn id<T>(t: T) -> T { t }
1768 let a = A { x: id(y) };
1775 [53; 57) 'self': A<T2>
1776 [65; 87) '{ ... }': T2
1777 [75; 79) 'self': A<T2>
1778 [75; 81) 'self.x': T2
1780 [110; 115) '{ t }': T
1782 [135; 261) '{ ....x() }': i128
1783 [146; 147) 'x': i128
1784 [150; 151) '1': i128
1785 [162; 163) 'y': i128
1786 [166; 168) 'id': fn id<i128>(T) -> T
1787 [166; 171) 'id(x)': i128
1788 [169; 170) 'x': i128
1789 [182; 183) 'a': A<i128>
1790 [186; 200) 'A { x: id(y) }': A<i128>
1791 [193; 195) 'id': fn id<i128>(T) -> T
1792 [193; 198) 'id(y)': i128
1793 [196; 197) 'y': i128
1794 [211; 212) 'z': i128
1795 [215; 217) 'id': fn id<i128>(T) -> T
1796 [215; 222) 'id(a.x)': i128
1797 [218; 219) 'a': A<i128>
1798 [218; 221) 'a.x': i128
1799 [233; 234) 'b': A<i128>
1800 [237; 247) 'A { x: z }': A<i128>
1801 [244; 245) 'z': i128
1802 [254; 255) 'b': A<i128>
1803 [254; 259) 'b.x()': i128
1809 fn infer_associated_const() {
1830 impl Trait for TraitTest {
1835 let x = Struct::FOO;
1837 let z = TraitTest::ID;
1844 [229; 307) '{ ...:ID; }': ()
1846 [243; 254) 'Struct::FOO': u32
1848 [268; 277) 'Enum::BAR': u32
1850 [291; 304) 'TraitTest::ID': u32
1856 fn infer_associated_method_struct() {
1872 [49; 75) '{ ... }': A
1873 [59; 69) 'A { x: 0 }': A
1875 [88; 122) '{ ...a.x; }': ()
1877 [102; 108) 'A::new': fn new() -> A
1878 [102; 110) 'A::new()': A
1880 [116; 119) 'a.x': u32
1886 fn infer_associated_method_enum() {
1907 [47; 67) '{ ... }': A
1909 [88; 108) '{ ... }': A
1911 [121; 178) '{ ... c; }': ()
1913 [135; 139) 'A::b': fn b() -> A
1914 [135; 141) 'A::b()': A
1917 [162; 166) 'A::c': fn c() -> A
1918 [162; 168) 'A::c()': A
1925 fn infer_associated_method_with_modules() {
1930 impl A { pub fn thing() -> A { A {} }}
1935 impl B { pub fn thing() -> u32 { 99 }}
1939 impl C { pub fn thing() -> C { C {} }}
1945 let x = a::A::thing();
1946 let y = b::B::thing();
1947 let z = c::C::thing();
1951 [56; 64) '{ A {} }': A
1953 [126; 132) '{ 99 }': u32
1954 [128; 130) '99': u32
1955 [202; 210) '{ C {} }': C
1956 [204; 208) 'C {}': C
1957 [241; 325) '{ ...g(); }': ()
1959 [255; 266) 'a::A::thing': fn thing() -> A
1960 [255; 268) 'a::A::thing()': A
1962 [282; 293) 'b::B::thing': fn thing() -> u32
1963 [282; 295) 'b::B::thing()': u32
1965 [309; 320) 'c::C::thing': fn thing() -> C
1966 [309; 322) 'c::C::thing()': C
1972 fn infer_associated_method_generics() {
1980 pub fn make(val: T) -> Gen<T> {
1986 let a = Gen::make(0u32);
1991 [82; 109) '{ ... }': Gen<T>
1992 [92; 103) 'Gen { val }': Gen<T>
1994 [123; 155) '{ ...32); }': ()
1995 [133; 134) 'a': Gen<u32>
1996 [137; 146) 'Gen::make': fn make<u32>(T) -> Gen<T>
1997 [137; 152) 'Gen::make(0u32)': Gen<u32>
1998 [147; 151) '0u32': u32
2004 fn infer_associated_method_generics_with_default_param() {
2012 pub fn make() -> Gen<T> {
2018 let a = Gen::make();
2022 [80; 104) '{ ... }': Gen<T>
2023 [90; 98) 'loop { }': !
2025 [118; 146) '{ ...e(); }': ()
2026 [128; 129) 'a': Gen<u32>
2027 [132; 141) 'Gen::make': fn make<u32>() -> Gen<T>
2028 [132; 143) 'Gen::make()': Gen<u32>
2034 fn infer_associated_method_generics_with_default_tuple_param() {
2043 pub fn make() -> Gen<T> {
2049 let a = Gen::make();
2054 assert_eq!(t, "()");
2058 fn infer_associated_method_generics_without_args() {
2066 pub fn make() -> Gen<T> {
2072 let a = Gen::<u32>::make();
2076 [76; 100) '{ ... }': Gen<T>
2077 [86; 94) 'loop { }': !
2079 [114; 149) '{ ...e(); }': ()
2080 [124; 125) 'a': Gen<u32>
2081 [128; 144) 'Gen::<...::make': fn make<u32>() -> Gen<T>
2082 [128; 146) 'Gen::<...make()': Gen<u32>
2088 fn infer_associated_method_generics_2_type_params_without_args() {
2096 impl<T> Gen<u32, T> {
2097 pub fn make() -> Gen<u32,T> {
2103 let a = Gen::<u32, u64>::make();
2107 [102; 126) '{ ... }': Gen<u32, T>
2108 [112; 120) 'loop { }': !
2109 [117; 120) '{ }': ()
2110 [140; 180) '{ ...e(); }': ()
2111 [150; 151) 'a': Gen<u32, u64>
2112 [154; 175) 'Gen::<...::make': fn make<u64>() -> Gen<u32, T>
2113 [154; 177) 'Gen::<...make()': Gen<u32, u64>
2119 fn infer_type_alias() {
2122 struct A<X, Y> { x: X, y: Y }
2123 type Foo = A<u32, i128>;
2124 type Bar<T> = A<T, u128>;
2125 type Baz<U, V> = A<V, U>;
2126 fn test(x: Foo, y: Bar<&str>, z: Baz<i8, u8>) {
2136 [116; 117) 'x': A<u32, i128>
2137 [124; 125) 'y': A<&str, u128>
2138 [138; 139) 'z': A<u8, i8>
2139 [154; 211) '{ ...z.y; }': ()
2140 [160; 161) 'x': A<u32, i128>
2141 [160; 163) 'x.x': u32
2142 [169; 170) 'x': A<u32, i128>
2143 [169; 172) 'x.y': i128
2144 [178; 179) 'y': A<&str, u128>
2145 [178; 181) 'y.x': &str
2146 [187; 188) 'y': A<&str, u128>
2147 [187; 190) 'y.y': u128
2148 [196; 197) 'z': A<u8, i8>
2149 [196; 199) 'z.x': u8
2150 [205; 206) 'z': A<u8, i8>
2151 [205; 208) 'z.y': i8
2157 fn recursive_type_alias() {
2166 [59; 60) 'x': {unknown}
2173 fn no_panic_on_field_of_enum() {
2184 [26; 47) '{ ...eld; }': ()
2186 [32; 44) 'x.some_field': {unknown}
2204 [11; 89) '{ ... } }': ()
2205 [17; 21) 'X {}': {unknown}
2206 [27; 87) 'match ... }': ()
2207 [33; 34) 'x': {unknown}
2208 [45; 52) 'A::B {}': {unknown}
2210 [68; 74) 'A::Y()': {unknown}
2226 [11; 41) '{ ...+ y; }': ()
2230 [33; 38) '1 + y': i32
2237 fn recursive_vars() {
2238 covers!(type_var_cycles_resolve_completely);
2239 covers!(type_var_cycles_resolve_as_possible);
2248 [11; 48) '{ ...&y]; }': ()
2249 [21; 22) 'y': &{unknown}
2250 [25; 32) 'unknown': &{unknown}
2251 [38; 45) '[y, &y]': [&&{unknown};_]
2252 [39; 40) 'y': &{unknown}
2253 [42; 44) '&y': &&{unknown}
2254 [43; 44) 'y': &{unknown}
2260 fn recursive_vars_2() {
2261 covers!(type_var_cycles_resolve_completely);
2262 covers!(type_var_cycles_resolve_as_possible);
2272 [11; 80) '{ ...x)]; }': ()
2273 [21; 22) 'x': &&{unknown}
2274 [25; 32) 'unknown': &&{unknown}
2275 [42; 43) 'y': &&{unknown}
2276 [46; 53) 'unknown': &&{unknown}
2277 [59; 77) '[(x, y..., &x)]': [(&&&{unknown}, &&&{unknown});_]
2278 [60; 66) '(x, y)': (&&&{unknown}, &&&{unknown})
2279 [61; 62) 'x': &&{unknown}
2280 [64; 65) 'y': &&{unknown}
2281 [68; 76) '(&y, &x)': (&&&{unknown}, &&&{unknown})
2282 [69; 71) '&y': &&&{unknown}
2283 [70; 71) 'y': &&{unknown}
2284 [73; 75) '&x': &&&{unknown}
2285 [74; 75) 'x': &&{unknown}
2291 fn infer_type_param() {
2294 fn id<T>(x: T) -> T {
2298 fn clone<T>(x: &T) -> T {
2305 let x: bool = clone(z);
2314 [56; 66) '{ *x }': T
2317 [78; 158) '{ ...(1); }': ()
2319 [92; 97) '10u32': u32
2320 [103; 105) 'id': fn id<u32>(T) -> T
2321 [103; 108) 'id(y)': u32
2323 [118; 119) 'x': bool
2324 [128; 133) 'clone': fn clone<bool>(&T) -> T
2325 [128; 136) 'clone(z)': bool
2326 [134; 135) 'z': &bool
2327 [142; 152) 'id::<i128>': fn id<i128>(T) -> T
2328 [142; 155) 'id::<i128>(1)': i128
2329 [153; 154) '1': i128
2335 fn infer_std_crash_1() {
2336 // caused stack overflow, taken from std
2345 match something_unknown {
2346 Maybe::Real(ref mut something) => (),
2351 [54; 139) '{ ... } }': ()
2352 [60; 137) 'match ... }': ()
2353 [66; 83) 'someth...nknown': Maybe<{unknown}>
2354 [94; 124) 'Maybe:...thing)': Maybe<{unknown}>
2355 [106; 123) 'ref mu...ething': &mut {unknown}
2362 fn infer_std_crash_2() {
2363 covers!(type_var_resolves_to_int_var);
2364 // caused "equating two type variables, ...", taken from std
2367 fn test_line_buffer() {
2368 &[0, b'\n', 1, b'\n'];
2372 [23; 53) '{ ...n']; }': ()
2373 [29; 50) '&[0, b...b'\n']': &[u8;_]
2374 [30; 50) '[0, b'...b'\n']': [u8;_]
2376 [34; 39) 'b'\n'': u8
2378 [44; 49) 'b'\n'': u8
2384 fn infer_std_crash_3() {
2390 SizeSkeleton::Pointer { non_zero: true, tail } => {}
2395 [18; 108) '{ ... } }': ()
2396 [24; 106) 'match ... }': ()
2397 [30; 37) 'nope!()': {unknown}
2398 [48; 94) 'SizeSk...tail }': {unknown}
2399 [82; 86) 'true': {unknown}
2400 [88; 92) 'tail': {unknown}
2407 fn infer_std_crash_4() {
2411 pub fn primitive_type() {
2413 BorrowedRef { type_: Primitive(p), ..} => {},
2418 [25; 106) '{ ... } }': ()
2419 [31; 104) 'match ... }': ()
2420 [37; 42) '*self': {unknown}
2421 [38; 42) 'self': {unknown}
2422 [53; 91) 'Borrow...), ..}': {unknown}
2423 [74; 86) 'Primitive(p)': {unknown}
2424 [84; 85) 'p': {unknown}
2431 fn infer_std_crash_5() {
2435 fn extra_compiler_flags() {
2436 for content in doesnt_matter {
2437 let name = if doesnt_matter {
2443 let content = if ICE_REPORT_COMPILER_FLAGS_STRIP_VALUE.contains(&name) {
2452 [27; 323) '{ ... } }': ()
2453 [33; 321) 'for co... }': ()
2454 [37; 44) 'content': &{unknown}
2455 [48; 61) 'doesnt_matter': {unknown}
2456 [62; 321) '{ ... }': ()
2457 [76; 80) 'name': &&{unknown}
2458 [83; 167) 'if doe... }': &&{unknown}
2459 [86; 99) 'doesnt_matter': bool
2460 [100; 129) '{ ... }': &&{unknown}
2461 [114; 119) 'first': &&{unknown}
2462 [135; 167) '{ ... }': &&{unknown}
2463 [149; 157) '&content': &&{unknown}
2464 [150; 157) 'content': &{unknown}
2465 [182; 189) 'content': &{unknown}
2466 [192; 314) 'if ICE... }': &{unknown}
2467 [195; 232) 'ICE_RE..._VALUE': {unknown}
2468 [195; 248) 'ICE_RE...&name)': bool
2469 [242; 247) '&name': &&&{unknown}
2470 [243; 247) 'name': &&{unknown}
2471 [249; 277) '{ ... }': &&{unknown}
2472 [263; 267) 'name': &&{unknown}
2473 [283; 314) '{ ... }': &{unknown}
2474 [297; 304) 'content': &{unknown}
2480 fn infer_nested_generics_crash() {
2481 // another crash found typechecking rustc
2484 struct Canonical<V> {
2487 struct QueryResponse<V> {
2490 fn test<R>(query_response: Canonical<QueryResponse<R>>) {
2491 &query_response.value;
2495 [92; 106) 'query_response': Canonical<QueryResponse<R>>
2496 [137; 167) '{ ...lue; }': ()
2497 [143; 164) '&query....value': &QueryResponse<R>
2498 [144; 158) 'query_response': Canonical<QueryResponse<R>>
2499 [144; 164) 'query_....value': QueryResponse<R>
2506 assert_snapshot!(infer(r#"
2507 struct HashSet<T, H>;
2509 type FxHashSet<T> = HashSet<T, FxHasher>;
2511 impl<T, H> HashSet<T, H> {
2512 fn default() -> HashSet<T, H> {}
2515 pub fn main_loop() {
2516 FxHashSet::default();
2521 [169; 198) '{ ...t(); }': ()
2522 [175; 193) 'FxHash...efault': fn default<{unknown}, FxHasher>() -> HashSet<T, H>
2523 [175; 195) 'FxHash...ault()': HashSet<{unknown}, FxHasher>
2529 fn cross_crate_associated_method_call() {
2530 let (db, pos) = TestDB::with_position(
2532 //- /main.rs crate:main deps:other_crate
2534 let x = other_crate::foo::S::thing();
2538 //- /lib.rs crate:other_crate
2542 fn thing() -> i128 {}
2547 assert_eq!("i128", type_at_pos(&db, pos));
2555 impl Foo { const ASSOC_CONST: u32 = 0; }
2556 const GLOBAL_CONST: u32 = 101;
2558 const LOCAL_CONST: u32 = 99;
2559 let x = LOCAL_CONST;
2560 let z = GLOBAL_CONST;
2561 let id = Foo::ASSOC_CONST;
2567 [95; 213) '{ ...NST; }': ()
2568 [138; 139) 'x': {unknown}
2569 [142; 153) 'LOCAL_CONST': {unknown}
2571 [167; 179) 'GLOBAL_CONST': u32
2572 [189; 191) 'id': u32
2573 [194; 210) 'Foo::A..._CONST': u32
2582 static GLOBAL_STATIC: u32 = 101;
2583 static mut GLOBAL_STATIC_MUT: u32 = 101;
2585 static LOCAL_STATIC: u32 = 99;
2586 static mut LOCAL_STATIC_MUT: u32 = 99;
2587 let x = LOCAL_STATIC;
2588 let y = LOCAL_STATIC_MUT;
2589 let z = GLOBAL_STATIC;
2590 let w = GLOBAL_STATIC_MUT;
2596 [85; 280) '{ ...MUT; }': ()
2597 [173; 174) 'x': {unknown}
2598 [177; 189) 'LOCAL_STATIC': {unknown}
2599 [199; 200) 'y': {unknown}
2600 [203; 219) 'LOCAL_...IC_MUT': {unknown}
2602 [233; 246) 'GLOBAL_STATIC': u32
2604 [260; 277) 'GLOBAL...IC_MUT': u32
2610 fn infer_trait_method_simple() {
2611 // the trait implementation is intentionally incomplete -- it shouldn't matter
2615 fn method(&self) -> u32;
2618 impl Trait1 for S1 {}
2620 fn method(&self) -> i128;
2623 impl Trait2 for S2 {}
2625 S1.method(); // -> u32
2626 S2.method(); // -> i128
2630 [31; 35) 'self': &Self
2631 [110; 114) 'self': &Self
2632 [170; 228) '{ ...i128 }': ()
2634 [176; 187) 'S1.method()': u32
2636 [203; 214) 'S2.method()': i128
2642 fn infer_trait_method_scoped() {
2643 // the trait implementation is intentionally incomplete -- it shouldn't matter
2649 fn method(&self) -> u32;
2651 impl Trait1 for super::S {}
2655 fn method(&self) -> i128;
2657 impl Trait2 for super::S {}
2662 use super::foo::Trait1;
2664 S.method(); // -> u32
2670 use super::bar::Trait2;
2672 S.method(); // -> i128
2677 [63; 67) 'self': &Self
2678 [169; 173) 'self': &Self
2679 [300; 337) '{ ... }': ()
2681 [310; 320) 'S.method()': u32
2682 [416; 454) '{ ... }': ()
2684 [426; 436) 'S.method()': i128
2690 fn infer_trait_method_generic_1() {
2691 // the trait implementation is intentionally incomplete -- it shouldn't matter
2695 fn method(&self) -> T;
2698 impl Trait<u32> for S {}
2704 [33; 37) 'self': &Self
2705 [92; 111) '{ ...d(); }': ()
2707 [98; 108) 'S.method()': u32
2713 fn infer_trait_method_generic_more_params() {
2714 // the trait implementation is intentionally incomplete -- it shouldn't matter
2717 trait Trait<T1, T2, T3> {
2718 fn method1(&self) -> (T1, T2, T3);
2719 fn method2(&self) -> (T3, T2, T1);
2722 impl Trait<u8, u16, u32> for S1 {}
2724 impl<T> Trait<i8, i16, T> for S2 {}
2726 S1.method1(); // u8, u16, u32
2727 S1.method2(); // u32, u16, u8
2728 S2.method1(); // i8, i16, {unknown}
2729 S2.method2(); // {unknown}, i16, i8
2733 [43; 47) 'self': &Self
2734 [82; 86) 'self': &Self
2735 [210; 361) '{ ..., i8 }': ()
2737 [216; 228) 'S1.method1()': (u8, u16, u32)
2739 [250; 262) 'S1.method2()': (u32, u16, u8)
2741 [284; 296) 'S2.method1()': (i8, i16, {unknown})
2743 [324; 336) 'S2.method2()': ({unknown}, i16, i8)
2749 fn infer_trait_method_generic_2() {
2750 // the trait implementation is intentionally incomplete -- it shouldn't matter
2754 fn method(&self) -> T;
2757 impl<U> Trait<U> for S<U> {}
2763 [33; 37) 'self': &Self
2764 [102; 127) '{ ...d(); }': ()
2765 [108; 109) 'S': S<u32>(T) -> S<T>
2766 [108; 115) 'S(1u32)': S<u32>
2767 [108; 124) 'S(1u32...thod()': u32
2768 [110; 114) '1u32': u32
2774 fn infer_trait_assoc_method() {
2778 fn default() -> Self;
2781 impl Default for S {}
2783 let s1: S = Default::default();
2784 let s2 = S::default();
2785 let s3 = <S as Default>::default();
2789 [87; 193) '{ ...t(); }': ()
2791 [105; 121) 'Defaul...efault': fn default<S>() -> Self
2792 [105; 123) 'Defaul...ault()': S
2794 [138; 148) 'S::default': fn default<S>() -> Self
2795 [138; 150) 'S::default()': S
2797 [165; 188) '<S as ...efault': fn default<S>() -> Self
2798 [165; 190) '<S as ...ault()': S
2804 fn infer_trait_assoc_method_generics_1() {
2811 impl Trait<u32> for S {}
2813 impl<T> Trait<T> for G<T> {}
2816 let b = G::<u64>::make();
2817 let c: f64 = G::make();
2821 [127; 211) '{ ...e(); }': ()
2823 [141; 148) 'S::make': fn make<S, u32>() -> T
2824 [141; 150) 'S::make()': u32
2826 [164; 178) 'G::<u64>::make': fn make<G<u64>, u64>() -> T
2827 [164; 180) 'G::<u6...make()': u64
2829 [199; 206) 'G::make': fn make<G<f64>, f64>() -> T
2830 [199; 208) 'G::make()': f64
2836 fn infer_trait_assoc_method_generics_2() {
2840 fn make<U>() -> (T, U);
2843 impl Trait<u32> for S {}
2845 impl<T> Trait<T> for G<T> {}
2847 let a = S::make::<i64>();
2848 let b: (_, i64) = S::make();
2849 let c = G::<u32>::make::<i64>();
2850 let d: (u32, _) = G::make::<i64>();
2851 let e: (u32, i64) = G::make();
2855 [135; 313) '{ ...e(); }': ()
2856 [145; 146) 'a': (u32, i64)
2857 [149; 163) 'S::make::<i64>': fn make<S, u32, i64>() -> (T, U)
2858 [149; 165) 'S::mak...i64>()': (u32, i64)
2859 [175; 176) 'b': (u32, i64)
2860 [189; 196) 'S::make': fn make<S, u32, i64>() -> (T, U)
2861 [189; 198) 'S::make()': (u32, i64)
2862 [208; 209) 'c': (u32, i64)
2863 [212; 233) 'G::<u3...:<i64>': fn make<G<u32>, u32, i64>() -> (T, U)
2864 [212; 235) 'G::<u3...i64>()': (u32, i64)
2865 [245; 246) 'd': (u32, i64)
2866 [259; 273) 'G::make::<i64>': fn make<G<u32>, u32, i64>() -> (T, U)
2867 [259; 275) 'G::mak...i64>()': (u32, i64)
2868 [285; 286) 'e': (u32, i64)
2869 [301; 308) 'G::make': fn make<G<u32>, u32, i64>() -> (T, U)
2870 [301; 310) 'G::make()': (u32, i64)
2876 fn infer_trait_assoc_method_generics_3() {
2880 fn make() -> (Self, T);
2883 impl Trait<i64> for S<i32> {}
2889 [101; 127) '{ ...e(); }': ()
2890 [111; 112) 'a': (S<i32>, i64)
2891 [115; 122) 'S::make': fn make<S<i32>, i64>() -> (Self, T)
2892 [115; 124) 'S::make()': (S<i32>, i64)
2898 fn infer_trait_assoc_method_generics_4() {
2902 fn make() -> (Self, T);
2905 impl Trait<i64> for S<u64> {}
2906 impl Trait<i32> for S<u32> {}
2908 let a: (S<u64>, _) = S::make();
2909 let b: (_, i32) = S::make();
2913 [131; 203) '{ ...e(); }': ()
2914 [141; 142) 'a': (S<u64>, i64)
2915 [158; 165) 'S::make': fn make<S<u64>, i64>() -> (Self, T)
2916 [158; 167) 'S::make()': (S<u64>, i64)
2917 [177; 178) 'b': (S<u32>, i32)
2918 [191; 198) 'S::make': fn make<S<u32>, i32>() -> (Self, T)
2919 [191; 200) 'S::make()': (S<u32>, i32)
2925 fn infer_trait_assoc_method_generics_5() {
2929 fn make<U>() -> (Self, T, U);
2932 impl Trait<i64> for S<u64> {}
2934 let a = <S as Trait<i64>>::make::<u8>();
2935 let b: (S<u64>, _, _) = Trait::<i64>::make::<u8>();
2939 [107; 211) '{ ...>(); }': ()
2940 [117; 118) 'a': (S<u64>, i64, u8)
2941 [121; 150) '<S as ...::<u8>': fn make<S<u64>, i64, u8>() -> (Self, T, U)
2942 [121; 152) '<S as ...<u8>()': (S<u64>, i64, u8)
2943 [162; 163) 'b': (S<u64>, i64, u8)
2944 [182; 206) 'Trait:...::<u8>': fn make<S<u64>, i64, u8>() -> (Self, T, U)
2945 [182; 208) 'Trait:...<u8>()': (S<u64>, i64, u8)
2951 fn infer_from_bound_1() {
2956 impl<U> Trait<U> for S<U> {}
2957 fn foo<T: Trait<u32>>(t: T) {}
2966 [105; 144) '{ ...(s); }': ()
2967 [115; 116) 's': S<u32>
2968 [119; 120) 'S': S<u32>(T) -> S<T>
2969 [119; 129) 'S(unknown)': S<u32>
2970 [121; 128) 'unknown': u32
2971 [135; 138) 'foo': fn foo<S<u32>>(T) -> ()
2972 [135; 141) 'foo(s)': ()
2973 [139; 140) 's': S<u32>
2979 fn infer_from_bound_2() {
2984 impl<U> Trait<U> for S<U> {}
2985 fn foo<U, T: Trait<U>>(t: T) -> U {}
2988 let x: u32 = foo(s);
2994 [111; 163) '{ ...(s); }': ()
2995 [121; 122) 's': S<u32>
2996 [125; 126) 'S': S<u32>(T) -> S<T>
2997 [125; 135) 'S(unknown)': S<u32>
2998 [127; 134) 'unknown': u32
3000 [154; 157) 'foo': fn foo<u32, S<u32>>(T) -> U
3001 [154; 160) 'foo(s)': u32
3002 [158; 159) 's': S<u32>
3008 fn infer_call_trait_method_on_generic_param_1() {
3012 fn method(&self) -> u32;
3014 fn test<T: Trait>(t: T) {
3019 [30; 34) 'self': &Self
3021 [70; 89) '{ ...d(); }': ()
3023 [76; 86) 't.method()': u32
3029 fn infer_call_trait_method_on_generic_param_2() {
3033 fn method(&self) -> T;
3035 fn test<U, T: Trait<U>>(t: T) {
3040 [33; 37) 'self': &Self
3042 [77; 96) '{ ...d(); }': ()
3044 [83; 93) 't.method()': [missing name]
3050 fn infer_with_multiple_trait_impls() {
3057 impl Into<u32> for S {}
3058 impl Into<u64> for S {}
3060 let x: u32 = S.into();
3061 let y: u64 = S.into();
3062 let z = Into::<u64>::into(S);
3066 [29; 33) 'self': Self
3067 [111; 202) '{ ...(S); }': ()
3070 [130; 138) 'S.into()': u32
3073 [157; 165) 'S.into()': u64
3075 [179; 196) 'Into::...::into': fn into<S, u64>(Self) -> T
3076 [179; 199) 'Into::...nto(S)': u64
3083 fn infer_project_associated_type() {
3084 // y, z, a don't yet work because of https://github.com/rust-lang/chalk/issues/234
3091 impl Iterable for S { type Item = u32; }
3092 fn test<T: Iterable>() {
3093 let x: <S as Iterable>::Item = 1;
3094 let y: <T as Iterable>::Item = no_matter;
3095 let z: T::Item = no_matter;
3096 let a: <T>::Item = no_matter;
3100 [108; 261) '{ ...ter; }': ()
3103 [156; 157) 'y': {unknown}
3104 [183; 192) 'no_matter': {unknown}
3105 [202; 203) 'z': {unknown}
3106 [215; 224) 'no_matter': {unknown}
3107 [234; 235) 'a': {unknown}
3108 [249; 258) 'no_matter': {unknown}
3114 fn infer_return_associated_type() {
3121 impl Iterable for S { type Item = u32; }
3122 fn foo1<T: Iterable>(t: T) -> T::Item {}
3123 fn foo2<T: Iterable>(t: T) -> <T as Iterable>::Item {}
3124 fn foo3<T: Iterable>(t: T) -> <T>::Item {}
3138 [234; 300) '{ ...(S); }': ()
3140 [248; 252) 'foo1': fn foo1<S>(T) -> <T as Iterable>::Item
3141 [248; 255) 'foo1(S)': u32
3144 [269; 273) 'foo2': fn foo2<S>(T) -> <T as Iterable>::Item
3145 [269; 276) 'foo2(S)': u32
3148 [290; 294) 'foo3': fn foo3<S>(T) -> <T as Iterable>::Item
3149 [290; 297) 'foo3(S)': u32
3156 fn infer_associated_type_bound() {
3162 fn test<T: Iterable<Item=u32>>() {
3163 let y: T::Item = unknown;
3167 [67; 100) '{ ...own; }': ()
3168 [77; 78) 'y': {unknown}
3169 [90; 97) 'unknown': {unknown}
3175 fn infer_const_body() {
3178 const A: u32 = 1 + 1;
3179 static B: u64 = { let x = 1; x };
3183 [16; 21) '1 + 1': u32
3185 [39; 55) '{ let ...1; x }': u64
3194 fn tuple_struct_fields() {
3205 [38; 87) '{ ... a.1 }': u64
3207 [52; 53) 'S': S(i32, u64) -> S
3208 [52; 59) 'S(4, 6)': S
3221 fn tuple_struct_with_fn() {
3224 struct S(fn(u32) -> u64);
3232 [44; 102) '{ ...0(2) }': u64
3234 [58; 59) 'S': S(fn(u32) -> u64) -> S
3235 [58; 68) 'S(|i| 2*i)': S
3236 [60; 67) '|i| 2*i': |i32| -> i32
3243 [82; 85) 'a.0': fn(u32) -> u64
3244 [82; 88) 'a.0(4)': u64
3247 [94; 97) 'a.0': fn(u32) -> u64
3248 [94; 100) 'a.0(2)': u64
3255 fn indexing_arrays() {
3257 infer("fn main() { &mut [9][2]; }"),
3259 [10; 26) '{ &mut...[2]; }': ()
3260 [12; 23) '&mut [9][2]': &mut {unknown}
3261 [17; 20) '[9]': [i32;_]
3262 [17; 23) '[9][2]': {unknown}
3270 fn infer_macros_expanded() {
3273 struct Foo(Vec<i32>);
3276 ($($item:expr),*) => {
3278 Foo(vec![$($item,)*])
3288 ![0; 17) '{Foo(v...,2,])}': Foo
3289 ![1; 4) 'Foo': Foo({unknown}) -> Foo
3290 ![1; 16) 'Foo(vec![1,2,])': Foo
3291 ![5; 15) 'vec![1,2,]': {unknown}
3292 [156; 182) '{ ...,2); }': ()
3299 fn infer_legacy_textual_scoped_macros_expanded() {
3302 struct Foo(Vec<i32>);
3307 ($($item:expr),*) => {
3309 Foo(vec![$($item,)*])
3317 let y = crate::foo!(1,2);
3321 ![0; 17) '{Foo(v...,2,])}': Foo
3322 ![1; 4) 'Foo': Foo({unknown}) -> Foo
3323 ![1; 16) 'Foo(vec![1,2,])': Foo
3324 ![5; 15) 'vec![1,2,]': {unknown}
3325 [195; 251) '{ ...,2); }': ()
3327 [228; 229) 'y': {unknown}
3328 [232; 248) 'crate:...!(1,2)': {unknown}
3334 fn infer_path_qualified_macros_expanded() {
3343 pub use super::foo as bar;
3347 let x = crate::foo!();
3352 ![0; 5) '42i32': i32
3353 ![0; 5) '42i32': i32
3354 [111; 164) '{ ...!(); }': ()
3362 fn infer_type_value_macro_having_same_name() {
3380 let foo = foo::foo!(42i32);
3384 ![0; 5) '42i32': i32
3385 [171; 206) '{ ...32); }': ()
3386 [181; 184) 'foo': i32
3392 fn processes_impls_generated_by_macros() {
3397 ($ident:ident) => (impl Trait for $ident {})
3399 trait Trait { fn foo(self) -> u128 {} }
3402 fn test() { S.foo()<|>; }
3405 assert_eq!(t, "u128");
3409 fn infer_macro_with_dollar_crate_is_correct_in_expr() {
3410 let (db, pos) = TestDB::with_position(
3412 //- /main.rs crate:main deps:foo
3414 let x = (foo::foo!(1), foo::foo!(2));
3418 //- /lib.rs crate:foo
3421 (1) => { $crate::bar!() };
3422 (2) => { 1 + $crate::baz() };
3430 pub fn baz() -> usize { 31usize }
3433 assert_eq!("(i32, usize)", type_at_pos(&db, pos));
3438 fn method_resolution_trait_before_autoref() {
3442 trait Trait { fn foo(self) -> u128; }
3444 impl S { fn foo(&self) -> i8 { 0 } }
3445 impl Trait for S { fn foo(self) -> u128 { 0 } }
3446 fn test() { S.foo()<|>; }
3449 assert_eq!(t, "u128");
3454 fn method_resolution_by_value_before_autoref() {
3458 trait Clone { fn clone(&self) -> Self; }
3461 impl Clone for &S {}
3462 fn test() { (S.clone(), (&S).clone(), (&&S).clone())<|>; }
3465 assert_eq!(t, "(S, S, &S)");
3469 fn method_resolution_trait_before_autoderef() {
3473 trait Trait { fn foo(self) -> u128; }
3475 impl S { fn foo(self) -> i8 { 0 } }
3476 impl Trait for &S { fn foo(self) -> u128 { 0 } }
3477 fn test() { (&S).foo()<|>; }
3480 assert_eq!(t, "u128");
3484 fn method_resolution_impl_before_trait() {
3488 trait Trait { fn foo(self) -> u128; }
3490 impl S { fn foo(self) -> i8 { 0 } }
3491 impl Trait for S { fn foo(self) -> u128 { 0 } }
3492 fn test() { S.foo()<|>; }
3495 assert_eq!(t, "i8");
3499 fn method_resolution_trait_autoderef() {
3503 trait Trait { fn foo(self) -> u128; }
3505 impl Trait for S { fn foo(self) -> u128 { 0 } }
3506 fn test() { (&S).foo()<|>; }
3509 assert_eq!(t, "u128");
3513 fn method_resolution_trait_from_prelude() {
3514 let (db, pos) = TestDB::with_position(
3516 //- /main.rs crate:main deps:other_crate
3524 //- /lib.rs crate:other_crate
3525 #[prelude_import] use foo::*;
3529 fn clone(&self) -> Self;
3534 assert_eq!("S", type_at_pos(&db, pos));
3538 fn method_resolution_where_clause_for_unknown_trait() {
3539 // The blanket impl shouldn't apply because we can't even resolve UnknownTrait
3543 trait Trait { fn foo(self) -> u128; }
3545 impl<T> Trait for T where T: UnknownTrait {}
3546 fn test() { (&S).foo()<|>; }
3549 assert_eq!(t, "{unknown}");
3553 fn method_resolution_where_clause_not_met() {
3554 // The blanket impl shouldn't apply because we can't prove S: Clone
3559 trait Trait { fn foo(self) -> u128; }
3561 impl<T> Trait for T where T: Clone {}
3562 fn test() { (&S).foo()<|>; }
3565 // This is also to make sure that we don't resolve to the foo method just
3566 // because that's the only method named foo we can find, which would make
3567 // the below tests not work
3568 assert_eq!(t, "{unknown}");
3572 fn method_resolution_where_clause_inline_not_met() {
3573 // The blanket impl shouldn't apply because we can't prove S: Clone
3578 trait Trait { fn foo(self) -> u128; }
3580 impl<T: Clone> Trait for T {}
3581 fn test() { (&S).foo()<|>; }
3584 assert_eq!(t, "{unknown}");
3588 fn method_resolution_where_clause_1() {
3593 trait Trait { fn foo(self) -> u128; }
3596 impl<T> Trait for T where T: Clone {}
3597 fn test() { S.foo()<|>; }
3600 assert_eq!(t, "u128");
3604 fn method_resolution_where_clause_2() {
3608 trait Into<T> { fn into(self) -> T; }
3609 trait From<T> { fn from(other: T) -> Self; }
3612 impl From<S2> for S1 {}
3613 impl<T, U> Into<U> for T where U: From<T> {}
3614 fn test() { S2.into()<|>; }
3617 assert_eq!(t, "{unknown}");
3621 fn method_resolution_where_clause_inline() {
3625 trait Into<T> { fn into(self) -> T; }
3626 trait From<T> { fn from(other: T) -> Self; }
3629 impl From<S2> for S1 {}
3630 impl<T, U: From<T>> Into<U> for T {}
3631 fn test() { S2.into()<|>; }
3634 assert_eq!(t, "{unknown}");
3638 fn method_resolution_encountering_fn_type() {
3643 trait FnOnce { fn call(self); }
3644 fn test() { foo.call()<|>; }
3650 fn method_resolution_slow() {
3651 // this can get quite slow if we set the solver size limit too high
3657 struct S1; impl SendX for S1 {}
3658 struct S2; impl SendX for S2 {}
3661 trait Trait { fn method(self); }
3664 impl<A, B> SendX for X1<A, B> where A: SendX, B: SendX {}
3670 impl<B, C> Trait for S<B, C> where C: FnX, B: SendX {}
3672 fn test() { (S {}).method()<|>; }
3675 assert_eq!(t, "()");
3679 fn shadowing_primitive() {
3686 impl i32 { fn foo(&self) -> Foo { Foo } }
3693 assert_eq!(t, "Foo");
3697 fn not_shadowing_primitive_by_module() {
3705 fn foo() -> &'static str { "" }
3711 assert_eq!(t, "&str");
3715 fn not_shadowing_module_by_primitive() {
3723 fn foo() -> &'static str { "" }
3729 assert_eq!(t, "u32");
3740 fn deref(&self) -> &Self::Target;
3744 impl<T> Deref for Arc<T> {
3750 fn foo(&self) -> u128 {}
3753 fn test(s: Arc<S>) {
3758 assert_eq!(t, "(S, u128)");
3762 fn deref_trait_with_inference_var() {
3769 fn deref(&self) -> &Self::Target;
3773 fn new_arc<T>() -> Arc<T> {}
3774 impl<T> Deref for Arc<T> {
3779 fn foo(a: Arc<S>) {}
3792 fn deref_trait_infinite_recursion() {
3799 fn deref(&self) -> &Self::Target;
3813 assert_eq!(t, "{unknown}");
3817 fn deref_trait_with_question_mark_size() {
3824 fn deref(&self) -> &Self::Target;
3828 impl<T> Deref for Arc<T> {
3834 fn foo(&self) -> u128 {}
3837 fn test(s: Arc<S>) {
3842 assert_eq!(t, "(S, u128)");
3846 fn obligation_from_function_clause() {
3853 impl Trait<u32> for S {}
3855 fn foo<T: Trait<U>, U>(t: T) -> U {}
3862 assert_eq!(t, "u32");
3866 fn obligation_from_method_clause() {
3873 impl Trait<isize> for S {}
3877 fn foo<T: Trait<U>, U>(&self, t: T) -> U {}
3885 assert_eq!(t, "isize");
3889 fn obligation_from_self_method_clause() {
3896 impl Trait<i64> for S {}
3899 fn foo<U>(&self) -> U where Self: Trait<U> {}
3907 assert_eq!(t, "i64");
3911 fn obligation_from_impl_clause() {
3918 impl Trait<&str> for S {}
3921 impl<U, T: Trait<U>> O<T> {
3922 fn foo(&self) -> U {}
3930 assert_eq!(t, "&str");
3934 fn generic_param_env_1() {
3939 trait Trait { fn foo(self) -> u128; }
3942 impl<T> Trait for T where T: Clone {}
3943 fn test<T: Clone>(t: T) { t.foo()<|>; }
3946 assert_eq!(t, "u128");
3950 fn generic_param_env_1_not_met() {
3955 trait Trait { fn foo(self) -> u128; }
3958 impl<T> Trait for T where T: Clone {}
3959 fn test<T>(t: T) { t.foo()<|>; }
3962 assert_eq!(t, "{unknown}");
3966 fn generic_param_env_2() {
3970 trait Trait { fn foo(self) -> u128; }
3973 fn test<T: Trait>(t: T) { t.foo()<|>; }
3976 assert_eq!(t, "u128");
3980 fn generic_param_env_2_not_met() {
3984 trait Trait { fn foo(self) -> u128; }
3987 fn test<T>(t: T) { t.foo()<|>; }
3990 assert_eq!(t, "{unknown}");
3994 fn generic_param_env_deref() {
4003 impl<T> Deref for T where T: Trait {
4006 fn test<T: Trait>(t: T) { (*t)<|>; }
4009 assert_eq!(t, "i128");
4013 fn associated_type_placeholder() {
4021 pub struct RefMutL<T>;
4023 impl<T> ApplyL for RefMutL<T> {
4024 type Out = <T as ApplyL>::Out;
4027 fn test<T: ApplyL>() {
4028 let y: <RefMutL<T> as ApplyL>::Out = no_matter;
4033 // inside the generic function, the associated type gets normalized to a placeholder `ApplL::Out<T>` [https://rust-lang.github.io/rustc-guide/traits/associated-types.html#placeholder-associated-types].
4034 // FIXME: fix type parameter names going missing when going through Chalk
4035 assert_eq!(t, "ApplyL::Out<[missing name]>");
4039 fn associated_type_placeholder_2() {
4046 fn foo<T: ApplyL>(t: T) -> <T as ApplyL>::Out;
4048 fn test<T: ApplyL>(t: T) {
4054 // FIXME here Chalk doesn't normalize the type to a placeholder. I think we
4055 // need to add a rule like Normalize(<T as ApplyL>::Out -> ApplyL::Out<T>)
4056 // to the trait env ourselves here; probably Chalk can't do this by itself.
4057 // assert_eq!(t, "ApplyL::Out<[missing name]>");
4058 assert_eq!(t, "{unknown}");
4067 fn foo2(&self) -> i64;
4069 fn bar() -> impl Trait<u64> {}
4071 fn test(x: impl Trait<u64>, y: &impl Trait<u64>) {
4084 [30; 34) 'self': &Self
4085 [55; 59) 'self': &Self
4087 [111; 112) 'x': impl Trait<u64>
4088 [131; 132) 'y': &impl Trait<u64>
4089 [152; 269) '{ ...2(); }': ()
4090 [158; 159) 'x': impl Trait<u64>
4091 [165; 166) 'y': &impl Trait<u64>
4092 [176; 177) 'z': impl Trait<u64>
4093 [180; 183) 'bar': fn bar() -> impl Trait<u64>
4094 [180; 185) 'bar()': impl Trait<u64>
4095 [191; 192) 'x': impl Trait<u64>
4096 [191; 198) 'x.foo()': u64
4097 [204; 205) 'y': &impl Trait<u64>
4098 [204; 211) 'y.foo()': u64
4099 [217; 218) 'z': impl Trait<u64>
4100 [217; 224) 'z.foo()': u64
4101 [230; 231) 'x': impl Trait<u64>
4102 [230; 238) 'x.foo2()': i64
4103 [244; 245) 'y': &impl Trait<u64>
4104 [244; 252) 'y.foo2()': i64
4105 [258; 259) 'z': impl Trait<u64>
4106 [258; 266) 'z.foo2()': i64
4117 fn foo2(&self) -> i64;
4119 fn bar() -> dyn Trait<u64> {}
4121 fn test(x: dyn Trait<u64>, y: &dyn Trait<u64>) {
4134 [30; 34) 'self': &Self
4135 [55; 59) 'self': &Self
4137 [110; 111) 'x': dyn Trait<u64>
4138 [129; 130) 'y': &dyn Trait<u64>
4139 [149; 266) '{ ...2(); }': ()
4140 [155; 156) 'x': dyn Trait<u64>
4141 [162; 163) 'y': &dyn Trait<u64>
4142 [173; 174) 'z': dyn Trait<u64>
4143 [177; 180) 'bar': fn bar() -> dyn Trait<u64>
4144 [177; 182) 'bar()': dyn Trait<u64>
4145 [188; 189) 'x': dyn Trait<u64>
4146 [188; 195) 'x.foo()': u64
4147 [201; 202) 'y': &dyn Trait<u64>
4148 [201; 208) 'y.foo()': u64
4149 [214; 215) 'z': dyn Trait<u64>
4150 [214; 221) 'z.foo()': u64
4151 [227; 228) 'x': dyn Trait<u64>
4152 [227; 235) 'x.foo2()': i64
4153 [241; 242) 'y': &dyn Trait<u64>
4154 [241; 249) 'y.foo2()': i64
4155 [255; 256) 'z': dyn Trait<u64>
4156 [255; 263) 'z.foo2()': i64
4162 fn dyn_trait_bare() {
4166 fn foo(&self) -> u64;
4168 fn bar() -> Trait {}
4170 fn test(x: Trait, y: &Trait) -> u64 {
4180 [27; 31) 'self': &Self
4182 [73; 74) 'x': dyn Trait
4183 [83; 84) 'y': &dyn Trait
4184 [101; 176) '{ ...o(); }': ()
4185 [107; 108) 'x': dyn Trait
4186 [114; 115) 'y': &dyn Trait
4187 [125; 126) 'z': dyn Trait
4188 [129; 132) 'bar': fn bar() -> dyn Trait
4189 [129; 134) 'bar()': dyn Trait
4190 [140; 141) 'x': dyn Trait
4191 [140; 147) 'x.foo()': u64
4192 [153; 154) 'y': &dyn Trait
4193 [153; 160) 'y.foo()': u64
4194 [166; 167) 'z': dyn Trait
4195 [166; 173) 'z.foo()': u64
4206 let a: impl Trait + 'lifetime = foo;
4207 let b: impl 'lifetime = foo;
4208 let b: impl (Trait) = foo;
4209 let b: impl ('lifetime) = foo;
4210 let d: impl ?Sized = foo;
4211 let e: impl Trait + ?Sized = foo;
4215 [26; 237) '{ ...foo; }': ()
4216 [36; 37) 'a': impl Trait + {error}
4217 [64; 67) 'foo': impl Trait + {error}
4218 [77; 78) 'b': impl {error}
4219 [97; 100) 'foo': impl {error}
4220 [110; 111) 'b': impl Trait
4221 [128; 131) 'foo': impl Trait
4222 [141; 142) 'b': impl {error}
4223 [163; 166) 'foo': impl {error}
4224 [176; 177) 'd': impl {error}
4225 [193; 196) 'foo': impl {error}
4226 [206; 207) 'e': impl Trait + {error}
4227 [231; 234) 'foo': impl Trait + {error}
4233 fn assoc_type_bindings() {
4240 fn get<T: Trait>(t: T) -> <T as Trait>::Type {}
4241 fn get2<U, T: Trait<Type = U>>(t: T) -> U {}
4242 fn set<T: Trait<Type = u64>>(t: T) -> T {t}
4245 impl<T> Trait for S<T> { type Type = T; }
4247 fn test<T: Trait<Type = u32>>(x: T, y: impl Trait<Type = i64>) {
4266 [263; 264) 'y': impl Trait<Type = i64>
4267 [290; 398) '{ ...r>); }': ()
4268 [296; 299) 'get': fn get<T>(T) -> <T as Trait>::Type
4269 [296; 302) 'get(x)': {unknown}
4271 [308; 312) 'get2': fn get2<{unknown}, T>(T) -> U
4272 [308; 315) 'get2(x)': {unknown}
4274 [321; 324) 'get': fn get<impl Trait<Type = i64>>(T) -> <T as Trait>::Type
4275 [321; 327) 'get(y)': {unknown}
4276 [325; 326) 'y': impl Trait<Type = i64>
4277 [333; 337) 'get2': fn get2<{unknown}, impl Trait<Type = i64>>(T) -> U
4278 [333; 340) 'get2(y)': {unknown}
4279 [338; 339) 'y': impl Trait<Type = i64>
4280 [346; 349) 'get': fn get<S<u64>>(T) -> <T as Trait>::Type
4281 [346; 357) 'get(set(S))': u64
4282 [350; 353) 'set': fn set<S<u64>>(T) -> T
4283 [350; 356) 'set(S)': S<u64>
4284 [354; 355) 'S': S<u64>
4285 [363; 367) 'get2': fn get2<u64, S<u64>>(T) -> U
4286 [363; 375) 'get2(set(S))': u64
4287 [368; 371) 'set': fn set<S<u64>>(T) -> T
4288 [368; 374) 'set(S)': S<u64>
4289 [372; 373) 'S': S<u64>
4290 [381; 385) 'get2': fn get2<str, S<str>>(T) -> U
4291 [381; 395) 'get2(S::<str>)': str
4292 [386; 394) 'S::<str>': S<str>
4298 fn impl_trait_assoc_binding_projection_bug() {
4299 let (db, pos) = TestDB::with_position(
4301 //- /main.rs crate:main deps:std
4302 pub trait Language {
4305 pub enum RustLanguage {}
4306 impl Language for RustLanguage {
4307 type Kind = SyntaxKind;
4309 struct SyntaxNode<L> {}
4310 fn foo() -> impl Iterator<Item = SyntaxNode<RustLanguage>> {}
4313 fn clone(&self) -> Self;
4316 fn api_walkthrough() {
4322 //- /std.rs crate:std
4323 #[prelude_import] use iter::*;
4325 trait IntoIterator {
4331 impl<T: Iterator> IntoIterator for T {
4332 type Item = <T as Iterator>::Item;
4337 assert_eq!("{unknown}", type_at_pos(&db, pos));
4341 fn projection_eq_within_chalk() {
4342 // std::env::set_var("CHALK_DEBUG", "1");
4351 impl<T, U> Trait2<T> for U where U: Trait1<Type = T> {}
4353 fn test<T: Trait1<Type = u32>>(x: T) {
4358 [62; 66) 'self': Self
4360 [170; 186) '{ ...o(); }': ()
4362 [176; 183) 'x.foo()': {unknown}
4368 fn where_clause_trait_in_scope_for_method_resolution() {
4374 fn foo(&self) -> u32 {}
4378 fn test<T: foo::Trait>(x: T) {
4383 assert_eq!(t, "u32");
4387 fn super_trait_method_resolution() {
4392 fn foo(&self) -> u32 {}
4395 trait Trait1: foo::SuperTrait {}
4396 trait Trait2 where Self: foo::SuperTrait {}
4398 fn test<T: Trait1, U: Trait2>(x: T, y: U) {
4404 [50; 54) 'self': &Self
4408 [194; 223) '{ ...o(); }': ()
4410 [200; 207) 'x.foo()': u32
4412 [213; 220) 'y.foo()': u32
4418 fn super_trait_cycle() {
4419 // This just needs to not crash
4425 fn test<T: A>(x: T) {
4431 [50; 66) '{ ...o(); }': ()
4433 [56; 63) 'x.foo()': {unknown}
4439 fn super_trait_assoc_type_bounds() {
4442 trait SuperTrait { type Type; }
4443 trait Trait where Self: SuperTrait {}
4445 fn get2<U, T: Trait<Type = U>>(t: T) -> U {}
4446 fn set<T: Trait<Type = u64>>(t: T) -> T {t}
4449 impl<T> SuperTrait for S<T> { type Type = T; }
4450 impl<T> Trait for S<T> {}
4462 [259; 280) '{ ...S)); }': ()
4463 [265; 269) 'get2': fn get2<u64, S<u64>>(T) -> U
4464 [265; 277) 'get2(set(S))': u64
4465 [270; 273) 'set': fn set<S<u64>>(T) -> T
4466 [270; 276) 'set(S)': S<u64>
4467 [274; 275) 'S': S<u64>
4476 trait FnOnce<Args> {
4479 fn call_once(self, args: Args) -> <Self as FnOnce<Args>>::Output;
4482 fn test<F: FnOnce(u32, u64) -> u128>(f: F) {
4483 f.call_once((1, 2));
4487 [57; 61) 'self': Self
4488 [63; 67) 'args': Args
4490 [156; 184) '{ ...2)); }': ()
4492 [162; 181) 'f.call...1, 2))': {unknown}
4493 [174; 180) '(1, 2)': (u32, u64)
4505 trait FnOnce<Args> {
4509 enum Option<T> { Some(T), None }
4511 fn map<U, F: FnOnce(T) -> U>(self, f: F) -> Option<U> {}
4515 let x = Option::Some(1u32);
4518 let y: Option<i64> = x.map(|_v| 1);
4522 [148; 152) 'self': Option<T>
4525 [189; 308) '{ ... 1); }': ()
4526 [199; 200) 'x': Option<u32>
4527 [203; 215) 'Option::Some': Some<u32>(T) -> Option<T>
4528 [203; 221) 'Option...(1u32)': Option<u32>
4529 [216; 220) '1u32': u32
4530 [227; 228) 'x': Option<u32>
4531 [227; 243) 'x.map(...v + 1)': Option<u32>
4532 [233; 242) '|v| v + 1': |u32| -> u32
4535 [237; 242) 'v + 1': u32
4537 [249; 250) 'x': Option<u32>
4538 [249; 265) 'x.map(... 1u64)': Option<u64>
4539 [255; 264) '|_v| 1u64': |u32| -> u64
4540 [256; 258) '_v': u32
4541 [260; 264) '1u64': u64
4542 [275; 276) 'y': Option<i64>
4543 [292; 293) 'x': Option<u32>
4544 [292; 305) 'x.map(|_v| 1)': Option<i64>
4545 [298; 304) '|_v| 1': |u32| -> i64
4546 [299; 301) '_v': u32
4556 trait FnOnce<Args> {
4560 fn test<F: FnOnce(u32) -> u64>(f: F) {
4564 let h = |v| 1u128 + v;
4569 [79; 155) '{ ...+ v; }': ()
4571 [85; 89) 'f(1)': {unknown}
4573 [99; 100) 'g': |u64| -> i32
4574 [103; 112) '|v| v + 1': |u64| -> i32
4577 [107; 112) 'v + 1': i32
4579 [118; 119) 'g': |u64| -> i32
4580 [118; 125) 'g(1u64)': i32
4581 [120; 124) '1u64': u64
4582 [135; 136) 'h': |u128| -> u128
4583 [139; 152) '|v| 1u128 + v': |u128| -> u128
4584 [140; 141) 'v': u128
4585 [143; 148) '1u128': u128
4586 [143; 152) '1u128 + v': u128
4587 [151; 152) 'v': u128
4593 fn closure_as_argument_inference_order() {
4597 trait FnOnce<Args> {
4601 fn foo1<T, U, F: FnOnce(T) -> U>(x: T, f: F) -> U {}
4602 fn foo2<T, U, F: FnOnce(T) -> U>(f: F, x: T) -> U {}
4606 fn method(self) -> u64;
4608 fn foo1<T, U, F: FnOnce(T) -> U>(self, x: T, f: F) -> U {}
4609 fn foo2<T, U, F: FnOnce(T) -> U>(self, f: F, x: T) -> U {}
4613 let x1 = foo1(S, |s| s.method());
4614 let x2 = foo2(|s| s.method(), S);
4615 let x3 = S.foo1(S, |s| s.method());
4616 let x4 = S.foo2(|s| s.method(), S);
4626 [202; 206) 'self': S
4627 [254; 258) 'self': S
4631 [317; 321) 'self': S
4635 [356; 515) '{ ... S); }': ()
4636 [366; 368) 'x1': u64
4637 [371; 375) 'foo1': fn foo1<S, u64, |S| -> u64>(T, F) -> U
4638 [371; 394) 'foo1(S...hod())': u64
4640 [379; 393) '|s| s.method()': |S| -> u64
4643 [383; 393) 's.method()': u64
4644 [404; 406) 'x2': u64
4645 [409; 413) 'foo2': fn foo2<S, u64, |S| -> u64>(F, T) -> U
4646 [409; 432) 'foo2(|...(), S)': u64
4647 [414; 428) '|s| s.method()': |S| -> u64
4650 [418; 428) 's.method()': u64
4652 [442; 444) 'x3': u64
4654 [447; 472) 'S.foo1...hod())': u64
4656 [457; 471) '|s| s.method()': |S| -> u64
4659 [461; 471) 's.method()': u64
4660 [482; 484) 'x4': u64
4662 [487; 512) 'S.foo2...(), S)': u64
4663 [494; 508) '|s| s.method()': |S| -> u64
4666 [498; 508) 's.method()': u64
4673 fn unselected_projection_in_trait_env_1() {
4682 fn foo(&self) -> u32;
4685 fn test<T: Trait>() where T::Item: Trait2 {
4686 let x: T::Item = no_matter;
4691 assert_eq!(t, "u32");
4695 fn unselected_projection_in_trait_env_2() {
4704 fn foo(&self) -> u32;
4707 fn test<T, U>() where T::Item: Trait2, T: Trait<U::Item>, U: Trait<()> {
4708 let x: T::Item = no_matter;
4713 assert_eq!(t, "u32");
4717 fn trait_impl_self_ty() {
4727 impl Trait<Self> for S {}
4734 assert_eq!(t, "()");
4738 fn trait_impl_self_ty_cycle() {
4748 impl Trait for S<Self> {}
4755 assert_eq!(t, "{unknown}");
4759 fn unselected_projection_in_trait_env_cycle_1() {
4769 fn test<T: Trait>() where T: Trait2<T::Item> {
4770 let x: T::Item = no_matter<|>;
4774 // this is a legitimate cycle
4775 assert_eq!(t, "{unknown}");
4779 fn unselected_projection_in_trait_env_cycle_2() {
4787 fn test<T, U>() where T: Trait<U::Item>, U: Trait<T::Item> {
4788 let x: T::Item = no_matter<|>;
4792 // this is a legitimate cycle
4793 assert_eq!(t, "{unknown}");
4796 fn type_at_pos(db: &TestDB, pos: FilePosition) -> String {
4797 let file = db.parse(pos.file_id).ok().unwrap();
4798 let expr = algo::find_node_at_offset::<ast::Expr>(file.syntax(), pos.offset).unwrap();
4800 let module = db.module_for_file(pos.file_id);
4801 let crate_def_map = db.crate_def_map(module.krate);
4802 for decl in crate_def_map[module.local_id].scope.declarations() {
4803 if let ModuleDefId::FunctionId(func) = decl {
4804 let (_body, source_map) = db.body_with_source_map(func.into());
4805 if let Some(expr_id) = source_map.node_expr(InFile::new(pos.file_id.into(), &expr)) {
4806 let infer = db.infer(func.into());
4807 let ty = &infer[expr_id];
4808 return ty.display(db).to_string();
4812 panic!("Can't find expression")
4815 fn type_at(content: &str) -> String {
4816 let (db, file_pos) = TestDB::with_position(content);
4817 type_at_pos(&db, file_pos)
4820 fn infer(content: &str) -> String {
4821 let (db, file_id) = TestDB::with_single_file(content);
4823 let mut acc = String::new();
4825 let mut infer_def = |inference_result: Arc<InferenceResult>,
4826 body_source_map: Arc<BodySourceMap>| {
4827 let mut types = Vec::new();
4829 for (pat, ty) in inference_result.type_of_pat.iter() {
4830 let syntax_ptr = match body_source_map.pat_syntax(pat) {
4832 sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()))
4836 types.push((syntax_ptr, ty));
4839 for (expr, ty) in inference_result.type_of_expr.iter() {
4840 let syntax_ptr = match body_source_map.expr_syntax(expr) {
4842 sp.map(|ast| ast.either(|it| it.syntax_node_ptr(), |it| it.syntax_node_ptr()))
4846 types.push((syntax_ptr, ty));
4849 // sort ranges for consistency
4850 types.sort_by_key(|(src_ptr, _)| {
4851 (src_ptr.value.range().start(), src_ptr.value.range().end())
4853 for (src_ptr, ty) in &types {
4854 let node = src_ptr.value.to_node(&src_ptr.file_syntax(&db));
4856 let (range, text) = if let Some(self_param) = ast::SelfParam::cast(node.clone()) {
4857 (self_param.self_kw_token().text_range(), "self".to_string())
4859 (src_ptr.value.range(), node.text().to_string().replace("\n", " "))
4861 let macro_prefix = if src_ptr.file_id != file_id.into() { "!" } else { "" };
4867 ellipsize(text, 15),
4874 let module = db.module_for_file(file_id);
4875 let crate_def_map = db.crate_def_map(module.krate);
4877 let mut defs: Vec<DefWithBodyId> = Vec::new();
4878 visit_module(&db, &crate_def_map, module.local_id, &mut |it| defs.push(it));
4879 defs.sort_by_key(|def| match def {
4880 DefWithBodyId::FunctionId(it) => {
4881 it.lookup(&db).ast_id.to_node(&db).syntax().text_range().start()
4883 DefWithBodyId::ConstId(it) => {
4884 it.lookup(&db).ast_id.to_node(&db).syntax().text_range().start()
4886 DefWithBodyId::StaticId(it) => {
4887 it.lookup(&db).ast_id.to_node(&db).syntax().text_range().start()
4891 let (_body, source_map) = db.body_with_source_map(def);
4892 let infer = db.infer(def);
4893 infer_def(infer, source_map);
4896 acc.truncate(acc.trim_end().len());
4902 crate_def_map: &CrateDefMap,
4903 module_id: LocalModuleId,
4904 cb: &mut dyn FnMut(DefWithBodyId),
4906 for decl in crate_def_map[module_id].scope.declarations() {
4908 ModuleDefId::FunctionId(it) => cb(it.into()),
4909 ModuleDefId::ConstId(it) => cb(it.into()),
4910 ModuleDefId::StaticId(it) => cb(it.into()),
4911 ModuleDefId::TraitId(it) => {
4912 let trait_data = db.trait_data(it);
4913 for &(_, item) in trait_data.items.iter() {
4915 AssocItemId::FunctionId(it) => cb(it.into()),
4916 AssocItemId::ConstId(it) => cb(it.into()),
4917 AssocItemId::TypeAliasId(_) => (),
4921 ModuleDefId::ModuleId(it) => visit_module(db, crate_def_map, it.local_id, cb),
4925 for &impl_id in crate_def_map[module_id].impls.iter() {
4926 let impl_data = db.impl_data(impl_id);
4927 for &item in impl_data.items.iter() {
4929 AssocItemId::FunctionId(it) => cb(it.into()),
4930 AssocItemId::ConstId(it) => cb(it.into()),
4931 AssocItemId::TypeAliasId(_) => (),
4937 fn ellipsize(mut text: String, max_len: usize) -> String {
4938 if text.len() <= max_len {
4941 let ellipsis = "...";
4942 let e_len = ellipsis.len();
4943 let mut prefix_len = (max_len - e_len) / 2;
4944 while !text.is_char_boundary(prefix_len) {
4947 let mut suffix_len = max_len - e_len - prefix_len;
4948 while !text.is_char_boundary(text.len() - suffix_len) {
4951 text.replace_range(prefix_len..text.len() - suffix_len, ellipsis);
4956 fn typing_whitespace_inside_a_function_should_not_invalidate_types() {
4957 let (mut db, pos) = TestDB::with_position(
4966 let events = db.log_executed(|| {
4967 let module = db.module_for_file(pos.file_id);
4968 let crate_def_map = db.crate_def_map(module.krate);
4969 visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
4973 assert!(format!("{:?}", events).contains("infer"))
4985 db.query_mut(ra_db::FileTextQuery).set(pos.file_id, Arc::new(new_text));
4988 let events = db.log_executed(|| {
4989 let module = db.module_for_file(pos.file_id);
4990 let crate_def_map = db.crate_def_map(module.krate);
4991 visit_module(&db, &crate_def_map, module.local_id, &mut |def| {
4995 assert!(!format!("{:?}", events).contains("infer"), "{:#?}", events)
5000 fn no_such_field_diagnostics() {
5001 let diagnostics = TestDB::with_files(
5004 struct S { foo: i32, bar: () }
5017 assert_snapshot!(diagnostics, @r###"
5018 "baz: 62": no such field
5019 "{\n foo: 92,\n baz: 62,\n }": Missing structure fields:
5026 fn infer_builtin_macros_line() {
5029 #[rustc_builtin_macro]
5030 macro_rules! line {() => {}}
5038 [64; 88) '{ ...!(); }': ()
5045 fn infer_builtin_macros_file() {
5048 #[rustc_builtin_macro]
5049 macro_rules! file {() => {}}
5057 [64; 88) '{ ...!(); }': ()
5064 fn infer_builtin_macros_column() {
5067 #[rustc_builtin_macro]
5068 macro_rules! column {() => {}}
5076 [66; 92) '{ ...!(); }': ()