//! `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 { 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 { 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); } }