diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-23 18:23:17 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-05-23 18:23:17 +0100 |
commit | 1dc9adc6e27d603f05f794adda91bca8b6dec8ac (patch) | |
tree | c2ebe02614abf66618ed3975f7108cd03689416a /crates/ra_hir/src/ty | |
parent | ef00b5af1c7a7a7cac685eff661a10252825d84a (diff) | |
parent | 5d54aa678153d0af0edc8b4dd2d74709d10ca66c (diff) |
Merge #1290
1290: Add Union to code_model r=matklad a=matklad
@flodiebold I am conflicted about two possible implementation approaches:
* we can add a separate `struct Union` to code model
* we can add `fn is_union(&self)` to existing `Struct`
This PR goes with the former approach, because it seems like Unions are sufficiently different in semantics to warrant a separate types. Which is in contrast to Syntax Tree, where both structs and unions share the same node kind, because their syntax is the same.
What would be the right thing to do here?
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_hir/src/ty')
-rw-r--r-- | crates/ra_hir/src/ty/infer.rs | 21 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/lower.rs | 10 |
2 files changed, 21 insertions, 10 deletions
diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index a48272981..7d8250292 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs | |||
@@ -27,13 +27,13 @@ use ra_prof::profile; | |||
27 | use test_utils::tested_by; | 27 | use test_utils::tested_by; |
28 | 28 | ||
29 | use crate::{ | 29 | use crate::{ |
30 | Function, StructField, Path, Name, | 30 | Function, StructField, Path, Name, FnSignature, AdtDef, ConstSignature, HirDatabase, |
31 | FnSignature, AdtDef,ConstSignature, | 31 | DefWithBody, ImplItem, |
32 | HirDatabase, | ||
33 | DefWithBody, | ||
34 | ImplItem, | ||
35 | type_ref::{TypeRef, Mutability}, | 32 | type_ref::{TypeRef, Mutability}, |
36 | expr::{Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, FieldPat,Array, self}, | 33 | expr::{ |
34 | Body, Expr, BindingAnnotation, Literal, ExprId, Pat, PatId, UnaryOp, BinaryOp, Statement, | ||
35 | FieldPat, Array, self, | ||
36 | }, | ||
37 | generics::{GenericParams, HasGenericParams}, | 37 | generics::{GenericParams, HasGenericParams}, |
38 | path::{GenericArgs, GenericArg}, | 38 | path::{GenericArgs, GenericArg}, |
39 | ModuleDef, | 39 | ModuleDef, |
@@ -644,7 +644,8 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { | |||
644 | let ty = self.insert_type_vars(ty.apply_substs(substs)); | 644 | let ty = self.insert_type_vars(ty.apply_substs(substs)); |
645 | (ty, Some(var.into())) | 645 | (ty, Some(var.into())) |
646 | } | 646 | } |
647 | TypableDef::TypeAlias(_) | 647 | TypableDef::Union(_) |
648 | | TypableDef::TypeAlias(_) | ||
648 | | TypableDef::Function(_) | 649 | | TypableDef::Function(_) |
649 | | TypableDef::Enum(_) | 650 | | TypableDef::Enum(_) |
650 | | TypableDef::Const(_) | 651 | | TypableDef::Const(_) |
@@ -1407,7 +1408,11 @@ impl Expectation { | |||
1407 | } | 1408 | } |
1408 | 1409 | ||
1409 | mod diagnostics { | 1410 | mod diagnostics { |
1410 | use crate::{expr::ExprId, diagnostics::{DiagnosticSink, NoSuchField}, HirDatabase, Function}; | 1411 | use crate::{ |
1412 | expr::ExprId, | ||
1413 | diagnostics::{DiagnosticSink, NoSuchField}, | ||
1414 | HirDatabase, Function, | ||
1415 | }; | ||
1411 | 1416 | ||
1412 | #[derive(Debug, PartialEq, Eq, Clone)] | 1417 | #[derive(Debug, PartialEq, Eq, Clone)] |
1413 | pub(super) enum InferenceDiagnostic { | 1418 | pub(super) enum InferenceDiagnostic { |
diff --git a/crates/ra_hir/src/ty/lower.rs b/crates/ra_hir/src/ty/lower.rs index a1a2d0f6b..7defa7a9b 100644 --- a/crates/ra_hir/src/ty/lower.rs +++ b/crates/ra_hir/src/ty/lower.rs | |||
@@ -9,7 +9,7 @@ use std::sync::Arc; | |||
9 | use std::iter; | 9 | use std::iter; |
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | Function, Struct, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static, | 12 | Function, Struct, Union, StructField, Enum, EnumVariant, Path, ModuleDef, TypeAlias, Const, Static, |
13 | HirDatabase, | 13 | HirDatabase, |
14 | type_ref::TypeRef, | 14 | type_ref::TypeRef, |
15 | name::KnownName, | 15 | name::KnownName, |
@@ -124,6 +124,7 @@ impl Ty { | |||
124 | let def_generic: Option<GenericDef> = match resolved { | 124 | let def_generic: Option<GenericDef> = match resolved { |
125 | TypableDef::Function(func) => Some(func.into()), | 125 | TypableDef::Function(func) => Some(func.into()), |
126 | TypableDef::Struct(s) => Some(s.into()), | 126 | TypableDef::Struct(s) => Some(s.into()), |
127 | TypableDef::Union(u) => Some(u.into()), | ||
127 | TypableDef::Enum(e) => Some(e.into()), | 128 | TypableDef::Enum(e) => Some(e.into()), |
128 | TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()), | 129 | TypableDef::EnumVariant(var) => Some(var.parent_enum(db).into()), |
129 | TypableDef::TypeAlias(t) => Some(t.into()), | 130 | TypableDef::TypeAlias(t) => Some(t.into()), |
@@ -144,6 +145,7 @@ impl Ty { | |||
144 | let segment = match resolved { | 145 | let segment = match resolved { |
145 | TypableDef::Function(_) | 146 | TypableDef::Function(_) |
146 | | TypableDef::Struct(_) | 147 | | TypableDef::Struct(_) |
148 | | TypableDef::Union(_) | ||
147 | | TypableDef::Enum(_) | 149 | | TypableDef::Enum(_) |
148 | | TypableDef::Const(_) | 150 | | TypableDef::Const(_) |
149 | | TypableDef::Static(_) | 151 | | TypableDef::Static(_) |
@@ -293,12 +295,14 @@ pub(crate) fn type_for_def(db: &impl HirDatabase, def: TypableDef, ns: Namespace | |||
293 | (TypableDef::Struct(s), Namespace::Values) => type_for_struct_constructor(db, s), | 295 | (TypableDef::Struct(s), Namespace::Values) => type_for_struct_constructor(db, s), |
294 | (TypableDef::Enum(e), Namespace::Types) => type_for_adt(db, e), | 296 | (TypableDef::Enum(e), Namespace::Types) => type_for_adt(db, e), |
295 | (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), | 297 | (TypableDef::EnumVariant(v), Namespace::Values) => type_for_enum_variant_constructor(db, v), |
298 | (TypableDef::Union(u), Namespace::Types) => type_for_adt(db, u), | ||
296 | (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), | 299 | (TypableDef::TypeAlias(t), Namespace::Types) => type_for_type_alias(db, t), |
297 | (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), | 300 | (TypableDef::Const(c), Namespace::Values) => type_for_const(db, c), |
298 | (TypableDef::Static(c), Namespace::Values) => type_for_static(db, c), | 301 | (TypableDef::Static(c), Namespace::Values) => type_for_static(db, c), |
299 | 302 | ||
300 | // 'error' cases: | 303 | // 'error' cases: |
301 | (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, | 304 | (TypableDef::Function(_), Namespace::Types) => Ty::Unknown, |
305 | (TypableDef::Union(_), Namespace::Values) => Ty::Unknown, | ||
302 | (TypableDef::Enum(_), Namespace::Values) => Ty::Unknown, | 306 | (TypableDef::Enum(_), Namespace::Values) => Ty::Unknown, |
303 | (TypableDef::EnumVariant(_), Namespace::Types) => Ty::Unknown, | 307 | (TypableDef::EnumVariant(_), Namespace::Types) => Ty::Unknown, |
304 | (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, | 308 | (TypableDef::TypeAlias(_), Namespace::Values) => Ty::Unknown, |
@@ -467,19 +471,21 @@ fn type_for_type_alias(db: &impl HirDatabase, t: TypeAlias) -> Ty { | |||
467 | pub enum TypableDef { | 471 | pub enum TypableDef { |
468 | Function(Function), | 472 | Function(Function), |
469 | Struct(Struct), | 473 | Struct(Struct), |
474 | Union(Union), | ||
470 | Enum(Enum), | 475 | Enum(Enum), |
471 | EnumVariant(EnumVariant), | 476 | EnumVariant(EnumVariant), |
472 | TypeAlias(TypeAlias), | 477 | TypeAlias(TypeAlias), |
473 | Const(Const), | 478 | Const(Const), |
474 | Static(Static), | 479 | Static(Static), |
475 | } | 480 | } |
476 | impl_froms!(TypableDef: Function, Struct, Enum, EnumVariant, TypeAlias, Const, Static); | 481 | impl_froms!(TypableDef: Function, Struct, Union, Enum, EnumVariant, TypeAlias, Const, Static); |
477 | 482 | ||
478 | impl From<ModuleDef> for Option<TypableDef> { | 483 | impl From<ModuleDef> for Option<TypableDef> { |
479 | fn from(def: ModuleDef) -> Option<TypableDef> { | 484 | fn from(def: ModuleDef) -> Option<TypableDef> { |
480 | let res = match def { | 485 | let res = match def { |
481 | ModuleDef::Function(f) => f.into(), | 486 | ModuleDef::Function(f) => f.into(), |
482 | ModuleDef::Struct(s) => s.into(), | 487 | ModuleDef::Struct(s) => s.into(), |
488 | ModuleDef::Union(u) => u.into(), | ||
483 | ModuleDef::Enum(e) => e.into(), | 489 | ModuleDef::Enum(e) => e.into(), |
484 | ModuleDef::EnumVariant(v) => v.into(), | 490 | ModuleDef::EnumVariant(v) => v.into(), |
485 | ModuleDef::TypeAlias(t) => t.into(), | 491 | ModuleDef::TypeAlias(t) => t.into(), |