From 188208b95935617985e56e8911559dc92a9fc71b Mon Sep 17 00:00:00 2001 From: David Nadlinger Date: Fri, 22 Apr 2022 22:46:24 +0100 Subject: [PATCH] core/typecheck: Implement unification for scalar indexing of ndarrays --- nac3core/src/typecheck/type_inferencer/test.rs | 5 +++-- nac3core/src/typecheck/typedef/mod.rs | 14 +++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/nac3core/src/typecheck/type_inferencer/test.rs b/nac3core/src/typecheck/type_inferencer/test.rs index 0ac4d99d..129043cb 100644 --- a/nac3core/src/typecheck/type_inferencer/test.rs +++ b/nac3core/src/typecheck/type_inferencer/test.rs @@ -518,12 +518,13 @@ impl TestEnvironment { a = array([1, 2]) a0 = a[0] b = array([[1, 2], [3, 4]]) - # b0 = b[0] + b0 = b[0] b00 = b[0, 0] c = 1 ac = a[c] "}, - [("a", "ndarray[int32, 1]"), ("b", "ndarray[int32, 2]"), ("a0", "int32"), ("b00", "int32"), ("ac", "int32")].iter().cloned().collect(), + [("a", "ndarray[int32, 1]"), ("a0", "int32"), ("b", "ndarray[int32, 2]"), + ("b0", "ndarray[int32, 1]"), ("b00", "int32"), ("ac", "int32")].iter().cloned().collect(), &[] ; "array test")] #[test_case(indoc! {" diff --git a/nac3core/src/typecheck/typedef/mod.rs b/nac3core/src/typecheck/typedef/mod.rs index 95c16562..0935946f 100644 --- a/nac3core/src/typecheck/typedef/mod.rs +++ b/nac3core/src/typecheck/typedef/mod.rs @@ -662,13 +662,17 @@ impl Unifier { for (k, v) in fields.iter() { match *k { RecordKey::Int(_) => { - if *num_dims > 1 { - unreachable!("xxx implement unification for scalar indexing of multidimensional array"); - } - self.unify_impl(v.ty, *ty, false).map_err(|e| e.at(v.loc))? + // . is generated during generic scalar indexing lowering. + let indexed_ty = if *num_dims == 1 { + *ty + } else { + self.add_ty(TNDArray { ty: *ty, num_dims: *num_dims - 1 }) + }; + self.unify_impl(v.ty, indexed_ty, false).map_err(|e| e.at(v.loc))? } RecordKey::Str(_) => { - return Err(TypeError::new(TypeErrorKind::NoSuchField(*k, b), v.loc)) + // xxx: Implement .shape here? + return Err(TypeError::new(TypeErrorKind::NoSuchField(*k, b), v.loc)); } } }