From aa2a8688b868c85db862cfa576c807a1ea3eb2b7 Mon Sep 17 00:00:00 2001 From: Evan Haas Date: Sun, 29 Dec 2024 00:35:33 -0800 Subject: [PATCH] Type: disallow type specifiers from combining with types specified by typedefed names Closes #731 --- src/aro/Type.zig | 2 ++ test/cases/duplicate typedef.c | 7 +++++++ test/cases/typedef extra specifiers disallowed.c | 13 +++++++++++++ 3 files changed, 22 insertions(+) create mode 100644 test/cases/duplicate typedef.c create mode 100644 test/cases/typedef extra specifiers disallowed.c diff --git a/src/aro/Type.zig b/src/aro/Type.zig index 53d910a1..984b41cb 100644 --- a/src/aro/Type.zig +++ b/src/aro/Type.zig @@ -1905,6 +1905,8 @@ pub const Builder = struct { /// Try to combine type from typedef, returns true if successful. pub fn combineTypedef(b: *Builder, p: *Parser, typedef_ty: Type, name_tok: TokenIndex) bool { if (typedef_ty.is(.invalid)) return false; + if (b.specifier != .none) return false; + b.error_on_invalid = true; defer b.error_on_invalid = false; diff --git a/test/cases/duplicate typedef.c b/test/cases/duplicate typedef.c new file mode 100644 index 00000000..5440cab1 --- /dev/null +++ b/test/cases/duplicate typedef.c @@ -0,0 +1,7 @@ +typedef long foo; +typedef long foo; +_Static_assert(__builtin_types_compatible_p(long, foo), ""); + +typedef foo bar; +typedef foo bar; +_Static_assert(__builtin_types_compatible_p(bar, foo), ""); diff --git a/test/cases/typedef extra specifiers disallowed.c b/test/cases/typedef extra specifiers disallowed.c new file mode 100644 index 00000000..8e6be5f5 --- /dev/null +++ b/test/cases/typedef extra specifiers disallowed.c @@ -0,0 +1,13 @@ +typedef long foo; +typedef unsigned foo bar; + +typedef double baz; +typedef long baz; + +#define EXPECTED_ERRORS "typedef extra specifiers disallowed.c:2:18: error: typedef redefinition with different types ('unsigned int' vs 'long')" \ + "typedef extra specifiers disallowed.c:1:14: note: previous definition is here" \ + "typedef extra specifiers disallowed.c:2:22: error: expected ';', found 'an identifier'" \ + "typedef extra specifiers disallowed.c:2:22: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]" \ + "typedef extra specifiers disallowed.c:5:14: error: typedef redefinition with different types ('long' vs 'double')" \ + "typedef extra specifiers disallowed.c:4:16: note: previous definition is here" \ +