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...@@ -6065,6 +6065,27 @@ fn ret(gz: *GenZir, scope: *Scope, node: Ast.Node.Index) InnerError!Zir.Inst.Ref
6065 }6065 }
6066}6066}
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
6068fn identifier(6089fn identifier(
6069 gz: *GenZir,6090 gz: *GenZir,
6070 scope: *Scope,6091 scope: *Scope,
...@@ -6098,7 +6119,7 @@ fn identifier(...@@ -6098,7 +6119,7 @@ fn identifier(
6098 true => .signed,6119 true => .signed,
6099 false => .unsigned,6120 false => .unsigned,
6100 };6121 };
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) {
6102 error.Overflow => return astgen.failNode(6123 error.Overflow => return astgen.failNode(
6103 ident,6124 ident,
6104 "primitive integer type '{s}' exceeds maximum bit width of 65535",6125 "primitive integer type '{s}' exceeds maximum bit width of 65535",
...@@ -8985,7 +9006,7 @@ const GenZir = struct {...@@ -8985,7 +9006,7 @@ const GenZir = struct {
8985 parent: *Scope,9006 parent: *Scope,
8986 /// All `GenZir` scopes for the same ZIR share this.9007 /// All `GenZir` scopes for the same ZIR share this.
8987 astgen: *AstGen,9008 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.
8989 /// Indexes to instructions in `astgen`.9010 /// Indexes to instructions in `astgen`.
8990 instructions: *ArrayListUnmanaged(Zir.Inst.Index),9011 instructions: *ArrayListUnmanaged(Zir.Inst.Index),
8991 /// A sub-block may share its instructions ArrayList with containing GenZir,9012 /// A sub-block may share its instructions ArrayList with containing GenZir,
...@@ -10231,7 +10252,7 @@ pub fn isPrimitive(name: []const u8) bool {...@@ -10231,7 +10252,7 @@ pub fn isPrimitive(name: []const u8) bool {
10231 if (name.len < 2) return false;10252 if (name.len < 2) return false;
10232 const first_c = name[0];10253 const first_c = name[0];
10233 if (first_c != 'i' and first_c != 'u') return false;10254 if (first_c != 'i' and first_c != 'u') return false;
10234 if (std.fmt.parseInt(u16, name[1..], 10)) |_| {10255 if (parseBitCount(name[1..])) |_| {
10235 return true;10256 return true;
10236 } else |err| switch (err) {10257 } else |err| switch (err) {
10237 error.Overflow => return true,10258 error.Overflow => return true,
test/behavior/misc.zig+16
...@@ -148,3 +148,19 @@ test "lazy typeInfo value as generic parameter" {...@@ -148,3 +148,19 @@ test "lazy typeInfo value as generic parameter" {
148 };148 };
149 S.foo(@typeInfo(@TypeOf(.{})));149 S.foo(@typeInfo(@TypeOf(.{})));
150}150}
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}