1
0
Fork 0
mirror of synced 2025-09-24 04:40:05 +00:00

Merge pull request #846 from Zokrates/fix-struct-member-type-mismatch

Fix struct member type mismatch crash
This commit is contained in:
Thibaut Schaeffer 2021-05-05 16:24:27 +02:00 committed by GitHub
commit 401a77bdbe
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 51 additions and 0 deletions

View file

@ -0,0 +1 @@
Fix crash on struct member type mismatch

View file

@ -0,0 +1,17 @@
struct Foo {
field[2] values
}
struct Bar {
Foo foo
field bar
}
def main():
Bar s = Bar {
foo: Foo { values: [1] },
bar: 0,
}
field b = s.bar
return

View file

@ -0,0 +1,17 @@
struct Foo {
u8[2] values
}
struct Bar {
Foo foo
u8 bar
}
def main():
Bar s = Bar {
foo: Foo { values: [1] }, // notice the size mismatch here
bar: 0,
}
u8 b = s.bar
return

View file

@ -1409,6 +1409,22 @@ impl<'ast, 'a, T: Field> ResultFolder<'ast, T> for Propagator<'ast, 'a, T> {
)),
}
}
StructExpressionInner::Value(v) => {
let v = v.into_iter().zip(ty.iter()).map(|(v, member)|
match self.fold_expression(v) {
Ok(v) => match (ConcreteType::try_from(v.get_type().clone()), ConcreteType::try_from(*member.ty.clone())) {
(Ok(t1), Ok(t2)) => if t1 == t2 { Ok(v) } else { Err(Error::Type(format!(
"Struct member `{}` in struct `{}/{}` expected to have type `{}`, found type `{}`",
member.id, ty.canonical_location.clone().module.display(), ty.canonical_location.clone().name, t2, t1
))) },
_ => Ok(v)
}
e => e
}
).collect::<Result<_, _>>()?;
Ok(StructExpressionInner::Value(v))
}
e => fold_struct_expression_inner(self, ty, e),
}
}