diff --git a/src/core/compileStoreArray.go b/src/core/compileStoreArray.go index 66ad0d0..5c298fd 100644 --- a/src/core/compileStoreArray.go +++ b/src/core/compileStoreArray.go @@ -44,6 +44,10 @@ func (f *Function) compileStoreArray(node *ast.Assign) error { return err } + if !types.Is(rightValue.Type(), pointer.To) { + return errors.New(&TypeMismatch{Encountered: rightValue.Type().Name(), Expected: pointer.To.Name()}, f.File, right.Source().StartPos) + } + f.Append(&ssa.Store{ Address: addressValue, Index: indexValue, diff --git a/src/core/compileStoreField.go b/src/core/compileStoreField.go index eeefd4f..e57cc17 100644 --- a/src/core/compileStoreField.go +++ b/src/core/compileStoreField.go @@ -49,6 +49,10 @@ func (f *Function) compileStoreField(node *ast.Assign) error { return errors.New(&UnknownStructField{StructName: pointer.To.Name(), FieldName: fieldName}, f.File, left.Children[1].Source().StartPos) } + if !types.Is(rightValue.Type(), field.Type) { + return errors.New(&TypeMismatch{Encountered: rightValue.Type().Name(), Expected: field.Type.Name()}, f.File, right.Source().StartPos) + } + offset := f.Append(&ssa.Int{Int: int(field.Offset)}) f.Append(&ssa.Store{ diff --git a/src/core/errors.go b/src/core/errors.go index 9e0ff54..19d32f1 100644 --- a/src/core/errors.go +++ b/src/core/errors.go @@ -136,7 +136,7 @@ func (err *TypeMismatch) Error() string { return fmt.Sprintf("Expected parameter '%s' of %s '%s' (encountered '%s')", err.ParameterName, subject, err.Expected, err.Encountered) } - return fmt.Sprintf("Expected %s '%s' instead of '%s'", subject, err.Expected, err.Encountered) + return fmt.Sprintf("Expected %s '%s' (encountered '%s')", subject, err.Expected, err.Encountered) } // TypeNotIndexable represents an error where a type does not allow indexing. diff --git a/src/core/errors_test.go b/src/core/errors_test.go index 2e2e591..17f52f2 100644 --- a/src/core/errors_test.go +++ b/src/core/errors_test.go @@ -53,6 +53,8 @@ var errs = []struct { {"ReturnCountMismatch4.q", &core.ReturnCountMismatch{Count: 0, ExpectedCount: 1}}, {"TypeMismatch.q", &core.TypeMismatch{Encountered: "string", Expected: "int64", ParameterName: "x", IsReturn: false}}, {"TypeMismatch2.q", &core.TypeMismatch{Encountered: "string", Expected: "int64", ParameterName: "y", IsReturn: true}}, + {"TypeMismatch3.q", &core.TypeMismatch{Encountered: "string", Expected: "int64"}}, + {"TypeMismatch4.q", &core.TypeMismatch{Encountered: "string", Expected: "uint8"}}, {"UndefinedStructField.q", &core.UndefinedStructField{Identifier: "p", FieldName: "y"}}, {"UnknownIdentifier.q", &core.UnknownIdentifier{Name: "x"}}, {"UnknownIdentifier2.q", &core.UnknownIdentifier{Name: "x"}}, diff --git a/src/core/testdata/TypeMismatch3.q b/src/core/testdata/TypeMismatch3.q new file mode 100644 index 0000000..bf4275d --- /dev/null +++ b/src/core/testdata/TypeMismatch3.q @@ -0,0 +1,7 @@ +main() { + new(Data).number = "Hello" +} + +Data { + number int +} \ No newline at end of file diff --git a/src/core/testdata/TypeMismatch4.q b/src/core/testdata/TypeMismatch4.q new file mode 100644 index 0000000..52add60 --- /dev/null +++ b/src/core/testdata/TypeMismatch4.q @@ -0,0 +1,6 @@ +import mem + +main() { + buffer := mem.alloc(1) + buffer[0] = "Hello" +} \ No newline at end of file