authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-16 17:05:13+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2020-10-30 15:58:13+02:00
log3c96d799531dbfaf4127ed2fcaa0e69658f90e23
tree643fc5fee4e70e84f7624af258199b66bb9af397
parent7db17a2d89c866efadf9a487acf2f9b0535ba859
signaturelock-open Commit is signed but in an unrecognized format.

stage2: disallow switching on floats


2 files changed, 6 insertions(+), 5 deletions(-)

src/zir.zig+1-1
...@@ -275,7 +275,7 @@ pub const Inst = struct {...@@ -275,7 +275,7 @@ pub const Inst = struct {
275 /// A switch expression.275 /// A switch expression.
276 switchbr,276 switchbr,
277 /// A range in a switch case, `lhs...rhs`.277 /// A range in a switch case, `lhs...rhs`.
278 /// Only checks that `lhs >= rhs` if they are ints or floats, everything else is278 /// Only checks that `lhs >= rhs` if they are ints, everything else is
279 /// validated by the .switch instruction.279 /// validated by the .switch instruction.
280 switch_range,280 switch_range,
281281
src/zir_sema.zig+5-4
...@@ -1214,11 +1214,11 @@ fn analyzeInstSwitchRange(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) In...@@ -1214,11 +1214,11 @@ fn analyzeInstSwitchRange(mod: *Module, scope: *Scope, inst: *zir.Inst.BinOp) In
1214 const end = try resolveInst(mod, scope, inst.positionals.rhs);1214 const end = try resolveInst(mod, scope, inst.positionals.rhs);
12151215
1216 switch (start.ty.zigTypeTag()) {1216 switch (start.ty.zigTypeTag()) {
1217 .Int, .ComptimeInt, .Float, .ComptimeFloat => {},1217 .Int, .ComptimeInt => {},
1218 else => return mod.constVoid(scope, inst.base.src),1218 else => return mod.constVoid(scope, inst.base.src),
1219 }1219 }
1220 switch (end.ty.zigTypeTag()) {1220 switch (end.ty.zigTypeTag()) {
1221 .Int, .ComptimeInt, .Float, .ComptimeFloat => {},1221 .Int, .ComptimeInt => {},
1222 else => return mod.constVoid(scope, inst.base.src),1222 else => return mod.constVoid(scope, inst.base.src),
1223 }1223 }
1224 if (start.value()) |start_val| {1224 if (start.value()) |start_val| {
...@@ -1280,7 +1280,7 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw...@@ -1280,7 +1280,7 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw
1280 // check that target type supports ranges1280 // check that target type supports ranges
1281 if (inst.kw_args.range) |range_inst| {1281 if (inst.kw_args.range) |range_inst| {
1282 switch (target.ty.zigTypeTag()) {1282 switch (target.ty.zigTypeTag()) {
1283 .Int, .ComptimeInt, .Float, .ComptimeFloat => {},1283 .Int, .ComptimeInt => {},
1284 else => {1284 else => {
1285 return mod.fail(scope, target.src, "ranges not allowed when switching on type {}", .{target.ty});1285 return mod.fail(scope, target.src, "ranges not allowed when switching on type {}", .{target.ty});
1286 // TODO notes "range used here" range_inst.src1286 // TODO notes "range used here" range_inst.src
...@@ -1291,7 +1291,6 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw...@@ -1291,7 +1291,6 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw
1291 // validate for duplicate items/missing else prong1291 // validate for duplicate items/missing else prong
1292 switch (target.ty.zigTypeTag()) {1292 switch (target.ty.zigTypeTag()) {
1293 .Int, .ComptimeInt => return mod.fail(scope, inst.base.src, "TODO validateSwitch .Int, .ComptimeInt", .{}),1293 .Int, .ComptimeInt => return mod.fail(scope, inst.base.src, "TODO validateSwitch .Int, .ComptimeInt", .{}),
1294 .Float, .ComptimeFloat => return mod.fail(scope, inst.base.src, "TODO validateSwitch .Float, .ComptimeFloat", .{}),
1295 .Enum => return mod.fail(scope, inst.base.src, "TODO validateSwitch .Enum", .{}),1294 .Enum => return mod.fail(scope, inst.base.src, "TODO validateSwitch .Enum", .{}),
1296 .ErrorSet => return mod.fail(scope, inst.base.src, "TODO validateSwitch .ErrorSet", .{}),1295 .ErrorSet => return mod.fail(scope, inst.base.src, "TODO validateSwitch .ErrorSet", .{}),
1297 .Union => return mod.fail(scope, inst.base.src, "TODO validateSwitch .Union", .{}),1296 .Union => return mod.fail(scope, inst.base.src, "TODO validateSwitch .Union", .{}),
...@@ -1350,6 +1349,8 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw...@@ -1350,6 +1349,8 @@ fn validateSwitch(mod: *Module, scope: *Scope, target: *Inst, inst: *zir.Inst.Sw
1350 .Vector,1349 .Vector,
1351 .Frame,1350 .Frame,
1352 .AnyFrame,1351 .AnyFrame,
1352 .ComptimeFloat,
1353 .Float,
1353 => {1354 => {
1354 return mod.fail(scope, target.src, "invalid switch target type '{}'", .{target.ty});1355 return mod.fail(scope, target.src, "invalid switch target type '{}'", .{target.ty});
1355 },1356 },