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
18 changes: 16 additions & 2 deletions crates/compass-languages/src/program/python.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};
use std::path::Path;

use compass_ir::{
Expand All @@ -17,6 +17,7 @@ pub(super) fn extract(
let mut collector = Collector {
descriptor,
input,
function_occurrences: HashMap::new(),
functions: Vec::new(),
evidence: Vec::new(),
};
Expand All @@ -27,6 +28,7 @@ pub(super) fn extract(
struct Collector<'a> {
descriptor: ProviderDescriptor,
input: &'a FileInput<'a>,
function_occurrences: HashMap<String, u32>,
functions: Vec<FunctionIr>,
evidence: Vec<compass_ir::EvidenceRecord>,
}
Expand Down Expand Up @@ -78,7 +80,7 @@ impl Collector<'_> {
|owner| format!("{owner}.{short_name}"),
);
let signature = signature_bytes(self.input.source, node);
let symbol_id = hex_sha256(
let base_symbol_id = hex_sha256(
format!(
"{}\0{}\0{}",
self.input.source_file,
Expand All @@ -87,6 +89,7 @@ impl Collector<'_> {
)
.as_bytes(),
);
let symbol_id = unique_symbol_id(base_symbol_id, &mut self.function_occurrences);
let definition = evidence_record(
&self.descriptor.id,
Some(self.input.source_file),
Expand Down Expand Up @@ -455,6 +458,17 @@ fn contains_token(source: &[u8], expected: &str) -> bool {
.any(|token| token == expected)
}

fn unique_symbol_id(base: String, occurrences: &mut HashMap<String, u32>) -> String {
let occurrence = occurrences.entry(base.clone()).or_default();
let symbol = if *occurrence == 0 {
base.clone()
} else {
hex_sha256(format!("{base}\0{occurrence}").as_bytes())
};
*occurrence = occurrence.saturating_add(1);
symbol
}

fn signature_bytes<'a>(source: &'a [u8], node: Node<'_>) -> &'a [u8] {
let end = node
.child_by_field_name("body")
Expand Down
24 changes: 24 additions & 0 deletions crates/compass-languages/tests/program_evidence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,30 @@ fn repeated_signatures_in_distinct_lexical_scopes_have_unique_syntax_symbols()
Ok(())
}

#[test]
fn python_conditional_redefinitions_have_unique_syntax_symbols() -> Result<(), Box<dyn Error>> {
let source = b"try:\n def mogrify(sql, params, connection):\n return first(sql)\nexcept ImportError:\n def mogrify(sql, params, connection):\n return second(sql)\n";
let batch = TreeSitterSyntaxProvider::default()
.analyze_file(FileInput {
source_file: "django/db/backends/postgresql/psycopg_any.py",
language: "python",
source,
})?
.ok_or("missing Python batch")?;
let functions = &batch.modules[0].functions;
assert_eq!(functions.len(), 2);
assert_eq!(
functions
.iter()
.map(|function| function.symbol_id.as_str())
.collect::<BTreeSet<_>>()
.len(),
functions.len()
);
merge_evidence(vec![batch])?.validate()?;
Ok(())
}

#[test]
fn syntax_merge_resolves_unique_local_calls() -> Result<(), Box<dyn Error>> {
let batch = TreeSitterSyntaxProvider::default()
Expand Down
Loading