From 8f57a0cdd0c20354e171869d1e9128f74bc18755 Mon Sep 17 00:00:00 2001 From: Qian Wang Date: Sun, 21 Dec 2025 22:05:13 +0000 Subject: [PATCH] Seed each function with a bunch of variables --- generate/src/generation/mod.rs | 15 +++++++++++++++ generate/src/place_select.rs | 5 +++++ generate/src/ty.rs | 8 ++++++++ 3 files changed, 28 insertions(+) diff --git a/generate/src/generation/mod.rs b/generate/src/generation/mod.rs index 801b237..1a10abe 100644 --- a/generate/src/generation/mod.rs +++ b/generate/src/generation/mod.rs @@ -481,6 +481,16 @@ impl GenerationCtx { local } + // Declare a bunch of variables when we first start a function + fn declare_seed_vars(&mut self) { + for tyid in self + .ty_weights + .choose_tys(self.rng.get_mut(), &self.tcx, self.tcx.len() / 3) + { + self.declare_new_var(Mutability::Mut, tyid); + } + } + #[allow(dead_code)] fn generate_storage_live(&self) -> Result { let local = self @@ -1084,6 +1094,9 @@ impl GenerationCtx { args, return_dest, ); + + self.declare_seed_vars(); + new_fn } @@ -1108,6 +1121,8 @@ impl GenerationCtx { self.pt .enter_fn0(&self.program.functions[self.cursor.function]); + + self.declare_seed_vars(); } // Returns from the currnt function. Returns false if we're returning from diff --git a/generate/src/place_select.rs b/generate/src/place_select.rs index 8e41605..1cd9948 100644 --- a/generate/src/place_select.rs +++ b/generate/src/place_select.rs @@ -179,6 +179,11 @@ impl PlaceSelector { pt.reachable_nodes().filter(move |ppath| { let index = ppath.target_index(); + // Don't generate anything of unit type + if pt.ty(index) == TyCtxt::UNIT { + return false; + } + // Well-typedness if let Some(tys) = &self.tys && !tys.contains(&pt.ty(index)) diff --git a/generate/src/ty.rs b/generate/src/ty.rs index 71d6df0..75d6960 100644 --- a/generate/src/ty.rs +++ b/generate/src/ty.rs @@ -93,6 +93,14 @@ impl TySelect { .nth(self.weights.sample(rng)) .expect("tyctxt isn't empty") } + + pub fn choose_tys(&self, rng: &mut impl Rng, tcx: &TyCtxt, count: usize) -> Vec { + (&self.weights) + .sample_iter(rng) + .take(count) + .map(|tyid| tcx.indices().nth(tyid).expect("tyctxt isn't empty")) + .collect() + } } fn new_composite(tcx: &mut TyCtxt, rng: &mut impl Rng) {