Files
Guoliang ChenandClaude Mythos 5.2 532a64546c Round 8: semantic and refactor edge cases (#12, #31, #32, L-5/L-6/L-10)
DPI-export names are semantic references. verify_move reconstructs the
result independently and checks equation 3 per edit, with 16 attack tests.
Interface generate-block members are reachable through a port or virtual
interface; bind to a bare instance name is found; nested struct members
get their container key. Escaped identifiers join FileFacts::words
(FACTS_FORMAT 23); generate-scoped bind shorthand resolves. Scope reuses
index_order. Files topo #68-#70.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-26 17:18:59 -08:00

484 lines
24 KiB
Rust

// Author: Guoliang Chen, a Senior Digital Design Engineer
//! QA attacks on the rename's implicit-connection rewrite (TODO-MCP M3.5):
//! `.name` and `.*` written out explicitly when a rename would cut them.
//!
//! Every test drives the public `rename` over a little in-memory workspace
//! and asserts the whole text of each file afterwards, or the refusal's
//! sentence — the harness of `tests/rename_implicit.rs`. The verifier-direct
//! attacks (crafted corrupted edits handed to `check::verify`) need the
//! crate-internal entry point and live in `src/check/qa_implicit.rs`.
use std::path::{Path, PathBuf};
use vlog_refactor::{rename, RefactorEdit};
fn workspace(files: &[(&str, &str)]) -> Vec<(PathBuf, String)> {
files.iter().map(|(name, text)| (PathBuf::from("/rtl").join(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_ok = !is_word(start.checked_sub(1).and_then(|i| bytes.get(i)));
if before_ok && (name.ends_with(' ') || !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 Outcome = 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) -> Outcome {
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))
}
/// The same, over already-renamed texts (for the round trip).
fn renamed_owned(
files: &[(&str, String)],
file: &str,
prefix: &str,
name: &str,
new: &str,
) -> Outcome {
let borrowed: Vec<(&str, &str)> = files.iter().map(|(n, t)| (*n, t.as_str())).collect();
renamed(&borrowed, file, prefix, name, new)
}
const LEAF: &str = "module leaf (input logic clk, input logic [7:0] d, output logic [7:0] q);\n always_ff @(posedge clk) q <= d;\nendmodule\n";
// ----------------------------------------------------------------------
// port rename: list shapes
// ----------------------------------------------------------------------
#[test]
fn qa_port_rename_over_shorthand_wildcard_explicit_and_mixed_lists() {
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n leaf u_a (.clk, .d, .q);\n leaf u_b (.*);\n leaf u_c (.clk(clk), .d(d), .q(q));\n leaf u_d (.clk, .*);\n leaf u_e (.d, .*);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top)];
let (texts, warnings) = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect("renames");
let want = top
.replace("u_a (.clk, .d, .q)", "u_a (.clk, .din(d), .q)")
.replace("u_b (.*)", "u_b (.*, .din(d))")
.replace("u_c (.clk(clk), .d(d)", "u_c (.clk(clk), .din(d)")
.replace("u_d (.clk, .*)", "u_d (.clk, .*, .din(d))")
.replace("u_e (.d, .*)", "u_e (.din(d), .*)");
assert_eq!(texts[1], want);
// One sentence per rewrite: u_a and u_e shorthands, u_b and u_d wildcards.
assert_eq!(warnings.len(), 4, "{warnings:?}");
for (inst, how) in
[("u_a", "shorthand"), ("u_b", "`.*`"), ("u_d", "`.*`"), ("u_e", "shorthand")]
{
assert!(
warnings
.iter()
.any(|w| w.starts_with(&format!("`{inst}` at top.sv:")) && w.contains(how)),
"{inst}: {warnings:?}"
);
}
}
#[test]
fn qa_two_instances_in_one_instantiation_each_get_their_connection() {
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n leaf u_a (.*), u_b (.clk, .d, .q());\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top)];
let (texts, _) = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect("renames");
assert_eq!(
texts[1],
top.replace("u_a (.*)", "u_a (.*, .din(d))")
.replace("u_b (.clk, .d,", "u_b (.clk, .din(d),")
);
let (texts, _) = renamed(&files, "top.sv", "[7:0] ", "d", "x").expect("renames");
assert_eq!(
texts[1],
top.replace("[7:0] d", "[7:0] x")
.replace("u_a (.*)", "u_a (.*, .d(x))")
.replace("u_b (.clk, .d,", "u_b (.clk, .d(x),")
);
}
#[test]
fn qa_an_instance_array_with_a_wildcard_is_written_out() {
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q [3:0]);\n leaf u[3:0] (.*);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top)];
let (texts, _) = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect("renames");
assert_eq!(texts[1], top.replace("(.*)", "(.*, .din(d))"));
let (texts, _) = renamed(&files, "top.sv", "[7:0] ", "d", "x").expect("renames");
assert_eq!(texts[1], top.replace("[7:0] d", "[7:0] x").replace("(.*)", "(.*, .d(x))"));
}
#[test]
fn qa_a_non_ansi_port_is_written_out() {
let leaf = "module leaf (clk, d, q);\n input logic clk;\n input logic [7:0] d;\n output logic [7:0] q;\n always_ff @(posedge clk) q <= d;\nendmodule\n";
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n leaf u_a (.*);\n leaf u_b (.clk, .d, .q);\nendmodule\n";
let files = [("leaf.sv", leaf), ("top.sv", top)];
let (texts, _) = renamed(&files, "leaf.sv", "(clk, ", "d", "din").expect("renames");
assert_eq!(
texts[1],
top.replace("u_a (.*)", "u_a (.*, .din(d))")
.replace("u_b (.clk, .d,", "u_b (.clk, .din(d),")
);
}
// ----------------------------------------------------------------------
// layout
// ----------------------------------------------------------------------
#[test]
fn qa_one_per_line_with_a_line_comment_on_the_last_element() {
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n leaf u_leaf (\n .clk (clk), // clock\n .* // the rest\n );\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top)];
let (texts, _) = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect("renames");
assert_eq!(
texts[1],
top.replace(" .* // the rest\n", " .*, // the rest\n .din(d)\n")
);
}
#[test]
fn qa_an_inline_list_with_a_trailing_line_comment() {
// An inline list whose `)` is on the next line: the comma goes before the
// comment, the connection on its own line (at column 0 — the `.*` is not
// first on its line, so there is no indentation to copy).
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n leaf u_leaf (.* // all\n );\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top)];
let (texts, _) = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect("renames");
assert_eq!(texts[1], top.replace("(.* // all\n", "(.*, // all\n.din(d)\n"));
}
#[test]
fn qa_an_attribute_and_a_block_comment_on_the_last_element_survive() {
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n leaf u_leaf ((* keep *) .clk, (* dont_touch *) .* /* rest */);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top)];
let (texts, _) = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect("renames");
assert_eq!(texts[1], top.replace(".* /* rest */)", ".* /* rest */, .din(d))"));
}
#[test]
fn qa_an_escaped_net_is_written_out_with_its_terminating_blank() {
let leaf = "module leaf (input logic clk, input logic \\d$x );\nendmodule\n";
let top = "module top (input logic clk, input logic \\d$x );\n leaf u_a (.*);\n leaf u_b (.clk, .\\d$x );\nendmodule\n";
let files = [("leaf.sv", leaf), ("top.sv", top)];
// The parent's escaped net renamed to a plain name.
let (texts, _) = renamed(&files, "top.sv", "logic ", "\\d$x ", "dd").expect("renames");
assert_eq!(
texts[1],
// The blank that ended the escaped name stays where it was.
top.replacen("logic \\d$x )", "logic dd )", 1)
.replace("u_a (.*)", "u_a (.*, .\\d$x (dd))")
.replace("u_b (.clk, .\\d$x )", "u_b (.clk, .\\d$x (dd) )")
);
}
// ----------------------------------------------------------------------
// hierarchy: nested wildcards, bind, interfaces, generate
// ----------------------------------------------------------------------
#[test]
fn qa_a_leaf_port_under_nested_wildcards_touches_only_the_direct_parent() {
let mid = "module mid (input logic clk, input logic [7:0] d, output logic [7:0] q);\n leaf u_leaf (.*);\nendmodule\n";
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n mid u_mid (.*);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("mid.sv", mid), ("top.sv", top)];
let (texts, warnings) = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect("renames");
assert_eq!(texts[1], mid.replace("(.*)", "(.*, .din(d))"));
assert_eq!(texts[2], top, "top's `.*` binds mid's `d`, which did not move");
assert_eq!(warnings.len(), 1, "{warnings:?}");
}
#[test]
fn qa_a_net_rename_reaches_a_bind_in_a_file_spelling_neither_name() {
// The whole-set caller: binds.sv spells neither `d` nor `x`, so neither
// the watched-file rule nor a name search finds it — only the bind walk.
let bind = "module binds;\n bind top leaf u_b (.*);\nendmodule\n";
let top =
"module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top), ("binds.sv", bind)];
let (texts, warnings) = renamed(&files, "top.sv", "[7:0] ", "d", "x").expect("renames");
assert_eq!(texts[1], top.replace("[7:0] d", "[7:0] x"));
assert_eq!(texts[2], bind.replace("(.*)", "(.*, .d(x))"));
assert_eq!(
warnings,
vec!["`u_b` at binds.sv:2 bound `d` through `.*`; added `.d(x)`.".to_string()]
);
}
#[test]
fn qa_a_net_rename_behind_a_hierarchical_bind_is_refused() {
let bind = "module binds;\n bind top.u_x leaf u_b (.*);\nendmodule\n";
let top =
"module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top), ("binds.sv", bind)];
let refusal = renamed(&files, "top.sv", "[7:0] ", "d", "x").expect_err("refused");
assert!(
refusal.contains("`u_b` at binds.sv:2 may bind `d` implicitly through `.*`"),
"{refusal}"
);
}
#[test]
fn qa_a_bind_with_an_instance_list_target() {
// `bind top : u_t leaf ...` binds into top's instances named `u_t`; its
// scope is still `top`'s.
let bind = "module binds;\n bind top : u_t leaf u_b (.*);\nendmodule\n";
let top =
"module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top), ("binds.sv", bind)];
let (texts, warnings) = renamed(&files, "top.sv", "[7:0] ", "d", "x").expect("renames");
assert_eq!(texts[2], bind.replace("(.*)", "(.*, .d(x))"));
assert_eq!(warnings.len(), 1, "{warnings:?}");
}
#[test]
fn qa_an_interface_port_bound_by_wildcard_is_written_out() {
let bus = "interface bus_if;\n logic v;\nendinterface\n";
let sub = "module sub (bus_if bus, input logic clk);\nendmodule\n";
let top = "module top (input logic clk);\n bus_if bus ();\n sub u_sub (.*);\nendmodule\n";
let files = [("bus_if.sv", bus), ("sub.sv", sub), ("top.sv", top)];
let (texts, warnings) = renamed(&files, "sub.sv", "bus_if ", "bus", "b").expect("renames");
assert_eq!(texts[1], sub.replace("bus_if bus,", "bus_if b,"));
assert_eq!(texts[2], top.replace("(.*)", "(.*, .b(bus))"));
assert_eq!(warnings.len(), 1, "{warnings:?}");
}
#[test]
fn qa_a_generate_shadow_is_left_to_its_own_net() {
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n if (1) begin : g\n logic [7:0] d;\n leaf u_g (.*);\n leaf u_h (.clk, .d, .q());\n end\n leaf u_leaf (.*);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top)];
// The module-level port: only the instance outside the block sees it.
let (texts, _) = renamed(&files, "top.sv", "[7:0] ", "d", "x").expect("renames");
assert_eq!(
texts[1],
top.replacen("[7:0] d", "[7:0] x", 1).replace("u_leaf (.*)", "u_leaf (.*, .d(x))")
);
// The block's own net: only the instances inside it.
let (texts, _) = renamed(&files, "top.sv", " logic [7:0] ", "d", "y").expect("renames");
assert_eq!(
texts[1],
top.replace(" logic [7:0] d;", " logic [7:0] y;")
.replace("u_g (.*)", "u_g (.*, .d(y))")
.replace("u_h (.clk, .d,", "u_h (.clk, .d(y),")
);
}
// ----------------------------------------------------------------------
// refusals
// ----------------------------------------------------------------------
#[test]
fn qa_a_port_rename_into_a_module_declared_twice_is_refused() {
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n leaf u_leaf (.*);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("leaf_copy.sv", LEAF), ("top.sv", top)];
let refusal = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect_err("refused");
assert!(refusal.contains("`leaf`") && refusal.contains("more than one"), "{refusal}");
}
#[test]
fn qa_a_port_rename_over_an_ifdef_split_wildcard_list_is_refused() {
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n leaf u_leaf (\n`ifdef A\n .clk(1'b0),\n`endif\n .*\n );\nendmodule\n";
let refusal = renamed(&[("leaf.sv", LEAF), ("top.sv", top)], "leaf.sv", "[7:0] ", "d", "din")
.expect_err("refused");
assert!(refusal.contains("split by compiler directives"), "{refusal}");
}
#[test]
fn qa_a_port_rename_a_parent_wildcard_would_capture_is_refused() {
// top declares `din` but no `d`: `.*` bound nothing to `d`, and would
// start binding top's `din` to the renamed port.
let top = "module top (input logic clk, input logic [7:0] din, output logic [7:0] q);\n leaf u_leaf (.*);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top)];
let refusal = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect_err("refused");
assert!(refusal.contains("would start binding `din`"), "{refusal}");
}
#[test]
fn qa_a_parameter_named_like_the_port_is_no_binding() {
// A parameter `d` in top shadows nothing a `.*` can bind: leave it alone.
let top = "module top #(parameter int d = 1) (input logic clk, output logic [7:0] q);\n leaf u_leaf (.*);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top)];
let (texts, warnings) = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect("renames");
assert_eq!(texts[1], top);
assert!(warnings.is_empty(), "{warnings:?}");
}
// ----------------------------------------------------------------------
// round trip
// ----------------------------------------------------------------------
#[test]
fn qa_renaming_back_restores_everything_but_the_written_out_connections() {
let top = "module top (input logic clk, input logic [7:0] d, output logic [7:0] q);\n leaf u_a (.*);\n leaf u_b (.clk, .d, .q);\nendmodule\n";
let files = [("leaf.sv", LEAF), ("top.sv", top)];
let (once, _) = renamed(&files, "leaf.sv", "[7:0] ", "d", "din").expect("renames");
let back: Vec<(&str, String)> = vec![("leaf.sv", once[0].clone()), ("top.sv", once[1].clone())];
let (twice, warnings) =
renamed_owned(&back, "leaf.sv", "[7:0] ", "din", "d").expect("renames back");
assert_eq!(twice[0], LEAF, "the declaring file is byte-identical");
// The connections stay explicit: `.*` gains `.d(d)`, `.d` became `.d(d)`.
assert_eq!(
twice[1],
top.replace("u_a (.*)", "u_a (.*, .d(d))").replace("u_b (.clk, .d,", "u_b (.clk, .d(d),")
);
assert!(warnings.is_empty(), "the explicit connections are plain references now: {warnings:?}");
}
#[test]
fn qa_a_shorthand_into_an_unreadable_module_is_still_written_out() {
// The shorthand's port half does not move, so the module need not be read.
let top =
"module top (input logic clk, input logic [7:0] d);\n ghost u_g (.clk, .d);\nendmodule\n";
let (texts, warnings) =
renamed(&[("top.sv", top)], "top.sv", "[7:0] ", "d", "x").expect("renames");
assert_eq!(texts[0], top.replace("[7:0] d", "[7:0] x").replace(".clk, .d)", ".clk, .d(x))"));
assert_eq!(warnings.len(), 1, "{warnings:?}");
}
// ----------------------------------------------------------------------
// QA findings
// ----------------------------------------------------------------------
/// QA finding 2: a `bind` whose target is a lone *instance* name (opentitan's
/// `prim/fpv/tb/prim_fifo_sync_bind_fpv.sv`: `bind i_nopass1
/// prim_fifo_sync_assert_fpv … (.clk_i, .rst_ni, .clr_i, … .err_o)`) refuses
/// the rename of every module-level declaration spelled like one of its
/// shorthands, in *any* module — renaming `prim_count`'s `err_o` or
/// `aes_core`'s `clk_i` is refused over a bind into a `prim_fifo_sync`. The
/// index knows `i_f` is an instance of `fifo`, not of `cnt`.
#[test]
fn qa_a_bind_into_an_instance_of_another_module_does_not_refuse() {
let cnt =
"module cnt (input logic clk_i, output logic err_o);\n assign err_o = 1'b0;\nendmodule\n";
let fifo =
"module fifo (input logic clk_i, output logic err_o);\n assign err_o = 1'b0;\nendmodule\n";
let check = "module fifo_assert (input logic clk_i, input logic err_o);\nendmodule\n";
let tb = "module tb;\n logic clk_i, err_o;\n fifo i_f (.clk_i, .err_o);\nendmodule\n";
let bind = "module tb_bind;\n bind i_f fifo_assert u_a (.clk_i, .err_o);\nendmodule\n";
let files =
[("cnt.sv", cnt), ("fifo.sv", fifo), ("check.sv", check), ("tb.sv", tb), ("bind.sv", bind)];
let (texts, _) = renamed(&files, "cnt.sv", "output logic ", "err_o", "error_o")
.expect("the bind targets an instance of `fifo`, which the rename does not touch");
assert_eq!(texts[4], bind);
}
// ----------------------------------------------------------------------
// M3.5 re-check (2026-09-23): the enclosing-module rule for a bind onto a
// bare instance name
// ----------------------------------------------------------------------
const CNT: &str =
"module cnt (input logic clk_i, output logic err_o);\n assign err_o = 1'b0;\nendmodule\n";
const FIFO: &str =
"module fifo (input logic clk_i, output logic err_o);\n assign err_o = 1'b0;\nendmodule\n";
const CHECK: &str = "module chk (input logic clk_i, input logic err_o);\nendmodule\n";
/// Rename `cnt`'s `err_o` over the three modules plus `extra`.
fn rename_cnt_err(extra: &[(&'static str, &'static str)]) -> Outcome {
let mut files = vec![("cnt.sv", CNT), ("fifo.sv", FIFO), ("chk.sv", CHECK)];
files.extend_from_slice(extra);
renamed(&files, "cnt.sv", "output logic ", "err_o", "error_o")
}
#[test]
fn qa_b_the_enclosing_modules_own_instance_wins_and_is_another_module() {
// A binds onto its own `u` (a fifo); B also has a `u`, a cnt. A's wins.
let a = "module a;\n logic clk_i, err_o;\n fifo u (.clk_i, .err_o);\n bind u chk c_u (.clk_i, .err_o);\nendmodule\n";
let b = "module b;\n logic clk_i, err_o;\n cnt u (.clk_i, .err_o);\nendmodule\n";
let (texts, _) = rename_cnt_err(&[("a.sv", a), ("b.sv", b)])
.expect("A's `u` is a fifo: the bind never sees cnt's err_o");
assert_eq!(texts[3], a, "a.sv untouched");
assert_eq!(texts[4], b.replace(".err_o)", ".error_o(err_o))"), "b's shorthand written out");
}
#[test]
fn qa_b_the_enclosing_modules_own_instance_wins_and_is_ours() {
// Swapped: A's own `u` is the cnt, B's is a fifo — refused, not guessed.
let a = "module a;\n logic clk_i, err_o;\n cnt u (.clk_i, .err_o);\n bind u chk c_u (.clk_i, .err_o);\nendmodule\n";
let b = "module b;\n logic clk_i, err_o;\n fifo u (.clk_i, .err_o);\nendmodule\n";
let refusal = rename_cnt_err(&[("a.sv", a), ("b.sv", b)]).expect_err("refused");
assert!(refusal.contains("`c_u` at a.sv:4"), "{refusal}");
}
#[test]
fn qa_b_a_file_scope_bind_onto_another_modules_instance_is_skipped() {
let tb = "module tb;\n logic clk_i, err_o;\n fifo u (.clk_i, .err_o);\nendmodule\n";
let bind = "bind u chk c_u (.clk_i, .err_o);\n";
let (texts, warnings) =
rename_cnt_err(&[("tb.sv", tb), ("bind.sv", bind)]).expect("only a fifo is named `u`");
assert_eq!((texts[3].as_str(), texts[4].as_str()), (tb, bind));
assert!(warnings.is_empty(), "{warnings:?}");
}
#[test]
fn qa_b_a_file_scope_bind_onto_our_instance_is_refused() {
let tb = "module tb;\n logic clk_i, err_o;\n cnt u (.clk_i, .err_o);\nendmodule\n";
let bind = "bind u chk c_u (.clk_i, .err_o);\n";
let refusal = rename_cnt_err(&[("tb.sv", tb), ("bind.sv", bind)]).expect_err("refused");
assert!(refusal.contains("`c_u` at bind.sv:1"), "{refusal}");
}
#[test]
fn qa_b_a_bind_onto_a_name_no_indexed_instance_has_is_refused() {
let bind = "module tb_bind;\n bind u_ghost chk c_u (.clk_i, .err_o);\nendmodule\n";
let refusal = rename_cnt_err(&[("bind.sv", bind)]).expect_err("refused");
assert!(refusal.contains("`c_u` at bind.sv:2"), "{refusal}");
}
/// QA note 1, closed in Round 8 (`docs/features/semantic-refactor-r8`): an
/// instance inside a generate block of the enclosing module is not the
/// enclosing module's own. Upward resolution from `a` does not see `a.g.u`,
/// so `bind u` here may reach the parent's `u` — a `cnt` — whose `.err_o`
/// binds `cnt`'s renamed port. The engine used to count `a.g.u` as visible,
/// skip the bind, and cut the shorthand; it now refuses.
#[test]
fn qa_b_a_generate_scoped_instance_is_not_the_enclosing_modules_own() {
let a = "module a;\n logic clk_i, err_o;\n if (1) begin : g\n fifo u (.clk_i, .err_o);\n end\n bind u chk c_u (.clk_i, .err_o);\nendmodule\n";
let p = "module p;\n logic clk_i, err_o;\n a u_a ();\n cnt u (.clk_i, .err_o);\nendmodule\n";
let outcome = rename_cnt_err(&[("a.sv", a), ("p.sv", p)]);
let refusal = outcome.expect_err("the bind may reach p.u, a cnt");
assert!(refusal.contains("`c_u`"), "{refusal}");
}
/// The other half of QA note 1: a `bind` written *inside* the generate block
/// sees that block's own `u` — a fifo — first, so it never reaches `p.u`.
#[test]
fn a_bind_inside_the_generate_block_sees_the_blocks_own_instance() {
let a = "module a;\n logic clk_i, err_o;\n if (1) begin : g\n fifo u (.clk_i, .err_o);\n bind u chk c_u (.clk_i, .err_o);\n end\nendmodule\n";
let p = "module p;\n logic clk_i, err_o;\n a u_a ();\n cnt u (.clk_i, .err_o);\nendmodule\n";
let (texts, _) = rename_cnt_err(&[("a.sv", a), ("p.sv", p)])
.expect("g's own `u` is a fifo: the bind never sees cnt's err_o");
assert_eq!(texts[3], a, "a.sv untouched");
}
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com