Skip to content

Commit

Permalink
Type: fix integer rank comparison for incomplete enums
Browse files Browse the repository at this point in the history
Fixes #681
  • Loading branch information
ehaas authored and Vexu committed Apr 16, 2024
1 parent fd94d6f commit 9aeacf0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/aro/Type.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1318,13 +1318,18 @@ pub fn integerRank(ty: Type, comp: *const Compilation) usize {
.typeof_expr => ty.data.expr.ty.integerRank(comp),
.attributed => ty.data.attributed.base.integerRank(comp),

.@"enum" => {
std.debug.assert(real.data.@"enum".fixed);
return real.data.@"enum".tag_ty.integerRank(comp);
},
else => unreachable,
});
}

/// Returns true if `a` and `b` are integer types that differ only in sign
pub fn sameRankDifferentSign(a: Type, b: Type, comp: *const Compilation) bool {
if (!a.isInt() or !b.isInt()) return false;
if (a.hasIncompleteSize() or b.hasIncompleteSize()) return false;
if (a.integerRank(comp) != b.integerRank(comp)) return false;
return a.isUnsignedInt(comp) != b.isUnsignedInt(comp);
}
Expand Down
18 changes: 18 additions & 0 deletions test/cases/enum fixed.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,19 @@ _Static_assert(__builtin_types_compatible_p(enum Unsigned, unsigned), "");
_Static_assert(!__builtin_types_compatible_p(enum Signed, enum Plain), "");
_Static_assert(!__builtin_types_compatible_p(enum Unsigned, enum Plain), "");

void pointers(void) {
int x;
unsigned y;

enum Signed *e1 = &x;
enum Signed *e2 = &y;
enum Unsigned *e3 = &x;
enum Unsigned *e4 = &y;

enum Incomplete *i1 = &x;
enum Incomplete *i2 = &y;
}

#define EXPECTED_ERRORS "enum fixed.c:2:7: warning: enumeration types with a fixed underlying type are a Clang extension [-Wfixed-enum-extension]" \
"enum fixed.c:4:6: error: enumeration previously declared with fixed underlying type" \
"enum fixed.c:2:6: note: previous definition is here" \
Expand All @@ -67,3 +80,8 @@ _Static_assert(!__builtin_types_compatible_p(enum Unsigned, enum Plain), "");
"enum fixed.c:9:6: note: previous definition is here" \
"enum fixed.c:14:5: error: enumerator value is not representable in the underlying type 'unsigned char'" \
"enum fixed.c:18:5: error: enumerator value is not representable in the underlying type 'char'" \
"enum fixed.c:66:23: warning: incompatible pointer types initializing 'enum Signed: int *' from incompatible type 'unsigned int *' converts between pointers to integer types with different sign [-Wpointer-sign]" \
"enum fixed.c:67:25: warning: incompatible pointer types initializing 'enum Unsigned: unsigned int *' from incompatible type 'int *' [-Wincompatible-pointer-types]" \
"enum fixed.c:70:27: warning: incompatible pointer types initializing 'enum Incomplete *' from incompatible type 'int *' [-Wincompatible-pointer-types]" \
"enum fixed.c:71:27: warning: incompatible pointer types initializing 'enum Incomplete *' from incompatible type 'unsigned int *' [-Wincompatible-pointer-types]" \

0 comments on commit 9aeacf0

Please sign in to comment.