@@ -15,9 +15,10 @@ pub enum Shape {
15
15
/// represented by any single shape
16
16
Any ,
17
17
18
- /// `Optional(T)` represents that a value is nullable, or not always present
18
+ /// `Optional(T)` represents that a value is not always present
19
19
Optional ( Box < Shape > ) ,
20
-
20
+ /// `Nullable(T)` represents that a value is nullable
21
+ Nullable ( Box < Shape > ) ,
21
22
/// Equivalent to `Optional(Bottom)`, `Null` represents optionality with no further information
22
23
Null ,
23
24
@@ -50,7 +51,7 @@ pub fn common_shape(a: Shape, b: Shape) -> Shape {
50
51
match ( a, b) {
51
52
( a, Bottom ) | ( Bottom , a) => a,
52
53
( Integer , Floating ) | ( Floating , Integer ) => Floating ,
53
- ( a, Null ) | ( Null , a) => a. into_optional ( ) ,
54
+ ( a, Null ) | ( Null , a) => a. into_nullable ( ) ,
54
55
( a, Optional ( b) ) | ( Optional ( b) , a) => common_shape ( a, * b) . into_optional ( ) ,
55
56
( Tuple ( shapes1, n1) , Tuple ( shapes2, n2) ) => {
56
57
if shapes1. len ( ) == shapes2. len ( ) {
@@ -81,6 +82,7 @@ pub fn common_shape(a: Shape, b: Shape) -> Shape {
81
82
fields : common_field_shapes ( f1, f2) ,
82
83
} ,
83
84
( Opaque ( t) , _) | ( _, Opaque ( t) ) => Opaque ( t) ,
85
+ ( a, Nullable ( b) ) | ( Nullable ( b) , a) => common_shape ( a, * b) . into_nullable ( ) ,
84
86
_ => Any ,
85
87
}
86
88
}
@@ -113,10 +115,19 @@ impl Shape {
113
115
fn into_optional ( self ) -> Self {
114
116
use self :: Shape :: * ;
115
117
match self {
116
- Null | Any | Bottom | Optional ( _) => self ,
118
+ Null => Nullable ( Box :: new ( self ) ) ,
119
+ Any | Bottom | Optional ( _) => self ,
117
120
non_nullable => Optional ( Box :: new ( non_nullable) ) ,
118
121
}
119
122
}
123
+ fn into_nullable ( self ) -> Self {
124
+ use self :: Shape :: * ;
125
+ match self {
126
+ Null => Nullable ( Box :: new ( self ) ) ,
127
+ Any | Bottom | Nullable ( _) => self ,
128
+ non_nullable => Nullable ( Box :: new ( non_nullable) ) ,
129
+ }
130
+ }
120
131
121
132
/// Note: This is asymmetrical because we don't unify based on this,
122
133
/// but check if `self` can be used *as is* as a replacement for `other`
0 commit comments