translate-c: fix logic for checking primitive names

isZigPrimitiveType had a bug where it checked the integer names (e.g.
u32) before primitives, leading it to incorrectly return `false` for
`undefined` which starts with `u`.

Related: #9928
This commit is contained in:
Andrew Kelley 2021-10-10 11:39:35 -07:00
parent a5ecffa461
commit d1fd864da7
3 changed files with 6 additions and 19 deletions

View File

@ -6540,7 +6540,7 @@ fn identifier(
const ident_name = try astgen.identifierTokenString(ident_token);
if (ident_name_raw[0] != '@') {
if (simple_types.get(ident_name)) |zir_const_ref| {
if (primitives.get(ident_name)) |zir_const_ref| {
return rvalue(gz, rl, zir_const_ref, ident);
}
@ -8071,7 +8071,7 @@ fn calleeExpr(
}
}
pub const simple_types = std.ComptimeStringMap(Zir.Inst.Ref, .{
const primitives = std.ComptimeStringMap(Zir.Inst.Ref, .{
.{ "anyerror", .anyerror_type },
.{ "anyframe", .anyframe_type },
.{ "bool", .bool_type },
@ -10505,8 +10505,8 @@ fn nullTerminatedString(astgen: AstGen, index: usize) [*:0]const u8 {
return @ptrCast([*:0]const u8, astgen.string_bytes.items.ptr) + index;
}
fn isPrimitive(name: []const u8) bool {
if (simple_types.get(name) != null) return true;
pub fn isPrimitive(name: []const u8) bool {
if (primitives.get(name) != null) return true;
if (name.len < 2) return false;
const first_c = name[0];
if (first_c != 'i' and first_c != 'u') return false;

View File

@ -1644,7 +1644,7 @@ pub const Inst = struct {
/// be derived by subtracting `typed_value_map.len`.
///
/// When adding a tag to this enum, consider adding a corresponding entry to
/// `simple_types` in astgen.
/// `primitives` in astgen.
///
/// The tag type is specified so that it is safe to bitcast between `[]u32`
/// and `[]Ref`.

View File

@ -804,21 +804,8 @@ const Context = struct {
return c.addTokenFmt(tag, "{s}", .{bytes});
}
fn isZigPrimitiveType(name: []const u8) bool {
if (name.len > 1 and (name[0] == 'u' or name[0] == 'i')) {
for (name[1..]) |c| {
switch (c) {
'0'...'9' => {},
else => return false,
}
}
return true;
}
return @import("../AstGen.zig").simple_types.has(name);
}
fn addIdentifier(c: *Context, bytes: []const u8) Allocator.Error!TokenIndex {
if (isZigPrimitiveType(bytes))
if (@import("../AstGen.zig").isPrimitive(bytes))
return c.addTokenFmt(.identifier, "@\"{s}\"", .{bytes});
return c.addTokenFmt(.identifier, "{s}", .{std.zig.fmtId(bytes)});
}