Fix conversion with record types

This commit is contained in:
Tim Wojtulewicz 2023-08-10 13:42:23 -07:00
parent af9e852c28
commit fe9926e538
3 changed files with 47 additions and 9 deletions

View file

@ -4268,17 +4268,23 @@ ValPtr cast_value_to_type(Val* v, Type* t)
auto set_type = v->GetType<SetType>();
auto indices = set_type->GetIndices();
if ( indices->GetTypes().size() > 1 || ! indices->IsPure() )
if ( indices->GetTypes().size() > 1 )
return nullptr;
auto ret_type = IntrusivePtr<VectorType>{NewRef{}, t->AsVectorType()};
auto ret = make_intrusive<VectorVal>(ret_type);
auto lv = v->AsTableVal()->ToPureListVal();
auto n = lv->Length();
for ( int i = 0; i < n; ++i )
ret->Assign(i, lv->Idx(i));
auto* table = v->AsTable();
auto* tval = v->AsTableVal();
int index = 0;
for ( const auto& te : *table )
{
auto k = te.GetHashKey();
auto lv = tval->RecreateIndex(*k);
ValPtr entry_key = lv->Length() == 1 ? lv->Idx(0) : lv;
ret->Assign(index, entry_key);
index++;
}
return ret;
}
@ -4320,11 +4326,11 @@ static bool can_cast_set_and_vector(const Type* t1, const Type* t2)
if ( st && vt )
{
auto set_indices = st->GetIndices();
if ( set_indices->GetTypes().size() > 1 || ! set_indices->IsPure() )
auto set_indices = st->GetIndices()->GetTypes();
if ( set_indices.size() > 1 )
return false;
return same_type(set_indices->GetPureType(), vt->Yield());
return same_type(set_indices[0], vt->Yield());
}
return false;

View file

@ -23,3 +23,17 @@ set to vector (port)
21/tcp
}
[23/tcp, 21/tcp]
set to vector (record)
{
[a=a, b=b],
[a=a1, b=b1]
}
{
[a=a, b=b],
[a=a1, b=b1]
}
vector to set (record)
[[a=a, b=b], [a=a1, b=b1]]
[[a=a, b=b], [a=a1, b=b1]]

View file

@ -3,6 +3,11 @@
# @TEST-EXEC: btest-diff out
# @TEST-EXEC: btest-diff .stderr
type r: record {
a: string;
b: string;
};
print("vector to set");
local v1 = vector(1, 1, 1, 2, 2, 3, 3, 4);
local s1 = v1 as set[count];
@ -22,3 +27,16 @@ local s3 = set(21/tcp, 23/tcp);
local v3 = s3 as vector of port;
print(s3);
print(v3);
local s: set[r] = set([$a="a", $b="b"], [$a="a1", $b="b1"]);
local v: vector of r = vector([$a="a", $b="b"], [$a="a1", $b="b1"]);
print("");
print("set to vector (record)");
print s;
print v as set[r];
print("");
print("vector to set (record)");
print v;
print s as vector of r;