TODO §36 (docs/features/small-items-3). Closes topo §2.9-24 and §2.9-25 and the §1 atomic-writes item. 36.1 `ItemKind::GenBlock(Box<GenBlock>)`: in tolerant mode a `begin … end` at module scope or straight inside a generate region is parsed as an unnamed generate block with a diagnostic at the keyword, so the items inside and after it survive; strict mode is untouched. Consumers walk its items; `module-begin-block`, `v2001-generate-begin` and `mismatched-labels` read the node. The formatter's `gen_block` is split by a pure extraction into `begin_block`. One server test that parsed a 3 000-deep begin nest on libtest's thread now runs on PARSE_STACK_SIZE. 36.2 `Parser::value_runs_into_next_instance`, a tolerant-mode lookahead in `parse_connection`: an unclosed `.NAME(` whose value would swallow the next instance as a call fails the element instead; `recover_element` takes the bracket the element stopped on before skipping so the skip stays at the list's level. The balanced-parens spelling stays greedy. 36.3 `lucisv_core::atomic::write_atomic`: sibling temp file + rename, permissions preserved, symlink target replaced, read-only target refused as `fs::write` refused it; used by format, `check --fix` and the cache. Also: TODO housekeeping (QA lines of §27–§35 ticked as recorded in their QA logs; the §27 P6 line and the sva-bodies README checkbox annotated as delivered by §29), topo §2.9-24/25 struck. Co-Authored-By: Claude Fable 5.1 <[email protected]>
183 lines
7.2 KiB
Rust
183 lines
7.2 KiB
Rust
//! `module-begin-block` — no `begin`/`end` block at module scope.
|
|
|
|
use crate::context::LintContext;
|
|
use crate::registry::Rule;
|
|
use crate::severity::Severity;
|
|
use crate::violation::Violation;
|
|
use vlog_parser::ast::{Item, ItemKind};
|
|
use vlog_parser::span::TextRange;
|
|
|
|
/// See [`Rule::doc`].
|
|
pub struct ModuleBeginBlock;
|
|
|
|
const NAME: &str = "module-begin-block";
|
|
|
|
impl Rule for ModuleBeginBlock {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"do not write a `begin`/`end` block at module scope"
|
|
}
|
|
|
|
fn severity(&self) -> Severity {
|
|
Severity::Style
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"A `begin … end` written straight into a module body is not a module \
|
|
item: IEEE 1800 §23.2.1 lists what may stand there, and a sequential \
|
|
block is not on the list. Tools that accept it treat it as an \
|
|
unnamed generate block, so the declarations inside it land in an \
|
|
invented scope (`genblk1.x`) and every hierarchical path written \
|
|
against them is wrong. Verible's `module-begin-block` (\"Checks that \
|
|
there are no begin-end blocks declared at the module level.\", \
|
|
enabled by default) is the same rule and the same default.\n\n\
|
|
lucisv's parser **recovers** the construct: in error-tolerant mode \
|
|
the block is read as the unnamed generate block every tool that \
|
|
accepts it makes of it, and a diagnostic is recorded at the `begin` \
|
|
— so the file is still reported as not parsing cleanly and the \
|
|
formatter still refuses to format it, but the items written inside \
|
|
the block are visible to this rule and to every other one. What this \
|
|
rule reads is that block standing where a module item belongs. No \
|
|
automatic fix: whether the block should become a generate block, an \
|
|
`always` or nothing at all is the author's to decide."
|
|
}
|
|
|
|
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
|
let mut out = Vec::new();
|
|
for item in &ctx.file().items {
|
|
module_items(item, &mut |inner| {
|
|
if let Some(span) = begin_block(inner) {
|
|
out.push(Violation::new(
|
|
NAME,
|
|
"`begin`/`end` block at module scope; a module item belongs here",
|
|
span,
|
|
));
|
|
}
|
|
});
|
|
}
|
|
out
|
|
}
|
|
}
|
|
|
|
/// The `begin` keyword of an item that is a bare `begin … end` block, or
|
|
/// `None` for any other item.
|
|
///
|
|
/// Shared with `v2001-generate-begin`, which asks the same question one scope
|
|
/// further in: the two rules must agree about what a stray `begin` looks like,
|
|
/// and one reading is how that is guaranteed.
|
|
pub(crate) fn begin_block(item: &Item) -> Option<TextRange> {
|
|
let ItemKind::GenBlock(block) = &item.kind else { return None };
|
|
// The node's span opens at the keyword, whatever the item's own span does
|
|
// with the attributes written in front of it.
|
|
Some(TextRange::new(block.span.start, block.span.start + 5))
|
|
}
|
|
|
|
/// Calls `f` on every item written directly in a module, interface or program
|
|
/// body — nested units included, generate constructs deliberately not, since
|
|
/// a `begin` inside one of those is `v2001-generate-begin`'s business.
|
|
fn module_items(item: &Item, f: &mut dyn FnMut(&Item)) {
|
|
match &item.kind {
|
|
ItemKind::Module(module) => {
|
|
for inner in &module.items {
|
|
f(inner);
|
|
module_items(inner, f);
|
|
}
|
|
}
|
|
ItemKind::Package { items, .. } => {
|
|
for inner in items {
|
|
module_items(inner, f);
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
/// Whether `file` holds a module at all — used by the tests below only.
|
|
#[cfg(test)]
|
|
fn has_module(file: &vlog_parser::ast::SourceFile) -> bool {
|
|
file.items.iter().any(|item| matches!(item.kind, ItemKind::Module(_)))
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn violations(src: &str) -> Vec<Violation> {
|
|
ModuleBeginBlock.check(&LintContext::new(src, None))
|
|
}
|
|
|
|
#[test]
|
|
fn flags_a_module_scope_block_and_points_at_its_begin() {
|
|
let src = "module m;\n begin\n wire a;\n end\nendmodule\n";
|
|
let ctx = LintContext::new(src, None);
|
|
assert!(has_module(ctx.file()), "the probe needs the module to survive recovery");
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert_eq!(&src[found[0].span.as_range()], "begin");
|
|
assert!(found[0].fix.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn an_ordinary_module_body_is_silent() {
|
|
let src = "module m;\n\
|
|
\x20 wire a;\n\
|
|
\x20 initial begin\n a = 1;\n end\n\
|
|
\x20 always_comb begin\n b = a;\n end\n\
|
|
\x20 if (A) begin : g\n wire c;\n end\n\
|
|
endmodule\n";
|
|
assert!(violations(src).is_empty(), "{:?}", violations(src));
|
|
}
|
|
|
|
#[test]
|
|
fn a_block_inside_a_generate_region_belongs_to_the_other_rule() {
|
|
let src = "module m;\n generate\n begin : b\n wire a;\n end\n \
|
|
endgenerate\nendmodule\n";
|
|
assert!(violations(src).is_empty(), "{:?}", violations(src));
|
|
}
|
|
|
|
#[test]
|
|
fn an_identifier_that_starts_with_begin_is_not_the_keyword() {
|
|
// The error leaf of an unparsable item starting with `beginning` must
|
|
// not be read as a block.
|
|
let src = "module m;\n beginning @@@;\nendmodule\n";
|
|
assert!(violations(src).is_empty(), "{:?}", violations(src));
|
|
}
|
|
|
|
#[test]
|
|
fn the_block_keeps_its_items_and_the_module_keeps_the_items_after_it() {
|
|
// §36.1: the block is a node now, not an error leaf, so what is
|
|
// written inside it and after it is still there for every rule.
|
|
// Before, recovery skipped to `endmodule` and the `defparam` was
|
|
// inside one opaque leaf.
|
|
let src = "module m;\n begin\n defparam u.P = 1;\n end\n wire a;\nendmodule\n";
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert_eq!(&src[found[0].span.as_range()], "begin");
|
|
let ctx = LintContext::new(src, None);
|
|
let inside = crate::rules::forbid_defparam::ForbidDefparam.check(&ctx);
|
|
assert_eq!(inside.len(), 1, "a rule reading the block's items: {inside:?}");
|
|
assert_eq!(ctx.file().items.len(), 1, "one module");
|
|
}
|
|
|
|
#[test]
|
|
fn only_the_outermost_block_of_a_nest_is_reported_at_module_scope() {
|
|
// The inner block is written inside a block, not at module scope, so
|
|
// it is not this rule's finding — exactly as before §36.1, when the
|
|
// whole nest was one leaf whose first token was the outer `begin`.
|
|
let src = "module m;\n begin\n begin\n wire a;\n end\n end\nendmodule\n";
|
|
let found = violations(src);
|
|
assert_eq!(found.len(), 1, "{found:?}");
|
|
assert_eq!(found[0].span.start, 12, "the outer `begin`");
|
|
}
|
|
|
|
#[test]
|
|
fn a_file_that_parses_cleanly_has_nothing_to_report() {
|
|
let src = "module m;\n wire a;\nendmodule\n";
|
|
assert!(LintContext::new(src, None).parsed_cleanly());
|
|
assert!(violations(src).is_empty());
|
|
}
|
|
}
|