Skip to content

Commit 68c6a88

Browse files
committed
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.
1 parent 35e804b commit 68c6a88

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

src/link/Wasm.zig

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2836,14 +2836,44 @@ pub const Feature = packed struct(u8) {
28362836
@"shared-mem",
28372837

28382838
pub fn fromCpuFeature(feature: std.Target.wasm.Feature) Tag {
2839-
return @enumFromInt(@intFromEnum(feature));
2839+
return switch (feature) {
2840+
.atomics => .atomics,
2841+
.bulk_memory => .@"bulk-memory",
2842+
.exception_handling => .@"exception-handling",
2843+
.extended_const => .@"extended-const",
2844+
.half_precision => .@"half-precision",
2845+
.multimemory => .multimemory,
2846+
.multivalue => .multivalue,
2847+
.mutable_globals => .@"mutable-globals",
2848+
.nontrapping_bulk_memory_len0 => .@"nontrapping-bulk-memory-len0", // Zig extension.
2849+
.nontrapping_fptoint => .@"nontrapping-fptoint",
2850+
.reference_types => .@"reference-types",
2851+
.relaxed_simd => .@"relaxed-simd",
2852+
.sign_ext => .@"sign-ext",
2853+
.simd128 => .simd128,
2854+
.tail_call => .@"tail-call",
2855+
};
28402856
}
28412857

28422858
pub fn toCpuFeature(tag: Tag) ?std.Target.wasm.Feature {
2843-
return if (@intFromEnum(tag) < @typeInfo(std.Target.wasm.Feature).@"enum".fields.len)
2844-
@enumFromInt(@intFromEnum(tag))
2845-
else
2846-
null;
2859+
return switch (tag) {
2860+
.atomics => .atomics,
2861+
.@"bulk-memory" => .bulk_memory,
2862+
.@"exception-handling" => .exception_handling,
2863+
.@"extended-const" => .extended_const,
2864+
.@"half-precision" => .half_precision,
2865+
.multimemory => .multimemory,
2866+
.multivalue => .multivalue,
2867+
.@"mutable-globals" => .mutable_globals,
2868+
.@"nontrapping-bulk-memory-len0" => .nontrapping_bulk_memory_len0, // Zig extension.
2869+
.@"nontrapping-fptoint" => .nontrapping_fptoint,
2870+
.@"reference-types" => .reference_types,
2871+
.@"relaxed-simd" => .relaxed_simd,
2872+
.@"sign-ext" => .sign_ext,
2873+
.simd128 => .simd128,
2874+
.@"tail-call" => .tail_call,
2875+
.@"shared-mem" => null, // Linker-only feature.
2876+
};
28472877
}
28482878

28492879
pub const format = @compileError("use @tagName instead");

0 commit comments

Comments
 (0)