//! Extracting a selection of whole module items into a module of its own — //! TODO §53.1. //! //! Three groups. What the extraction *writes* (a combinational region, a //! sequential one, one carrying a parameter and an import, the two homes the //! new module can have); every **refusal**, one test per sentence; and the //! acceptance checks that say the result is real Verilog — it parses, the //! formatter accepts it, formatting it twice changes nothing, and the two //! files together name every signal the one file named. use std::path::{Path, PathBuf}; use vlog_refactor::{extract_module, extract_site, Home, RefactorEdit}; const M: &str = "/rtl/m.sv"; fn files(text: &str) -> Vec<(PathBuf, String)> { vec![(PathBuf::from(M), text.to_string())] } fn workspace(entries: &[(&str, &str)]) -> Vec<(PathBuf, String)> { entries.iter().map(|(p, t)| (PathBuf::from(*p), t.to_string())).collect() } /// The byte range of `needle` in `text`. fn at(text: &str, needle: &str) -> (u32, u32) { let start = text.find(needle).expect("needle") as u32; (start, start + needle.len() as u32) } /// One file's text after the edit. A file the extraction creates starts empty. fn applied(text: &str, edit: &RefactorEdit, path: &str) -> String { let created = edit.created.iter().any(|created| created == Path::new(path)); let mut out = match created { true => String::new(), false => text.to_string(), }; for one in edit.edits_for(Path::new(path)).iter().rev() { out.replace_range(one.range.start as usize..one.range.end as usize, &one.new_text); } out } /// Extract `needle` out of `text` into `name`, into a file of its own. fn extract(text: &str, needle: &str, name: &str) -> Result { let (start, end) = at(text, needle); extract_module(&files(text), Path::new(M), start, end, name, Home::NewFile) } /// The two halves of an extraction, as the editor would have them. fn halves(text: &str, needle: &str, name: &str) -> (String, String) { let edit = extract(text, needle, name).expect("extracts"); let new = format!("/rtl/{name}.sv"); (applied(text, &edit, M), applied(text, &edit, &new)) } /// Every result has to be Verilog the formatter accepts and leaves alone the /// second time — the acceptance bar every refactoring in this crate meets. fn accepted(label: &str, text: &str) { let config = vlog_formatter::Config::default(); let once = vlog_formatter::format_source(text, &config) .unwrap_or_else(|e| panic!("the formatter refuses {label}: {e}\n{text}")); let twice = vlog_formatter::format_source(&once, &config).expect("formats again"); assert_eq!(once, twice, "formatting {label} does not converge"); } // ---------------------------------------------------------------------- // what it writes // ---------------------------------------------------------------------- const COMB: &str = "\ module m ( input logic [7:0] a, input logic [7:0] b, output logic [7:0] q ); logic [7:0] s; assign s = a + b; // the sum assign q = s ^ 8'hFF; endmodule "; #[test] fn a_combinational_region_reads_two_names_and_drives_one() { let (old, new) = halves(COMB, "assign s = a + b; // the sum", "sum"); assert_eq!( new, "\ module sum ( input logic [7:0] a, input logic [7:0] b, output logic [7:0] s ); assign s = a + b; // the sum endmodule " ); assert!(old.contains("sum u_sum (\n .a(a),\n .b(b),\n .s(s)\n );"), "{old}"); // The comment travelled: it is in the new file and gone from the old one. assert!(!old.contains("// the sum"), "{old}"); accepted("the new module", &new); accepted("what is left", &old); } const SEQ: &str = "\ module m ( input logic clk, input logic rst_n, input logic [7:0] d, output logic [7:0] q ); logic [7:0] r; always_ff @(posedge clk or negedge rst_n) begin if (!rst_n) r <= '0; else r <= d; end assign q = r; endmodule "; #[test] fn a_sequential_region_makes_the_clock_an_input_and_the_register_an_output() { let start = SEQ.find("always_ff").expect("always") as u32; let end = SEQ.find(" end\n").expect("end") as u32 + 5; let edit = extract_module(&files(SEQ), Path::new(M), start, end, "reg_stage", Home::NewFile) .expect("extracts"); let new = applied(SEQ, &edit, "/rtl/reg_stage.sv"); assert!(new.contains("input logic clk,"), "{new}"); assert!(new.contains("input logic rst_n,"), "{new}"); assert!(new.contains("input logic [7:0] d,"), "{new}"); assert!(new.contains("output logic [7:0] r"), "{new}"); accepted("the sequential module", &new); accepted("what is left", &applied(SEQ, &edit, M)); } const PARAM: &str = "\ package w_pkg; typedef logic [7:0] word_t; endpackage module m #( parameter int W = 8 ) ( input word_t a, output word_t q ); import w_pkg::*; localparam int MASK = W - 1; word_t t; assign t = a & word_t'(MASK); assign q = t; endmodule "; #[test] fn a_region_using_a_parameter_and_an_import_carries_both() { let (old, new) = halves(PARAM, "assign t = a & word_t'(MASK);", "masker"); // The `localparam` the selection reads becomes a parameter of the new // module, and so does the parameter *its own default* names. assert!(new.contains("parameter int W = 8,"), "{new}"); assert!(new.contains("parameter int MASK = W - 1"), "{new}"); assert!(new.contains("import w_pkg::*;"), "{new}"); assert!(new.contains("input word_t a,"), "{new}"); assert!(new.contains("output word_t t"), "{new}"); assert!(old.contains(".W(W),"), "{old}"); assert!(old.contains(".MASK(MASK)"), "{old}"); accepted("the parameterized module", &new); accepted("what is left", &old); } #[test] fn a_parameter_a_ports_own_width_names_crosses_with_it() { // The corpus found this one: `binary_to_gray`'s `N` is read by no // statement the selection carries — only by the *type* of the port the // extraction is about to write — so a closure over the region's // references alone left the new module naming nothing. let text = "\ module m #( parameter int N = -1 ) ( input logic [N-1:0] a, output logic [N-1:0] q ); assign q = a ^ (a >> 1); endmodule "; let (old, new) = halves(text, "assign q = a ^ (a >> 1);", "gray"); assert!(new.contains("parameter int N = -1"), "{new}"); assert!(new.contains("input logic [N-1:0] a,"), "{new}"); assert!(new.contains("output logic [N-1:0] q"), "{new}"); assert!(old.contains(".N(N)"), "{old}"); accepted("the new module", &new); accepted("what is left", &old); } #[test] fn a_wildcard_import_in_the_header_is_carried_across() { // The opentitan trial found this one: the binder records a wildcard // import in a reference's `paths` as `p::*` — the spelling the source // wrote — not as `p::`, so a judgement matching on the latter // copied no import at all and the new file named a package's types with // nothing importing them. let pkg = "package p;\n typedef logic [1:0] w_t;\n parameter int W = 2;\nendpackage\n"; let top = "\ module m import p::*; ( input logic [W-1:0] a, output w_t q ); assign q = w_t'(a); endmodule "; let sources = workspace(&[("/rtl/p.sv", pkg), (M, top)]); let (start, end) = at(top, "assign q = w_t'(a);"); let edit = extract_module(&sources, Path::new(M), start, end, "caster", Home::NewFile) .expect("extracts"); let new = applied(top, &edit, "/rtl/caster.sv"); assert!(new.contains("import p::*;"), "{new}"); assert!(new.contains("input logic [W-1:0] a,"), "{new}"); assert!(new.contains("output w_t q"), "{new}"); assert!(edit.warnings.iter().any(|w| w.contains("`p::*`")), "{:?}", edit.warnings); accepted("the new module", &new); } #[test] fn a_named_import_is_carried_across_only_when_the_name_it_supplies_is_needed() { let a = "package a_pkg;\n typedef logic [1:0] a_t;\nendpackage\n"; let b = "package b_pkg;\n typedef logic [3:0] b_t;\nendpackage\n"; let top = "\ module m ( input logic [1:0] i, output logic [1:0] o ); import a_pkg::a_t; import b_pkg::b_t; assign o = a_t'(i); endmodule "; let sources = workspace(&[("/rtl/a.sv", a), ("/rtl/b.sv", b), (M, top)]); let (start, end) = at(top, "assign o = a_t'(i);"); let edit = extract_module(&sources, Path::new(M), start, end, "caster", Home::NewFile) .expect("extracts"); let new = applied(top, &edit, "/rtl/caster.sv"); assert!(new.contains("import a_pkg::a_t;"), "{new}"); assert!(!new.contains("b_pkg"), "an import nothing moved needs is not copied: {new}"); accepted("the new module", &new); } #[test] fn a_type_declared_in_the_module_that_a_crossing_ports_type_names_is_refused() { let text = "\ module m ( input logic [7:0] i, output logic [7:0] o ); typedef logic [7:0] local_t; local_t mid; assign mid = i; assign o = mid; endmodule "; let message = refusal(text, "assign mid = i;"); assert!(message.contains("`local_t`"), "{message}"); } #[test] fn a_client_that_cannot_create_files_gets_the_module_below_the_one_it_came_from() { let (start, end) = at(COMB, "assign s = a + b; // the sum"); let edit = extract_module(&files(COMB), Path::new(M), start, end, "sum", Home::SameFile) .expect("extracts"); assert!(edit.created.is_empty(), "nothing is created"); assert_eq!(edit.file_count(), 1, "one file is touched"); let text = applied(COMB, &edit, M); assert!(text.contains("endmodule\n\nmodule sum ("), "{text}"); assert!(edit.warnings.iter().any(|w| w.contains("did not say it can create files"))); accepted("the one-file result", &text); } #[test] fn the_new_module_is_named_in_the_warnings_with_the_file_it_went_to() { let edit = extract(COMB, "assign s = a + b; // the sum", "sum").expect("extracts"); assert_eq!(edit.created, vec![PathBuf::from("/rtl/sum.sv")]); assert!(edit.warnings.iter().any(|w| w.contains("`sum.sv`")), "{:?}", edit.warnings); assert!(edit.warnings.iter().any(|w| w.contains("3 ports")), "{:?}", edit.warnings); } #[test] fn an_instantiation_whose_module_the_index_knows_gives_its_outputs_the_right_direction() { let sub = "module sub (\n input logic i,\n output logic o\n);\nendmodule\n"; let top = "\ module m ( input logic i, output logic o ); logic mid; sub u_sub (.i(i), .o(mid)); assign o = mid; endmodule "; let sources = workspace(&[("/rtl/sub.sv", sub), (M, top)]); let (start, end) = at(top, "sub u_sub (.i(i), .o(mid));"); let edit = extract_module(&sources, Path::new(M), start, end, "wrap", Home::NewFile) .expect("extracts"); let new = applied(top, &edit, "/rtl/wrap.sv"); assert!(new.contains("input logic i,"), "{new}"); assert!(new.contains("output logic mid"), "{new}"); accepted("the wrapper", &new); } // ---------------------------------------------------------------------- // the refusals // ---------------------------------------------------------------------- /// The sentence an extraction of `needle` refuses with. fn refusal(text: &str, needle: &str) -> String { extract(text, needle, "n").expect_err("a refusal") } #[test] fn half_an_item_is_refused() { let (start, _) = at(COMB, "assign s = a + b;"); let edit = extract_module(&files(COMB), Path::new(M), start, start + 8, "n", Home::NewFile); assert!(edit.expect_err("a refusal").contains("cuts an item in half")); } #[test] fn a_selection_outside_every_module_is_refused() { let text = "package p;\n typedef int t;\nendpackage\n"; let (start, end) = at(text, "typedef int t;"); let edit = extract_module(&files(text), Path::new(M), start, end, "n", Home::NewFile); assert!(edit.expect_err("a refusal").contains("not inside one module's body")); } /// The same three items, written as each of the three declarations the parser /// gives one node to. const INTERFACE: &str = "interface bus_if;\n logic [7:0] a;\n logic [7:0] q;\n assign q = a;\nendinterface\n"; const PROGRAM: &str = "program p;\n logic a;\n logic q;\n assign q = a;\nendprogram\n"; const MODULE: &str = "module m;\n logic [7:0] a;\n logic [7:0] q;\n assign q = a;\nendmodule\n"; #[test] fn a_selection_inside_an_interface_or_a_program_is_refused() { // `vlog_parser` writes a `module`, an `interface` and a `program` as one // `ItemKind::Module`, and only `Module::kw` says which — so the gate has // to ask. QA's finding V2: without it an `interface` body extracted into a // module instantiated inside what remained an `interface`, and four names // in opentitan's `aes_masking_reseed_if.sv` stopped resolving. let said = refusal(INTERFACE, "assign q = a;"); assert!(said.contains("inside an `interface`"), "{said}"); assert!(said.contains("signals are its surface"), "{said}"); let said = refusal(PROGRAM, "assign q = a;"); assert!(said.contains("inside a `program`"), "{said}"); assert!(said.contains("testbench code"), "{said}"); // The same three items written as a module still extract. assert!(extract(MODULE, "assign q = a;", "n").is_ok(), "a `module` is still extractable"); } #[test] fn the_action_is_not_offered_inside_an_interface_or_a_program() { // The cheap gate reads the same `Region`, so the lightbulb never appears // where the engine would refuse. for text in [INTERFACE, PROGRAM] { let (start, end) = at(text, "assign q = a;"); assert!(extract_site(text, start, end).is_none(), "not offered in: {text}"); } let (start, end) = at(MODULE, "assign q = a;"); assert!(extract_site(MODULE, start, end).is_some(), "and still offered in a module"); } #[test] fn a_port_a_parameter_a_genvar_and_a_bind_are_each_refused_by_name() { let non_ansi = "module m (a);\n input a;\n wire b;\n assign b = a;\nendmodule\n"; assert!(refusal(non_ansi, "input a;").contains("a port declaration")); let param = "module m;\n localparam int W = 8;\n wire [W-1:0] x;\nendmodule\n"; assert!(refusal(param, "localparam int W = 8;").contains("a `localparam` declaration")); let genvar = "module m;\n genvar g;\nendmodule\n"; assert!(refusal(genvar, "genvar g;").contains("a `genvar` declaration")); let bind = "module m;\n bind m sub u_s ();\nendmodule\n"; assert!(refusal(bind, "bind m sub u_s ();").contains("a `bind` directive")); } #[test] fn a_macro_call_and_a_compiler_directive_are_refused() { let macro_item = "module m;\n `ASSERT(a, b)\n wire w;\nendmodule\n"; assert!(refusal(macro_item, "`ASSERT(a, b)").contains("macro invocation")); let directive = "module m;\n`ifdef FOO\n`endif\n wire w;\nendmodule\n"; assert!(refusal(directive, "`ifdef FOO").contains("compiler directive")); } #[test] fn an_initial_and_a_final_block_are_refused_as_testbench_code() { let initial = "module m;\n logic x;\n initial x = 1'b0;\nendmodule\n"; assert!(refusal(initial, "initial x = 1'b0;").contains("`initial` block")); let last = "module m;\n logic x;\n final $display(\"%d\", x);\nendmodule\n"; assert!(refusal(last, "final $display(\"%d\", x);").contains("`final` block")); } #[test] fn a_name_declared_inside_the_selection_and_used_outside_it_is_refused() { let text = "\ module m ( input logic a, output logic q ); wire t; assign t = a; assign q = t; endmodule "; // `t`'s declaration is in the selection; `assign q = t;` is not. let (start, _) = at(text, "wire t;"); let (_, end) = at(text, "assign t = a;"); let edit = extract_module(&files(text), Path::new(M), start, end, "n", Home::NewFile); let message = edit.expect_err("a refusal"); assert!( message.contains("move the declaration of `t` out of the selection first"), "{message}" ); } #[test] fn a_name_driven_on_both_sides_of_the_selection_is_refused() { let text = "\ module m ( input logic a, input logic b, output logic q ); always_comb q = a; always_comb q = b; endmodule "; let message = refusal(text, "always_comb q = a;"); assert!(message.contains("`q` is driven on both sides of the selection."), "{message}"); } #[test] fn a_task_or_function_the_selection_calls_from_outside_it_is_refused() { let text = "\ module m ( input logic [7:0] a, output logic [7:0] q ); function automatic logic [7:0] twice(input logic [7:0] x); return x + x; endfunction assign q = twice(a); endmodule "; let message = refusal(text, "assign q = twice(a);"); assert!(message.contains("`twice`"), "{message}"); assert!(message.contains("a function"), "{message}"); } #[test] fn a_signal_passed_to_a_routine_inside_the_selection_refuses_rather_than_guesses() { let text = "\ module m ( input logic clk, output logic [7:0] q ); always_ff @(posedge clk) begin $cast(q, 8'h01); end endmodule "; let message = refusal(text, "always_ff @(posedge clk) begin\n $cast(q, 8'h01);\n end"); assert!(message.contains("passes `q` to a task or function"), "{message}"); } #[test] fn a_hierarchical_reference_out_of_the_selection_is_refused() { let sub = "module sub (\n input logic i\n);\n logic probe;\nendmodule\n"; let top = "\ module m ( input logic i, output logic o ); sub u_sub (.i(i)); assign o = u_sub.probe; endmodule "; let sources = workspace(&[("/rtl/sub.sv", sub), (M, top)]); let (start, end) = at(top, "assign o = u_sub.probe;"); let edit = extract_module(&sources, Path::new(M), start, end, "n", Home::NewFile); let message = edit.expect_err("a refusal"); assert!(message.contains("through the design hierarchy"), "{message}"); } #[test] fn a_crossing_name_with_an_unpacked_dimension_is_refused() { let text = "\ module m ( input logic [7:0] a, output logic [7:0] q ); logic [7:0] mem [0:3]; assign q = mem[0] ^ a; endmodule "; let message = refusal(text, "assign q = mem[0] ^ a;"); assert!(message.contains("unpacked dimension"), "{message}"); } #[test] fn a_name_the_workspace_already_declares_is_refused() { let sub = "module taken;\nendmodule\n"; let sources = workspace(&[("/rtl/lib/taken.sv", sub), (M, COMB)]); let (start, end) = at(COMB, "assign s = a + b; // the sum"); let edit = extract_module(&sources, Path::new(M), start, end, "taken", Home::NewFile); assert!(edit.expect_err("a refusal").contains("already declares `taken`")); } #[test] fn a_module_declared_twice_is_refused() { let twin = "module m;\nendmodule\n"; let sources = workspace(&[("/rtl/twin.sv", twin), (M, COMB)]); let (start, end) = at(COMB, "assign s = a + b; // the sum"); let edit = extract_module(&sources, Path::new(M), start, end, "sum", Home::NewFile); assert!(edit.expect_err("a refusal").contains("declared in more than one indexed file")); } #[test] fn a_timescale_a_new_file_would_not_inherit_is_refused_and_the_same_file_is_not() { let text = format!("`timescale 1ns/1ps\n{COMB}"); let (start, end) = at(&text, "assign s = a + b; // the sum"); let sources = vec![(PathBuf::from(M), text.clone())]; let refused = extract_module(&sources, Path::new(M), start, end, "sum", Home::NewFile); let message = refused.expect_err("a refusal"); assert!(message.contains("`timescale"), "{message}"); // The same directive is no obstacle when the module stays in the file. extract_module(&sources, Path::new(M), start, end, "sum", Home::SameFile) .expect("the same file inherits it"); } #[test] fn a_default_nettype_is_refused_the_same_way() { let text = format!("`default_nettype none\n{COMB}"); let (start, end) = at(&text, "assign s = a + b; // the sum"); let sources = vec![(PathBuf::from(M), text.clone())]; let refused = extract_module(&sources, Path::new(M), start, end, "sum", Home::NewFile); assert!(refused.expect_err("a refusal").contains("`default_nettype")); } #[test] fn a_name_that_is_not_an_identifier_is_refused_before_anything_else() { let (start, end) = at(COMB, "assign s = a + b; // the sum"); for name in ["", "2fast", "\\odd-name", "with space"] { let edit = extract_module(&files(COMB), Path::new(M), start, end, name, Home::NewFile); assert!( edit.expect_err("a refusal").contains("not a plain SystemVerilog identifier"), "{name}" ); } } #[test] fn a_positional_instantiation_inside_the_selection_is_refused() { let sub = "module sub (\n input logic i,\n output logic o\n);\nendmodule\n"; let top = "module m (\n input logic i,\n output logic o\n);\n sub u_sub (i, o);\nendmodule\n"; let sources = workspace(&[("/rtl/sub.sv", sub), (M, top)]); let (start, end) = at(top, "sub u_sub (i, o);"); let edit = extract_module(&sources, Path::new(M), start, end, "n", Home::NewFile); assert!(edit.expect_err("a refusal").contains("by position")); } #[test] fn a_wildcard_instantiation_inside_the_selection_is_refused() { let sub = "module sub (\n input logic i,\n output logic o\n);\nendmodule\n"; let top = "module m (\n input logic i,\n output logic o\n);\n sub u_sub (.*);\nendmodule\n"; let sources = workspace(&[("/rtl/sub.sv", sub), (M, top)]); let (start, end) = at(top, "sub u_sub (.*);"); let edit = extract_module(&sources, Path::new(M), start, end, "n", Home::NewFile); assert!(edit.expect_err("a refusal").contains("`.*`")); } #[test] fn an_instantiation_of_a_module_the_index_does_not_know_is_refused() { let top = "\ module m ( input logic i, output logic o ); stranger u_s (.i(i), .o(o)); endmodule "; let message = refusal(top, "stranger u_s (.i(i), .o(o));"); assert!(message.contains("no single indexed declaration"), "{message}"); } #[test] fn a_name_declared_outside_the_module_in_the_same_file_is_refused_for_a_new_file() { let text = "\ typedef logic [7:0] byte_t; module m ( input byte_t a, output byte_t q ); byte_t t; assign t = a; assign q = t; endmodule "; let message = refusal(text, "assign t = a;"); assert!(message.contains("declared outside `m` in the same file"), "{message}"); // The same selection is fine when the module stays in that file. let (start, end) = at(text, "assign t = a;"); extract_module(&files(text), Path::new(M), start, end, "n", Home::SameFile) .expect("the same file sees it"); } #[test] fn a_selection_that_covers_nothing_is_refused() { let (start, _) = at(COMB, "module m"); let edit = extract_module(&files(COMB), Path::new(M), start, start, "n", Home::NewFile); assert!(edit.expect_err("a refusal").contains("no module item")); } #[test] fn a_file_that_does_not_parse_is_refused() { let text = "module m;\n assign = ;\nendmodule\n"; let edit = extract_module(&files(text), Path::new(M), 0, text.len() as u32, "n", Home::NewFile); assert!(edit.expect_err("a refusal").contains("does not parse")); } #[test] fn an_occupied_file_name_is_refused() { let sources = workspace(&[("/rtl/sum.sv", "// notes\n"), (M, COMB)]); let (start, end) = at(COMB, "assign s = a + b; // the sum"); let edit = extract_module(&sources, Path::new(M), start, end, "sum", Home::NewFile); assert!(edit.expect_err("a refusal").contains("already a `sum.sv`")); } // ---------------------------------------------------------------------- // the cheap gate // ---------------------------------------------------------------------- #[test] fn the_gate_names_the_module_and_everything_the_engine_will_read() { let text = "\ module m ( input logic i, output logic o ); import p::*; sub u_sub (.i(i), .o(o)); endmodule "; let (start, end) = at(text, "sub u_sub (.i(i), .o(o));"); let site = extract_site(text, start, end).expect("a site"); assert_eq!(site.module, "m"); assert_eq!(site.default_name, "m_extracted"); assert_eq!(site.units, vec!["m".to_string(), "sub".to_string(), "p".to_string()]); } #[test] fn the_gate_is_empty_where_the_engine_would_refuse_for_want_of_an_item() { assert!(extract_site(COMB, 0, 3).is_none(), "outside every module"); assert!(extract_site("not verilog at all", 0, 4).is_none(), "an unparsed buffer"); } #[test] fn a_cursor_with_no_selection_extracts_the_item_it_is_on() { let at_assign = COMB.find("assign s").expect("assign") as u32 + 3; let edit = extract_module(&files(COMB), Path::new(M), at_assign, at_assign, "sum", Home::NewFile) .expect("extracts"); let new = applied(COMB, &edit, "/rtl/sum.sv"); assert!(new.contains("assign s = a + b;"), "{new}"); } // ---------------------------------------------------------------------- // the census: the design still names what it named // ---------------------------------------------------------------------- #[test] fn the_two_files_together_name_every_signal_the_one_file_named() { // The reference census the corpus trials run, in miniature: the names the // design writes down are the same before and after, because an extraction // moves items and adds a header, and adds no name that was not there. let start = SEQ.find("always_ff").expect("always") as u32; let end = SEQ.find(" end\n").expect("end") as u32 + 5; let edit = extract_module(&files(SEQ), Path::new(M), start, end, "reg_stage", Home::NewFile) .expect("extracts"); let old = applied(SEQ, &edit, M); let new = applied(SEQ, &edit, "/rtl/reg_stage.sv"); let before = words(SEQ); let after: Vec = words(&old).into_iter().chain(words(&new)).collect(); for name in ["clk", "rst_n", "d", "q", "r"] { let was = before.iter().filter(|w| *w == name).count(); let now = after.iter().filter(|w| *w == name).count(); assert!(now >= was, "`{name}` was named {was} times and is now named {now}"); } } /// Every identifier-shaped word of a text. fn words(text: &str) -> Vec { let mut out = Vec::new(); let mut word = String::new(); for c in text.chars() { if c.is_alphanumeric() || c == '_' { word.push(c); } else if !word.is_empty() { out.push(std::mem::take(&mut word)); } } if !word.is_empty() { out.push(word); } out } #[test] fn a_crossing_member_access_names_the_head_and_the_field_second() { // QA's LOW: `b.data` and `s.f` cross the boundary as `b` and `s` — the // port and the variable — and a sentence leading with the field reads as // if the field were a signal of `m`'s own. let iface = "interface bus_if;\n logic data;\n modport mp (output data);\nendinterface\nmodule m (bus_if.mp b, input logic a);\n logic q;\n assign b.data = a;\n assign q = a;\nendmodule\n"; let said = refusal(iface, "assign b.data = a;"); assert!(said.contains("uses `b.data`"), "{said}"); assert!(said.contains("`b` is declared with a type written outside `m`"), "{said}"); let packed = "typedef struct packed {\n logic f;\n logic g;\n} s_t;\nmodule m (input logic a, output s_t s);\n logic q;\n assign s.f = a;\n assign q = a;\nendmodule\n"; let said = refusal(packed, "assign s.f = a;"); assert!(said.contains("uses `s.f`"), "{said}"); assert!(said.contains("`s` is declared with a type written outside `m`"), "{said}"); }