Files
lucisv/crates/vlog_refactor/src/check/qa_block_attacks.rs
T
lucioandClaude Opus 5.5 7794d3938b TODO §65.1: rename and references through a generate block label; rename warnings and mentions scoped to the renamed declaration
A path segment naming a block (bare, indexed, nested, cross-file) is now a
reference to its label: vlog_semantic declares a `for` block's label in the
enclosing scope, makes `generate … endgenerate` no scope, gives a block a
member scope, walks an index in a path, and files blocks on a module's
hierarchical surface under `module.label` (FACTS_FORMAT 21). The LSP, CLI
and MCP rename/references get it through the engine; lucisv-mcp's `block`
refusal is lifted. No verifier normalization (attacks in
check/qa_block_paths.rs, qa_block_attacks.rs).

Reworks after QA: unresolved path segments warned in every file of the set;
sibling-arm members warned; a label beside a net is a duplicate-declaration;
function-local block keys never collide; file names path-qualified when base
names collide; rename warnings and lucisv-mcp mentions scoped to the
declaration's home set (rename/home.rs, scoped.rs), falling back to unscoped
on any doubt. TODO §65.4 and §65.6 filed.

docs/features/generate-label-rename. Workspace lib/bins/tests: 79 suites,
5 064 passed, 0 failed, 51 ignored (guard exit 0).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-07 01:01:23 -07:00

176 lines
6.4 KiB
Rust

//! 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<TextEdit>)],
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<TextEdit>)> = 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<TextEdit>)> {
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::<Vec<_>>()),
("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());
}