Skip to content

Commit

Permalink
Fix some type checking regressions.
Browse files Browse the repository at this point in the history
Fixes passing inner structs to any[].

Fixes coercing any[] to int[].

Bug: issue #973
Test: new test case
  • Loading branch information
dvander committed Oct 28, 2024
1 parent 6e00170 commit 91397a3
Show file tree
Hide file tree
Showing 5 changed files with 381 additions and 2 deletions.
2 changes: 1 addition & 1 deletion compiler/semantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,7 @@ Expr* Semantics::CheckArgument(CallExpr* call, ArgDecl* arg, Expr* param,
// If the input type is an index into an array, create an implicit
// array type to represent the slice.
Type* type = val->type();
if (val->ident == iARRAYCELL || val->ident == iARRAYCHAR)
if ((val->ident == iARRAYCELL || val->ident == iARRAYCHAR) && !type->isEnumStruct())
type = types_->defineArray(type, 0);

TypeChecker tc(param, arg->type(), type, TypeChecker::Argument);
Expand Down
2 changes: 2 additions & 0 deletions compiler/type-checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ bool TypeChecker::CheckArrays(ArrayType* formal, ArrayType* actual) {
}
if (formal_elt->isAny() && actual_elt->hasCellSize())
return true;
if (actual_elt->isAny() && formal_elt->hasCellSize())
return true;
}

return DiagnoseFailure();
Expand Down
20 changes: 20 additions & 0 deletions tests/compile-only/ok-inner-enum-struct-to-any.sp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
enum struct InnerStruct
{
int num;
}

enum struct OuterStruct
{
InnerStruct inner;
}

public void OnPluginStart()
{
OuterStruct myStruct;
Test(myStruct.inner);
}

void Test(any[] arr)
{
// no viable conversion from InnerStruct[] to any[]
}
50 changes: 49 additions & 1 deletion tests/sourcemod/include/testing.inc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ stock void SetTestContext(const char[] context)
strcopy(TestContext, sizeof(TestContext), context);
}

stock void AssertEq(const char[] text, int cell1, int cell2)
stock void AssertEq(const char[] text, any cell1, any cell2)
{
TestNumber++;
if (cell1 == cell2)
Expand All @@ -52,6 +52,39 @@ stock void AssertEq(const char[] text, int cell1, int cell2)
}
}

stock void AssertArrayEq(const char[] text, const any[] value, const any[] expected, int len)
{
TestNumber++;
for (int i = 0; i < len; ++i)
{
if (value[i] != expected[i])
{
PrintToServer("[%d] %s FAIL: %s should be %d at index %d, got %d", TestNumber, TestContext, text, expected[i], i, value[i]);
ThrowError("test %d (%s in %s) failed", TestNumber, text, TestContext);
break;
}
}
PrintToServer("[%d] %s: '%s' arrays are equal OK", TestNumber, TestContext, text);
}

stock void AssertArray2DEq(const char[] text, const any[][] value, const any[][] expected, int len, int innerlen)
{
TestNumber++;
for (int i=0; i < len; ++i)
{
for (int j=0; j < innerlen; ++j)
{
if (value[i][j] != expected[i][j])
{
PrintToServer("[%d] %s FAIL: %s should be %d at index [%d][%d], got %d", TestNumber, TestContext, text, expected[i][j], i, j, value[i][j]);
ThrowError("test %d (%s in %s) failed", TestNumber, text, TestContext);
break;
}
}
}
PrintToServer("[%d] %s: '%s' 2D arrays are equal OK", TestNumber, TestContext, text);
}

stock void AssertFalse(const char[] text, bool value)
{
TestNumber++;
Expand Down Expand Up @@ -93,3 +126,18 @@ stock void AssertStrEq(const char[] text, const char[] value, const char[] expec
ThrowError("test %d (%s in %s) failed", TestNumber, text, TestContext);
}
}

stock void AssertStrArrayEq(const char[] text, const char[][] value, const char[][] expected, int len)
{
TestNumber++;
for (int i = 0; i < len; ++i)
{
if (!StrEqual(value[i], expected[i]))
{
PrintToServer("[%d] %s FAIL: %s should be '%s' at index %d, got '%s'", TestNumber, TestContext, text, expected[i], i, value[i]);
ThrowError("test %d (%s in %s) failed", TestNumber, text, TestContext);
break;
}
}
PrintToServer("[%d] %s: '%s' arrays are equal OK", TestNumber, TestContext, text);
}
Loading

0 comments on commit 91397a3

Please sign in to comment.