Files
Guoliang ChenandClaude Mythos 5.2 71d13b5fbe
ci / checks (push) Successful in 1m0s
ci / corpus (push) Successful in 18s
ci / linux (push) Successful in 6m4s
release / build-and-publish (push) Successful in 5m31s
Release 0.66.2: every source file names its author
Every tracked file that can carry a comment (880 of them) now opens with
"Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with
"Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love,
luciochen.com", in the file's own comment syntax. No behaviour change: the
diff outside the version bump and changelogs is comment lines only.

- tools/stamp_author.py: writes the lines; idempotent, `--check` lists
  what is missing. Headers go under a shebang and under Markdown front
  matter.
- Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON
  and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the
  LICENSE files (already carry the copyright line).
- tools/linux_check.sh: --help reads its header from line 3, now that the
  author line sits under the shebang.
- tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-24 12:11:09 -08:00

806 lines
28 KiB
Rust

// Author: Guoliang Chen, a Senior Digital Design Engineer
//! QA attacks on TODO §65.1 (rename and references through a block label).
//!
//! Every test drives the public `rename` / `find_references` over a small
//! in-memory workspace. The contract under attack: every site is rewritten,
//! or the rename is refused or warns — never a silent partial rename.
//! `docs/features/generate-label-rename/README.md`, QA log 2026-09-30.
use std::path::{Path, PathBuf};
use vlog_refactor::{find_references, rename, RefactorEdit};
fn workspace(files: &[(&str, &str)]) -> Vec<(PathBuf, String)> {
files.iter().map(|(name, text)| (path(name), text.to_string())).collect()
}
fn path(name: &str) -> PathBuf {
PathBuf::from("/rtl").join(name)
}
/// The offset of the first whole-word `name` at or after `prefix`.
fn at(text: &str, prefix: &str, name: &str) -> u32 {
let from =
text.find(prefix).unwrap_or_else(|| panic!("`{prefix}` is not in the text")) + prefix.len();
let is_word = |b: Option<&u8>| {
b.is_some_and(|b| b.is_ascii_alphanumeric() || *b == b'_' || *b == b'$' || *b == b'\\')
};
let bytes = text.as_bytes();
let mut search = from;
while let Some(hit) = text.get(search..).and_then(|s| s.find(name)) {
let start = search + hit;
let end = start + name.len();
let before = start.checked_sub(1).and_then(|i| bytes.get(i));
if (name.starts_with('\\') || !is_word(before)) && !is_word(bytes.get(end)) {
return start as u32;
}
search = start + 1;
}
panic!("`{name}` does not occur after `{prefix}`");
}
fn apply(text: &str, edit: &RefactorEdit, file: &Path) -> String {
let mut out = String::new();
let mut cursor = 0usize;
for e in edit.edits_for(file) {
out.push_str(&text[cursor..e.range.start as usize]);
out.push_str(&e.new_text);
cursor = e.range.end as usize;
}
out.push_str(&text[cursor..]);
out
}
type Renamed = Result<(Vec<String>, Vec<String>), String>;
/// Rename the `name` after `prefix` in `file`; every file's text afterwards,
/// and the warnings.
fn renamed(files: &[(&str, &str)], file: &str, prefix: &str, name: &str, new: &str) -> Renamed {
let sources = workspace(files);
let text = files.iter().find(|(n, _)| *n == file).map(|(_, t)| *t).expect("the file");
let edit = rename(&sources, &path(file), at(text, prefix, name), new)?;
let texts = files.iter().map(|(n, t)| apply(t, &edit, &path(n))).collect();
Ok((texts, edit.warnings))
}
/// `file:line:text` of every reference to the `name` after `prefix`.
fn references(files: &[(&str, &str)], file: &str, prefix: &str, name: &str) -> Vec<String> {
let sources = workspace(files);
let text = files.iter().find(|(n, _)| *n == file).map(|(_, t)| *t).expect("the file");
let found = find_references(&sources, &path(file), at(text, prefix, name), true)
.unwrap_or_else(|why| panic!("find_references refused: {why}"));
let mut out: Vec<String> = found
.iter()
.map(|(p, span)| {
let name = p.file_name().and_then(|n| n.to_str()).unwrap_or("?");
let text = files.iter().find(|(n, _)| *n == name).map(|(_, t)| *t).expect("file");
let line = text[..span.start as usize].matches('\n').count() + 1;
format!("{name}:{line}:{}", &text[span.as_range()])
})
.collect();
out.sort();
out
}
/// A rename either rewrote every spelling of `old` in code (not comments) in
/// every file — edited or not — or said something: never a silent partial.
/// Callers keep unrelated declarations of `old` out of the workspace.
fn complete_or_loud(result: &Renamed, old: &str) {
let Ok((texts, warnings)) = result else { return };
if !warnings.is_empty() {
return;
}
for text in texts {
let code: String =
text.lines().map(|l| l.split("//").next().unwrap_or("")).collect::<Vec<_>>().join("\n");
let is_word = |b: u8| b.is_ascii_alphanumeric() || b == b'_';
let left = code.match_indices(old).any(|(i, _)| {
let b = code.as_bytes();
!(i > 0 && is_word(b[i - 1])) && !b.get(i + old.len()).is_some_and(|&c| is_word(c))
});
assert!(!left, "silent partial rename, `{old}` left behind:\n{text}");
}
}
// ---------------------------------------------------------------------------
// B. Scope semantics seen through references and rename
// ---------------------------------------------------------------------------
/// Two sibling `for` loops carrying one label (illegal, seen in the wild):
/// no crash, and a rename of the label is all or nothing.
const SIBLINGS: &str = "module sib (output logic o);
for (genvar i = 0; i < 2; i++) begin : g
logic a;
end
for (genvar j = 0; j < 2; j++) begin : g
logic b;
end
assign o = g[0].a;
endmodule
";
#[test]
fn sibling_loops_with_one_label_rename_as_one_label_without_a_crash() {
let result = renamed(&[("s.sv", SIBLINGS)], "s.sv", "begin : ", "g", "gg");
let (texts, _) = result.clone().expect("renames");
assert_eq!(texts[0], SIBLINGS.replace(": g\n", ": gg\n").replace("g[0]", "gg[0]"));
complete_or_loud(&result, "g");
let found = references(&[("s.sv", SIBLINGS)], "s.sv", "o = ", "g");
assert_eq!(found, vec!["s.sv:2:g", "s.sv:5:g", "s.sv:8:g"]);
}
/// A label that shadows an outer net of the same name: inside the outer
/// block the path's head is the inner block, outside it is the net.
const SHADOW: &str = "module sh (output logic o, output logic p);
logic g_a;
assign g_a = 1'b0;
if (1) begin : outer
if (1) begin : g_a
logic q;
end
assign o = g_a.q;
end
assign p = g_a;
endmodule
";
#[test]
fn a_label_shadowing_an_outer_net_takes_only_its_own_path() {
let (texts, warnings) =
renamed(&[("sh.sv", SHADOW)], "sh.sv", "begin : ", "g_a", "g_b").expect("renames");
assert!(warnings.is_empty(), "{warnings:?}");
assert_eq!(
texts[0],
SHADOW.replace("begin : g_a", "begin : g_b").replace("o = g_a.q", "o = g_b.q")
);
let (texts, _) =
renamed(&[("sh.sv", SHADOW)], "sh.sv", "logic ", "g_a", "n_a").expect("the net renames");
assert_eq!(
texts[0],
SHADOW
.replace("logic g_a;", "logic n_a;")
.replace("assign g_a", "assign n_a")
.replace("p = g_a;", "p = n_a;")
);
}
/// A declaration inside an **unnamed** generate `if` stays invisible outside
/// it; one inside `generate … endgenerate` is a name of the module.
#[test]
fn an_unnamed_generate_if_still_hides_its_declarations() {
let text = "module u (output logic o, output logic p);
generate
wire in_region;
if (1) begin
wire hidden;
end
endgenerate
assign o = in_region;
assign p = hidden;
endmodule
";
let found = references(&[("u.sv", text)], "u.sv", "wire ", "in_region");
assert_eq!(found, vec!["u.sv:3:in_region", "u.sv:8:in_region"]);
let found = references(&[("u.sv", text)], "u.sv", "wire ", "hidden");
assert_eq!(found, vec!["u.sv:5:hidden"], "the outer `hidden` is an implicit net, not this one");
}
/// Escaped labels: the label, its closing label and a path through it.
#[test]
fn an_escaped_label_is_renamed_with_its_path() {
let text = "module e (output logic o);
for (genvar i = 0; i < 2; i++) begin : \\g$lane
logic q;
end : \\g$lane
assign o = \\g$lane [0].q;
endmodule
";
let result = renamed(&[("e.sv", text)], "e.sv", "begin : ", "\\g$lane", "g_ln");
match &result {
Ok((texts, _)) => {
assert!(!texts[0].contains("\\g$lane"), "every escaped spelling moves:\n{}", texts[0]);
}
Err(why) => assert!(!why.is_empty()),
}
}
/// A label written in both arms of an `ifdef`: one name, all or nothing.
const IFDEF: &str = "module d (output logic o);
`ifdef WIDE
for (genvar i = 0; i < 4; i++) begin : g_w
logic q;
end : g_w
`else
for (genvar i = 0; i < 2; i++) begin : g_w
logic q;
end : g_w
`endif
assign o = g_w[0].q;
endmodule
";
#[test]
fn a_label_inside_an_ifdef_is_renamed_in_every_arm_or_the_rename_says_so() {
let result = renamed(&[("d.sv", IFDEF)], "d.sv", "o = ", "g_w", "g_v");
complete_or_loud(&result, "g_w");
if let Ok((texts, _)) = &result {
assert_eq!(texts[0], IFDEF.replace("g_w", "g_v"));
}
}
/// A label that is used only by its closing label.
#[test]
fn a_label_used_only_by_its_closing_label_moves_with_it() {
let text = "module c (input logic clk);
if (1) begin : g_only
logic q;
end : g_only
always_ff @(posedge clk) begin : p_only
end : p_only
endmodule
";
let (texts, _) = renamed(&[("c.sv", text)], "c.sv", "end : ", "g_only", "g_x").expect("g");
assert_eq!(texts[0], text.replace("g_only", "g_x"));
let (texts, _) = renamed(&[("c.sv", text)], "c.sv", "begin : ", "p_only", "p_x").expect("p");
assert_eq!(texts[0], text.replace("p_only", "p_x"));
}
/// Named blocks inside a function and a task, with `disable`.
#[test]
fn named_blocks_in_functions_and_tasks_rename_with_disable_and_closing_label() {
let text = "module f;
function automatic int ff(int a);
begin : fblk
if (a == 0) disable fblk;
ff = a;
end : fblk
endfunction
task automatic tt();
begin : tblk
disable tblk;
end : tblk
endtask
endmodule
";
let (texts, _) = renamed(&[("f.sv", text)], "f.sv", "disable ", "fblk", "f2").expect("f");
assert_eq!(texts[0], text.replace("fblk", "f2"));
let (texts, _) = renamed(&[("f.sv", text)], "f.sv", "end : ", "tblk", "t2").expect("t");
assert_eq!(texts[0], text.replace("tblk", "t2"));
}
/// Nested `g_a.g_b[2].x` from another file: every level renames.
const NSUB: &str = "module nsub;
if (1) begin : g_a
for (genvar j = 0; j < 4; j++) begin : g_b
logic x;
end : g_b
end : g_a
endmodule
";
const NTOP: &str = "module ntop (output logic o);
nsub u_n ();
assign o = u_n.g_a.g_b[2].x;
endmodule
";
#[test]
fn every_level_of_a_nested_cross_file_path_renames() {
let files = [("nsub.sv", NSUB), ("ntop.sv", NTOP)];
for (prefix, name, new) in
[("begin : ", "g_a", "g_top"), ("g_a\n for", "g_b", "g_bit"), ("logic ", "x", "xx")]
{
let (texts, warnings) = renamed(&files, "nsub.sv", prefix, name, new).expect(name);
assert!(warnings.is_empty(), "{name}: {warnings:?}");
let word = |t: &str| {
t.replace(&format!(": {name}\n"), &format!(": {new}\n"))
.replace(&format!(".{name}"), &format!(".{new}"))
.replace(&format!("logic {name};"), &format!("logic {new};"))
};
assert_eq!(texts[1], word(NTOP), "{name}");
assert!(!texts[0].contains(&format!(" {name}\n")), "{name}:\n{}", texts[0]);
// Started from the other file, the same edit.
let other = renamed(&files, "ntop.sv", "u_n.", name, new).expect(name);
assert_eq!(other.0, texts, "{name}");
}
}
/// A generate `case` with the same label in several arms, each with a
/// closing label.
#[test]
fn a_case_label_in_three_arms_renames_all_arms_and_the_path() {
let text = "module k #(parameter int P = 0) (output logic o);
case (P)
0: begin : g_c
logic z;
end : g_c
1: begin : g_c
logic z;
end : g_c
default: begin : g_c
logic z;
end : g_c
endcase
assign o = g_c.z;
endmodule
";
for start in ["0: begin : ", "1: begin : ", "default: begin : ", "o = "] {
let (texts, _) = renamed(&[("k.sv", text)], "k.sv", start, "g_c", "g_d").expect(start);
assert_eq!(texts[0], text.replace("g_c", "g_d"), "from `{start}`");
}
}
/// Legal SystemVerilog: both arms of a generate `if` are labelled `g`, and a
/// member exists only in the second one (IEEE 1800 §27.5 — only one arm is
/// elaborated, so `g.b` is valid whenever the `else` arm is).
const ARMS: &str = "module a #(parameter bit P = 0) (output logic o, output logic p);
if (P) begin : g
logic a;
end else begin : g
logic b;
end
assign o = g.a;
assign p = g.b;
endmodule
";
#[test]
fn a_member_only_the_second_arm_declares_is_never_silently_left_behind() {
let result = renamed(&[("a.sv", ARMS)], "a.sv", "logic ", "b", "b2");
complete_or_loud(&result, "b");
}
#[test]
fn a_member_both_arms_declare_is_never_silently_split() {
// `g.x` means the first arm's `x` or the second's depending on `P`:
// renaming one arm's `x` and the path leaves the other arm without it.
let text = "module b2 #(parameter bit P = 0) (output logic o);
if (P) begin : g
logic x;
end else begin : g
logic x;
end
assign o = g.x;
endmodule
";
let result = renamed(&[("b.sv", text)], "b.sv", "logic ", "x", "y");
complete_or_loud(&result, "x");
// Lead's ruling on F2: a warning naming the other arm's declaration; no
// refusal and no extra rewrite.
let (texts, warnings) = result.expect("renames");
assert_eq!(texts[0], text.replacen("logic x;", "logic y;", 1).replace("g.x", "g.y"));
assert!(
warnings.iter().any(|w| w.contains("b.sv:5") && w.contains("another arm labelled `g`")),
"{warnings:?}"
);
}
// ---------------------------------------------------------------------------
// Element selects on things that are not generate arrays
// ---------------------------------------------------------------------------
const PKG: &str = "package pk;
typedef struct packed { logic f; logic g; } pkt_t;
typedef pkt_t arr_t [2];
endpackage
";
#[test]
fn an_element_select_resolves_only_where_the_element_type_is_the_recorded_type() {
let text = "module s import pk::*; (output logic o, output logic p, output logic q);
pkt_t [1:0] packed_arr;
pkt_t unpacked_arr [2];
logic [3:0] vec;
assign o = packed_arr[0].f;
assign p = unpacked_arr[1].f;
assign q = vec[0].f;
endmodule
";
let files = [("pk.sv", PKG), ("s.sv", text)];
let found = references(&files, "pk.sv", "packed { logic ", "f");
// The packed array's element is a `pkt_t`: a reference. The others must
// never resolve to the struct's `f` through a type they do not have.
assert!(found.contains(&"s.sv:5:f".to_string()), "{found:?}");
assert!(
!found.contains(&"s.sv:7:f".to_string()),
"`vec[0].f` is not a struct field: {found:?}"
);
// Renaming the field is complete or loud about the unpacked array.
let result = renamed(&files, "pk.sv", "packed { logic ", "f", "ff");
complete_or_loud(&result, "f");
}
#[test]
fn an_instance_array_and_an_interface_array_path_is_not_a_wrong_answer() {
let leaf = "module leaf (output logic sig);
assign sig = 1'b0;
endmodule
interface bus_if;
logic sig;
endinterface
";
let top = "module top (output logic o, output logic p);
leaf u_arr [2] (.sig());
bus_if b_arr [2] ();
assign o = u_arr[1].sig;
assign p = b_arr[0].sig;
endmodule
";
let files = [("leaf.sv", leaf), ("top.sv", top)];
let found = references(&files, "leaf.sv", "interface bus_if;\n logic ", "sig");
for f in &found {
assert!(!f.starts_with("top.sv:4"), "`u_arr[1].sig` is leaf's port, not bus_if's: {f}");
}
let found = references(&files, "leaf.sv", "output logic ", "sig");
for f in &found {
assert!(!f.starts_with("top.sv:5"), "`b_arr[0].sig` is bus_if's, not leaf's: {f}");
}
}
// ---------------------------------------------------------------------------
// C. Rename attacks
// ---------------------------------------------------------------------------
#[test]
fn renaming_a_label_to_a_name_the_enclosing_scope_declares_is_refused() {
let text = "module r (output logic o);
logic taken;
if (1) begin : g_x
logic q;
end
for (genvar i = 0; i < 2; i++) begin : g_y
logic q;
end
assign o = taken;
endmodule
";
for (prefix, label) in [("begin : ", "g_x"), ("begin : g_x", "g_y")] {
let why = renamed(&[("r.sv", text)], "r.sv", prefix, label, "taken");
assert!(why.is_err(), "`{label}` → `taken` collides in the module: {why:?}");
// and to a sibling block's label
let other = if label == "g_x" { "g_y" } else { "g_x" };
let why = renamed(&[("r.sv", text)], "r.sv", prefix, label, other);
assert!(why.is_err(), "`{label}` → `{other}` collides with the other block: {why:?}");
}
// A net renamed to a block's label collides too.
let why = renamed(&[("r.sv", text)], "r.sv", "logic ", "taken", "g_x");
assert!(why.is_err(), "`taken` → `g_x` collides with the block label: {why:?}");
}
#[test]
fn a_label_renamed_there_and_back_restores_every_byte() {
let files = [("nsub.sv", NSUB), ("ntop.sv", NTOP)];
let (once, _) = renamed(&files, "nsub.sv", "g_a\n for", "g_b", "g_q").expect("there");
let back_files = [("nsub.sv", once[0].as_str()), ("ntop.sv", once[1].as_str())];
let (twice, _) = renamed(&back_files, "ntop.sv", "u_n.g_a.", "g_q", "g_b").expect("back");
assert_eq!(twice, vec![NSUB.to_string(), NTOP.to_string()]);
}
#[test]
fn a_net_inside_a_block_renamed_through_a_bind_and_a_path_from_two_files() {
let sub = "module sub (input logic clk);
for (genvar i = 0; i < 2; i++) begin : g_lane
logic sig;
assign sig = clk;
end
endmodule
";
let top = "module top (input logic clk, output logic o);
sub u_x (.clk);
assign o = u_x.g_lane[1].sig;
endmodule
";
let tb = "module tb;
logic clk, o;
top dut (.clk, .o);
initial $display(\"%b\", dut.u_x.g_lane[0].sig);
endmodule
";
let files = [("sub.sv", sub), ("top.sv", top), ("tb.sv", tb)];
let (texts, warnings) = renamed(&files, "tb.sv", "g_lane[0].", "sig", "sig_q").expect("ok");
assert!(warnings.is_empty(), "{warnings:?}");
assert_eq!(
texts[0],
sub.replace("logic sig;", "logic sig_q;").replace("assign sig ", "assign sig_q ")
);
assert_eq!(texts[1], top.replace(".sig", ".sig_q"));
assert_eq!(texts[2], tb.replace(".sig)", ".sig_q)"));
}
/// The opentitan DV shape (`keymgr_dpe_if.sv:514`, `force
/// tb.dut.gen_sw_assigns[i].hw_key_sel_buf[0] = …`): a path rooted at a
/// top-level module declared in another file. lucisv records nothing for it
/// (0.59.0 neither, for a net), and the file is not edited, so the stray
/// scan never looks at it either — the rename is silently partial.
#[test]
fn a_path_rooted_at_another_files_top_module_is_never_silently_left_behind() {
let dut = "module dut_a;
for (genvar i = 0; i < 2; i++) begin : g_sw
logic buf_q;
end
endmodule
";
let tb = "module tb;
dut_a dut ();
endmodule
";
let probe = "module probe (output logic o);
assign o = tb.dut.g_sw[0].buf_q;
endmodule
";
let files = [("dut_a.sv", dut), ("tb.sv", tb), ("probe.sv", probe)];
complete_or_loud(&renamed(&files, "dut_a.sv", "begin : ", "g_sw", "g_hw"), "g_sw");
complete_or_loud(&renamed(&files, "dut_a.sv", "logic ", "buf_q", "buf_r"), "buf_q");
// TODO §65.4 (superseding the lead's F1 ruling, which warned): the root
// `tb` is the top module `tb.sv` declares (IEEE 1800 §23.6), so the path
// resolves and is renamed with the declaration — and nothing is warned.
for (prefix, old, new) in [("begin : ", "g_sw", "g_hw"), ("logic ", "buf_q", "buf_r")] {
let (texts, warnings) = renamed(&files, "dut_a.sv", prefix, old, new).expect("renames");
assert_eq!(texts[0], dut.replace(old, new), "the declaration moves");
assert_eq!(texts[1], tb, "the testbench does not spell the name");
assert_eq!(texts[2], probe.replace(old, new), "the path moves with it");
assert!(warnings.is_empty(), "{warnings:?}");
}
}
#[test]
fn a_same_named_block_in_another_module_is_never_touched() {
let sub = "module sub (input logic clk);
for (genvar i = 0; i < 2; i++) begin : g_lane
logic sig;
end : g_lane
endmodule
";
let other = sub.replace("module sub", "module other");
let top = "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
";
let files = [("sub.sv", sub), ("other.sv", other.as_str()), ("top.sv", top)];
let (texts, warnings) = renamed(&files, "top.sv", "u_x.", "g_lane", "g_ln").expect("renames");
assert!(warnings.is_empty(), "{warnings:?}");
assert_eq!(texts[0], sub.replace("g_lane", "g_ln"));
assert_eq!(texts[1], other, "`other`'s block is another declaration");
assert_eq!(texts[2], top.replace("u_x.g_lane", "u_x.g_ln"));
}
#[test]
fn renaming_a_label_to_its_loops_genvar_is_refused_when_a_path_inside_would_be_captured() {
let text = "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
";
let why = renamed(&[("i.sv", text)], "i.sv", "begin : ", "g_lane", "gi");
assert!(why.is_err(), "`gi[0].sig` inside the body would name the genvar: {why:?}");
}
// ---------------------------------------------------------------------------
// Re-check 2026-09-30: the rework's unresolved-path and sibling-arm warnings
// ---------------------------------------------------------------------------
const RC_DUT: &str = "module dut_a;
for (genvar i = 0; i < 2; i++) begin : g_sw
logic buf_q;
end
endmodule
";
const RC_OTHER: &str = "module dut_b;
for (genvar i = 0; i < 2; i++) begin : g_sw
logic buf_q;
end
endmodule
";
const RC_TB: &str = "module tb;
dut_a dut ();
dut_b other ();
endmodule
";
/// Every shape a path through `g_sw` rooted at another file's `tb` takes.
const RC_PROBE: &str = "module probe (output logic o);
initial force tb.dut.g_sw[0].buf_q = 1'b0;
initial release tb.dut.g_sw[0].buf_q;
initial $assertoff(0, tb.dut.g_sw[1]);
assign o = tb.dut
.g_sw[1]
.buf_q;
// tb.dut.g_sw[0].buf_q in a comment
initial $display(\"tb.dut.g_sw[0]\");
unknown_mod u_unk (.g_sw(1'b0));
endmodule
";
fn rc_files() -> Vec<(&'static str, &'static str)> {
vec![("dut_a.sv", RC_DUT), ("dut_b.sv", RC_OTHER), ("tb.sv", RC_TB), ("probe.sv", RC_PROBE)]
}
/// The set without `tb.sv`: nothing declares the root `tb`, so no path
/// rooted at it can resolve (TODO §65.4 — with `tb.sv` in the set they do).
fn rc_files_without_the_root() -> Vec<(&'static str, &'static str)> {
rc_files().into_iter().filter(|(name, _)| *name != "tb.sv").collect()
}
#[test]
fn recheck_every_unresolved_path_shape_is_named_once_with_its_lines() {
let (texts, warnings) =
renamed(&rc_files_without_the_root(), "dut_a.sv", "begin : ", "g_sw", "g_hw")
.expect("renames");
assert_eq!(texts[2], RC_PROBE, "the probe is never edited");
let said: Vec<&String> = warnings.iter().filter(|w| w.contains("probe.sv")).collect();
assert_eq!(said.len(), 1, "one line for the file: {warnings:?}");
assert!(said[0].starts_with("probe.sv:2,3,4,6:"), "{said:?}");
}
/// TODO §65.4: with the root declared, every path shape is renamed and the
/// comment, the string and the connection to an unknown module are not.
#[test]
fn every_path_shape_rooted_at_a_declared_top_module_is_renamed() {
let (texts, warnings) =
renamed(&rc_files(), "dut_a.sv", "begin : ", "g_sw", "g_hw").expect("renames");
let code: Vec<&str> = RC_PROBE.lines().collect();
let want: Vec<String> = code
.iter()
.enumerate()
.map(
|(i, l)| {
if [1, 2, 3, 5].contains(&i) {
l.replace("g_sw", "g_hw")
} else {
l.to_string()
}
},
)
.collect();
assert_eq!(texts[3], want.join("\n") + "\n");
assert_eq!(texts[1], RC_OTHER, "`dut_b`'s block is another declaration");
assert!(warnings.is_empty(), "{warnings:?}");
}
#[test]
fn recheck_a_path_that_resolves_to_another_declaration_is_not_warned_about() {
let user = "module user (output logic o);
dut_b u_b ();
assign o = u_b.g_sw[0].buf_q;
endmodule
";
let mut files = rc_files();
files.push(("user.sv", user));
let (_, warnings) = renamed(&files, "dut_a.sv", "begin : ", "g_sw", "g_hw").expect("renames");
assert!(!warnings.iter().any(|w| w.contains("user.sv")), "{warnings:?}");
}
#[test]
fn recheck_an_included_fragment_is_scanned_like_any_file() {
let frag = " initial force tb.dut.g_sw[0].buf_q = 1'b1;\n";
let host = "module host;\n`include \"frag.svh\"\nendmodule\n";
let mut files = rc_files_without_the_root();
files.push(("frag.svh", frag));
files.push(("host.sv", host));
let (_, warnings) = renamed(&files, "dut_a.sv", "begin : ", "g_sw", "g_hw").expect("renames");
assert!(warnings.iter().any(|w| w.starts_with("frag.svh:1:")), "{warnings:?}");
// TODO §65.4: with the root declared, the fragment's path is renamed.
let mut files = rc_files();
files.push(("frag.svh", frag));
files.push(("host.sv", host));
let (texts, _) = renamed(&files, "dut_a.sv", "begin : ", "g_sw", "g_hw").expect("renames");
assert_eq!(texts[4], frag.replace("g_sw", "g_hw"));
}
/// The word census holds no escaped identifier (`words.rs`: "an escaped
/// identifier is not a word"), so `paths.rs`'s census filter drops the file.
#[test]
fn recheck_an_escaped_label_behind_an_unresolved_root_is_named() {
let dut = "module dut_e;\n if (1) begin : \\g$sw \n logic q;\n end\nendmodule\n";
let tb = "module tbe;\n dut_e dut ();\nendmodule\n";
let probe = "module pe (output logic o);\n assign o = tbe.dut.\\g$sw .q;\nendmodule\n";
// Without `tbe.sv` nothing declares the root, and the path is named.
let files = [("dut_e.sv", dut), ("pe.sv", probe)];
let result = renamed(&files, "dut_e.sv", "begin : ", "\\g$sw", "g_hw");
complete_or_loud(&result, "\\g$sw");
let (_, warnings) = result.expect("renames");
assert!(warnings.iter().any(|w| w.starts_with("pe.sv:2:")), "{warnings:?}");
// TODO §65.4: with it, the escaped segment is renamed.
let files = [("dut_e.sv", dut), ("tbe.sv", tb), ("pe.sv", probe)];
let (texts, warnings) =
renamed(&files, "dut_e.sv", "begin : ", "\\g$sw", "g_hw").expect("renames");
assert_eq!(texts[2], probe.replace("\\g$sw", "g_hw"), "{warnings:?}");
}
#[test]
fn recheck_sibling_arm_warnings_in_every_arm_shape() {
let three = "module t3 #(parameter int P = 0) (output logic o);
if (P == 0) begin : g
logic x;
end else if (P == 1) begin : g
logic x;
end else begin : g
logic x;
end
assign o = g.x;
endmodule
";
let case = "module tc #(parameter int P = 0) (output logic o);
case (P)
0: begin : g
logic x;
end
1: begin : g
logic x;
end
default: begin : g
logic x;
end
endcase
assign o = g.x;
endmodule
";
let ifdef = "module td (output logic o);
`ifdef A
if (1) begin : g
logic x;
end
`else
if (1) begin : g
logic x;
end
`endif
assign o = g.x;
endmodule
";
for (name, text, prefix, others) in [
("t3.sv", three, "P == 0) begin : g\n logic ", vec![5, 7]),
("t3.sv", three, "P == 1) begin : g\n logic ", vec![3, 7]),
("tc.sv", case, "0: begin : g\n logic ", vec![7, 10]),
("tc.sv", case, "default: begin : g\n logic ", vec![4, 7]),
("td.sv", ifdef, "`ifdef A\n if (1) begin : g\n logic ", vec![8]),
] {
let (_, warnings) = renamed(&[(name, text)], name, prefix, "x", "y").expect(name);
for line in others {
let at = format!("{name}:{line},");
assert!(
warnings.iter().any(|w| w.contains(&at) && w.contains("another arm")),
"{name} {prefix:?}: no warning for line {line}: {warnings:?}"
);
}
}
}
/// Nested arms: `g.h.x` reaches the `h` inside *either* arm of `g`. The
/// sibling scan looks only at the arms of the innermost label, so renaming
/// the `x` under `g`'s `else` arm leaves `g.h.x` (which resolves to the first
/// arm's) and says nothing — and renaming the first one warns about its
/// `h`-sibling at line 6 but not about line 10.
#[test]
fn recheck_sibling_arms_of_an_enclosing_label_are_warned_about() {
let nested = "module tn #(parameter bit P = 0, parameter bit Q = 0) (output logic o);
if (P) begin : g
if (Q) begin : h
logic x;
end else begin : h
logic x;
end
end else begin : g
if (1) begin : h
logic x;
end
end
assign o = g.h.x;
endmodule
";
let result = renamed(&[("tn.sv", nested)], "tn.sv", "if (1) begin : h\n logic ", "x", "y");
complete_or_loud(&result, "x");
let (_, warnings) =
renamed(&[("tn.sv", nested)], "tn.sv", "if (Q) begin : h\n logic ", "x", "y")
.expect("renames");
assert!(warnings.iter().any(|w| w.contains("tn.sv:10,")), "{warnings:?}");
}
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com