comptime enum value params: $o: EnumType binds+resolves variant tag
A comptime value param whose constraint is a plain enum ($o: Ord) now binds its enum-literal argument to the variant tag during inlined comptime-call lowering. The tag is recorded in comptime_value_bindings (readable downstream via comptimeIntNamed / direct map lookup, and as an array-dim style const-int leaf) AND the param is bound into scope as an enum_init value so body comparisons like 'if o == .a' lower as ordinary enum comparisons. Distinct ordering args monomorphize the inlined body per value. A non-constant argument or an unknown variant emits a loud diagnostic and binds nothing — never a silent default. Locked by examples/0627-comptime-enum-value-param.sx.
This commit is contained in:
27
examples/0627-comptime-enum-value-param.sx
Normal file
27
examples/0627-comptime-enum-value-param.sx
Normal file
@@ -0,0 +1,27 @@
|
||||
// Comptime ENUM value parameter: `$o: <EnumType>` binds the enum-literal
|
||||
// argument to its variant tag, monomorphizes the inlined body per distinct
|
||||
// ordering value, and resolves `o` in the body as a compile-time-known enum
|
||||
// constant — usable both in `if o == .a` comparisons AND as a comptime-readable
|
||||
// variant tag during lowering (a downstream lowerer reads it via
|
||||
// `comptime_value_bindings`, exercised here as the `[o]i64` array dimension).
|
||||
#import "modules/std.sx";
|
||||
|
||||
Ord :: enum { a; b; c; }
|
||||
|
||||
pick :: ($o: Ord) -> i64 {
|
||||
if o == .a { return 10; }
|
||||
if o == .b { return 20; }
|
||||
return 30;
|
||||
}
|
||||
|
||||
// `o` read as a compile-time integer (its variant tag) in a TYPE position.
|
||||
tag_dim :: ($o: Ord) -> i64 {
|
||||
arr : [o]i64 = ---;
|
||||
return arr.len;
|
||||
}
|
||||
|
||||
main :: () {
|
||||
print("{}\n", pick(.b)); // 20
|
||||
print("{} {} {}\n", pick(.a), pick(.b), pick(.c)); // 10 20 30
|
||||
print("{} {} {}\n", tag_dim(.a), tag_dim(.b), tag_dim(.c)); // 0 1 2
|
||||
}
|
||||
1
examples/expected/0627-comptime-enum-value-param.exit
Normal file
1
examples/expected/0627-comptime-enum-value-param.exit
Normal file
@@ -0,0 +1 @@
|
||||
0
|
||||
1
examples/expected/0627-comptime-enum-value-param.stderr
Normal file
1
examples/expected/0627-comptime-enum-value-param.stderr
Normal file
@@ -0,0 +1 @@
|
||||
|
||||
3
examples/expected/0627-comptime-enum-value-param.stdout
Normal file
3
examples/expected/0627-comptime-enum-value-param.stdout
Normal file
@@ -0,0 +1,3 @@
|
||||
20
|
||||
10 20 30
|
||||
0 1 2
|
||||
Reference in New Issue
Block a user