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]>
102 lines
3.5 KiB
Rust
102 lines
3.5 KiB
Rust
//! `module-parameter` — override a multi-parameter instance by name.
|
|
|
|
use crate::context::LintContext;
|
|
use crate::registry::Rule;
|
|
use crate::severity::Severity;
|
|
use crate::violation::Violation;
|
|
use crate::visit::walk_instantiations;
|
|
|
|
/// See [`Rule::doc`].
|
|
pub struct ModuleParameter;
|
|
|
|
const NAME: &str = "module-parameter";
|
|
|
|
impl Rule for ModuleParameter {
|
|
fn name(&self) -> &'static str {
|
|
NAME
|
|
}
|
|
|
|
fn summary(&self) -> &'static str {
|
|
"override more than one parameter by name"
|
|
}
|
|
|
|
fn severity(&self) -> Severity {
|
|
Severity::Warning
|
|
}
|
|
|
|
fn doc(&self) -> &'static str {
|
|
"`sub #(8, 4) u ()` binds by *order*, so a parameter added to the \
|
|
instantiated module — or two of them reordered — silently changes \
|
|
what every instance elaborates to, and two integers in a row give a \
|
|
reader nothing to check against. `#(.Width(8), .Depth(4))` names each \
|
|
one where it is written. Verible's `module-parameter` (\"Checks that \
|
|
module instantiations with more than one parameter are passed in as \
|
|
named parameters.\", enabled by default) is the same rule and the \
|
|
same default; a single positional override is left alone by both. No \
|
|
automatic fix: the names live in the instantiated module, usually in \
|
|
another file."
|
|
}
|
|
|
|
fn check(&self, ctx: &LintContext) -> Vec<Violation> {
|
|
let mut out = Vec::new();
|
|
walk_instantiations(ctx.file(), &mut |inst| {
|
|
let Some(params) = &inst.params else { return };
|
|
let positional = params.iter().filter(|conn| conn.name.is_empty()).count();
|
|
if positional < 2 {
|
|
return;
|
|
}
|
|
out.push(Violation::new(
|
|
NAME,
|
|
format!("`{}` takes {positional} parameters by position; name them", inst.module),
|
|
inst.module_span,
|
|
));
|
|
});
|
|
out
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn violations(src: &str) -> Vec<Violation> {
|
|
ModuleParameter.check(&LintContext::new(src, None))
|
|
}
|
|
|
|
#[test]
|
|
fn flags_more_than_one_positional_override() {
|
|
let src = "module m;\n sub #(8, 4) u_sub ();\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("2 parameters"), "{}", found[0].message);
|
|
assert!(found[0].fix.is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn a_named_override_is_what_the_rule_wants() {
|
|
let src = "module m;\n sub #(.Width(8), .Depth(4)) u_sub ();\nendmodule\n";
|
|
assert!(violations(src).is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn one_override_or_none_has_no_order_to_get_wrong() {
|
|
assert!(violations("module m;\n sub #(8) u ();\nendmodule\n").is_empty());
|
|
assert!(violations("module m;\n sub #() u ();\nendmodule\n").is_empty());
|
|
assert!(violations("module m;\n sub u ();\nendmodule\n").is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn a_bind_and_a_generate_body_are_both_reached() {
|
|
assert_eq!(violations("bind top sub #(8, 4) u ();\n").len(), 1);
|
|
let generate = "module m;\n\
|
|
generate\n\
|
|
if (A) begin : g\n\
|
|
sub #(8, 4) u ();\n\
|
|
end\n\
|
|
endgenerate\n\
|
|
endmodule\n";
|
|
assert_eq!(violations(generate).len(), 1);
|
|
}
|
|
}
|