Reserve the whole zi_ generated-name prefix
`reserved_declaration` rejected only `zi_default_`, so a program could still declare `zi_new`, `zi_ref` or a `zi_` SIMD helper name the compiler and its backends spell themselves. It now rejects every `zi_` name. Two families stay allowed because the compiler writes them itself: the default-parameter helpers (`zi_default_<_field_>`), and the declarations marked generated in the IR, which cover nested local procedures (`zi_local_`), enum member adapters (`zi_enum_name_`) and the C backend's own `zi_print_` helpers. The saved-IR path re-checks those, so exempting only the source parser's spelling failed there; `lowered_exec` caught it. The seven file-local kind helpers in `cmd/compiler_zir_lower.zi` move off the prefix, and the explain entry now describes the wider rule. Co-authored-by: CommandCodeBot <[email protected]>
This commit is contained in:
1 parent
64de51ca06
commit
c041afb6de
6 files changed
+45
-27
No files matched your search
@@ -178,8 +178,18 @@ check_native_storage :: (module: *IR.ZirModule) -> s32 {
|
||||
|
||||
#scope_file
|
||||
generated_helper_name :: (name: *u8) -> s32 {
|
||||
// The compiler spells these itself: nested local procedures, enum member
|
||||
// adapters and the C backend's own names.
|
||||
generated: [3]string = .["zi_local_", "zi_enum_name_", "zi_print_"]
|
||||
prefix_storage_0 := cast(*S12Array12_u8)AllocateOrExit(size_of(S12Array12_u8))
|
||||
defer CompilerFree(cast(*void)prefix_storage_0)
|
||||
name_length: usize = cast(usize)(cast(u64)(strlen(name)))
|
||||
for prefix: generated {
|
||||
memset(cast(*void)prefix_storage_0, 0, size_of(S12Array12_u8))
|
||||
copy_text(cast(*u8)prefix_storage_0, size_of(S12Array12_u8), CheckCString(prefix))
|
||||
width: usize = cast(usize)(cast(u64)prefix.count)
|
||||
if name_length >= width && strncmp(name, cast(*u8)(*((prefix_storage_0[0]).items)), width) == 0 { return 1 }
|
||||
}
|
||||
memset(cast(*void)prefix_storage_0, 0, size_of(S12Array12_u8))
|
||||
copy_text(cast(*u8)prefix_storage_0, size_of(S12Array12_u8), CheckCString("zi_default_"))
|
||||
at: usize = cast(usize)((cast(u64)size_of(S12Array12_u8) - cast(u64)(1)))
|
||||
@@ -219,14 +229,15 @@ reserved_declaration :: (module: *IR.ZirModule) -> s32 {
|
||||
prefix_storage_0 := cast(*S12Array12_u8)AllocateOrExit(size_of(S12Array12_u8))
|
||||
defer CompilerFree(cast(*void)prefix_storage_0)
|
||||
memset(cast(*void)prefix_storage_0, 0, size_of(S12Array12_u8))
|
||||
copy_text(cast(*u8)prefix_storage_0, size_of(S12Array12_u8), CheckCString("zi_default_"))
|
||||
at: usize = cast(usize)((cast(u64)size_of(S12Array12_u8) - cast(u64)(1)))
|
||||
copy_text(cast(*u8)prefix_storage_0, size_of(S12Array12_u8), CheckCString("zi_"))
|
||||
at: usize = cast(usize)((cast(u64)(3)))
|
||||
{
|
||||
i: s32 = 0
|
||||
while check_loop_39 := (i < (module).function_count) {
|
||||
name: *u8 = cast(*u8)(*((((module).functions)[i]).name))
|
||||
if ((strncmp(name, cast(*u8)(*((prefix_storage_0[0]).items)), at) == 0) && !(generated_helper_name(name) != 0)) {
|
||||
Diagnostic((((module).functions)[i]).span, CheckCString("check.reserved_name"), CheckCString("declaration name starts with the compiler's generated-helper prefix: %s"), name)
|
||||
generated: s32 = cast(s32)((((((module).functions)[i]).is_specialization != 0) || (((module).functions)[i]).is_global_initializer != 0))
|
||||
if ((strncmp(name, cast(*u8)(*((prefix_storage_0[0]).items)), at) == 0) && !(generated != 0) && !(generated_helper_name(name) != 0)) {
|
||||
Diagnostic((((module).functions)[i]).span, CheckCString("check.reserved_name"), CheckCString("declaration name starts with the compiler's reserved zi_ prefix: %s"), name)
|
||||
return 0
|
||||
}
|
||||
S12Post2(*(i))
|
||||
@@ -237,7 +248,7 @@ reserved_declaration :: (module: *IR.ZirModule) -> s32 {
|
||||
while check_loop_40 := (i < (module).global_count) {
|
||||
name: *u8 = cast(*u8)(*((((module).globals)[i]).name))
|
||||
if ((strncmp(name, cast(*u8)(*((prefix_storage_0[0]).items)), at) == 0) && !(generated_helper_name(name) != 0)) {
|
||||
Diagnostic((((module).globals)[i]).span, CheckCString("check.reserved_name"), CheckCString("declaration name starts with the compiler's generated-helper prefix: %s"), name)
|
||||
Diagnostic((((module).globals)[i]).span, CheckCString("check.reserved_name"), CheckCString("declaration name starts with the compiler's reserved zi_ prefix: %s"), name)
|
||||
return 0
|
||||
}
|
||||
S12Post2(*(i))
|
||||
@@ -248,7 +259,7 @@ reserved_declaration :: (module: *IR.ZirModule) -> s32 {
|
||||
while check_loop_41 := (i < (module).define_count) {
|
||||
name: *u8 = cast(*u8)(*((((module).defines)[i]).name))
|
||||
if ((strncmp(name, cast(*u8)(*((prefix_storage_0[0]).items)), at) == 0) && !(generated_helper_name(name) != 0)) {
|
||||
Diagnostic((((module).defines)[i]).span, CheckCString("check.reserved_name"), CheckCString("declaration name starts with the compiler's generated-helper prefix: %s"), name)
|
||||
Diagnostic((((module).defines)[i]).span, CheckCString("check.reserved_name"), CheckCString("declaration name starts with the compiler's reserved zi_ prefix: %s"), name)
|
||||
return 0
|
||||
}
|
||||
S12Post2(*(i))
|
||||
@@ -259,7 +270,7 @@ reserved_declaration :: (module: *IR.ZirModule) -> s32 {
|
||||
while check_loop_42 := (i < (module).type_count) {
|
||||
name: *u8 = cast(*u8)(*((((module).types)[i]).name))
|
||||
if ((strncmp(name, cast(*u8)(*((prefix_storage_0[0]).items)), at) == 0) && !(generated_helper_name(name) != 0)) {
|
||||
Diagnostic((((module).types)[i]).span, CheckCString("check.reserved_name"), CheckCString("declaration name starts with the compiler's generated-helper prefix: %s"), name)
|
||||
Diagnostic((((module).types)[i]).span, CheckCString("check.reserved_name"), CheckCString("declaration name starts with the compiler's reserved zi_ prefix: %s"), name)
|
||||
return 0
|
||||
}
|
||||
S12Post2(*(i))
|
||||
@@ -270,7 +281,7 @@ reserved_declaration :: (module: *IR.ZirModule) -> s32 {
|
||||
while check_loop_43 := (i < (module).import_count) {
|
||||
name: *u8 = cast(*u8)(*((((module).imports)[i]).name))
|
||||
if ((strncmp(name, cast(*u8)(*((prefix_storage_0[0]).items)), at) == 0) && !(generated_helper_name(name) != 0)) {
|
||||
Diagnostic((((module).imports)[i]).span, CheckCString("check.reserved_name"), CheckCString("declaration name starts with the compiler's generated-helper prefix: %s"), name)
|
||||
Diagnostic((((module).imports)[i]).span, CheckCString("check.reserved_name"), CheckCString("declaration name starts with the compiler's reserved zi_ prefix: %s"), name)
|
||||
return 0
|
||||
}
|
||||
S12Post2(*(i))
|
||||
|
||||
+13
-12
@@ -202,29 +202,30 @@ ZIR_BIT_CTZ :: 2
|
||||
ZIR_BIT_POPCOUNT :: 3
|
||||
ZIR_BIT_BSWAP :: 4
|
||||
|
||||
// zi_kbits, zi_ksigned, zi_kfloat, zi_kmask, zi_sx and zi_fput.
|
||||
zi_kbits :: (kind: s32) -> s32 {
|
||||
// Kind helpers: width, signedness, float classification, mask, sign extension
|
||||
// and float bit moves.
|
||||
LowerKindBits :: (kind: s32) -> s32 {
|
||||
if kind == ZI_K_F32 { return 32 }
|
||||
if kind == ZI_K_F64 { return 64 }
|
||||
shift: s32 = kind / 2
|
||||
return cast(s32)8 << shift
|
||||
}
|
||||
zi_ksigned :: (kind: s32) -> bool { return kind <= ZI_K_S64 && (kind & 1) != 0 }
|
||||
zi_kfloat :: (kind: s32) -> bool { return kind >= ZI_K_F32 }
|
||||
zi_kmask :: (kind: s32) -> u64 {
|
||||
bits: u64 = cast(u64)zi_kbits(kind)
|
||||
LowerKindSigned :: (kind: s32) -> bool { return kind <= ZI_K_S64 && (kind & 1) != 0 }
|
||||
LowerKindFloat :: (kind: s32) -> bool { return kind >= ZI_K_F32 }
|
||||
LowerKindMask :: (kind: s32) -> u64 {
|
||||
bits: u64 = cast(u64)LowerKindBits(kind)
|
||||
if bits == cast(u64)64 { return 18446744073709551615 }
|
||||
return (cast(u64)1 << bits) - cast(u64)1
|
||||
}
|
||||
zi_sx :: (value: u64, kind: s32) -> s64 {
|
||||
width: u64 = cast(u64)zi_kbits(kind)
|
||||
LowerSignExtend :: (value: u64, kind: s32) -> s64 {
|
||||
width: u64 = cast(u64)LowerKindBits(kind)
|
||||
if width == cast(u64)64 { return cast(s64)value }
|
||||
sign := cast(u64)1 << (width - cast(u64)1)
|
||||
bits := value & zi_kmask(kind)
|
||||
if (bits & sign) != cast(u64)0 { return cast(s64)(bits | ~zi_kmask(kind)) }
|
||||
bits := value & LowerKindMask(kind)
|
||||
if (bits & sign) != cast(u64)0 { return cast(s64)(bits | ~LowerKindMask(kind)) }
|
||||
return cast(s64)bits
|
||||
}
|
||||
zi_fget :: (bits: u64, kind: s32) -> float64 {
|
||||
LowerFloatGet :: (bits: u64, kind: s32) -> float64 {
|
||||
if kind == ZI_K_F32 {
|
||||
word: s32 = cast(s32)(bits & 4294967295)
|
||||
single: float32
|
||||
@@ -235,7 +236,7 @@ zi_fget :: (bits: u64, kind: s32) -> float64 {
|
||||
memcpy(cast(*void)*value, cast(*void)*bits, 8)
|
||||
return value
|
||||
}
|
||||
zi_fput :: (value: float64, kind: s32) -> u64 {
|
||||
LowerFloatPut :: (value: float64, kind: s32) -> u64 {
|
||||
if kind == ZI_K_F32 {
|
||||
single := cast(float32)value
|
||||
word: s32
|
||||
|
||||
@@ -68,7 +68,7 @@ PkgPrintExplanation :: (code: string, json: bool) -> void {
|
||||
.{prefix="check.slice_lifetime", summary="A borrowed slice or text view has an invalid lifetime or mutable-backing conflict.", action="Keep the owner live and immutable for the whole view lifetime, or copy the bytes when independence is needed."},
|
||||
.{prefix="check.cleanup", summary="A declaration conflicts with lexical cleanup or ownership rules.", action="Check declaration order, moves, and lexical scope; consume owned storage exactly once."},
|
||||
.{prefix="check.vec_copy", summary="An owned vector or aggregate containing a vector is being copied.", action="Move it, pass a pointer, or redesign the aggregate so ownership is not duplicated."},
|
||||
.{prefix="check.reserved_name", summary="A declaration uses the compiler's generated default-parameter helper namespace.", action="Rename it; generated helpers spell zi_default_ followed by generated hexadecimal digits."},
|
||||
.{prefix="check.reserved_name", summary="A declaration uses the compiler's reserved generated-name prefix zi_.", action="Rename it; the compiler spells its own generated names with zi_, such as zi_default_ followed by generated hexadecimal digits, zi_new, zi_free and the zi_ SIMD helpers."},
|
||||
.{prefix="check.", summary="The type checker rejected a declaration, expression, signature, or effect.", action="Inspect the span and inferred types, query ziran api for visible declarations, and make source and saved-IR semantics agree."},
|
||||
.{prefix="proof.", summary="A scalar theorem certificate has invalid structure or identity.", action="Check typed theorem bindings, import qualification, proof steps, and duplicate certificates in docs/LAWS.md."},
|
||||
.{prefix="law.", summary="A compile-time law gate rejected the program.", action="Inspect the named law and payload; fix the checked behavior or add a narrowly scoped valid waiver."},
|
||||
|
||||
+5
-5
@@ -57,11 +57,11 @@ line when the work lands. Keep detail in the referenced plan.
|
||||
`MachineVerify::DirectProven` stays conservative; test with
|
||||
`tests/proven_pointer_cases.zi`, `tests/guarded_index_ranges.sh`,
|
||||
`tests/trust_boundary_cases.zi`.
|
||||
- Language quick wins: reserve the `zi_` generated-name prefix, add
|
||||
statement-level parse recovery with a per-region error cap, and list
|
||||
ambiguity candidates in `module.ambiguous`/`package.ambiguous`; new codes
|
||||
enter the explain registry and `tests/explain.sh` must pass; clean-file
|
||||
`.zir` output stays byte-identical.
|
||||
- Language quick wins, remaining: add statement-level parse recovery with a
|
||||
per-region error cap, and list ambiguity candidates in
|
||||
`module.ambiguous`/`package.ambiguous`; new codes enter the explain registry
|
||||
and `tests/explain.sh` must pass; clean-file `.zir` output stays
|
||||
byte-identical.
|
||||
- Several parse errors per file: a recovery point around each top-level declaration, added where the parser port puts recovery (today ParserWithRecovery in cmd/zir/zir_parse.c covers a whole parse).
|
||||
- Exact caret columns inside a declaration's value: re-land the PartSpan span change (reverted in 527c3d0b) once the saved-IR codec stores literal values instead of reparsing statement-text slices.
|
||||
- Check control: one documented switch builds the most thorough program
|
||||
|
||||
@@ -103,7 +103,7 @@ ApiMetadata :: (test: *NativeTest) -> bool {
|
||||
rows := ApiRows(*source); saved_rows := ApiRows(*saved)
|
||||
for row: rows {
|
||||
name := CheckField(*source, row.row, "name")
|
||||
if CheckField(*source, row.row, "symbol_kind") != "law" && !NativeAssert(test, !TextHasPrefix(name, "Hidden") && !TextHasPrefix(name, "__") && !TextHasPrefix(name, "zi_default_"), "API exposed a private or generated name") { return false }
|
||||
if CheckField(*source, row.row, "symbol_kind") != "law" && !NativeAssert(test, !TextHasPrefix(name, "Hidden") && !TextHasPrefix(name, "__") && !TextHasPrefix(name, "zi_"), "API exposed a private or generated name") { return false }
|
||||
if row.kind == "laws" && !NativeAssert(test, CheckField(*source, row.row, "visibility") == "module_contract", "law lost module-contract visibility") { return false }
|
||||
if row.module == "vec" { continue }
|
||||
document := JsonMember(*source, row.row, "document"); span := JsonMember(*source, row.row, "span")
|
||||
|
||||
@@ -45,6 +45,12 @@ ToolDeclarationNames :: (test: *NativeTest) -> bool {
|
||||
"zi_default_0123456789abcdef_field_ :: 3",
|
||||
"zi_default_0123456789abcdef_field_x :: 3",
|
||||
"zi_default_0123456789abcdef_extra :: 3",
|
||||
// The whole generated-name prefix is reserved, not only the helper form.
|
||||
"zi_new :: () -> s32 { return 1 }",
|
||||
"zi_free :: 3",
|
||||
"zi_simd_at: s32 = 4;",
|
||||
"zi_enum_name_point :: struct { x: s32 }",
|
||||
"zi_kbits :: (kind: s32) -> s32 { return kind }",
|
||||
]
|
||||
for declaration: declarations {
|
||||
if !NativeWrite(test, source, CheckConcat(declaration, "\n#program_export\nAnswer :: () -> s32 { return 0 }\n")) { return false }
|
||||
|
||||
Reference in new issue
Block a user