From 68c6a88770420b897467073ec328ddc2bb3317d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Wed, 22 Jan 2025 21:15:25 +0100 Subject: [PATCH] link.Wasm.Feature: Make fromCpuFeature() and toCpuFeature() less cute. This is more verbose, but at least we now get a compile error instead of UB when a new feature is added to std.Target.wasm.Feature but not to link.Wasm.Feature. --- src/link/Wasm.zig | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/link/Wasm.zig b/src/link/Wasm.zig index 9da66563e9..643c9ea952 100644 --- a/src/link/Wasm.zig +++ b/src/link/Wasm.zig @@ -2836,14 +2836,44 @@ pub const Feature = packed struct(u8) { @"shared-mem", pub fn fromCpuFeature(feature: std.Target.wasm.Feature) Tag { - return @enumFromInt(@intFromEnum(feature)); + return switch (feature) { + .atomics => .atomics, + .bulk_memory => .@"bulk-memory", + .exception_handling => .@"exception-handling", + .extended_const => .@"extended-const", + .half_precision => .@"half-precision", + .multimemory => .multimemory, + .multivalue => .multivalue, + .mutable_globals => .@"mutable-globals", + .nontrapping_bulk_memory_len0 => .@"nontrapping-bulk-memory-len0", // Zig extension. + .nontrapping_fptoint => .@"nontrapping-fptoint", + .reference_types => .@"reference-types", + .relaxed_simd => .@"relaxed-simd", + .sign_ext => .@"sign-ext", + .simd128 => .simd128, + .tail_call => .@"tail-call", + }; } pub fn toCpuFeature(tag: Tag) ?std.Target.wasm.Feature { - return if (@intFromEnum(tag) < @typeInfo(std.Target.wasm.Feature).@"enum".fields.len) - @enumFromInt(@intFromEnum(tag)) - else - null; + return switch (tag) { + .atomics => .atomics, + .@"bulk-memory" => .bulk_memory, + .@"exception-handling" => .exception_handling, + .@"extended-const" => .extended_const, + .@"half-precision" => .half_precision, + .multimemory => .multimemory, + .multivalue => .multivalue, + .@"mutable-globals" => .mutable_globals, + .@"nontrapping-bulk-memory-len0" => .nontrapping_bulk_memory_len0, // Zig extension. + .@"nontrapping-fptoint" => .nontrapping_fptoint, + .@"reference-types" => .reference_types, + .@"relaxed-simd" => .relaxed_simd, + .@"sign-ext" => .sign_ext, + .simd128 => .simd128, + .@"tail-call" => .tail_call, + .@"shared-mem" => null, // Linker-only feature. + }; } pub const format = @compileError("use @tagName instead");