diff --git a/src/Type.cc b/src/Type.cc index e900d8165a..2d96f636e7 100644 --- a/src/Type.cc +++ b/src/Type.cc @@ -2170,6 +2170,9 @@ bool record_promotion_compatible(const RecordType* super_rec, const RecordType* if ( same_type(sub_field_type, super_field_type) ) continue; + if ( BothArithmetic(sub_field_type->Tag(), super_field_type->Tag()) ) + continue; + if ( sub_field_type->Tag() != TYPE_RECORD ) return false; diff --git a/testing/btest/Baseline/language.arithmetic-record-vec-fail/err b/testing/btest/Baseline/language.arithmetic-record-vec-fail/err new file mode 100644 index 0000000000..de5ae689f4 --- /dev/null +++ b/testing/btest/Baseline/language.arithmetic-record-vec-fail/err @@ -0,0 +1,7 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +error in <...>/arithmetic-record-vec-fail.zeek, line 13 and int: type clash for field "a" ((coerce [$a=+1] to WithCount) and int) +error in <...>/arithmetic-record-vec-fail.zeek, line 13 and int: type clash for field "a" ((coerce [$a=-2] to WithCount) and int) +error in <...>/arithmetic-record-vec-fail.zeek, line 13 and double: type clash for field "a" ((coerce [$a=3.0] to WithCount) and double) +error in <...>/arithmetic-record-vec-fail.zeek, line 16 and double: type clash for field "a" ((coerce [$a=1.0] to WithInt) and double) +error in <...>/arithmetic-record-vec-fail.zeek, line 16 and double: type clash for field "a" ((coerce [$a=+2.0] to WithInt) and double) +error in <...>/arithmetic-record-vec-fail.zeek, line 16 and double: type clash for field "a" ((coerce [$a=-3.0] to WithInt) and double) diff --git a/testing/btest/Baseline/language.arithmetic-record-vec/out b/testing/btest/Baseline/language.arithmetic-record-vec/out new file mode 100644 index 0000000000..48be3f6b00 --- /dev/null +++ b/testing/btest/Baseline/language.arithmetic-record-vec/out @@ -0,0 +1,7 @@ +### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63. +[[a=1], [a=2], [a=3]] +[[a=1], [a=2], [a=3]] +[[a=1], [a=2], [a=3]] +[[a=1.0], [a=2.0], [a=3.0]] +[[a=1.0], [a=2.0], [a=3.0]] +[[a=1.0], [a=2.0], [a=-3.0]] diff --git a/testing/btest/language/arithmetic-record-vec-fail.zeek b/testing/btest/language/arithmetic-record-vec-fail.zeek new file mode 100644 index 0000000000..3d17927157 --- /dev/null +++ b/testing/btest/language/arithmetic-record-vec-fail.zeek @@ -0,0 +1,16 @@ +# @TEST-EXEC-FAIL: zeek -b %INPUT 2>err +# @TEST-EXEC: TEST_DIFF_CANONIFIER=$SCRIPTS/diff-remove-abspath btest-diff err + +type WithCount: record { + a: count; +}; + +type WithInt: record { + a: int; +}; + +type CountRecVec: vector of WithCount; +global vec_count_fail = CountRecVec([$a=+1], [$a=-2], [$a=3.0]); + +type IntRecVec: vector of WithInt; +global vec_int_fail = IntRecVec([$a=1.0], [$a=+2.0], [$a=-3.0]); diff --git a/testing/btest/language/arithmetic-record-vec.zeek b/testing/btest/language/arithmetic-record-vec.zeek new file mode 100644 index 0000000000..820edfeb2c --- /dev/null +++ b/testing/btest/language/arithmetic-record-vec.zeek @@ -0,0 +1,37 @@ +# @TEST-DOC: Ensures arithmetic type checking works when adding to vector; regression test for #4722 +# +# @TEST-EXEC: zeek -b %INPUT >out +# @TEST-EXEC: btest-diff out + +type WithCount: record { + a: count; +}; + +type WithInt: record { + a: int; +}; + +type WithDouble: record { + a: double; +}; + +type CountRecVec: vector of WithCount; +global vec_count = CountRecVec([$a=1], [$a=2], [$a=3]); + +type IntRecVec: vector of WithInt; +global vec_int = IntRecVec([$a=+1], [$a=+2], [$a=+3]); +global vec_count_int = IntRecVec([$a=1], [$a=2], [$a=3]); + +type DoubleRecVec: vector of WithDouble; +global vec_double = DoubleRecVec([$a=1.0], [$a=2.0], [$a=3.0]); +global vec_count_double = DoubleRecVec([$a=1], [$a=2], [$a=3]); +global vec_int_double = DoubleRecVec([$a=+1], [$a=+2], [$a=-3]); + +print vec_count; + +print vec_int; +print vec_count_int; + +print vec_double; +print vec_count_double; +print vec_int_double;