From 5fbd5f6ed9eb1c0d189729ba2ef670ee217be097 Mon Sep 17 00:00:00 2001 From: tignear Date: Tue, 20 Oct 2026 22:42:01 +0900 Subject: [PATCH] fix(sv): validate module and interface names without interface ports Package setup previously relied on interface-port planning to detect a module and interface sharing the same name. Sources using only local interface instances could therefore bypass the shared namespace check. Check module/interface declaration names before package semantic analysis, retaining syntactic package and compilation-unit warnings on failure. Keep all interface names, including non-ANSI declarations, separate from the ANSI sources used for semantic binding. Check module analysis against interfaces in prebuilt Packages, including declaration-only analysis, without analyzing unrelated interface bodies. Fixes #1497. Validation: failed-before regressions cover both declaration orders, non-ANSI modules and interfaces, declarations split across files, and the public prebuilt-packages and declaration-only APIs. Regressions also check warning preservation and selective handling of unused interface bodies. On the current master base, analyzer tests pass (762 tests, 3 existing ignores, 2 doctests), as do all 3,174 SV integration tests (90 existing ignores), affected-crate Clippy with warnings denied, formatting and diff checks. No shared executable cases changed, so external simulators were not rerun. The master rebase preserves the analysis-options package entry point, including the mixed-timescale compatibility option, alongside declaration namespace validation and warning retention. Change-Id: I95004bcea16e94aa69e9444bbaae669840f51d33 --- crates/celox-sv-analyzer/src/ast/packages.rs | 3 + crates/celox-sv-analyzer/src/parsed.rs | 158 +++++++++++++++++++ 2 files changed, 161 insertions(+) diff --git a/crates/celox-sv-analyzer/src/ast/packages.rs b/crates/celox-sv-analyzer/src/ast/packages.rs index 42278e04dc2..c32ab382d95 100644 --- a/crates/celox-sv-analyzer/src/ast/packages.rs +++ b/crates/celox-sv-analyzer/src/ast/packages.rs @@ -24,6 +24,9 @@ pub struct Packages { /// Source-call identities shared with modules importing these package bodies. pub(super) call_sites: std::rc::Rc>, pub(crate) interface_plan: Arc, + /// All interface declaration names, including unsupported non-ANSI bodies. + /// Namespace validation must not depend on sources selected for binding. + pub(crate) interface_names: HashSet, /// Original syntax shared with interface instances; never generated source. pub(crate) interface_sources: HashMap, PathBuf)>, pub(super) timescales: Arc, diff --git a/crates/celox-sv-analyzer/src/parsed.rs b/crates/celox-sv-analyzer/src/parsed.rs index 890a7f99f95..fb1b1d798df 100644 --- a/crates/celox-sv-analyzer/src/parsed.rs +++ b/crates/celox-sv-analyzer/src/parsed.rs @@ -103,9 +103,41 @@ impl ParsedSource { .map(|source| (source.tree.as_ref(), source.path.as_path())) .collect(); let mut warnings = Vec::new(); + for source in sources { + ast::warnings::append(&mut warnings, ast::warnings::packages(&source.tree)); + } let packages = (|| { + // Module and interface names share a namespace regardless of + // whether interface ports require a full binding plan. Inspect + // declarations only, so unrelated interface bodies stay selective. + let mut design_units = HashMap::default(); + for source in sources { + for handle in source.tree.root_scopes()? { + let Some(unit) = handle.unit(source.tree.declarations()) else { + continue; + }; + if !matches!( + unit.kind, + celox_sv_syntax::UnitKind::Module | celox_sv_syntax::UnitKind::Interface + ) { + continue; + } + let name = ast::normalize_identifier(&unit.name.spelling); + if let Some(previous) = design_units.insert(name.clone(), unit.kind) + && previous != unit.kind + { + return Err(AnalyzerError::DuplicateModule { name }); + } + } + } let mut packages = Packages::analyze_with_options(&trees, roots, options.allow_mixed_timescales)?; + packages.interface_names = design_units + .into_iter() + .filter_map(|(name, kind)| { + (kind == celox_sv_syntax::UnitKind::Interface).then_some(name) + }) + .collect(); ast::warnings::append(&mut warnings, packages.warnings().iter().cloned()); for source in sources { for handle in source.tree.root_scopes()? { @@ -185,6 +217,7 @@ impl ParsedSource { overrides: &HashMap, packages: &Packages, ) -> Result { + self.validate_module_namespace(module_name, packages)?; let overrides = overrides .iter() .map(|(name, value)| (name.clone(), value.clone().into())) @@ -218,6 +251,26 @@ impl ParsedSource { self.analyze_module_with_packages(module_name, overrides, &[], &[], interfaces, &packages) } + fn validate_module_namespace( + &self, + module_name: &str, + packages: &Packages, + ) -> Result<(), AnalyzerError> { + let name = ast::normalize_identifier(module_name); + if packages.interface_names.contains(&name) + && self + .index + .names() + .iter() + .any(|module| ast::normalize_identifier(module) == name) + { + return Err( + AnalyzerError::DuplicateModule { name }.with_warnings(packages.warnings().to_vec()) + ); + } + Ok(()) + } + fn analyze_module_with_packages( &self, module_name: &str, @@ -238,6 +291,7 @@ impl ParsedSource { .map_or(module_name, |plan| plan.original.as_str()); let warnings = self.warnings.get(original).cloned().unwrap_or_default(); let ir = (|| { + self.validate_module_namespace(original, packages)?; let source = ast::Source::from_indexed_syntax_module( &self.tree, &self.path, @@ -1204,3 +1258,107 @@ mod declaration_tests { assert!(module.initial_processes().is_empty()); } } + +#[cfg(test)] +mod shared_namespace_tests { + use super::*; + + #[test] + fn rejects_module_interface_name_collisions_without_interface_ports() { + for (first, second) in [ + ("interface I; endinterface", "module I; endmodule"), + ("module I; endmodule", "interface I; endinterface"), + ( + "interface I; endinterface", + "module I(y); output y; endmodule", + ), + ] { + for split in [false, true] { + let text = format!( + "{first} {} module Top; I i(); endmodule", + if split { "" } else { second } + ); + let a = ParsedSource::parse(&text, Path::new("namespace-a.sv")).unwrap(); + let b = ParsedSource::parse(second, Path::new("namespace-b.sv")).unwrap(); + let sources = if split { vec![&a, &b] } else { vec![&a] }; + assert!( + matches!(ParsedSource::analyze_packages(&sources), + Err(AnalyzerError::DuplicateModule { name }) if name == "I"), + "accepted conflicting design-unit names: {first}; {second}; split={split}" + ); + } + } + } + + #[test] + fn namespace_collision_preserves_package_warnings() { + for function in [ + "package P; function int f(); $exit; return 0; endfunction endpackage", + "function int f(); $exit; return 0; endfunction", + ] { + let source = ParsedSource::parse( + &format!("{function} interface I; endinterface module I; endmodule"), + Path::new("namespace-warnings.sv"), + ) + .unwrap(); + let error = ParsedSource::analyze_packages(&[&source]).unwrap_err(); + assert!(error.to_string().contains("I")); + assert_eq!(error.warnings().len(), 1, "{error:?}"); + } + } + + #[test] + fn rejects_module_collision_with_prebuilt_interface_packages() { + for interface in [ + "interface I; endinterface", + "interface I(a); input a; endinterface", + ] { + let source = ParsedSource::parse(interface, Path::new("interface.sv")).unwrap(); + let packages = ParsedSource::analyze_packages(&[&source]).unwrap(); + for module in ["module I; endmodule", "module I(y); output y; endmodule"] { + let error = crate::analyze_source_module_with_packages( + module, + Path::new("module.sv"), + "I", + &HashMap::default(), + &packages, + ) + .unwrap_err(); + assert!(matches!(error, AnalyzerError::DuplicateModule { name } if name == "I")); + let parsed = ParsedSource::parse(module, Path::new("module.sv")).unwrap(); + assert!( + matches!(parsed.analyze_module_declarations("I", &HashMap::default(), &packages), + Err(AnalyzerError::DuplicateModule { name }) if name == "I") + ); + } + // Namespace metadata must not eagerly analyze the non-ANSI body. + crate::analyze_source_module_with_packages( + "module Top; endmodule", + Path::new("top.sv"), + "Top", + &HashMap::default(), + &packages, + ) + .unwrap(); + } + } + + #[test] + fn namespace_validation_does_not_analyze_unused_interface_semantics() { + let source = ParsedSource::parse( + "interface Unused; real unsupported_value; endinterface module Top(output logic y); assign y = 1; endmodule", + Path::new("unused-interface.sv"), + ).unwrap(); + let packages = ParsedSource::analyze_packages(&[&source]).unwrap(); + source + .analyze_module( + "Top", + &HashMap::default(), + &[], + &[], + &ModuleInterfaces::default(), + &packages, + ) + .unwrap(); + } +}