Skip to content

Commit fdcb77b

Browse files
committed
types: remove one calls to ResolvedType::as_inner
Our goal is to reduce/eliminate the places outside of the `types` module that use the `TypeInner` type, since this is (ideally) an implementation detail of `ResolvedType`. There aren't a lot of these places, it turns out. This commit removes one "easy" one, in pattern.rs. There, previously we were matching arrays and using a wildcard _ match to return an error on mismatches. By adding a bit of code repetiton (the 'return error' line 3 times) and calling TypeDestructible::as_list and as_array, we can get rid of the wildcard match, which eliminates the `TypeInner` but also is more robust against extensions to the Pattern enum. This leaves the use in ast.rs in `cast_preserves_enum_identity` which seems quite difficult to remove correctly (the existing code is not correct either, but ok, let's leave it alone unless we can fix it completely). We want to make this nonrecursive and correct, but it will have to wait for a later PR. Aside from that, the only parts of the code, outside the ResolvedType module itself, that now need to know the internals of ResolvedType are in value.rs. Since the structure of Value mirrors the structure of ResolvedType probably we will just have to live with this; when we change the representation of ResolvedType we will need to make parallel changes in value.rs.
1 parent f00b08f commit fdcb77b

1 file changed

Lines changed: 22 additions & 10 deletions

File tree

src/pattern.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::array::BTreeSlice;
99
use crate::error::Error;
1010
use crate::named::{CoreExt, PairBuilder, SelectorBuilder};
1111
use crate::str::Identifier;
12-
use crate::types::{ResolvedType, TypeInner};
12+
use crate::types::{ResolvedType, TypeDeconstructible};
1313
use crate::unstable::impl_require_feature;
1414

1515
/// Pattern for binding values to variables.
@@ -51,25 +51,37 @@ impl Pattern {
5151
let mut stack = vec![(self, ty)];
5252
let mut output = HashMap::new();
5353
while let Some((pattern, ty)) = stack.pop() {
54-
match (pattern, ty.as_inner()) {
55-
(Pattern::Identifier(i), _) => match output.entry(i.clone()) {
54+
let unexpected_err = || Err(Error::ExpressionUnexpectedType { ty: ty.clone() });
55+
match pattern {
56+
Pattern::Identifier(i) => match output.entry(i.clone()) {
5657
Entry::Occupied(..) => {
5758
return Err(Error::VariableReuseInPattern {
5859
identifier: i.clone(),
59-
})
60+
});
6061
}
6162
Entry::Vacant(entry) => {
6263
entry.insert(ty.clone());
6364
}
6465
},
65-
(Pattern::Ignore, _) => {}
66-
(Pattern::Tuple(pats), TypeInner::Tuple(types)) => {
67-
stack.extend(pats.iter().zip(types.iter().map(Arc::as_ref)));
66+
Pattern::Ignore => {}
67+
Pattern::Tuple(pats) => {
68+
if let Some(types) = ty.as_tuple() {
69+
stack.extend(pats.iter().zip(types.iter().map(Arc::as_ref)));
70+
} else {
71+
return unexpected_err();
72+
}
6873
}
69-
(Pattern::Array(pats), TypeInner::Array(ty, size)) if pats.len() == *size => {
70-
stack.extend(pats.iter().zip(std::iter::repeat(ty.as_ref())));
74+
Pattern::Array(pats) => {
75+
if let Some((ty, size)) = ty.as_array() {
76+
if pats.len() == size {
77+
stack.extend(pats.iter().zip(std::iter::repeat(ty)));
78+
} else {
79+
return unexpected_err();
80+
}
81+
} else {
82+
return unexpected_err();
83+
}
7184
}
72-
_ => return Err(Error::ExpressionUnexpectedType { ty: ty.clone() }),
7385
}
7486
}
7587
Ok(output)

0 commit comments

Comments
 (0)