Files
lucisv/crates/vlog_semantic/tests/sva.rs
T
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

219 lines
7.4 KiB
Rust

// Author: Guoliang Chen, a Senior Digital Design Engineer
//! SVA bodies: what a `property`/`sequence` declaration declares, where the
//! names inside a property spec bind, and how a sequence instance reaches its
//! declaration across files. The rules live in
//! `docs/features/sva-bodies/README.md`.
use std::path::PathBuf;
use vlog_parser::parse_source;
use vlog_semantic::{
analyze, resolve_external, Def, ExternalTarget, Semantics, SymbolKind, WorkspaceIndex,
};
fn sem(src: &str) -> Semantics {
let file = parse_source(src).expect("fixture must parse");
analyze(&file, src)
}
/// Offset of the `n`th (0-based) occurrence of `needle`.
fn nth(src: &str, needle: &str, n: usize) -> u32 {
src.match_indices(needle).nth(n).expect("occurrence must exist").0 as u32
}
/// The declaration the use at `offset` points at, by name and kind.
fn defines(s: &Semantics, offset: u32) -> Option<(String, SymbolKind)> {
let d = s.definition_of(offset)?;
Some((d.name.clone(), d.kind))
}
fn kind_of(s: &Semantics, name: &str) -> SymbolKind {
s.symbols()
.iter()
.find(|sym| sym.name == name)
.unwrap_or_else(|| panic!("no symbol {name}"))
.kind
}
fn index(files: &[(&str, &str)]) -> WorkspaceIndex {
WorkspaceIndex::build(files.iter().map(|(name, src)| (PathBuf::from(name), *src)))
}
fn overlay_def(ws: &WorkspaceIndex, src: &str, offset: u32) -> Option<Def> {
let s = sem(src);
let id = s.reference_id_at(offset)?;
resolve_external(&s, ws).get(id).and_then(ExternalTarget::best).cloned()
}
// ----------------------------------------------------------------------
// What a declaration declares
// ----------------------------------------------------------------------
#[test]
fn a_property_declares_itself_its_formals_and_its_locals() {
let src = "\
module m;
logic clk, a, b;
property p (int w, sequence s);
logic v;
@(posedge clk) a ##w b |-> s;
endproperty : p
endmodule
";
let s = sem(src);
assert_eq!(kind_of(&s, "p"), SymbolKind::Property);
assert_eq!(kind_of(&s, "w"), SymbolKind::TfArg);
assert_eq!(kind_of(&s, "s"), SymbolKind::TfArg);
assert_eq!(kind_of(&s, "v"), SymbolKind::Var);
// The formals and the local live in the declaration's own scope, so the
// module's own `a` is not shadowed by them.
assert_eq!(defines(&s, nth(src, "a ##w", 0)), Some(("a".into(), SymbolKind::Var)));
assert_eq!(defines(&s, nth(src, "##w", 0) + 2), Some(("w".into(), SymbolKind::TfArg)));
assert_eq!(defines(&s, nth(src, "|-> s", 0) + 4), Some(("s".into(), SymbolKind::TfArg)));
}
#[test]
fn a_sequence_declares_a_sequence_symbol() {
let src = "module m;\n sequence sq;\n a ##1 b;\n endsequence\nendmodule\n";
let s = sem(src);
assert_eq!(kind_of(&s, "sq"), SymbolKind::Sequence);
}
// ----------------------------------------------------------------------
// Every identifier in a body is an ordinary use — M8
// ----------------------------------------------------------------------
#[test]
fn every_name_in_a_concurrent_assertion_is_a_reference() {
let src = "\
module m;
logic clk, rst, req, gnt, ack;
assert property (@(posedge clk) disable iff (rst) req |-> ##[1:2] gnt) else $error(ack);
endmodule
";
let s = sem(src);
for (name, occurrence) in [("clk", 1), ("rst", 1), ("req", 1), ("gnt", 1), ("ack", 1)] {
let at = nth(src, name, occurrence);
assert_eq!(
defines(&s, at),
Some((name.to_string(), SymbolKind::Var)),
"`{name}` inside the assertion must resolve to its declaration"
);
}
}
#[test]
fn every_operand_shape_of_the_grammar_records_its_names() {
// One fixture per node kind that carries an expression: the repetition
// bound, the delay count, the abort condition, the `if` selector, the
// `case` labels, the match items and a `first_match` filter.
// The names are spelled so that no one of them is a substring of another
// word in the fixture — `y` alone would match inside `property`.
let src = "\
module m;
logic n_sel, n_cond, n_rep, n_hi, n_lhs, n_rhs;
assert property (
accept_on (n_cond) if (n_sel) n_lhs[*n_rep] ##[1:n_hi] n_rhs
else case (n_sel) n_rep: n_lhs; default: n_rhs; endcase
);
assert property (first_match(n_lhs ##1 n_rhs, n_hi = n_rep) |->
not strong(n_lhs within n_rhs));
endmodule
";
let s = sem(src);
for name in ["n_cond", "n_sel", "n_rep", "n_hi", "n_lhs", "n_rhs"] {
let at = nth(src, name, 1);
assert!(
s.reference_at(at).is_some(),
"`{name}` inside the property expression must be a reference"
);
}
}
#[test]
fn a_default_disable_iff_records_its_condition() {
let src = "module m;\n logic rst_n;\n default disable iff !rst_n;\nendmodule\n";
let s = sem(src);
assert_eq!(defines(&s, nth(src, "rst_n", 1)), Some(("rst_n".into(), SymbolKind::Var)));
}
// ----------------------------------------------------------------------
// Instances resolve to their declaration
// ----------------------------------------------------------------------
#[test]
fn a_sequence_instance_resolves_to_its_declaration_in_the_same_file() {
let src = "\
module m;
logic clk, a, b;
sequence handshake;
a ##1 b;
endsequence
assert property (@(posedge clk) handshake);
assert property (@(posedge clk) handshake.triggered);
endmodule
";
let s = sem(src);
assert_eq!(
defines(&s, nth(src, "handshake", 1)),
Some(("handshake".into(), SymbolKind::Sequence))
);
assert_eq!(
defines(&s, nth(src, "handshake", 2)),
Some(("handshake".into(), SymbolKind::Sequence))
);
}
#[test]
fn a_property_declared_in_a_package_resolves_across_files() {
let lib = "package p;\n property req_ack;\n a |-> b;\n endproperty\nendpackage\n";
let user = "module m;\n import p::*;\n assert property (req_ack);\nendmodule\n";
let ws = index(&[("lib.sv", lib), ("user.sv", user)]);
let def = overlay_def(&ws, user, nth(user, "req_ack", 0)).expect("the overlay resolves it");
assert_eq!(def.kind, SymbolKind::Property);
}
// ----------------------------------------------------------------------
// Round 3's pins for the rest of the tree
// ----------------------------------------------------------------------
#[test]
fn a_property_is_a_document_symbol_with_its_own_children() {
let src = "\
module m;
property p (int w);
logic v;
a ##w b;
endproperty
endmodule
";
let s = sem(src);
let outline = s.document_symbols();
let module = outline.first().expect("the module");
let prop = module
.children
.iter()
.find(|d| s.symbol(d.symbol).is_some_and(|sym| sym.name == "p"))
.expect("the property is in the outline");
let names: Vec<&str> = prop
.children
.iter()
.filter_map(|c| s.symbol(c.symbol).map(|sym| sym.name.as_str()))
.collect();
assert!(names.contains(&"w") && names.contains(&"v"), "{names:?}");
}
#[test]
fn a_spec_the_grammar_cannot_read_stays_opaque() {
// The verbatim fallback keeps the pre-round posture exactly: nothing
// inside binds, and nothing about the enclosing module changes.
let src = "module m;\n logic sig;\n assert property (sig |-> $blah $blah);\nendmodule\n";
let s = sem(src);
assert!(
s.reference_at(nth(src, "sig", 1)).is_none(),
"a fallback region is opaque to name resolution, as it always was"
);
}
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com