@@ -1170,8 +1170,7 @@ impl<'a> Typechecker<'a> {
11701170 return ANY_TYPE ;
11711171 }
11721172
1173- let mut basic = HashSet :: < TypeId > :: new ( ) ;
1174- let mut list = HashSet :: new ( ) ;
1173+ let mut res = HashSet :: < TypeId > :: new ( ) ;
11751174
11761175 let mut flattened = HashSet :: new ( ) ;
11771176 for ty_id in types {
@@ -1186,37 +1185,37 @@ impl<'a> Typechecker<'a> {
11861185 }
11871186
11881187 for ty_id in flattened {
1189- match self . types [ ty_id. 0 ] {
1190- Type :: List ( inner) => {
1191- list. insert ( inner) ;
1188+ if res. contains ( & ty_id) {
1189+ continue ;
1190+ }
1191+
1192+ let ty = self . types [ ty_id. 0 ] ;
1193+ let mut add = true ;
1194+ let mut remove = HashSet :: new ( ) ;
1195+ for other_id in res. iter ( ) {
1196+ let other = self . types [ other_id. 0 ] ;
1197+ if self . is_subtype ( ty, other) {
1198+ add = false ;
1199+ break ;
11921200 }
1193- ty => {
1194- if !basic. contains ( & ty_id)
1195- && basic. iter ( ) . all ( |b| !self . is_subtype ( ty, self . types [ b. 0 ] ) )
1196- {
1197- basic. insert ( ty_id) ;
1198- }
1201+ if self . is_subtype ( other, ty) {
1202+ remove. insert ( * other_id) ;
11991203 }
12001204 }
1201- }
12021205
1203- if list. is_empty ( ) {
1204- if basic. len ( ) == 1 {
1205- * basic. iter ( ) . next ( ) . unwrap ( )
1206- } else {
1207- self . oneof_types . push ( basic) ;
1208- self . push_type ( Type :: OneOf ( OneOfId ( self . oneof_types . len ( ) - 1 ) ) )
1206+ if add {
1207+ res. insert ( ty_id) ;
1208+ for other in remove {
1209+ res. remove ( & other) ;
1210+ }
12091211 }
1212+ }
1213+
1214+ if res. len ( ) == 1 {
1215+ * res. iter ( ) . next ( ) . unwrap ( )
12101216 } else {
1211- let list_inner = self . create_oneof ( list) ;
1212- let list_ty = self . push_type ( Type :: List ( list_inner) ) ;
1213- if basic. is_empty ( ) {
1214- list_ty
1215- } else {
1216- basic. insert ( list_ty) ;
1217- self . oneof_types . push ( basic) ;
1218- self . push_type ( Type :: OneOf ( OneOfId ( self . oneof_types . len ( ) - 1 ) ) )
1219- }
1217+ self . oneof_types . push ( res) ;
1218+ self . push_type ( Type :: OneOf ( OneOfId ( self . oneof_types . len ( ) - 1 ) ) )
12201219 }
12211220 }
12221221}
0 commit comments