aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir_ty/src/tests/simple.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-02-09 11:35:08 +0000
committerGitHub <[email protected]>2020-02-09 11:35:08 +0000
commit01836a0f35fa163025c64cabe1d0c34bb4f69c92 (patch)
treeaa1a3cf97173b2885f8b6d23002c73196f9a0b61 /crates/ra_hir_ty/src/tests/simple.rs
parent961a69b88f923d4477ca4f746a793217a0cc8576 (diff)
parenteefe02ce6e1750b771cf99125429358e87485745 (diff)
Merge #3050
3050: Refactor type parameters, implement argument position impl trait r=matklad a=flodiebold I wanted to implement APIT by lowering to type parameters because we need to do that anyway for correctness and don't need Chalk support for it; this grew into some more wide-ranging refactoring of how type parameters are handled :sweat_smile: - use Ty::Bound instead of Ty::Param to represent polymorphism, and explicitly count binders. This gets us closer to Chalk's way of doing things, and means that we now only use Param as a placeholder for an unknown type, e.g. within a generic function. I.e. we're never using Param in a situation where we want to substitute it, and the method to do that is gone; `subst` now always works on bound variables. (This changes how the types of generic functions print; previously, you'd get something like `fn identity<i32>(T) -> T`, but now we display the substituted signature `fn identity<i32>(i32) -> i32`, which I think makes more sense.) - once we do this, it's more natural to represent `Param` by a globally unique ID; the use of indices was mostly to make substituting easier. This also means we fix the bug where `Param` loses its name when going through Chalk. - I would actually like to rename `Param` to `Placeholder` to better reflect its use and get closer to Chalk, but I'll leave that to a follow-up. - introduce a context for type lowering, to allow lowering `impl Trait` to different things depending on where we are. And since we have that, we can also lower type parameters directly to variables instead of placeholders. Also, we'll be able to use this later to collect diagnostics. - implement argument position impl trait by lowering it to type parameters. I've realized that this is necessary to correctly implement it; e.g. consider `fn foo(impl Display) -> impl Something`. It's observable that the return type of e.g. `foo(1u32)` unifies with itself, but doesn't unify with e.g. `foo(1i32)`; so the return type needs to be parameterized by the argument type. This fixes a few bugs as well: - type parameters 'losing' their name when they go through Chalk, as mentioned above (i.e. getting `[missing name]` somewhere) - impl trait not being considered as implementing the super traits (very noticeable for the `db` in RA) - the fact that argument impl trait was only turned into variables when the function got called caused type mismatches when the function was used as a value (fixes a few type mismatches in RA) The one thing I'm not so happy with here is how we're lowering `impl Trait` types to variables; since `TypeRef`s don't have an identity currently, we just count how many of them we have seen while going through the function signature. That's quite fragile though, since we have to do it while desugaring generics and while lowering the type signature, and in the exact same order in both cases. We could consider either giving only `TypeRef::ImplTrait` a local id, or maybe just giving all `TypeRef`s an identity after all (we talked about this before)... Follow-up tasks: - handle return position impl trait; we basically need to create a variable and some trait obligations for that variable - rename `Param` to `Placeholder` Co-authored-by: Florian Diebold <[email protected]> Co-authored-by: Florian Diebold <[email protected]>
Diffstat (limited to 'crates/ra_hir_ty/src/tests/simple.rs')
-rw-r--r--crates/ra_hir_ty/src/tests/simple.rs42
1 files changed, 21 insertions, 21 deletions
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs
index fdab9c187..3803f5938 100644
--- a/crates/ra_hir_ty/src/tests/simple.rs
+++ b/crates/ra_hir_ty/src/tests/simple.rs
@@ -754,15 +754,15 @@ fn test() {
754 [289; 295) 'self.0': T 754 [289; 295) 'self.0': T
755 [315; 353) '{ ...))); }': () 755 [315; 353) '{ ...))); }': ()
756 [325; 326) 't': &i32 756 [325; 326) 't': &i32
757 [329; 335) 'A::foo': fn foo<i32>(&A<T>) -> &T 757 [329; 335) 'A::foo': fn foo<i32>(&A<i32>) -> &i32
758 [329; 350) 'A::foo...42))))': &i32 758 [329; 350) 'A::foo...42))))': &i32
759 [336; 349) '&&B(B(A(42)))': &&B<B<A<i32>>> 759 [336; 349) '&&B(B(A(42)))': &&B<B<A<i32>>>
760 [337; 349) '&B(B(A(42)))': &B<B<A<i32>>> 760 [337; 349) '&B(B(A(42)))': &B<B<A<i32>>>
761 [338; 339) 'B': B<B<A<i32>>>(T) -> B<T> 761 [338; 339) 'B': B<B<A<i32>>>(B<A<i32>>) -> B<B<A<i32>>>
762 [338; 349) 'B(B(A(42)))': B<B<A<i32>>> 762 [338; 349) 'B(B(A(42)))': B<B<A<i32>>>
763 [340; 341) 'B': B<A<i32>>(T) -> B<T> 763 [340; 341) 'B': B<A<i32>>(A<i32>) -> B<A<i32>>
764 [340; 348) 'B(A(42))': B<A<i32>> 764 [340; 348) 'B(A(42))': B<A<i32>>
765 [342; 343) 'A': A<i32>(T) -> A<T> 765 [342; 343) 'A': A<i32>(i32) -> A<i32>
766 [342; 347) 'A(42)': A<i32> 766 [342; 347) 'A(42)': A<i32>
767 [344; 346) '42': i32 767 [344; 346) '42': i32
768 "### 768 "###
@@ -817,16 +817,16 @@ fn test(a: A<i32>) {
817 [326; 327) 'a': A<i32> 817 [326; 327) 'a': A<i32>
818 [337; 383) '{ ...))); }': () 818 [337; 383) '{ ...))); }': ()
819 [347; 348) 't': &i32 819 [347; 348) 't': &i32
820 [351; 352) 'A': A<i32>(*mut T) -> A<T> 820 [351; 352) 'A': A<i32>(*mut i32) -> A<i32>
821 [351; 365) 'A(0 as *mut _)': A<i32> 821 [351; 365) 'A(0 as *mut _)': A<i32>
822 [351; 380) 'A(0 as...B(a)))': &i32 822 [351; 380) 'A(0 as...B(a)))': &i32
823 [353; 354) '0': i32 823 [353; 354) '0': i32
824 [353; 364) '0 as *mut _': *mut i32 824 [353; 364) '0 as *mut _': *mut i32
825 [370; 379) '&&B(B(a))': &&B<B<A<i32>>> 825 [370; 379) '&&B(B(a))': &&B<B<A<i32>>>
826 [371; 379) '&B(B(a))': &B<B<A<i32>>> 826 [371; 379) '&B(B(a))': &B<B<A<i32>>>
827 [372; 373) 'B': B<B<A<i32>>>(T) -> B<T> 827 [372; 373) 'B': B<B<A<i32>>>(B<A<i32>>) -> B<B<A<i32>>>
828 [372; 379) 'B(B(a))': B<B<A<i32>>> 828 [372; 379) 'B(B(a))': B<B<A<i32>>>
829 [374; 375) 'B': B<A<i32>>(T) -> B<T> 829 [374; 375) 'B': B<A<i32>>(A<i32>) -> B<A<i32>>
830 [374; 378) 'B(a)': B<A<i32>> 830 [374; 378) 'B(a)': B<A<i32>>
831 [376; 377) 'a': A<i32> 831 [376; 377) 'a': A<i32>
832 "### 832 "###
@@ -1169,16 +1169,16 @@ fn test() {
1169"#), 1169"#),
1170 @r###" 1170 @r###"
1171 [76; 184) '{ ...one; }': () 1171 [76; 184) '{ ...one; }': ()
1172 [82; 83) 'A': A<i32>(T) -> A<T> 1172 [82; 83) 'A': A<i32>(i32) -> A<i32>
1173 [82; 87) 'A(42)': A<i32> 1173 [82; 87) 'A(42)': A<i32>
1174 [84; 86) '42': i32 1174 [84; 86) '42': i32
1175 [93; 94) 'A': A<u128>(T) -> A<T> 1175 [93; 94) 'A': A<u128>(u128) -> A<u128>
1176 [93; 102) 'A(42u128)': A<u128> 1176 [93; 102) 'A(42u128)': A<u128>
1177 [95; 101) '42u128': u128 1177 [95; 101) '42u128': u128
1178 [108; 112) 'Some': Some<&str>(T) -> Option<T> 1178 [108; 112) 'Some': Some<&str>(&str) -> Option<&str>
1179 [108; 117) 'Some("x")': Option<&str> 1179 [108; 117) 'Some("x")': Option<&str>
1180 [113; 116) '"x"': &str 1180 [113; 116) '"x"': &str
1181 [123; 135) 'Option::Some': Some<&str>(T) -> Option<T> 1181 [123; 135) 'Option::Some': Some<&str>(&str) -> Option<&str>
1182 [123; 140) 'Option...e("x")': Option<&str> 1182 [123; 140) 'Option...e("x")': Option<&str>
1183 [136; 139) '"x"': &str 1183 [136; 139) '"x"': &str
1184 [146; 150) 'None': Option<{unknown}> 1184 [146; 150) 'None': Option<{unknown}>
@@ -1205,14 +1205,14 @@ fn test() {
1205 [21; 26) '{ t }': T 1205 [21; 26) '{ t }': T
1206 [23; 24) 't': T 1206 [23; 24) 't': T
1207 [38; 98) '{ ...(1); }': () 1207 [38; 98) '{ ...(1); }': ()
1208 [44; 46) 'id': fn id<u32>(T) -> T 1208 [44; 46) 'id': fn id<u32>(u32) -> u32
1209 [44; 52) 'id(1u32)': u32 1209 [44; 52) 'id(1u32)': u32
1210 [47; 51) '1u32': u32 1210 [47; 51) '1u32': u32
1211 [58; 68) 'id::<i128>': fn id<i128>(T) -> T 1211 [58; 68) 'id::<i128>': fn id<i128>(i128) -> i128
1212 [58; 71) 'id::<i128>(1)': i128 1212 [58; 71) 'id::<i128>(1)': i128
1213 [69; 70) '1': i128 1213 [69; 70) '1': i128
1214 [81; 82) 'x': u64 1214 [81; 82) 'x': u64
1215 [90; 92) 'id': fn id<u64>(T) -> T 1215 [90; 92) 'id': fn id<u64>(u64) -> u64
1216 [90; 95) 'id(1)': u64 1216 [90; 95) 'id(1)': u64
1217 [93; 94) '1': u64 1217 [93; 94) '1': u64
1218 "### 1218 "###
@@ -1220,7 +1220,7 @@ fn test() {
1220} 1220}
1221 1221
1222#[test] 1222#[test]
1223fn infer_impl_generics() { 1223fn infer_impl_generics_basic() {
1224 assert_snapshot!( 1224 assert_snapshot!(
1225 infer(r#" 1225 infer(r#"
1226struct A<T1, T2> { 1226struct A<T1, T2> {
@@ -1349,16 +1349,16 @@ fn test() -> i128 {
1349 [146; 147) 'x': i128 1349 [146; 147) 'x': i128
1350 [150; 151) '1': i128 1350 [150; 151) '1': i128
1351 [162; 163) 'y': i128 1351 [162; 163) 'y': i128
1352 [166; 168) 'id': fn id<i128>(T) -> T 1352 [166; 168) 'id': fn id<i128>(i128) -> i128
1353 [166; 171) 'id(x)': i128 1353 [166; 171) 'id(x)': i128
1354 [169; 170) 'x': i128 1354 [169; 170) 'x': i128
1355 [182; 183) 'a': A<i128> 1355 [182; 183) 'a': A<i128>
1356 [186; 200) 'A { x: id(y) }': A<i128> 1356 [186; 200) 'A { x: id(y) }': A<i128>
1357 [193; 195) 'id': fn id<i128>(T) -> T 1357 [193; 195) 'id': fn id<i128>(i128) -> i128
1358 [193; 198) 'id(y)': i128 1358 [193; 198) 'id(y)': i128
1359 [196; 197) 'y': i128 1359 [196; 197) 'y': i128
1360 [211; 212) 'z': i128 1360 [211; 212) 'z': i128
1361 [215; 217) 'id': fn id<i128>(T) -> T 1361 [215; 217) 'id': fn id<i128>(i128) -> i128
1362 [215; 222) 'id(a.x)': i128 1362 [215; 222) 'id(a.x)': i128
1363 [218; 219) 'a': A<i128> 1363 [218; 219) 'a': A<i128>
1364 [218; 221) 'a.x': i128 1364 [218; 221) 'a.x': i128
@@ -1502,14 +1502,14 @@ fn test() {
1502 [78; 158) '{ ...(1); }': () 1502 [78; 158) '{ ...(1); }': ()
1503 [88; 89) 'y': u32 1503 [88; 89) 'y': u32
1504 [92; 97) '10u32': u32 1504 [92; 97) '10u32': u32
1505 [103; 105) 'id': fn id<u32>(T) -> T 1505 [103; 105) 'id': fn id<u32>(u32) -> u32
1506 [103; 108) 'id(y)': u32 1506 [103; 108) 'id(y)': u32
1507 [106; 107) 'y': u32 1507 [106; 107) 'y': u32
1508 [118; 119) 'x': bool 1508 [118; 119) 'x': bool
1509 [128; 133) 'clone': fn clone<bool>(&T) -> T 1509 [128; 133) 'clone': fn clone<bool>(&bool) -> bool
1510 [128; 136) 'clone(z)': bool 1510 [128; 136) 'clone(z)': bool
1511 [134; 135) 'z': &bool 1511 [134; 135) 'z': &bool
1512 [142; 152) 'id::<i128>': fn id<i128>(T) -> T 1512 [142; 152) 'id::<i128>': fn id<i128>(i128) -> i128
1513 [142; 155) 'id::<i128>(1)': i128 1513 [142; 155) 'id::<i128>(1)': i128
1514 [153; 154) '1': i128 1514 [153; 154) '1': i128
1515 "### 1515 "###