From 6210e8afec1ef9eb1ea1fdca4093adc7ab92f758 Mon Sep 17 00:00:00 2001 From: Jonatan Waern Date: Thu, 29 Oct 2026 16:57:00 +0100 Subject: [PATCH 1/3] Do not conflict assignments with themselves when their conditions differ Signed-off-by: Jonatan Waern --- CHANGELOG.md | 1 + src/analysis/templating/objects.rs | 56 +++++++++------ tests/lsp_lookup_tests.rs | 30 ++++++++ .../conditional_parameter_paths.dml | 68 +++++++++++++++++++ 4 files changed, 135 insertions(+), 20 deletions(-) create mode 100644 tests/test_files/conditional_parameter_paths.dml diff --git a/CHANGELOG.md b/CHANGELOG.md index 8dfc507a..ca9b1fd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ # Change Log ## 0.9.22 +- Fixed false parameter self-conflicts when the same template is instantiated through multiple conditional paths - Fixed an issue in server logging where the large allocations could be made by debug prints even at lower debug levels ## 0.9.21 diff --git a/src/analysis/templating/objects.rs b/src/analysis/templating/objects.rs index b64727bf..869c52f5 100644 --- a/src/analysis/templating/objects.rs +++ b/src/analysis/templating/objects.rs @@ -1392,9 +1392,14 @@ fn resolve_parameter(obj_loc: &ZeroSpan, for (def, _) in &sorted_definitions { if def.obj.value.as_ref().is_some_and( |v|matches!(v, ParamValue::Auto(_))) { - let conflicts: Vec<_> = sorted_definitions.iter().filter( - |(other, _)|other != def && - !def.cond.guaranteed_excluded_from(&other.cond)).collect(); + let mut conflicts: Vec<_> = sorted_definitions.iter().filter( + |(other, _)|other.obj != def.obj && + sorted_definitions.iter().any(|(path, _)| + path.obj == def.obj && + !path.cond.guaranteed_excluded_from(&other.cond))) + .collect(); + conflicts.sort_by_key(|(other, _)|*other.obj.span()); + conflicts.dedup_by_key(|(other, _)|*other.obj.span()); if !conflicts.is_empty() { report.push(DMLError { span: *def.obj.span(), @@ -1444,23 +1449,34 @@ fn resolve_parameter(obj_loc: &ZeroSpan, // Sort declarations by span for consistent reporting rest.sort_by_key(|o|o.0.loc_span()); - // TODO: This could be improved to report conflicts between rest - let (first, rest) = rest.split_first().unwrap(); - let conflicts: Vec<_> = rest.iter().filter( - |(o, _)|!first.0.cond.guaranteed_excluded_from(&o.cond) - ).collect(); - - if !conflicts.is_empty() { - report.push(DMLError { - span: *first.0.obj.span(), - description: format!( - "Conflicting assignments to parameter '{}'", name), - related: conflicts.into_iter().map( - |(def2, _)| (*def2.obj.span(), - "Conflicting assignment here" - .to_string())).collect(), - severity: Some(DiagnosticSeverity::ERROR), - }); + // A single assignment can arrive through multiple conditional paths. + // Keep every guard, but compare distinct source assignments only. + let mut assignments: Vec>> = vec![]; + for (def, _) in rest { + if let Some(paths) = assignments.iter_mut().find( + |paths|paths[0].obj == def.obj) { + paths.push(def); + } else { + assignments.push(vec![def]); + } + } + for (index, paths) in assignments.iter().enumerate() { + let conflicts: Vec<_> = assignments[index + 1..].iter().filter( + |others|paths.iter().any(|first|others.iter().any( + |other|!first.cond.guaranteed_excluded_from(&other.cond)))) + .collect(); + if !conflicts.is_empty() { + report.push(DMLError { + span: *paths[0].obj.span(), + description: format!( + "Conflicting assignments to parameter '{}'", name), + related: conflicts.into_iter().map( + |others| (*others[0].obj.span(), + "Conflicting assignment here".to_string())) + .collect(), + severity: Some(DiagnosticSeverity::ERROR), + }); + } } } diff --git a/tests/lsp_lookup_tests.rs b/tests/lsp_lookup_tests.rs index 37e5136c..2e1573b1 100644 --- a/tests/lsp_lookup_tests.rs +++ b/tests/lsp_lookup_tests.rs @@ -808,6 +808,36 @@ mod tests { "expected one auto-parameter error, got: {:#?}", errors); } + #[test] + fn conditional_paths_do_not_conflict_with_the_same_parameter_assignment() { + init_logging(); + let setup = setup_test(&["conditional_parameter_paths.dml"]); + let analysis = setup.analysis.lock().unwrap(); + let device = analysis.get_device_analysis(&setup.main_canon_path) + .expect("device analysis should exist"); + let errors: Vec<_> = device.errors.values().flatten().filter(|error| + error.description.starts_with("Conflicting assignments") || + error.description.contains("auto-parameter")).collect(); + let mut descriptions: Vec<_> = errors.iter().map( + |error|error.description.as_str()).collect(); + descriptions.sort(); + assert_eq!(descriptions, vec![ + "Conflicting assignments to parameter 'shared_default'", + "Conflicting assignments to parameter 'shared_default'", + "Conflicting assignments to parameter 'shared_fixed'", + "Conflicting assignments to parameter 'shared_fixed'", + "Invalid assignment to auto-parameter 'shared_auto'", + "Invalid assignment to auto-parameter 'shared_auto'", + ], "unexpected parameter conflicts: {:#?}", errors); + for error in errors { + assert!(!error.related.is_empty()); + assert_eq!(error.related.len(), 1, + "duplicate conflicting paths: {:#?}", error); + assert!(error.related.iter().all(|(span, _)|*span != error.span), + "assignment conflicts with itself: {:#?}", error); + } + } + #[test] fn hashif_conflicts_respect_branch_reachability() { init_logging(); diff --git a/tests/test_files/conditional_parameter_paths.dml b/tests/test_files/conditional_parameter_paths.dml new file mode 100644 index 00000000..ea9feaee --- /dev/null +++ b/tests/test_files/conditional_parameter_paths.dml @@ -0,0 +1,68 @@ +// © 2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT +dml 1.4; + +device conditional_parameter_paths; + +template shared_parameters { + param shared_default default 1; + param shared_fixed = 2; + param shared_auto auto; + group child { + param child_default default 3; + param child_auto auto; + } +} + +template left_path is shared_parameters {} +template right_path is shared_parameters {} + +#if (simics_api_version == 1) { + is left_path; +} +#if (simics_api_version == 2) { + is right_path; +} + +template conflicting_parameters { + param shared_default default 4; + param shared_fixed = 5; + param shared_auto = 6; +} + +group real_conflicts { + #if (simics_api_version == 3) { + is left_path; + } + #if (simics_api_version == 4) { + is right_path; + is conflicting_parameters; + } +} + +group exclusive_paths { + #if (simics_api_version == 5) { + is left_path; + } #else { + is right_path; + } +} + +group exclusive_assignments { + #if (simics_api_version == 6) { + is shared_parameters; + } #else { + is conflicting_parameters; + } +} + +group guarded_conflicts { + #if (simics_api_version == 7) { + is left_path; + } #else { + #if (simics_api_version == 8) { + is right_path; + is conflicting_parameters; + } + } +} From c2aa62b90de7c1f5d3967f0f9bd21cd3d1e2b82e Mon Sep 17 00:00:00 2001 From: Jonatan Waern Date: Thu, 29 Oct 2026 17:04:35 +0100 Subject: [PATCH 2/3] Merge equivalent externs Signed-off-by: Jonatan Waern --- CHANGELOG.md | 3 +- src/analysis/mod.rs | 22 ++++++++++---- src/analysis/templating/objects.rs | 33 ++++++++++++++++----- tests/lsp_lookup_tests.rs | 21 +++++++++++++ tests/test_files/repeated_extern_import.dml | 6 ++++ tests/test_files/repeated_externs.dml | 19 ++++++++++++ 6 files changed, 90 insertions(+), 14 deletions(-) create mode 100644 tests/test_files/repeated_extern_import.dml create mode 100644 tests/test_files/repeated_externs.dml diff --git a/CHANGELOG.md b/CHANGELOG.md index ca9b1fd3..f5fe8c91 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,9 @@ # Change Log ## 0.9.22 -- Fixed false parameter self-conflicts when the same template is instantiated through multiple conditional paths - Fixed an issue in server logging where the large allocations could be made by debug prints even at lower debug levels +- Fixed false parameter self-conflicts in cases where a parameter could be instantiated through different `#if` clauses +- Allowed repeated extern declarations without checking type equivalence ## 0.9.21 - The DLS will now properly not report conflicts between statements in a `#if` and its corresponding `#else` branch diff --git a/src/analysis/mod.rs b/src/analysis/mod.rs index 1ef63398..4ea2bb7b 100644 --- a/src/analysis/mod.rs +++ b/src/analysis/mod.rs @@ -984,7 +984,8 @@ impl DeviceAnalysis { .get(m.location()) .and_then(|m|m.get(&parent.key)), DMLShallowObjectVariant::Extern(e) => - self.symbol_info.variable_symbols.get(e.loc_span()), + self.symbol_info.variable_symbols.get( + e.get_likely_definition().loc_span()), DMLShallowObjectVariant::Session(s) | DMLShallowObjectVariant::Saved(s) => self.symbol_info.variable_symbols.get(s.loc_span()), @@ -2057,10 +2058,13 @@ fn add_new_symbol_from_shallow(maker: &SymbolMaker, (vec![*constant.loc_span()], vec![*constant.loc_span()], vec![*constant.loc_span()]), - DMLShallowObjectVariant::Extern(e) => - (vec![*e.loc_span()], - vec![*e.loc_span()], - vec![*e.loc_span()]), + DMLShallowObjectVariant::Extern(e) => { + let mut locations: Vec<_> = e.used_definitions.iter() + .map(|(_, def)|*def.loc_span()).collect(); + locations.sort(); + locations.dedup(); + (locations.clone(), locations.clone(), locations) + }, DMLShallowObjectVariant::Session(data) | DMLShallowObjectVariant::Saved(data) => (vec![*data.declaration.loc_span()], @@ -2096,7 +2100,13 @@ fn add_new_symbol_from_shallow(maker: &SymbolMaker, DMLShallowObjectVariant::Method(method_ref) => { internal_error!("Unreachable method_ref case reached, ignored. ({:?})", method_ref); }, - DMLShallowObjectVariant::Extern(_) | + DMLShallowObjectVariant::Extern(e) => { + for (_, declaration) in &e.used_definitions { + log_non_same_insert(&mut storage.variable_symbols, + *declaration.loc_span(), + Arc::clone(&new_sym)); + } + }, DMLShallowObjectVariant::Constant(_) | DMLShallowObjectVariant::Session(_) | DMLShallowObjectVariant::Saved(_) | diff --git a/src/analysis/templating/objects.rs b/src/analysis/templating/objects.rs index 869c52f5..3bfe76c9 100644 --- a/src/analysis/templating/objects.rs +++ b/src/analysis/templating/objects.rs @@ -640,7 +640,7 @@ impl DMLHierarchyMember for DMLShallowObject { #[derive(Clone, PartialEq, Eq)] pub enum DMLShallowObjectVariant { - Extern(Extern), + Extern(DMLAmbiguousDef), Method(Arc), Session(DMLVariable), Saved(DMLVariable), @@ -654,8 +654,8 @@ impl std::fmt::Debug for DMLShallowObjectVariant { match self { DMLShallowObjectVariant::Extern(e) => f .debug_struct("Extern") - .field("name", &e.var.name().val) - .field("location", e.loc_span()) + .field("name", &e.name().val) + .field("location", e.get_likely_definition().loc_span()) .finish(), DMLShallowObjectVariant::Method(m) => f .debug_struct("Method") @@ -750,7 +750,7 @@ impl DMLNamedMember for DMLShallowObjectVariant { DMLShallowObjectVariant::Method(m) => m.identity(), DMLShallowObjectVariant::Session(d) | DMLShallowObjectVariant::Saved(d) => &d.declaration.name.val, - DMLShallowObjectVariant::Extern(e) => &e.var.name().val, + DMLShallowObjectVariant::Extern(e) => &e.name().val, DMLShallowObjectVariant::Parameter(p) => &p.identity, DMLShallowObjectVariant::Constant(c) => &c.name().val, DMLShallowObjectVariant::Hook(h) => &h.obj.name().val, @@ -780,7 +780,8 @@ impl DMLNamedMember for DMLShallowObjectVariant { c.loc_span(), DMLShallowObjectVariant::Hook(h) => h.loc_span(), - DMLShallowObjectVariant::Extern(e) => e.loc_span(), + DMLShallowObjectVariant::Extern(e) => + e.get_likely_definition().loc_span(), } } } @@ -1196,8 +1197,22 @@ fn add_hooks(obj: &mut DMLCompositeObject, fn add_externs(obj: &mut DMLCompositeObject, externs: Vec<(ExistCondition, Extern)>) { - for (_, extrn) in externs { - obj.add_shallow_component(DMLShallowObjectVariant::Extern(extrn)); + let mut declarations: HashMap> = + HashMap::new(); + for (condition, extrn) in externs { + let paths = declarations.entry(extrn.name().val.clone()).or_default(); + if !paths.iter().any(|(guard, declaration)| + *guard == condition && *declaration == extrn) { + paths.push((condition, extrn)); + } + } + for (name, mut paths) in declarations { + paths.sort_by_key(|(_, extrn)|*extrn.loc_span()); + // DMLC merges externs by name and checks resolved type equivalence. + // TODO: Check extern type compatibility and report EEXTERNINCOMP once + // DMLType supports resolved types rather than just source spans. + obj.add_shallow_component(DMLShallowObjectVariant::Extern( + DMLAmbiguousDef::new(name, paths, vec![], vec![]))); } } @@ -1635,6 +1650,10 @@ struct CollisionCandidate { impl CollisionCandidate { fn overlaps(&self, other: &Self) -> bool { + if self.kind == CollisionKind::Extern && + other.kind == CollisionKind::Extern { + return false; + } let same_logical_symbol = self.kind == other.kind && matches!( (self.logical_group, other.logical_group), (Some(first), Some(second)) if first == second); diff --git a/tests/lsp_lookup_tests.rs b/tests/lsp_lookup_tests.rs index 2e1573b1..6bdcf4c8 100644 --- a/tests/lsp_lookup_tests.rs +++ b/tests/lsp_lookup_tests.rs @@ -808,6 +808,27 @@ mod tests { "expected one auto-parameter error, got: {:#?}", errors); } + #[test] + fn repeated_externs_share_a_symbol_and_keep_all_declarations() { + init_logging(); + let setup = setup_test(&[ + "repeated_externs.dml", "repeated_extern_import.dml"]); + { + let analysis = setup.analysis.lock().unwrap(); + let device = analysis.get_device_analysis(&setup.main_canon_path) + .expect("device analysis should exist"); + let errors: Vec<_> = device.errors.values().flatten().filter( + |error|!error.description.starts_with("Could not find file;")) + .collect(); + let descriptions: Vec<_> = errors.iter().map( + |error|error.description.as_str()).collect(); + assert_eq!(descriptions, vec![ + "Name collision in declaration on 'colliding_name'", + ], "unexpected extern diagnostics: {:#?}", errors); + } + run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); + } + #[test] fn conditional_paths_do_not_conflict_with_the_same_parameter_assignment() { init_logging(); diff --git a/tests/test_files/repeated_extern_import.dml b/tests/test_files/repeated_extern_import.dml new file mode 100644 index 00000000..78e285e5 --- /dev/null +++ b/tests/test_files/repeated_extern_import.dml @@ -0,0 +1,6 @@ +// © 2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT +dml 1.4; + +extern int shared_value; // @loc[12]=imported_value +extern int shared_function(int argument); // @loc[12]=imported_function diff --git a/tests/test_files/repeated_externs.dml b/tests/test_files/repeated_externs.dml new file mode 100644 index 00000000..c032860d --- /dev/null +++ b/tests/test_files/repeated_externs.dml @@ -0,0 +1,19 @@ +// © 2024 Intel Corporation +// SPDX-License-Identifier: Apache-2.0 and MIT +dml 1.4; + +device repeated_externs; +import "repeated_extern_import.dml"; + +extern int shared_value; // @loc[12]=value @goto-ref[12]->value_use +extern int shared_value; // @loc[12]=value_again @goto-ref[12]->value_use +extern int shared_function(int argument); // @loc[12]=function +extern int shared_function(int renamed_argument); // @loc[12]=function_again + +method use_externs() -> (int) { + local int result = shared_value; // @loc[24]=value_use @goto-def-decl[24]->value,value_again,repeated_extern_import.dml:imported_value + return shared_function(result); // @goto-def-decl[12]->function,function_again,repeated_extern_import.dml:imported_function +} + +extern int colliding_name; +session int colliding_name; From 25dd9a02ae82b764bedc3b3a775bcf26a61aaf9a Mon Sep 17 00:00:00 2001 From: Jonatan Waern Date: Mon, 2 Nov 2026 11:15:47 +0100 Subject: [PATCH 3/3] Rework of how extern merging works Used to be one symbol per merged decl, now one symbol contains all merged decls as possible decl spans Signed-off-by: Jonatan Waern --- src/analysis/mod.rs | 157 ++++++++++++++++++++++++-------------- tests/lsp_lookup_tests.rs | 54 +++++++++++++ 2 files changed, 153 insertions(+), 58 deletions(-) diff --git a/src/analysis/mod.rs b/src/analysis/mod.rs index 4ea2bb7b..a520b299 100644 --- a/src/analysis/mod.rs +++ b/src/analysis/mod.rs @@ -349,7 +349,8 @@ pub struct SymbolStorage { // This is doubly-indexed, by decl location and then // by parent object key pub method_symbols: HashMap>, - // constants, sessions, saveds, hooks, method args + // variable symbols keyed by canonical decl location + // Includes externs, constants, sessions, saveds, hooks and method args. pub variable_symbols: HashMap, } @@ -1983,59 +1984,24 @@ fn log_non_same_insert(map: &mut HashMap, val: SymbolRef) -> bool where K: std::hash::Hash + Eq + Clone + std::fmt::Debug, { - // NOTE: We should not need to do these comparisons, when - // object symbol creation is properly guided by structural AST - // this code can be simplified and the comparison discarded - - // NOTE: Insert first rather than checking for key is faster, I think - if let Some(old) = map.insert(key.clone(), val) { - // NOTE: the equivalent operation is a slight-better-than-eq - // comparison, skipping the comparison of structure keys and - // some meta-info - if !old.lock().unwrap().equivalent( - &map.get(&key).unwrap().lock().unwrap()) { + // NOTE: We should not need these equivalence comparisons, once symbol + // creation is properly guided by the structural AST this code can be + // probably be simplified and the comparisons discarded + let mut conflicting = false; + map.entry(key.clone()).and_modify(|stored| { + if Arc::ptr_eq(stored, &val) { + return; + } + let old = std::mem::replace(stored, Arc::clone(&val)); + // Compare declaration equivalence, ignoring structure keys and metadata. + if !old.lock().unwrap().equivalent(&stored.lock().unwrap()) { internal_error!( "Overwrote previous symbol {:?} at {:?} with non-similar symbol {:?}", - old, key, map.get(&key)); - return true; + old, key, stored); + conflicting = true; } - } - false -} - -// The current strategy for method symbols is: -// Create a symbol for each level of overriding for each object where the method -// is actualized. We then end up with many symbols for the same decl location, -// and leave it to requests to collect the aggregate information at the point -fn add_new_symbol_from_method(maker: &SymbolMaker, parent_obj_key: &StructureKey, method_ref: &Arc, errors: &mut Vec, storage: &mut SymbolStorage, method_structure: &mut HashMap) { - let (bases, definitions, declarations) = ( - method_ref.get_bases().iter().map(|b|*b.location()).collect(), - vec![*method_ref.get_decl().location()], - vec![*method_ref.get_decl().location()], - ); - debug!("Made symbol for method {:?}", method_ref); - let new_sym = symbol_ref!( - maker, - *method_ref.location(), - DMLSymbolKind::Method, - SymbolSource::Method(*parent_obj_key, Arc::clone(method_ref)), - bases = bases, - definitions = definitions, - declarations = declarations); - let insert_at = storage.method_symbols.entry(*method_ref.location()) - .or_default(); - if !log_non_same_insert(insert_at, *parent_obj_key, new_sym) { - for arg in method_ref.args() { - let new_argsymbol = new_symbol_from_arg(maker, method_ref, arg); - log_non_same_insert(&mut storage.variable_symbols, *arg.loc_span(), new_argsymbol); - } - add_method_scope_symbols(maker, method_ref, method_structure, storage, errors); - if let Some(defaults) = method_ref.get_default() { - for default in defaults.flat_refs() { - add_new_symbol_from_method(maker, parent_obj_key, default, errors, storage, method_structure); - } - } - } + }).or_insert(val); + conflicting } #[allow(clippy::ptr_arg)] @@ -2100,13 +2066,7 @@ fn add_new_symbol_from_shallow(maker: &SymbolMaker, DMLShallowObjectVariant::Method(method_ref) => { internal_error!("Unreachable method_ref case reached, ignored. ({:?})", method_ref); }, - DMLShallowObjectVariant::Extern(e) => { - for (_, declaration) in &e.used_definitions { - log_non_same_insert(&mut storage.variable_symbols, - *declaration.loc_span(), - Arc::clone(&new_sym)); - } - }, + DMLShallowObjectVariant::Extern(_) | DMLShallowObjectVariant::Constant(_) | DMLShallowObjectVariant::Session(_) | DMLShallowObjectVariant::Saved(_) | @@ -2703,3 +2663,84 @@ fn bind_method_implementations(method_symbols: &mut HashMap, errors: &mut Vec, storage: &mut SymbolStorage, method_structure: &mut HashMap) { + let (bases, definitions, declarations) = ( + method_ref.get_bases().iter().map(|b|*b.location()).collect(), + vec![*method_ref.get_decl().location()], + vec![*method_ref.get_decl().location()], + ); + debug!("Made symbol for method {:?}", method_ref); + let new_sym = symbol_ref!( + maker, + *method_ref.location(), + DMLSymbolKind::Method, + SymbolSource::Method(*parent_obj_key, Arc::clone(method_ref)), + bases = bases, + definitions = definitions, + declarations = declarations); + let insert_at = storage.method_symbols.entry(*method_ref.location()) + .or_default(); + if !log_non_same_insert(insert_at, *parent_obj_key, new_sym) { + for arg in method_ref.args() { + let new_argsymbol = new_symbol_from_arg(maker, method_ref, arg); + log_non_same_insert(&mut storage.variable_symbols, *arg.loc_span(), new_argsymbol); + } + add_method_scope_symbols(maker, method_ref, method_structure, storage, errors); + if let Some(defaults) = method_ref.get_default() { + for default in defaults.flat_refs() { + add_new_symbol_from_method(maker, parent_obj_key, default, errors, storage, method_structure); + } + } + } +} + +#[cfg(test)] +mod symbol_storage_tests { + use super::*; + + fn symbol(maker: &SymbolMaker) -> SymbolRef { + let location = ZeroSpan::invalid("symbol-storage-test.dml"); + maker.new_symbol(location, DMLSymbolKind::Extern, + SymbolSource::Type(DMLResolvedType::Dummy(location))) + } + + #[test] + fn inserting_the_same_symbol_does_not_lock_it_again() { + let symbol = symbol(&SymbolMaker::new()); + let mut map = HashMap::new(); + assert!(!log_non_same_insert(&mut map, 0, Arc::clone(&symbol))); + let _guard = symbol.lock().unwrap(); + assert!(!log_non_same_insert(&mut map, 0, Arc::clone(&symbol))); + assert_eq!(map.len(), 1); + assert!(Arc::ptr_eq(&map[&0], &symbol)); + } + + #[test] + fn inserting_equivalent_symbols_preserves_replacement_behavior() { + let maker = SymbolMaker::new(); + let first = symbol(&maker); + let second = symbol(&maker); + let mut map = HashMap::new(); + assert!(!log_non_same_insert(&mut map, 0, first)); + assert!(!log_non_same_insert(&mut map, 0, Arc::clone(&second))); + assert!(Arc::ptr_eq(&map[&0], &second)); + } + + #[test] + fn inserting_different_symbols_reports_the_replacement() { + let maker = SymbolMaker::new(); + let first = symbol(&maker); + let second = symbol(&maker); + second.lock().unwrap().kind = DMLSymbolKind::Constant; + let mut map = HashMap::new(); + assert!(!log_non_same_insert(&mut map, 0, first)); + assert!(log_non_same_insert(&mut map, 0, Arc::clone(&second))); + assert!(Arc::ptr_eq(&map[&0], &second)); + } + +} diff --git a/tests/lsp_lookup_tests.rs b/tests/lsp_lookup_tests.rs index 6bdcf4c8..0980410a 100644 --- a/tests/lsp_lookup_tests.rs +++ b/tests/lsp_lookup_tests.rs @@ -825,6 +825,60 @@ mod tests { assert_eq!(descriptions, vec![ "Name collision in declaration on 'colliding_name'", ], "unexpected extern diagnostics: {:#?}", errors); + let symbols: Vec<_> = device.symbol_info.all_symbols().collect(); + let unique_ids: HashSet<_> = symbols.iter().map(|symbol|symbol.id) + .collect(); + assert_eq!(symbols.len(), unique_ids.len(), + "all_symbols returned duplicate symbols"); + let merged_externs: Vec<_> = device.symbol_info.variable_symbols + .values().filter(|symbol| + symbol.lock().unwrap().declarations.len() == 3).collect(); + assert_eq!(merged_externs.len(), 2, + "each merged extern should be stored exactly once"); + for symbol in merged_externs { + let symbol = symbol.lock().unwrap(); + assert_eq!(symbol.definitions.len(), 3); + assert_eq!(symbol.references.len(), 1); + } + } + for annotation in &setup.annotations { + let position = ZeroFilePosition::new( + annotation.location.position, + annotation.location.file_path.as_ref().unwrap().clone()); + for lookup in [definitions_at_fp as LookupFn, + declarations_at_fp, references_at_fp] { + let spans = lookup(&setup.ctx, &position, &mut HashSet::new()) + .expect("extern lookup should succeed"); + let unique_spans: HashSet<_> = spans.iter().collect(); + assert_eq!(spans.len(), unique_spans.len(), + "extern lookup returned duplicate locations: {:?}", + spans); + } + } + let declaration_locations: Vec<_> = { + let analysis = setup.analysis.lock().unwrap(); + let device = analysis.get_device_analysis(&setup.main_canon_path) + .unwrap(); + device.symbol_info.variable_symbols.values().flat_map(|symbol| { + let symbol = symbol.lock().unwrap(); + if symbol.declarations.len() == 3 { + symbol.declarations.clone() + } else { + vec![] + } + }).collect() + }; + for location in declaration_locations { + let position = ZeroFilePosition::new( + location.range.start(), location.path()); + for lookup in [definitions_at_fp as LookupFn, declarations_at_fp] { + let locations = lookup(&setup.ctx, &position, &mut HashSet::new()) + .expect("lookup from every extern declaration should succeed"); + assert_eq!(locations.len(), 3); + } + let references = references_at_fp( + &setup.ctx, &position, &mut HashSet::new()).unwrap(); + assert_eq!(references.len(), 1); } run_annotation_tests(&setup.ctx, &setup.main_file, setup.annotations); }