From 314843ffc3a81c763220fa0d84215798a4f3a8b0 Mon Sep 17 00:00:00 2001 From: Petr Skocik Date: Mon, 12 Nov 2018 17:19:15 +0100 Subject: [PATCH] Fix how _Generic treats pointers to arrays. _Generic should distinguish pointers to differently sized arrays such as `int(*)[2]` and `int(*)[4]`. --- tccgen.c | 5 ++++- tests/tests2/94_generic.c | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/tccgen.c b/tccgen.c index cf16b4d..5b0c2a6 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2820,11 +2820,14 @@ static int compare_types(CType *type1, CType *type2, int unqualified) if (t1 != t2) return 0; /* test more complicated cases */ - bt1 = t1 & VT_BTYPE; + bt1 = t1 & (VT_BTYPE | (unqualified ? 0 : VT_ARRAY) ); if (bt1 == VT_PTR) { type1 = pointed_type(type1); type2 = pointed_type(type2); return is_compatible_types(type1, type2); + } else if (bt1 & VT_ARRAY) { + return type1->ref->c < 0 || type2->ref->c < 0 + || type1->ref->c == type2->ref->c; } else if (bt1 == VT_STRUCT) { return (type1->ref == type2->ref); } else if (bt1 == VT_FUNC) { diff --git a/tests/tests2/94_generic.c b/tests/tests2/94_generic.c index 4804f65..6e20282 100644 --- a/tests/tests2/94_generic.c +++ b/tests/tests2/94_generic.c @@ -68,5 +68,8 @@ int main() printf("%d\n", i); i = _Generic(foo, fptr: 3, int: 4); printf("%d\n", i); + + (void)_Generic((int(*)[2]){0}, int(*)[2]:0, int(*)[4]:0); //shouldn't match twice + return 0; }