authorgravatar for 94326797+riverbl@users.noreply.github.comriverbl <94326797+riverbl@users.noreply.github.com> 2021-12-30 22:54:11+00:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-01-17 16:54:48+02:00
logc71cf48cb5886cdac2172a602b6a6015f483ff93
tree2cb4c2494f5b4d025c14b1cd6b8e84705cbddca5
parent4addb26bba94e9e5abb919b39efcd5d1b99934af

stage2: do not interpret identifier containing underscores (eg: u3_2) as int primitive type


2 files changed, 40 insertions(+), 3 deletions(-)

src/AstGen.zig+24-3
......@@ -6065,6 +6065,27 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
60656065 }
60666066}
60676067
6068/// Parses the string `buf` as a base 10 integer of type `u16`.
6069///
6070/// Unlike std.fmt.parseInt, does not allow the '_' character in `buf`.
6071fn parseBitCount(buf: []const u8) std.fmt.ParseIntError!u16 {
6072 if (buf.len == 0) return error.InvalidCharacter;
6073
6074 var x: u16 = 0;
6075
6076 for (buf) |c| {
6077 const digit = switch (c) {
6078 '0'...'9' => c - '0',
6079 else => return error.InvalidCharacter,
6080 };
6081
6082 if (x != 0) x = try std.math.mul(u16, x, 10);
6083 x = try std.math.add(u16, x, @as(u16, digit));
6084 }
6085
6086 return x;
6087}
6088
60686089fn identifier(
60696090 gz: *GenZir,
60706091 scope: *Scope,
......@@ -6098,7 +6119,7 @@ fn identifier(
60986119 true => .signed,
60996120 false => .unsigned,
61006121 };
6101 const bit_count = std.fmt.parseInt(u16, ident_name_raw[1..], 10) catch |err| switch (err) {
6122 const bit_count = parseBitCount(ident_name_raw[1..]) catch |err| switch (err) {
61026123 error.Overflow => return astgen.failNode(
61036124 ident,
61046125 "primitive integer type '{s}' exceeds maximum bit width of 65535",
......@@ -8985,7 +9006,7 @@ const GenZir = struct {
89859006 parent: *Scope,
89869007 /// All `GenZir` scopes for the same ZIR share this.
89879008 astgen: *AstGen,
8988 /// Keeps track of the list of instructions in this scope. Possibly shared.
9009 /// Keeps track of the list of instructions in this scope. Possibly shared.
89899010 /// Indexes to instructions in `astgen`.
89909011 instructions: *ArrayListUnmanaged(Zir.Inst.Index),
89919012 /// A sub-block may share its instructions ArrayList with containing GenZir,
......@@ -10231,7 +10252,7 @@ pub fn isPrimitive(name: []const u8) bool {
1023110252 if (name.len < 2) return false;
1023210253 const first_c = name[0];
1023310254 if (first_c != 'i' and first_c != 'u') return false;
10234 if (std.fmt.parseInt(u16, name[1..], 10)) |_| {
10255 if (parseBitCount(name[1..])) |_| {
1023510256 return true;
1023610257 } else |err| switch (err) {
1023710258 error.Overflow => return true,
test/behavior/misc.zig+16
......@@ -148,3 +148,19 @@ test "lazy typeInfo value as generic parameter" {
148148 };
149149 S.foo(@typeInfo(@TypeOf(.{})));
150150}
151
152test "variable name containing underscores does not shadow int primitive" {
153 const _u0 = 0;
154 const i_8 = 0;
155 const u16_ = 0;
156 const i3_2 = 0;
157 const u6__4 = 0;
158 const i2_04_8 = 0;
159
160 _ = _u0;
161 _ = i_8;
162 _ = u16_;
163 _ = i3_2;
164 _ = u6__4;
165 _ = i2_04_8;
166}