Files
lucisv/crates/vlog_linter/src/rules/module_port.rs
T
lucioandClaude Fable 5 cebeff45b4 Linter expansion, wave 1: severities end to end, per-rule editor toggles generated from the registry, and nineteen Error/Warning-class rules
TODO section 33. Every rule states a Severity (error: would likely fail
to compile or elaborate, or silently mis-synthesize; warning: a probable
bug; style), printed in lucisv check text and JSON, mapped to
ERROR/WARNING/INFORMATION in the editor, grouped in --rules-doc, and
accepted by --select/--ignore and [lint] as a class word. VS Code gains
lucisv.lint.severity.{error,warning,style} and one tri-state
lucisv.lint.rules.<rule> per rule (default/on/off - a client sends
every key, so a boolean cannot say unset), generated by
vlog_linter::vscode with a drift test, and lint::enabled_rules is the
one documented ladder: defaults -> select -> class switches -> per-rule
-> ignore. Nineteen new rules (17 -> 36), one file each with true
positives and near-misses: Error - mismatched-labels,
duplicate-declaration (Semantics::duplicates(), additive),
undeclared-identifier (with a project; silent for macro text, included
fragments, hierarchical tails, any name another file mentions),
unknown-port, unknown-parameter; Warning - forbid-defparam,
invalid-system-task-function, truncated-numeric-literal,
undersized-binary-literal (safe fix), suspicious-semicolon,
packed-dimensions-range-ordering, module-port, module-parameter,
explicit-function-task-parameter-type, plusarg-assignment,
instance-shadowing, forbid-negative-array-dim, void-cast,
incomplete-assignment. Every false-positive class found on opentitan
closed with a named test; 3 383 tests / 48 binaries; corpus 814/820
and byte-identical (formatter untouched); vsix smoke OK.

docs/features/linter-expansion-1/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-09-03 17:49:03 -07:00

120 lines
4.2 KiB
Rust

//! `module-port` — connect a multi-port instance by name.
use crate::context::LintContext;
use crate::registry::Rule;
use crate::severity::Severity;
use crate::violation::Violation;
use crate::visit::walk_instantiations;
use vlog_parser::ast::Connections;
/// See [`Rule::doc`].
pub struct ModulePort;
const NAME: &str = "module-port";
impl Rule for ModulePort {
fn name(&self) -> &'static str {
NAME
}
fn summary(&self) -> &'static str {
"connect an instance with more than one port by name"
}
fn severity(&self) -> Severity {
Severity::Warning
}
fn doc(&self) -> &'static str {
"A positional connection list binds by *order*, so inserting a port in \
the instantiated module silently rewires every instance of it — and \
two ports of the same width swapped by hand compile perfectly and \
fail in simulation. A named list (`.clk_i(clk)`) says what is \
connected to what at the site, breaks loudly when a port is renamed, \
and is what every reviewer can check without opening the other file. \
Verible's `module-port` (\"Checks that module instantiations with \
more than one port are passed in as named ports.\", enabled by \
default) is the same rule and the same default; a single-port \
instance is left alone by both, because there is no order to get \
wrong. No automatic fix: naming the ports needs the instantiated \
module's port list, which is usually in another file."
}
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
let mut out = Vec::new();
walk_instantiations(ctx.file(), &mut |inst| {
for instance in &inst.instances {
let Connections::Ordered(conns) = &instance.conns else { continue };
if conns.len() < 2 {
continue;
}
out.push(Violation::new(
NAME,
format!(
"`{}` is connected by position; name its {} ports",
inst.module,
conns.len()
),
instance.span,
));
}
});
out
}
}
#[cfg(test)]
mod tests {
use super::*;
fn violations(src: &str) -> Vec<Violation> {
ModulePort.check(&LintContext::new(src, None))
}
#[test]
fn flags_a_positional_list_of_more_than_one() {
let src = "module m;\n sub u_sub (a, b, c);\nendmodule\n";
let found = violations(src);
assert_eq!(found.len(), 1, "{found:?}");
assert!(found[0].message.contains("`sub`"), "{}", found[0].message);
assert!(found[0].message.contains("3 ports"), "{}", found[0].message);
assert_eq!(&src[found[0].span.as_range()], "u_sub (a, b, c)");
assert!(found[0].fix.is_none());
}
#[test]
fn a_named_list_is_what_the_rule_wants() {
let src = "module m;\n sub u_sub (.a(x), .b(y), .c(z));\nendmodule\n";
assert!(violations(src).is_empty());
// `.*` is a named list too.
assert!(violations("module m;\n sub u_sub (.*);\nendmodule\n").is_empty());
}
#[test]
fn one_port_or_none_has_no_order_to_get_wrong() {
assert!(violations("module m;\n sub u_sub (a);\nendmodule\n").is_empty());
assert!(violations("module m;\n sub u_sub ();\nendmodule\n").is_empty());
}
#[test]
fn every_instance_of_one_statement_is_judged() {
let src = "module m;\n sub u1 (a, b), u2 (.a(x), .b(y)), u3 (c, d);\nendmodule\n";
let found = violations(src);
assert_eq!(found.len(), 2, "{found:?}");
assert!(found[0].span.start < found[1].span.start, "in source order");
}
#[test]
fn an_instantiation_inside_generate_and_a_bind_are_both_reached() {
let generate = "module m;\n\
generate\n\
if (A) begin : g\n\
sub u (a, b);\n\
end\n\
endgenerate\n\
endmodule\n";
assert_eq!(violations(generate).len(), 1);
assert_eq!(violations("bind top sub u (a, b);\n").len(), 1);
}
}