Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

## 0.9.22
- 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
Expand Down
163 changes: 107 additions & 56 deletions src/analysis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,8 @@ pub struct SymbolStorage {
// This is doubly-indexed, by decl location and then
// by parent object key
pub method_symbols: HashMap<ZeroSpan, HashMap<StructureKey, SymbolRef>>,
// 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<ZeroSpan, SymbolRef>,
}

Expand Down Expand Up @@ -984,7 +985,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()),
Expand Down Expand Up @@ -1982,59 +1984,24 @@ fn log_non_same_insert<K>(map: &mut HashMap<K, SymbolRef>,
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;
}
}
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<DMLMethodRef>, errors: &mut Vec<DMLError>, storage: &mut SymbolStorage, method_structure: &mut HashMap<ZeroSpan, RangeEntry>) {
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);
}
old, key, stored);
conflicting = true;
}
}
}).or_insert(val);
conflicting
}

#[allow(clippy::ptr_arg)]
Expand All @@ -2057,10 +2024,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)
Comment thread
JonatanWaern marked this conversation as resolved.
Resolved
},
DMLShallowObjectVariant::Session(data) |
DMLShallowObjectVariant::Saved(data) =>
(vec![*data.declaration.loc_span()],
Expand Down Expand Up @@ -2693,3 +2663,84 @@ fn bind_method_implementations(method_symbols: &mut HashMap<ZeroSpan, HashMap<St
}
}
}

// 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<DMLMethodRef>, errors: &mut Vec<DMLError>, storage: &mut SymbolStorage, method_structure: &mut HashMap<ZeroSpan, RangeEntry>) {
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));
}

}
89 changes: 62 additions & 27 deletions src/analysis/templating/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ impl DMLHierarchyMember for DMLShallowObject {

#[derive(Clone, PartialEq, Eq)]
pub enum DMLShallowObjectVariant {
Extern(Extern),
Extern(DMLAmbiguousDef<Extern>),
Method(Arc<DMLMethodRef>),
Session(DMLVariable),
Saved(DMLVariable),
Expand All @@ -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")
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(),
}
}
}
Expand Down Expand Up @@ -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<String, Vec<(ExistCondition, Extern)>> =
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![])));
}
}

Expand Down Expand Up @@ -1392,9 +1407,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(),
Expand Down Expand Up @@ -1444,23 +1464,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<&ObjectDecl<Parameter>>> = 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),
});
}
}
}

Expand Down Expand Up @@ -1619,6 +1650,10 @@ struct CollisionCandidate {

impl CollisionCandidate {
fn overlaps(&self, other: &Self) -> bool {
if self.kind == CollisionKind::Extern &&
other.kind == CollisionKind::Extern {
return false;
Comment thread
JonatanWaern marked this conversation as resolved.
Resolved
}
let same_logical_symbol = self.kind == other.kind && matches!(
(self.logical_group, other.logical_group),
(Some(first), Some(second)) if first == second);
Expand Down
Loading
Loading