svlog: ty: Improve pop_dim
@@ -656,17 +656,16 @@ impl<'a> PackedType<'a> {
/// Pop the outermost dimension off the type.
pub fn pop_dim(&self, cx: &impl TypeContext<'a>) -> Option<&'a Self> {
- if !self.dims.is_empty() {
- let mut new = self.clone();
- new.dims.pop().expect("pop_dim should pop a dim");
- Some(new.intern(cx))
- } else if !self.resolve().dims.is_empty() {
- let mut new = self.resolve().clone();
+ let mut next = Some(self);
+ while let Some(ty) = next {
+ if !ty.dims.is_empty() {
+ let mut new = ty.clone();
new.dims.pop().expect("pop_dim should pop a dim");
- Some(new.intern(cx))
- } else {
- None
+ return Some(new.intern(cx));
+ }
+ next = ty.resolved;
}
+ None
}
/// Get the underlying struct, or `None` if the type is no struct.
@@ -1340,20 +1339,19 @@ impl<'a> UnpackedType<'a> {
/// Pop the outermost dimension off the type.
pub fn pop_dim(&self, cx: &impl TypeContext<'a>) -> Option<&'a Self> {
- if !self.dims.is_empty() {
- let mut new = self.clone();
- new.dims.pop().expect("pop_dim should drop a dim");
- Some(new.intern(cx))
- } else if !self.resolve().dims.is_empty() {
- let mut new = self.resolve().clone();
+ let mut next = Some(self);
+ while let Some(ty) = next {
+ if !ty.dims.is_empty() {
+ let mut new = ty.clone();
new.dims.pop().expect("pop_dim should drop a dim");
- Some(new.intern(cx))
- } else {
+ return Some(new.intern(cx));
+ }
+ next = ty.resolved;
+ }
self.get_packed()
.and_then(|p| p.pop_dim(cx))
.map(|p| p.to_unpacked(cx))
}
- }
/// Get the underlying struct, or `None` if the type is no struct.
pub fn get_struct(&self) -> Option<&StructType<'a>> {
