blob: bd071ed76a4229bccc1ac7df5b39bc2d03d4cc2b [file]
-- Copyright 2026 The Fuchsia Authors
--
-- Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
-- <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
-- license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
-- This file may not be copied, modified, or distributed except according to
-- those terms.
prelude
import Init.NotationExtra
import GuardRefuteWf
/-!
# Negative Refutations for `Trusted.MirSemantics.Enum` Validity Guards
Exercises both clauses of `isUnicodeScalar` and every validity,
enum-admission, identity-uniqueness, value-typing, slot-initialization, and
Profile v2 overflow-pair pointee, `Vec`, `Box`, and slice element guard in
`Trusted/MirSemantics/Enum.lean`.
-/
namespace GuardMutation
open Trusted.MirAst Trusted.MirSemantics
/-- Guard `enum_char_above_unicode_max`: code point `0x110000` exceeds the
maximum Unicode scalar value `0x10FFFF` and faults with `.undefined`. -/
theorem refute_enum_char_above_unicode_max :
ReachesResult (mkConstRetProg .char 0x110000) queryEmpty
(.fault .undefined) ∧
¬ WellFormed (mkConstRetProg .char 0x110000) queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Guard `enum_char_surrogate_gap`: UTF-16 surrogate code point `0xD800` is
not a Unicode scalar value and faults with `.undefined`. -/
theorem refute_enum_char_surrogate_gap :
ReachesResult (mkConstRetProg .char 0xd800) queryEmpty
(.fault .undefined) ∧
¬ WellFormed (mkConstRetProg .char 0xd800) queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Guard `enum_v2_overflow_pair_pointee`: no Profile v2 local may be a
reference or raw pointer to an overflow pair `(T, bool)`, so the pair's
unchecked byte layout stays unobservable. -/
theorem refute_enum_v2_overflow_pair_pointee :
isSupportedV2LocalTy (.rawPtr .shared (.tuple [.int .i8 false, .bool])) =
false ∧
isSupportedV2LocalTy (.ref .mut (.tuple [.int .i32 true, .bool])) = false :=
⟨rfl, rfl⟩
/-- Anti-overreach canary: an overflow pair itself, and pointers to its
integer or `bool` field types, remain admitted local types. -/
theorem positive_enum_v2_overflow_pair_local :
isSupportedV2LocalTy (.tuple [.int .i8 false, .bool]) = true ∧
isSupportedV2LocalTy (.rawPtr .shared (.int .i8 false)) = true ∧
isSupportedV2LocalTy (.ref .shared .bool) = true :=
⟨rfl, rfl, rfl⟩
/-- Guard `enum_v2_overflow_pair_vec_elem`: a `Vec`'s elements are
addressable, so no Profile v2 local may be a `Vec` of overflow pairs. -/
theorem refute_enum_v2_overflow_pair_vec_elem :
isSupportedV2LocalTy (.vec (.tuple [.int .i8 false, .bool])) = false :=
rfl
/-- Guard `enum_v2_overflow_pair_box_pointee`: no Profile v2 local may be a
`Box` of an overflow pair. -/
theorem refute_enum_v2_overflow_pair_box_pointee :
isSupportedV2LocalTy (.box (.tuple [.int .i32 true, .bool])) = false :=
rfl
/-- Guard `enum_v2_overflow_pair_slice_elem`: no Profile v2 local may be a
reference or raw pointer to a slice of overflow pairs. -/
theorem refute_enum_v2_overflow_pair_slice_elem :
isSupportedV2LocalTy
(.ref .shared (.slice (.tuple [.int .i8 false, .bool]))) = false ∧
isSupportedV2LocalTy
(.rawPtr .mut (.slice (.tuple [.int .i16 true, .bool]))) = false :=
⟨rfl, rfl⟩
/-- Anti-overreach canary: `Vec`, `Box`, and slices of scalar elements remain
admitted local types. -/
theorem positive_enum_v2_vec_box_slice_scalar_elems :
isSupportedV2LocalTy (.vec (.int .i8 false)) = true ∧
isSupportedV2LocalTy (.box (.int .i64 false)) = true ∧
isSupportedV2LocalTy (.box (.slice .bool)) = true ∧
isSupportedV2LocalTy (.ref .shared (.slice .char)) = true :=
⟨rfl, rfl, rfl, rfl⟩
/-- Minimal valid unit body with the given identity string. -/
noncomputable def mkUnitBodyWithId (ident : String) : Body := {
argumentCount := 0
locals := [.unit]
basicBlocks := [{ statements := [], terminator := .return_ }]
identity := ident
}
/-- Guard `enum_identity_empty_rejected`: `identityKey?` rejects a body
with an empty `identity` string so `bodiesHaveUniqueIdentities` returns
`false`. -/
theorem refute_enum_identity_empty_rejected :
bodiesHaveUniqueIdentities [mkUnitBodyWithId ""] = false :=
rfl
/-- Guard `enum_identity_duplicate_rejected`: `identityRbInsertAux` rejects
duplicate body identities so `bodiesHaveUniqueIdentities` returns `false`. -/
theorem refute_enum_identity_duplicate_rejected :
bodiesHaveUniqueIdentities
[mkUnitBodyWithId "crate::dup", mkUnitBodyWithId "crate::dup"] = false :=
rfl
/-- Zero-variant scalar-tagged enum (vacuously satisfies `all` and pairwise
distinctness, isolating `!ety.discriminants.isEmpty`). -/
noncomputable def zeroVariantScalarEnum : EnumTy := {
id := 10
discrWidth := .i8
discrSigned := false
tagRepr := .scalar .i8 false
discriminants := []
}
/-- Guard `enum_admitted_nonempty`: 0-variant enum fails `enumAdmitted` and
refutes `WellFormed` at entry. -/
theorem refute_enum_admitted_nonempty :
ReachesResult (mkSingleBlockProg 0 [.unit, .enum zeroVariantScalarEnum] [])
queryEmpty (.fault .unsupported) ∧
¬ WellFormed (mkSingleBlockProg 0 [.unit, .enum zeroVariantScalarEnum] [])
queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Guard `enum_admitted_variant_count_bound`: any enum with `2^32` variants
fails `enumAdmitted` without constructing a `2^32`-element list. -/
theorem refute_enum_admitted_variant_count_bound (ety : EnumTy)
(hLen : ety.discriminants.length = Nat.pow 2 32) :
enumAdmitted ety = false := by
unfold enumAdmitted
rw [hLen]
cases ety.discriminants.isEmpty <;> rfl
/-- Single-variant `.zeroSized` enum whose discriminant `256` overflows
`discrWidth = .i8` (using `.zeroSized` so the scalar tag roundtrip check does
not shadow the discriminant-width check). -/
noncomputable def overflowDiscrZstEnum : EnumTy := {
id := 11
discrWidth := .i8
discrSigned := false
tagRepr := .zeroSized
discriminants := [256]
}
/-- Guard `enum_admitted_discr_fits_width`: discriminant `256` in a `u8`
discriminant enum fails `enumAdmitted` and refutes `WellFormed`. -/
theorem refute_enum_admitted_discr_fits_width :
ReachesResult (mkSingleBlockProg 0 [.unit, .enum overflowDiscrZstEnum] [])
queryEmpty (.fault .unsupported) ∧
¬ WellFormed (mkSingleBlockProg 0 [.unit, .enum overflowDiscrZstEnum] [])
queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Enum with duplicate discriminants `[10, 10]`. -/
noncomputable def dupDiscrEnum : EnumTy := {
id := 12
discrWidth := .i8
discrSigned := false
tagRepr := .scalar .i8 false
discriminants := [10, 10]
}
/-- Guard `enum_admitted_discr_distinct`: duplicate discriminants fail
`enumAdmitted` and refute `WellFormed`. -/
theorem refute_enum_admitted_discr_distinct :
ReachesResult (mkSingleBlockProg 0 [.unit, .enum dupDiscrEnum] [])
queryEmpty (.fault .unsupported) ∧
¬ WellFormed (mkSingleBlockProg 0 [.unit, .enum dupDiscrEnum] [])
queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- `.zeroSized` enum claiming two distinct variants `[0, 1]`. -/
noncomputable def multiVariantZstEnum : EnumTy := {
id := 13
discrWidth := .i8
discrSigned := false
tagRepr := .zeroSized
discriminants := [0, 1]
}
/-- Guard `enum_admitted_zst_single_variant`: `.zeroSized` enum with 2 variants
fails `enumAdmitted` and refutes `WellFormed`. -/
theorem refute_enum_admitted_zst_single_variant :
ReachesResult (mkSingleBlockProg 0 [.unit, .enum multiVariantZstEnum] [])
queryEmpty (.fault .unsupported) ∧
¬ WellFormed (mkSingleBlockProg 0 [.unit, .enum multiVariantZstEnum] [])
queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- `isize`-discriminant enum with `u8` tag whose discriminant `300` truncates
lossily to `44`. -/
noncomputable def lossyTagEnum : EnumTy := {
id := 14
discrWidth := .isize
discrSigned := true
tagRepr := .scalar .i8 false
discriminants := [0, 300]
}
/-- Guard `enum_admitted_scalar_tag_lossless`: scalar tag that loses bits on
discriminant `300` fails `enumAdmitted` and refutes `WellFormed`. -/
theorem refute_enum_admitted_scalar_tag_lossless :
ReachesResult (mkSingleBlockProg 0 [.unit, .enum lossyTagEnum] [])
queryEmpty (.fault .unsupported) ∧
¬ WellFormed (mkSingleBlockProg 0 [.unit, .enum lossyTagEnum] [])
queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Guard `enum_ordering_invalid_tag_none`: `orderingOfTagBits` rejects invalid
tag `2` so an `Ordering` constant with bits `2` faults with `.undefined`. -/
theorem refute_enum_ordering_invalid_tag_none :
ReachesResult (mkConstRetProg .ordering 2) queryEmpty (.fault .undefined) ∧
¬ WellFormed (mkConstRetProg .ordering 2) queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Guard `enum_val_ty_enum_variant_in_bounds`: `Val.ty?` rejects an
out-of-range variant index (`5` for 2-variant `reprU8EnumTy`) with
`.unsupported`. -/
theorem refute_enum_val_ty_enum_variant_in_bounds :
ReachesResult (mkSingleBlockProg 1 [.unit, .enum reprU8EnumTy] [])
{ args := [.enumVal reprU8EnumTy 5] } (.fault .unsupported) ∧
¬ WellFormed (mkSingleBlockProg 1 [.unit, .enum reprU8EnumTy] [])
{ args := [.enumVal reprU8EnumTy 5] } :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Guard `enum_val_ty_overflow_pair_wf`: `Val.ty?` rejects an overflow pair
carrying a non-canonical `u8` scalar (`bits = 256`) with `.unsupported`. -/
theorem refute_enum_val_ty_overflow_pair_wf :
ReachesResult (mkSingleBlockProg 1 [.unit, .tuple [u8Ty, .bool]] [])
{ args := [.tuple [u8Val 256, .bool false]] } (.fault .unsupported) ∧
¬ WellFormed (mkSingleBlockProg 1 [.unit, .tuple [u8Ty, .bool]] [])
{ args := [.tuple [u8Val 256, .bool false]] } :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Guard `enum_check_args_length_mismatch`: `checkArgsMatch` rejects passing
0 arguments to a 1-argument function with `.unsupported`. -/
theorem refute_enum_check_args_length_mismatch :
ReachesResult (mkSingleBlockProg 1 [.unit, u32Ty] []) queryEmpty
(.fault .unsupported) ∧
¬ WellFormed (mkSingleBlockProg 1 [.unit, u32Ty] []) queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Guard `enum_supported_ty_reject_non_overflow_tuple`:
`isSupportedScalarOrUnitTy` rejects non-overflow-pair tuples `(u32, u32)`. -/
theorem refute_enum_supported_ty_reject_non_overflow_tuple :
ReachesResult (mkSingleBlockProg 0 [.unit, .tuple [u32Ty, u32Ty]] [])
queryEmpty (.fault .unsupported) ∧
¬ WellFormed (mkSingleBlockProg 0 [.unit, .tuple [u32Ty, u32Ty]] [])
queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Guard `enum_supported_ty_reject_ref_ptr`: `isSupportedScalarOrUnitTy`
rejects reference and raw-pointer local types. -/
theorem refute_enum_supported_ty_reject_ref_ptr :
ReachesResult (mkSingleBlockProg 0 [.unit, .ref .shared u32Ty] [])
queryEmpty (.fault .unsupported) ∧
¬ WellFormed (mkSingleBlockProg 0 [.unit, .ref .shared u32Ty] [])
queryEmpty :=
refutes_wf_of_execFault (fuel := 1) rfl
/-- Program reading an uninitialized scalar-tagged enum local `_1`. -/
noncomputable def progReadUninitScalarEnum : Program :=
mkSingleBlockProg 0 [.enum reprU8EnumTy, .enum reprU8EnumTy]
[.storageLive 1, .assign ⟨0, []⟩ (.use (.copy ⟨1, []⟩))]
/-- Guard `enum_live_uninit_scalar_enum_uninit`: `liveUninitSlot` starts
scalar-tagged enums uninitialized (`.immediate none`) so reading one before
initialization faults with `.undefined`. -/
theorem refute_enum_live_uninit_scalar_enum_uninit :
ReachesResult progReadUninitScalarEnum queryEmpty (.fault .undefined) ∧
¬ WellFormed progReadUninitScalarEnum queryEmpty :=
refutes_wf_of_execFault (fuel := 2) rfl
/-- Guard `enum_v2_option_nested_ref_rejected`: `Option<&&int>` stays excluded
from Profile v2 even though intra-body `&&int` locals are admitted. -/
theorem refute_enum_v2_option_nested_ref_rejected :
isSupportedV2LocalTy
(.option (.ref .shared (.ref .shared (.int .i32 false)))) = false :=
rfl
/-- Anti-overreach canary: intra-body `&&int`, `&&bool`, `&&Box<u8>`,
`Option<&int>`, and `& &mut int` are admitted while `&mut &int`, `&&[u8]`,
and `&&&int` stay rejected. -/
theorem positive_enum_v2_nested_shared_ref :
isSupportedV2LocalTy (.ref .shared (.ref .shared (.int .i32 false))) =
true ∧
isSupportedV2LocalTy (.ref .shared (.ref .shared .bool)) = true ∧
isSupportedV2LocalTy
(.ref .shared (.ref .shared (.box (.int .i8 false)))) = true ∧
isSupportedV2LocalTy (.option (.ref .shared (.int .i32 false))) = true ∧
isSupportedV2LocalTy (.ref .mut (.ref .shared (.int .i32 false))) =
false ∧
isSupportedV2LocalTy (.ref .shared (.ref .mut (.int .i32 false))) =
true ∧
isSupportedV2LocalTy
(.ref .shared (.ref .shared (.slice (.int .i8 false)))) = false ∧
isSupportedV2LocalTy
(.ref .shared (.ref .shared (.ref .shared (.int .i32 false)))) =
false :=
⟨rfl, rfl, rfl, rfl, rfl, rfl, rfl, rfl⟩
/-- Guard `enum_v2_nested_ref_outer_shared`: the outer reference of a
two-level local must be shared, so `&mut &T` and `&mut &mut T` are not local
types. -/
theorem refute_enum_v2_nested_ref_outer_shared :
isSupportedV2LocalTy (.ref .mut (.ref .shared (.int .i32 false))) =
false ∧
isSupportedV2LocalTy (.ref .mut (.ref .mut .bool)) = false :=
⟨rfl, rfl⟩
/-- Guard `enum_v2_nested_ref_arm_pointee`: the inner reference of a
two-level local, shared or mutable, must have an admitted pointee, so
`& &mut [T]` and `& &mut &T` are not local types. -/
theorem refute_enum_v2_nested_ref_arm_pointee :
isSupportedV2LocalTy
(.ref .shared (.ref .mut (.slice (.int .i8 false)))) = false ∧
isSupportedV2LocalTy
(.ref .shared (.ref .mut (.ref .shared .bool))) = false :=
⟨rfl, rfl⟩
/-- Guard `enum_v2_nested_ref_inner_pointee`: a two-level local is exactly
two references deep over a sized pointee, so `&&&T` and `&&[T]` are not
local types. -/
theorem refute_enum_v2_nested_ref_inner_pointee :
isSupportedV2LocalTy
(.ref .shared (.ref .shared (.ref .shared (.int .i32 false)))) =
false ∧
isSupportedV2LocalTy
(.ref .shared (.ref .shared (.slice (.int .i8 false)))) = false :=
⟨rfl, rfl⟩
/-- Guard `enum_v2_owned_struct_fields`: a struct is owned data only when every
field is, so a struct with a reference field is not an admitted local. -/
theorem refute_enum_v2_owned_struct_fields :
isSupportedV2LocalTy (.struct_ ⟨7, 8, 3, [0]⟩ [.ref .shared .bool]) =
false :=
rfl
/-- Guard `enum_v2_owned_option_handle_only`: an `Option` is owned data only
of a `Vec` or `Box` handle or of plain sized data (`Option<u8>`, whose
direct-tag layout is `rustc`'s); an `Option` of a struct has no `.option`
layout (capture gives it a payload enum instead). -/
theorem refute_enum_v2_owned_option_handle_only :
isOwnedDataTyV2 (.option (.struct_ ⟨7, 1, 0, [0]⟩ [.int .i8 false])) =
false ∧
isOwnedDataTyV2 (.option (.int .i8 false)) = true :=
⟨rfl, rfl⟩
/-- Guard `enum_v2_raw_ptr_owned_pointee_refused`: a raw pointer may not point
to owned data that contains a handle, so the handle encoding stays
unobservable. -/
theorem refute_enum_v2_raw_ptr_owned_pointee_refused :
isSupportedV2LocalTy
(.rawPtr .shared (.struct_ ⟨7, 8, 3, [0]⟩ [.box .bool])) = false :=
rfl
/-- Anti-overreach canary: owned data with handles is admitted in a `Vec`, a
`Box<[_]>`, a struct, behind a reference, and as an `Option` of a handle. -/
theorem positive_enum_v2_owned_data_admitted :
isSupportedV2LocalTy (.vec (.vec (.int .i8 false))) = true ∧
isSupportedV2LocalTy
(.box (.slice (.struct_ ⟨7, 32, 0, [0]⟩
[.array (.int .i8 false) 32]))) = true ∧
isSupportedV2LocalTy (.struct_ ⟨8, 16, 3, [0]⟩ [.box (.slice .bool)]) =
true ∧
isSupportedV2LocalTy
(.ref .shared (.struct_ ⟨8, 16, 3, [0]⟩ [.box (.slice .bool)])) =
true ∧
isSupportedV2LocalTy (.option (.box .bool)) = true :=
⟨rfl, rfl, rfl, rfl, rfl⟩
/-! ### Self-recursive structs (`recTyWfV2`, `recTysAdmittedV2`) -/
/-- Layout of `struct Tree { v: u64, kids: Vec<Tree> }` (layout id `7`). -/
noncomputable def recTreeLayout : StructLayout :=
{ id := 7, size := 32, alignLog2 := 3, fieldOffsets := [24, 0] }
/-- `Tree`'s definition, with its recursive occurrence as `.recSelf 7`. -/
noncomputable def recTreeDefn : MirTy :=
.struct_ recTreeLayout [.int .i64 false, .vec (.recSelf 7)]
/-- A struct with layout id `8` used as a second binder. -/
noncomputable def recOtherLayout : StructLayout :=
{ id := 8, size := 8, alignLog2 := 3, fieldOffsets := [0] }
/-- Guard `enum_v2_rec_self_inline`: a `.recSelf` outside a `Box`/`Vec`
pointee (here an inline `Option<Self>`, which `rustc` rejects with `E0072`)
is ill-formed. -/
theorem refute_enum_v2_rec_self_inline :
recTyWfV2 none
(.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false, .option (.recSelf 7)])) = false :=
rfl
/-- Guard `enum_v2_rec_self_scope`: a back edge must name the innermost
binder, so a mutually recursive pair (`7 → Box<8> → Box<7>`) is refused. -/
theorem refute_enum_v2_rec_self_scope :
recTyWfV2 none
(.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false,
.box (.recAdt 8 (.struct_ recOtherLayout
[.box (.recSelf 7)]))])) = false :=
rfl
/-- Guard `enum_v2_rec_adt_no_rebind`: a binder may not rebind the id of an
enclosing binder. -/
theorem refute_enum_v2_rec_adt_no_rebind :
recTyWfV2 none
(.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false,
.vec (.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false, .vec (.recSelf 7)]))])) = false :=
rfl
/-- Guard `enum_v2_rec_adt_head`: a binder's definition must be the struct
whose layout id is the binder's id. -/
theorem refute_enum_v2_rec_adt_head :
recTyWfV2 none
(.recAdt 7 (.struct_ recOtherLayout [.box (.recSelf 7)])) = false :=
rfl
/-- Guard `enum_v2_rec_coherent_defs`: two binders with the same id must have
the same definition. -/
theorem refute_enum_v2_rec_coherent_defs :
recTysAdmittedV2
[.recAdt 7 recTreeDefn,
.ref .shared (.recAdt 7 (.struct_ recTreeLayout
[.int .i32 false, .vec (.recSelf 7)]))] = false :=
rfl
/-- Guard `enum_v2_rec_coherent_struct_id`: no plain struct may reuse a
binder's layout id. -/
theorem refute_enum_v2_rec_coherent_struct_id :
recTysAdmittedV2
[.recAdt 7 recTreeDefn,
.struct_ recTreeLayout [.int .i64 false, .int .i64 false]] = false :=
rfl
/-- Guard `enum_v2_rec_coherent_binder_plain`: a plain struct seen before a
binder with its layout id also makes the binders incoherent. -/
theorem refute_enum_v2_rec_coherent_binder_plain :
recTysAdmittedV2
[.struct_ recTreeLayout [.int .i64 false, .int .i64 false],
.recAdt 7 recTreeDefn] = false :=
rfl
/-- Guard `enum_nat_trie_find_odd`: odd and even binder ids are looked up in
different subtries, so two different definitions of the odd id `7`, and of
the even id `8`, are found to conflict, while the distinct ids `7` and `8`
do not. -/
theorem refute_enum_nat_trie_find_odd :
recTysAdmittedV2
[.recAdt 7 recTreeDefn,
.recAdt 7 (.struct_ recTreeLayout
[.int .i32 false, .vec (.recSelf 7)])] = false ∧
recTysAdmittedV2
[.recAdt 8 (.struct_ recOtherLayout [.box (.recSelf 8)]),
.recAdt 8 (.struct_ recOtherLayout [.vec (.recSelf 8)])] = false ∧
recTysAdmittedV2
[.recAdt 7 recTreeDefn,
.recAdt 8 (.struct_ recOtherLayout [.box (.recSelf 8)])] = true :=
⟨rfl, rfl, rfl⟩
/-- Guard `enum_nat_trie_fuel_fail_closed`: a binder id of more than 64 bits
fails closed, so binders with such an id are never accepted, even well-formed
coherent ones. -/
theorem refute_enum_nat_trie_fuel_fail_closed :
recTysAdmittedV2
[.recAdt 18446744073709551623
(.struct_ { recTreeLayout with id := 18446744073709551623 }
[.int .i64 false, .vec (.recSelf 18446744073709551623)])] =
false :=
rfl
/-- Guard `enum_v2_rec_admit_self_inline`: program admission rejects a
`.recSelf` outside a `Box`/`Vec` pointee, inside a binder or at top level
outside any binder. -/
theorem refute_enum_v2_rec_admit_self_inline :
recTysAdmittedV2
[.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false, .option (.recSelf 7)])] = false ∧
recTysAdmittedV2 [.recSelf 7] = false :=
⟨rfl, rfl⟩
/-- Guard `enum_v2_rec_admit_self_scope`: program admission requires a back
edge to name the innermost binder, so a mutually recursive pair and a
back edge outside any binder are refused. -/
theorem refute_enum_v2_rec_admit_self_scope :
recTysAdmittedV2
[.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false,
.box (.recAdt 8 (.struct_ recOtherLayout
[.box (.recSelf 7)]))])] = false ∧
recTysAdmittedV2 [.box (.recSelf 7)] = false :=
⟨rfl, rfl⟩
/-- Guard `enum_v2_rec_admit_head`: program admission requires a binder's
definition to be the struct whose layout id is the binder's id. -/
theorem refute_enum_v2_rec_admit_head :
recTysAdmittedV2
[.recAdt 7 (.struct_ recOtherLayout [.box (.recSelf 7)])] = false :=
rfl
/-- Guard `enum_v2_rec_admit_first_walk`: the definition of a binder is
walked when its id is first bound, so a back edge in it that names another
binder is refused, including when the binder appears again later. -/
theorem refute_enum_v2_rec_admit_first_walk :
recTysAdmittedV2
[.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false, .vec (.recSelf 8)])] = false ∧
recTysAdmittedV2
[.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false, .vec (.recSelf 8)]),
.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false, .vec (.recSelf 8)])] = false :=
⟨rfl, rfl⟩
/-- A payload enum `enum E { A(u8), B(Box<..>) }` with layout id `5` whose
`B` field has type `b`. -/
noncomputable def recPlainEnum (b : MirTy) : MirTy :=
.adtEnum ⟨5, 16, 3, .isize, true, [0, 1], 0, .i8, false, 0, 1, .direct⟩
[.struct_ ⟨51, 16, 3, [1]⟩ [.int .i8 false],
.struct_ ⟨52, 16, 3, [8]⟩ [.box b]]
/-- Guard `enum_v2_rec_admit_plain_repeat_struct`: a plain struct whose
layout id was already seen is still walked, so an unbound back edge in its
fields is refused. -/
theorem refute_enum_v2_rec_admit_plain_repeat_struct :
recTysAdmittedV2
[.struct_ recOtherLayout [.int .i64 false],
.struct_ recOtherLayout [.box (.recSelf 7)]] = false :=
rfl
/-- Guard `enum_v2_rec_admit_plain_repeat_enum`: likewise for a plain
payload enum whose layout id was already seen. -/
theorem refute_enum_v2_rec_admit_plain_repeat_enum :
recTysAdmittedV2
[recPlainEnum (.int .i8 false), recPlainEnum (.recSelf 7)] = false :=
rfl
/-- A binder that rebinds the id of an enclosing binder, which `recTyWfV2`
rejects, is also refused by program admission: its definition is a proper
part of the enclosing binder's, so the two binders are incoherent. -/
theorem refute_enum_v2_rec_admit_rebind :
recTysAdmittedV2
[.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false,
.vec (.recAdt 7 (.struct_ recTreeLayout
[.int .i64 false, .vec (.recSelf 7)]))])] = false :=
rfl
/-- Anti-overreach canary for program admission: a recursive struct, its use
behind a reference and a `Box`, a closed recursive struct nested in another
one and then used on its own, and an `Option<Box<Self>>` back edge are all
admitted, as are repeated plain structs and payload enums. -/
theorem positive_enum_v2_rec_admitted :
recTysAdmittedV2
[.recAdt 7 recTreeDefn,
.ref .shared (.box (.recAdt 7 recTreeDefn))] = true ∧
recTysAdmittedV2
[.recAdt 8 (.struct_ { recOtherLayout with size := 40 }
[.recAdt 7 recTreeDefn, .box (.recSelf 8)]),
.recAdt 7 recTreeDefn,
.vec (.recAdt 8 (.struct_ { recOtherLayout with size := 40 }
[.recAdt 7 recTreeDefn, .box (.recSelf 8)]))] = true ∧
recTysAdmittedV2
[.recAdt 8 (.struct_ recOtherLayout
[.option (.box (.recSelf 8))])] = true ∧
recTysAdmittedV2
[.struct_ recOtherLayout [.int .i64 false],
.struct_ recOtherLayout [.int .i64 false],
recPlainEnum (.int .i8 false),
recPlainEnum (.int .i8 false)] = true :=
⟨rfl, rfl, rfl, rfl⟩
/-- Anti-overreach canary: a well-formed recursive struct, its use behind a
reference and a `Box`, a closed recursive struct nested in another one, and
repeated coherent binders are all accepted. -/
theorem positive_enum_v2_rec_wf :
recTyWfV2 none (.recAdt 7 recTreeDefn) = true ∧
recTyWfV2 none (.ref .shared (.box (.recAdt 7 recTreeDefn))) = true ∧
recTyWfV2 none
(.recAdt 8 (.struct_ recOtherLayout
[.option (.box (.recSelf 8))])) = true ∧
recTyWfV2 none
(.recAdt 8 (.struct_ { recOtherLayout with size := 40 }
[.recAdt 7 recTreeDefn, .box (.recSelf 8)])) = true ∧
recTysAdmittedV2
[.recAdt 7 recTreeDefn, .ref .shared (.recAdt 7 recTreeDefn)] = true :=
⟨rfl, rfl, rfl, rfl, rfl⟩
/-- Canary: unfolding closes the definition, so a field projection of `Tree`
has the type `rustc` gives the field, `Vec<Tree>`. -/
theorem positive_enum_v2_rec_unfold :
unfoldRecV2 7 recTreeDefn =
.struct_ recTreeLayout [.int .i64 false, .vec (.recAdt 7 recTreeDefn)] :=
rfl
/-- One-variant payload enum holding `Vec<Vec<u8>>` (depth-2 nested buffer). -/
noncomputable def eNestedVecAdtTy : MirTy :=
.adtEnum ⟨9, 24, 3, .isize, true, [0], 0, .i8, false, 0, 0, .single⟩
[.struct_ ⟨91, 24, 3, [0]⟩ [.vec (.vec (.int .i8 false))]]
/-- One-variant payload enum holding `Vec<u8>` (depth-1 buffer only). -/
noncomputable def eFlatVecAdtTy : MirTy :=
.adtEnum ⟨10, 24, 3, .isize, true, [0], 0, .i8, false, 0, 0, .single⟩
[.struct_ ⟨101, 24, 3, [0]⟩ [.vec (.int .i8 false)]]
/-- Guard `enum_adt_nested_heap_handle`: `tyHasNestedHeapHandleV2` recurses
into `.adtEnum` variants so `valueAccessSpans` does not short-circuit nested
heap handles inside payload enums. -/
theorem refute_enum_adt_nested_heap_handle :
tyHasNestedHeapHandleV2 eNestedVecAdtTy = true ∧
tyHasNestedHeapHandleV2 eFlatVecAdtTy = false :=
⟨rfl, rfl⟩
/-- Guard `enum_ref_nested_heap_handle`: `tyHasNestedHeapHandleV2` returns
`true` for references to heap-owning pointees or slices (`&mut Vec<u8>`,
`&[Vec<u8>]`, `&Box<u64>`) while staying `false` for plain scalar references
(`&mut u8`, `&[u8]`). -/
theorem refute_enum_ref_nested_heap_handle :
tyHasNestedHeapHandleV2 (.ref .mut (.vec (.int .i8 false))) = true ∧
tyHasNestedHeapHandleV2 (.ref .shared (.slice (.vec (.int .i8 false)))) =
true ∧
tyHasNestedHeapHandleV2 (.ref .mut (.box (.int .i64 false))) = true ∧
tyHasNestedHeapHandleV2 (.ref .mut (.int .i8 false)) = false ∧
tyHasNestedHeapHandleV2 (.ref .shared (.slice (.int .i8 false))) = false :=
⟨rfl, rfl, rfl, rfl, rfl⟩
/-- Guard `enum_v2_vec_cap_valid`: a `Vec` whose capacity is above
`isize::MAX` is not a valid value. -/
theorem refute_enum_v2_vec_cap_valid :
checkValV2 (.vec ⟨some 1, 0, .isize⟩ (UInt64.ofNat 9223372036854775808) 0)
(.vec (.int .i8 false)) = false ∧
checkValV2 (.vec ⟨some 1, 0, .isize⟩ (UInt64.ofNat 9223372036854775807) 0)
(.vec (.int .i8 false)) = true :=
⟨rfl, rfl⟩
end GuardMutation