Merge remote-tracking branch 'origin/topic/jsiwek/gh-926-ternary-type-checking'

* origin/topic/jsiwek/gh-926-ternary-type-checking:
  GH-926: Improve type-checking for ternary conditional operator

Fixes GH-926
This commit is contained in:
Johanna Amann 2020-08-28 16:51:40 -07:00
commit b8a47de26c
10 changed files with 153 additions and 40 deletions

View file

@ -5,3 +5,10 @@ false condition (PASS)
associativity (PASS)
associativity (PASS)
associativity (PASS)
0, set[string]
0, set[string]
0, table[count] of string
0, table[count] of string
0, vector of string
0, vector of string
[1, 5, 3], vector of count

View file

@ -1 +0,0 @@
error in /Users/jon/pro/zeek/zeek/testing/btest/.tmp/language.ternary-record-mismatch/ternary-record-mismatch.zeek, lines 13-14: operands must be of the same type ((F) ? (coerce [$a=a string, $b=6] to MyRecord) : [$a=a different string, $b=7])

View file

@ -0,0 +1,9 @@
error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.ternary-type-check/ternary-type-check.zeek, lines 13-14: operands must be of the same type ((F) ? (coerce [$a=a string, $b=6] to MyRecord) : [$a=a different string, $b=7])
warning in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.ternary-type-check/ternary-type-check.zeek, line 18: Wrong number of arguments for function. Expected 1, got 2. (function(y:count; x:count;) : bool)
error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.ternary-type-check/ternary-type-check.zeek, line 32: operands must be of the same type (b < a ? foo : bar)
warning in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.ternary-type-check/ternary-type-check.zeek, line 21: Wrong number of arguments for function. Expected 2, got 1. (function(y:count;) : bool)
error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.ternary-type-check/ternary-type-check.zeek, line 35: operands must be of the same type (b < a ? bar : foo)
error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.ternary-type-check/ternary-type-check.zeek, line 50: operands must be of the same type (T ? s : ss)
error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.ternary-type-check/ternary-type-check.zeek, line 51: operands must be of the same type (T ? t : tt)
error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.ternary-type-check/ternary-type-check.zeek, line 52: operands must be of the same type (T ? v : vv)
error in /home/jon/pro/zeek/zeek/testing/btest/.tmp/language.ternary-type-check/ternary-type-check.zeek, line 53: operands must be of the same type (T ? v : s)

View file

@ -2,9 +2,9 @@
# @TEST-EXEC: btest-diff out
function test_case(msg: string, expect: bool)
{
print fmt("%s (%s)", msg, expect ? "PASS" : "FAIL");
}
{
print fmt("%s (%s)", msg, expect ? "PASS" : "FAIL");
}
global ct: count;
@ -20,9 +20,8 @@ function f2(): bool
return F;
}
event zeek_init()
{
{
local a: count;
local b: count;
local res: count;
@ -62,5 +61,29 @@ event zeek_init()
(T ? f1() : T) ? f1() : f2();
test_case( "associativity", ct == 2 );
}
# Test for unspecified set coercion
local s: set[string] = { "one", "two", "three" };
local sT = T ? set() : s;
local sF = F ? s : set();
print |sT|, type_name(sT);
print |sF|, type_name(sF);
# Test for unspecified table coercion
local t: table[count] of string = { [1] = "one", [2] = "two", [3] = "three" };
local tT = T ? table() : t;
local tF = F ? t : table();
print |tT|, type_name(tT);
print |tF|, type_name(tF);
# Test for unspecified vector coercion
local v: vector of string = { "one", "two", "three" };
local vT = T ? vector() : v;
local vF = F ? v : vector();
print |vT|, type_name(vT);
print |vF|, type_name(vF);
# Test for ternary vector condition
local tvc = vector(T, F, T);
local tvr = tvc ? vector(1, 2, 3) : vector(4, 5, 6);
print tvr, type_name(tvr);
}

View file

@ -1,16 +0,0 @@
# @TEST-EXEC-FAIL: zeek -b %INPUT >out 2>&1
# @TEST-EXEC: TEST_DIFF_CANONIFIER="$SCRIPTS/diff-remove-abspath" btest-diff out
type MyRecord: record {
a: string;
b: count;
c: bool &default = T;
};
event zeek_init()
{
local rec: MyRecord = record($a = "a string", $b = 6);
local rec2: MyRecord = (F) ? MyRecord($a = "a string", $b = 6) :
record($a = "a different string", $b = 7);
rec2$c = F;
}

View file

@ -0,0 +1,54 @@
# @TEST-EXEC-FAIL: zeek -b %INPUT >out 2>&1
# @TEST-EXEC: TEST_DIFF_CANONIFIER="$SCRIPTS/diff-remove-abspath" btest-diff out
type MyRecord: record {
a: string;
b: count;
c: bool &default = T;
};
event zeek_init()
{
local rec: MyRecord = record($a = "a string", $b = 6);
local rec2: MyRecord = (F) ? MyRecord($a = "a string", $b = 6) :
record($a = "a different string", $b = 7);
rec2$c = F;
}
function foo(y: count, x: count): bool
{ return F; }
function bar(y: count): bool
{ return F; }
function qux(a: count, b: count): bool
{ return T; }
function myfunc(a: count, b: count)
{
local f = a > b ? foo : qux;
print f(5, 7);
local ff = a > b ? foo : bar;
print ff(5, 7);
local fff = a > b ? bar : foo;
print fff(5, 7);
}
function ternaries()
{
local s: set[string] = { "one" };
local ss: set[count] = { 1 };
local t: table[count] of string = { [1] = "one" };
local tt: table[count] of int = { [1] = -1 };
local v: vector of string = { "one" };
local vv: vector of count = { 111 };
print T ? s : s;
print T ? t : t;
print T ? v : v;
print T ? s : ss;
print T ? t : tt;
print T ? v : vv;
print T ? v : s;
}

View file

@ -39,8 +39,8 @@ function test_cmd(label: string, cmd: Exec::Command)
print fmt("%s - exit: %s, signal: %s, stdout: %s, stderr: %s, files: %s",
label, result$exit_code, result$signal_exit,
result?$stdout ? result$stdout : "",
result?$stderr ? result$stderr : "",
result?$stdout ? cat(result$stdout) : "",
result?$stderr ? cat(result$stderr) : "",
file_content);
check_exit_condition();