Every fix makes the AST carry a fact the formatter used to invent or discard: SensSep preserves each event-list separator as written (, stays , / or stays or, mixed lists round-trip); module-header imports keep one statement per import; Instantiation::params and Module::param_ports become Option<Vec> so empty #() survives; and seven distinct comment-motion shapes (closing paren, empty connection, declarator-to-;, after binary operator, case labels, ternary, assign-to-value) keep comments where the source put them, with Fmt::inline_comments as the single comment-run printer. verify.rs is byte-identical: the net was right on all 46 refusing files. Seven new rejection tests pin that judgment (sabotage-checked by QA). Also removes a latent split_off underflow panic (Parser::take_around) that QA reproduced on the pre-fix binary, and fixes the ternary comment refusal from the earlier bug round. Corpus after: 468/820 parse, 0 verification errors, 0 crashes — corpus_check.sh exits 0 for the first time. Exactly 46 files moved verification-error -> would-reformat by path diff; nothing else changed. 111 workspace tests (was 98), case table 237 entries, clippy silent. Dev: Opus agent; QA: Sonnet agent (all claims independently reproduced); review in docs/features/corpus-bugs/README.md. Co-Authored-By: Claude Fable 5 <[email protected]>
352 lines
15 KiB
Rust
352 lines
15 KiB
Rust
//! Tests for the runtime safety net: token-stream equivalence, convergence,
|
|
//! and the failsafe behavior of `format_source`.
|
|
|
|
use vlog_formatter::{
|
|
format_source, verify_convergence, verify_equivalence, Config, FormatError, VerificationError,
|
|
};
|
|
|
|
fn expect_divergence(input: &str, output: &str) -> VerificationError {
|
|
match verify_equivalence(input, output) {
|
|
Ok(()) => panic!("expected a divergence between\n{input}\n---\n{output}"),
|
|
Err(e) => e,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn identical_streams_are_equivalent() {
|
|
let src = "module m;\n assign x = a & b;\nendmodule\n";
|
|
verify_equivalence(src, src).expect("a text is equivalent to itself");
|
|
}
|
|
|
|
#[test]
|
|
fn whitespace_only_differences_are_equivalent() {
|
|
let input = "module m(input a,output b);assign b=~a;endmodule";
|
|
let output = "module m (input a, output b);\n assign b = ~a;\nendmodule\n";
|
|
verify_equivalence(input, output).expect("whitespace is not part of the token stream");
|
|
}
|
|
|
|
#[test]
|
|
fn comments_are_part_of_the_stream_but_may_be_reindented() {
|
|
let input = "module m;\n// note\nwire x;/* block */\nendmodule\n";
|
|
let output = "module m;\n // note\n wire x; /* block */\nendmodule\n";
|
|
verify_equivalence(input, output).expect("comment text survived");
|
|
}
|
|
|
|
#[test]
|
|
fn multiline_block_comment_may_be_reindented() {
|
|
let input = "module m;\n/* one\n two\n three */\nwire x;\nendmodule\n";
|
|
let output = "module m;\n /* one\n two\n three */\n wire x;\nendmodule\n";
|
|
verify_equivalence(input, output).expect("only per-line indentation changed");
|
|
}
|
|
|
|
#[test]
|
|
fn multiline_macro_invocation_may_be_reindented() {
|
|
// A macro usage lexes as one identifier token holding the whole
|
|
// invocation, so re-indenting its continuation lines changes that token's
|
|
// text — exactly like a continued `define, and normalized the same way.
|
|
let input = "module m;\n`A(x,\n y)\nendmodule\n";
|
|
let output = "module m;\n `A(x,\n y)\nendmodule\n";
|
|
verify_equivalence(input, output).expect("only per-line indentation changed");
|
|
}
|
|
|
|
/// QA repro (2026-08-19): a string literal inside a macro argument that spans
|
|
/// physical lines. Its continuation line's leading whitespace is part of the
|
|
/// string's *value*, so re-indenting it corrupts the string — and a blind
|
|
/// per-line trim in `normalize` used to report the pair as equivalent, so
|
|
/// `vfmt` exited 0 and wrote the corrupted file. The net must reject this.
|
|
#[test]
|
|
fn reindented_string_inside_a_macro_argument_is_rejected() {
|
|
let input = "module m;\nif (X) begin : g\n`uvm_info(\"id\", \"line one\n line two\", UVM_LOW);\nend\nendmodule\n";
|
|
let output = "module m;\n if (X) begin : g\n `uvm_info(\"id\", \"line one\n line two\", UVM_LOW);\n end\nendmodule\n";
|
|
expect_divergence(input, output);
|
|
}
|
|
|
|
/// The same hole for a `` `define `` whose body carries a continued string,
|
|
/// and for an attribute — both are re-indented through the same path.
|
|
#[test]
|
|
fn reindented_string_inside_a_directive_or_attribute_is_rejected() {
|
|
let input =
|
|
"module m;\nif (X) begin : g\n`define MSG \"line one \\\n line two\"\nend\nendmodule\n";
|
|
let output = "module m;\n if (X) begin : g\n `define MSG \"line one \\\n line two\"\n end\nendmodule\n";
|
|
expect_divergence(input, output);
|
|
|
|
let input = "module m;\n(* note = \"line one\n line two\" *)\nwire w;\nendmodule\n";
|
|
let output = "module m;\n (* note = \"line one\n line two\" *)\n wire w;\nendmodule\n";
|
|
expect_divergence(input, output);
|
|
}
|
|
|
|
/// The whole point of the normalization survives the fix: a line break that is
|
|
/// *not* inside a string is still free to be re-indented.
|
|
#[test]
|
|
fn reindenting_outside_a_string_is_still_allowed() {
|
|
// The string ends on its own line, so the following break is layout.
|
|
let input = "module m;\n`A(\"msg\",\n x)\nendmodule\n";
|
|
let output = "module m;\n `A(\"msg\",\n x)\nendmodule\n";
|
|
verify_equivalence(input, output).expect("only per-line indentation changed");
|
|
// A quote written inside a comment must not be read as opening a string.
|
|
let input = "module m;\n`A(x, // it's a \"quote\n y)\nendmodule\n";
|
|
let output = "module m;\n `A(x, // it's a \"quote\n y)\nendmodule\n";
|
|
verify_equivalence(input, output).expect("a quote inside a comment opens nothing");
|
|
}
|
|
|
|
#[test]
|
|
fn a_changed_macro_argument_is_still_rejected() {
|
|
let input = "module m;\n `A(x,\n y)\nendmodule\n";
|
|
let output = "module m;\n `A(x,\n z)\nendmodule\n";
|
|
match expect_divergence(input, output) {
|
|
VerificationError::Divergence { expected, got, .. } => {
|
|
assert!(expected.expect("expected item").text.contains('y'));
|
|
assert!(got.expect("got item").text.contains('z'));
|
|
}
|
|
other => panic!("wrong error: {other}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn differing_token_is_rejected() {
|
|
let err = expect_divergence("assign x = a;\n", "assign x = b;\n");
|
|
match err {
|
|
VerificationError::Divergence { index, ref expected, ref got } => {
|
|
assert_eq!(index, 3);
|
|
assert_eq!(expected.as_ref().expect("expected item").text, "a");
|
|
assert_eq!(got.as_ref().expect("got item").text, "b");
|
|
}
|
|
other => panic!("wrong error: {other}"),
|
|
}
|
|
let rendered = err.to_string();
|
|
assert!(rendered.contains("index 3"), "{rendered}");
|
|
assert!(rendered.contains('a') && rendered.contains('b'), "{rendered}");
|
|
}
|
|
|
|
#[test]
|
|
fn dropped_comment_is_rejected() {
|
|
let input = "module m;\n // keep me\n wire x;\nendmodule\n";
|
|
let output = "module m;\n wire x;\nendmodule\n";
|
|
match expect_divergence(input, output) {
|
|
VerificationError::Divergence { expected, got, .. } => {
|
|
let expected = expected.expect("expected item");
|
|
assert_eq!(expected.text, "// keep me");
|
|
assert_eq!(got.expect("got item").text, "wire");
|
|
}
|
|
other => panic!("wrong error: {other}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn dropped_trailing_comment_is_rejected() {
|
|
let input = "module m;\n wire x; // trailing\nendmodule\n";
|
|
let output = "module m;\n wire x;\nendmodule\n";
|
|
assert!(matches!(expect_divergence(input, output), VerificationError::Divergence { .. }));
|
|
}
|
|
|
|
#[test]
|
|
fn comment_at_end_of_file_is_rejected_if_dropped() {
|
|
let input = "module m;\nendmodule\n// last word\n";
|
|
let output = "module m;\nendmodule\n";
|
|
assert!(matches!(expect_divergence(input, output), VerificationError::Divergence { .. }));
|
|
}
|
|
|
|
#[test]
|
|
fn truncated_output_is_rejected() {
|
|
let input = "module m;\n wire x;\nendmodule\n";
|
|
let output = "module m;\n wire x;\n";
|
|
match expect_divergence(input, output) {
|
|
VerificationError::Divergence { expected, got, .. } => {
|
|
assert_eq!(expected.expect("expected item").text, "endmodule");
|
|
assert!(got.is_none());
|
|
}
|
|
other => panic!("wrong error: {other}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn extra_token_in_output_is_rejected() {
|
|
let input = "module m;\nendmodule\n";
|
|
let output = "module m;\nendmodule\nmodule n;\nendmodule\n";
|
|
match expect_divergence(input, output) {
|
|
VerificationError::Divergence { expected, got, .. } => {
|
|
assert!(expected.is_none());
|
|
assert_eq!(got.expect("got item").text, "module");
|
|
}
|
|
other => panic!("wrong error: {other}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn same_text_different_kind_is_rejected() {
|
|
// `x` as an identifier vs. `"x"` as a string literal.
|
|
assert!(matches!(
|
|
expect_divergence("assign a = x;\n", "assign a = \"x\";\n"),
|
|
VerificationError::Divergence { .. }
|
|
));
|
|
}
|
|
|
|
#[test]
|
|
fn unlexable_output_is_reported() {
|
|
match verify_equivalence("module m;\nendmodule\n", "module m;\n\"unterminated\n") {
|
|
Err(VerificationError::Lex { side, .. }) => {
|
|
assert_eq!(side.to_string(), "output");
|
|
}
|
|
other => panic!("expected a lex error, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn implicit_sensitivity_spellings_are_equivalent() {
|
|
let input = "module m;\n always @* y = a;\nendmodule\n";
|
|
let output = "module m;\n always @(*) y = a;\nendmodule\n";
|
|
verify_equivalence(input, output).expect("`@*` and `@(*)` are the same construct");
|
|
verify_equivalence(output, input).expect("the canonicalization is symmetric");
|
|
}
|
|
|
|
#[test]
|
|
fn convergence_accepts_a_fixed_point() {
|
|
verify_convergence("module m;\nendmodule\n", "module m;\nendmodule\n").expect("fixed point");
|
|
}
|
|
|
|
#[test]
|
|
fn convergence_reports_the_first_differing_line() {
|
|
match verify_convergence(
|
|
"module m;\n wire x;\nendmodule\n",
|
|
"module m;\n wire x;\nendmodule\n",
|
|
) {
|
|
Err(VerificationError::NotConverged { line, first, second }) => {
|
|
assert_eq!(line, 2);
|
|
assert_eq!(first, " wire x;");
|
|
assert_eq!(second, " wire x;");
|
|
}
|
|
other => panic!("expected a convergence failure, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn convergence_reports_a_truncated_second_pass() {
|
|
match verify_convergence("module m;\nendmodule\n", "module m;\n") {
|
|
Err(VerificationError::NotConverged { line, second, .. }) => {
|
|
assert_eq!(line, 2);
|
|
assert_eq!(second, "<end of file>");
|
|
}
|
|
other => panic!("expected a convergence failure, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn format_source_runs_both_checks_and_succeeds_on_real_input() {
|
|
let src = "// header\n\
|
|
module m #(parameter W = 8) (input clk, output reg [W-1:0] q);\n\
|
|
always @(posedge clk) q <= q + 1; // count\n\
|
|
endmodule\n";
|
|
let out = format_source(src, &Config::default()).expect("formats cleanly");
|
|
verify_equivalence(src, &out).expect("equivalent");
|
|
verify_convergence(&out, &format_source(&out, &Config::default()).expect("second pass"))
|
|
.expect("converged");
|
|
}
|
|
|
|
#[test]
|
|
fn format_source_reports_source_errors_as_source_errors() {
|
|
match format_source("module m;\n", &Config::default()) {
|
|
Err(FormatError::Source(_)) => {}
|
|
other => panic!("expected a parse error, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn diagnostics_stay_on_one_physical_line() {
|
|
// Comment text can carry tabs and newlines; the rendered diagnostic
|
|
// must escape them, since CLI consumers (e.g. tools/corpus_check.sh)
|
|
// treat one stderr line as one diagnostic.
|
|
let input = "module m;\n /* a\tb\n c */\n wire x;\nendmodule\n";
|
|
let output = "module m;\n wire x;\nendmodule\n";
|
|
let rendered = expect_divergence(input, output).to_string();
|
|
assert!(!rendered.contains('\t'), "raw tab in: {rendered}");
|
|
assert!(!rendered.contains('\n'), "raw newline in: {rendered}");
|
|
assert!(rendered.contains("\\t") && rendered.contains("\\n"), "{rendered}");
|
|
}
|
|
|
|
#[test]
|
|
fn format_source_would_reject_a_non_equivalent_output() {
|
|
// The real formatter is expected never to produce such output, so the
|
|
// failsafe path is exercised through the checker with a crafted pair:
|
|
// this is exactly the call `format_source` makes before returning.
|
|
let src = "module m;\n wire x; // dropped by a hypothetical bug\nendmodule\n";
|
|
let bad_output = "module m;\n wire x;\nendmodule\n";
|
|
let err = FormatError::Verification(expect_divergence(src, bad_output));
|
|
assert!(err.to_string().starts_with("verification failed:"), "{err}");
|
|
}
|
|
|
|
// --------------------------------------------------------------------------
|
|
// corpus-bugs (2026-08-19): the four root causes the corpus check found were
|
|
// all fixed in the parser/formatter, with no new forgiveness in `normalize`.
|
|
// These pin that down: every shape the net used to catch is still caught, so a
|
|
// future regression in the fixes shows up as a refusal, not as a rewritten
|
|
// file. Per CLAUDE.md hard invariant #4 these are the attack tests that would
|
|
// have been mandatory had any of the fixes been made in `verify.rs` instead.
|
|
|
|
#[test]
|
|
fn an_event_list_comma_rewritten_as_or_is_still_rejected() {
|
|
// Root cause A. `,` and `or` mean the same thing to a simulator, so a
|
|
// normalization that collapsed them would have "fixed" the corpus files
|
|
// while letting the formatter rewrite tokens at will.
|
|
let input = "module m;\n always_ff @(posedge clk_i, negedge rst_ni) q <= d;\nendmodule\n";
|
|
let output = "module m;\n always_ff @(posedge clk_i or negedge rst_ni) q <= d;\nendmodule\n";
|
|
expect_divergence(input, output);
|
|
// ...and the reverse rewrite too.
|
|
expect_divergence(output, input);
|
|
}
|
|
|
|
#[test]
|
|
fn merged_header_imports_are_still_rejected() {
|
|
// Root cause C: two header `import` statements folded into one, turning a
|
|
// `;` into a `,`.
|
|
let input = "module m\n import a::*;\n import b::t;\n(input clk);\nendmodule\n";
|
|
let output = "module m\n import a::*, b::t;\n(input clk);\nendmodule\n";
|
|
expect_divergence(input, output);
|
|
}
|
|
|
|
#[test]
|
|
fn a_dropped_empty_parameter_override_is_still_rejected() {
|
|
// Root cause D: `#()` is two tokens, and dropping them is token loss even
|
|
// though the instantiation means the same thing without them.
|
|
let input = "module tb;\n sub #() u0 (.a(x));\nendmodule\n";
|
|
let output = "module tb;\n sub u0 (.a(x));\nendmodule\n";
|
|
expect_divergence(input, output);
|
|
}
|
|
|
|
#[test]
|
|
fn a_comment_moved_out_of_a_bracketed_list_is_still_rejected() {
|
|
// Root cause B: the comment is not dropped here, only moved across the
|
|
// `)` — which changes which region a `verilator lint_on` pragma covers.
|
|
// Stream order is what makes that detectable, so it must stay compared.
|
|
let input = "module m #(\n parameter int W = 8\n /* verilator lint_on UNUSED */\n) (input a);\nendmodule\n";
|
|
let output = "module m #(\n parameter int W = 8\n)\n/* verilator lint_on UNUSED */\n(input a);\nendmodule\n";
|
|
expect_divergence(input, output);
|
|
}
|
|
|
|
#[test]
|
|
fn a_comment_moved_across_a_binary_operator_is_still_rejected() {
|
|
// Root cause B in expressions: moving a comment from after the operator to
|
|
// before it keeps every token, so only the comment's position in the
|
|
// stream catches it.
|
|
let input = "module m;\n assign y = a && // note\n b;\nendmodule\n";
|
|
let output = "module m;\n assign y = a // note\n && b;\nendmodule\n";
|
|
expect_divergence(input, output);
|
|
}
|
|
|
|
#[test]
|
|
fn a_comment_moved_across_a_semicolon_is_still_rejected() {
|
|
// Root cause B on declarations: `/* verilator split_var */` printed after
|
|
// the `;` instead of before it applies to nothing.
|
|
let input = "module m;\n logic x /* verilator split_var */;\nendmodule\n";
|
|
let output = "module m;\n logic x; /* verilator split_var */\nendmodule\n";
|
|
expect_divergence(input, output);
|
|
}
|
|
|
|
#[test]
|
|
fn two_line_comments_merged_onto_one_line_are_still_rejected() {
|
|
// The failure mode the new comment placement has to avoid: printing two
|
|
// line comments on one physical line re-lexes as a single, longer comment.
|
|
let input = "module m;\n assign y = a && // one\n // two\n b;\nendmodule\n";
|
|
let output = "module m;\n assign y = a && // one // two\n b;\nendmodule\n";
|
|
expect_divergence(input, output);
|
|
}
|