//! QA re-attack (TODO §65.1, CLAUDE.md invariant 4) on the rename verifier //! over block labels, beyond the dev's `qa_block_paths`: corrupted outputs //! that rename an unrelated block of the same name, rebind a path to another //! module's block, split a same-label pair of arms, or let a genvar capture //! a path written inside the loop. Each lexes and parses; each must be //! refused. `docs/features/generate-label-rename/README.md`, QA log. use std::path::{Path, PathBuf}; use vlog_parser::span::TextRange; use super::verify; use crate::rename::Scope; use crate::trace::VerifyTrace; use crate::TextEdit; /// Two modules, each with its own `g_lane`, both instantiated in `top`. const SUB: &str = "module sub (input logic clk); for (genvar i = 0; i < 2; i++) begin : g_lane logic sig; assign sig = clk; end : g_lane endmodule "; const OTHER: &str = "module other (input logic clk); for (genvar i = 0; i < 2; i++) begin : g_lane logic sig; assign sig = clk; end : g_lane endmodule "; const TOP: &str = "module top (input logic clk, output logic o, output logic p); sub u_x (.clk); other u_y (.clk); assign o = u_x.g_lane[1].sig; assign p = u_y.g_lane[0].sig; endmodule "; /// Both arms of a generate `if` carry one label, each closed by it. const ARMS: &str = "module arms #(parameter bit P = 0) (output logic o); if (P) begin : g_arm logic q; end : g_arm else begin : g_arm logic q; end : g_arm assign o = g_arm.q; endmodule "; /// A path to the loop's own block written inside the loop's body. const INSIDE: &str = "module inside (output logic o); for (genvar gi = 0; gi < 2; gi++) begin : g_lane logic sig; assign sig = g_lane[0].sig; end assign o = g_lane[1].sig; endmodule "; /// A rename's edit at the `n`th (0-based) whole-word `name` in `text`. fn at_word(text: &str, name: &str, n: usize, new: &str) -> TextEdit { let is_word = |b: Option<&u8>| b.is_some_and(|b| b.is_ascii_alphanumeric() || *b == b'_'); let bytes = text.as_bytes(); let start = text .match_indices(name) .map(|(i, _)| i) .filter(|&i| { !is_word(i.checked_sub(1).and_then(|p| bytes.get(p))) && !is_word(bytes.get(i + name.len())) }) .nth(n) .expect("occurrence"); TextEdit { range: TextRange::new(start as u32, (start + name.len()) as u32), new_text: new.into(), } } /// `verify` over `files` with the given edits, keyed by file name. fn verdict( files: &[(&str, &str)], edits: &[(&str, Vec)], old: &str, new: &str, ) -> Result<(), String> { let sources: Vec<(PathBuf, String)> = files.iter().map(|(name, text)| (Path::new("/rtl").join(name), text.to_string())).collect(); let mut scope = Scope::build(&sources); let mut keyed: Vec<(usize, Vec)> = edits .iter() .map(|(name, edits)| { let i = scope.index_of(&Path::new("/rtl").join(name)).expect("in scope"); (i, edits.clone()) }) .collect(); keyed.sort_by_key(|(i, _)| *i); verify(&mut scope, &keyed, old, new, &mut VerifyTrace::default()) } fn three() -> Vec<(&'static str, &'static str)> { vec![("sub.sv", SUB), ("other.sv", OTHER), ("top.sv", TOP)] } /// The honest rename of `sub`'s `g_lane`: its label, its closing label and /// `u_x`'s path — never `other`'s block or `u_y`'s path. fn honest() -> Vec<(&'static str, Vec)> { vec![ ("sub.sv", (0..2).map(|n| at_word(SUB, "g_lane", n, "g_ln")).collect()), ("top.sv", vec![at_word(TOP, "g_lane", 0, "g_ln")]), ] } #[test] fn the_honest_rename_of_one_modules_block_verifies() { assert_eq!(verdict(&three(), &honest(), "g_lane", "g_ln"), Ok(())); } /// Not a corruption, and pinned as such: a rename that *also* moves an /// unrelated same-named block together with every use of it changes no /// meaning, and the verifier is a meaning check — it accepts it exactly as it /// accepts the same shape for a net (`sig` in both modules). That the engine /// never produces this edit is pinned by `tests/qa_generate_label.rs` /// (`a_same_named_block_in_another_module_is_never_touched`). #[test] fn a_rename_that_also_moved_the_other_modules_block_consistently_is_meaning_preserving() { let mut edits = honest(); edits.push(("other.sv", (0..2).map(|n| at_word(OTHER, "g_lane", n, "g_ln")).collect())); edits[1].1.push(at_word(TOP, "g_lane", 1, "g_ln")); assert_eq!(verdict(&three(), &edits, "g_lane", "g_ln"), Ok(())); let mut nets = vec![ ("sub.sv", (0..2).map(|n| at_word(SUB, "sig", n, "sg")).collect::>()), ("top.sv", vec![at_word(TOP, "sig", 0, "sg")]), ]; nets.push(("other.sv", (0..2).map(|n| at_word(OTHER, "sig", n, "sg")).collect())); nets[1].1.push(at_word(TOP, "sig", 1, "sg")); assert_eq!(verdict(&three(), &nets, "sig", "sg"), Ok(()), "the same for a net"); } #[test] fn a_rename_that_moved_the_other_instances_path_instead_is_rejected() { let edits = vec![ ("sub.sv", (0..2).map(|n| at_word(SUB, "g_lane", n, "g_ln")).collect()), ("top.sv", vec![at_word(TOP, "g_lane", 1, "g_ln")]), ]; assert!(verdict(&three(), &edits, "g_lane", "g_ln").is_err(), "`u_y.g_ln` names nothing"); } #[test] fn a_rename_of_one_arm_of_a_same_label_pair_is_rejected() { // The first arm's labels and the path move; the second arm keeps // `g_arm`, so the pair no longer names one block. let edits = vec![( "arms.sv", vec![0, 1, 4].into_iter().map(|n| at_word(ARMS, "g_arm", n, "g_x")).collect(), )]; assert!(verdict(&[("arms.sv", ARMS)], &edits, "g_arm", "g_x").is_err()); // …and the second arm's closing label alone left behind. let edits = vec![("arms.sv", (0..3).chain([4]).map(|n| at_word(ARMS, "g_arm", n, "g_x")).collect())]; assert!(verdict(&[("arms.sv", ARMS)], &edits, "g_arm", "g_x").is_err()); // The honest one: all five. let edits = vec![("arms.sv", (0..5).map(|n| at_word(ARMS, "g_arm", n, "g_x")).collect())]; assert_eq!(verdict(&[("arms.sv", ARMS)], &edits, "g_arm", "g_x"), Ok(())); } #[test] fn a_rename_to_the_genvar_that_captures_the_path_inside_the_loop_is_rejected() { // `g_lane` → `gi`: inside the body `gi[0].sig` would name the genvar. let edits = vec![("inside.sv", (0..3).map(|n| at_word(INSIDE, "g_lane", n, "gi")).collect())]; assert!(verdict(&[("inside.sv", INSIDE)], &edits, "g_lane", "gi").is_err()); }