aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-07-12 10:45:37 +0100
committerGitHub <[email protected]>2020-07-12 10:45:37 +0100
commit28f0171dbdf51ab02bf3e8090d80dcdf0b733662 (patch)
tree72dd0e6a6d4cb466c654986a903b974392d3375c /crates
parent1a9d7724dac54510c7b702d11a126364604af61c (diff)
parent9d114b9707fd3d6aaa3224cd1794e8e49e433f36 (diff)
Merge #5326
5326: infer: Add type inference support for Union types r=flodiebold a=otavio This adds the type inference to Union types and add a small test case for it, ensuring it keeps working in future. Fixes: #5277 Signed-off-by: Otavio Salvador <[email protected]> ---- # Co-authored-by: Otavio Salvador <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_hir_ty/src/infer/expr.rs11
-rw-r--r--crates/ra_hir_ty/src/tests/simple.rs23
2 files changed, 32 insertions, 2 deletions
diff --git a/crates/ra_hir_ty/src/infer/expr.rs b/crates/ra_hir_ty/src/infer/expr.rs
index 06baac2a9..bd9a387f5 100644
--- a/crates/ra_hir_ty/src/infer/expr.rs
+++ b/crates/ra_hir_ty/src/infer/expr.rs
@@ -405,8 +405,15 @@ impl<'a> InferenceContext<'a> {
405 .subst(&a_ty.parameters) 405 .subst(&a_ty.parameters)
406 }) 406 })
407 } 407 }
408 // FIXME: 408 TypeCtor::Adt(AdtId::UnionId(u)) => {
409 TypeCtor::Adt(AdtId::UnionId(_)) => None, 409 self.db.union_data(u).variant_data.field(name).map(|local_id| {
410 let field = FieldId { parent: u.into(), local_id };
411 self.write_field_resolution(tgt_expr, field);
412 self.db.field_types(u.into())[field.local_id]
413 .clone()
414 .subst(&a_ty.parameters)
415 })
416 }
410 _ => None, 417 _ => None,
411 }, 418 },
412 _ => None, 419 _ => None,
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs
index de63f4cce..6d3e264af 100644
--- a/crates/ra_hir_ty/src/tests/simple.rs
+++ b/crates/ra_hir_ty/src/tests/simple.rs
@@ -325,6 +325,29 @@ fn test() {
325} 325}
326 326
327#[test] 327#[test]
328fn infer_union() {
329 assert_snapshot!(
330 infer(r#"
331union MyUnion {
332 foo: u32,
333 bar: f32,
334}
335
336unsafe fn baz(u: MyUnion) {
337 let inner = u.foo;
338}
339"#),
340 @r###"
341 61..62 'u': MyUnion
342 73..99 '{ ...foo; }': ()
343 83..88 'inner': u32
344 91..92 'u': MyUnion
345 91..96 'u.foo': u32
346 "###
347 );
348}
349
350#[test]
328fn infer_refs() { 351fn infer_refs() {
329 assert_snapshot!( 352 assert_snapshot!(
330 infer(r#" 353 infer(r#"