diff --git a/pesto/src/alias.rs b/pesto/src/alias.rs index 69b7306..411eca0 100644 --- a/pesto/src/alias.rs +++ b/pesto/src/alias.rs @@ -23,6 +23,7 @@ use crate::pasta::{AliasList, Docs, ValuePath}; /// Match Non-Primop /// Eq position pub fn find_aliases(item: &Docs, list: &Vec<&Docs>) -> AliasList { + // dbg!("finding alias for", &item.path); let res: AliasList = list .iter() .filter_map(|other| { @@ -31,8 +32,13 @@ pub fn find_aliases(item: &Docs, list: &Vec<&Docs>) -> AliasList { if item.path == other.path { return None; } - + // Both functions MUST have the same source position. + // Otherwise they CANNOT be a real alias. + if s_meta.position != o_meta.position { + return None; + } return match (o_meta.isPrimop, s_meta.isPrimop) { + // Both PrimOp (true, true) => { let is_empty = match &s_meta.content { Some(c) => c.is_empty(), @@ -53,18 +59,25 @@ pub fn find_aliases(item: &Docs, list: &Vec<&Docs>) -> AliasList { } None } + // Both None PrimOp (false, false) => { + // Both functions may be an alias only if at least one of them is not partially applied + if s_meta.countApplied == Some(0) || o_meta.countApplied == Some(0) { + return Some(other.path.clone()); + } + + // Last resort try to find all functions with: + // - same source position + // - same isPrimop + // - same name + // It is very likely a real alias if s_meta.countApplied != Some(0) { if item.path.last() == other.path.last() { + // dbg!("ADDING Fallback ALIAS", &item.path); return Some(other.path.clone()); } } - if s_meta.position == o_meta.position - && (s_meta.countApplied == Some(0) || o_meta.countApplied == Some(0)) - { - return Some(other.path.clone()); - } None } _ => None, @@ -81,6 +94,10 @@ pub struct FnCategories<'a> { pub casual: Vec<&'a Docs>, pub partial: Vec<&'a Docs>, } +/// TODO: Migrate to use a HashMap> +/// Alias can only ever exist if they share the same lambda source position. +/// False positives can be filtered by using "findAlias" function +/// /// Build categories for efficiently finding aliases. (This is very expensive O(n^2). ) /// Aliases can only exist within one subgroup, iterating over other items is a waste of time. /// With the current value introspection, any value that is an alias of a builtin, also inherits the builtins docs and the isPrimop flag set. diff --git a/pesto/test_data/aliases/traceVal.expect b/pesto/test_data/aliases/traceVal.expect new file mode 100644 index 0000000..7f14254 --- /dev/null +++ b/pesto/test_data/aliases/traceVal.expect @@ -0,0 +1,36 @@ +[ + { + "aliases": [ + [ + "lib", + "debug", + "traceVal" + ] + ], + "path": [ + "lib", + "traceVal" + ] + }, + { + "aliases": [ + [ + "lib", + "traceVal" + ] + ], + "path": [ + "lib", + "debug", + "traceVal" + ] + }, + { + "aliases": [], + "path": [ + "lib", + "fileset", + "traceVal" + ] + } +] \ No newline at end of file diff --git a/pesto/test_data/aliases/traceVal.json b/pesto/test_data/aliases/traceVal.json new file mode 100644 index 0000000..8f30fe7 --- /dev/null +++ b/pesto/test_data/aliases/traceVal.json @@ -0,0 +1,62 @@ +[ + { + "docs": { + "attr": { + "position": { + "column": 25, + "file": "/nix/store/ba0p7n38ncj0gx55yj2vddx189nkljs0-nixpkgs-migrated/lib/default.nix", + "line": 153 + } + }, + "lambda": { + "isPrimop": false, + "position": { + "column": 5, + "file": "/nix/store/ba0p7n38ncj0gx55yj2vddx189nkljs0-nixpkgs-migrated/lib/debug.nix", + "line": 95 + } + } + }, + "path": ["lib", "traceVal"] + }, + { + "docs": { + "attr": { + "position": { + "column": 3, + "file": "/nix/store/ba0p7n38ncj0gx55yj2vddx189nkljs0-nixpkgs-migrated/lib/debug.nix", + "line": 114 + } + }, + "lambda": { + "isPrimop": false, + "position": { + "column": 5, + "file": "/nix/store/ba0p7n38ncj0gx55yj2vddx189nkljs0-nixpkgs-migrated/lib/debug.nix", + "line": 95 + } + } + }, + "path": ["lib", "debug", "traceVal"] + }, + { + "docs": { + "attr": { + "position": { + "column": 3, + "file": "/nix/store/ba0p7n38ncj0gx55yj2vddx189nkljs0-nixpkgs-migrated/lib/fileset/default.nix", + "line": 666 + } + }, + "lambda": { + "isPrimop": false, + "position": { + "column": 5, + "file": "/nix/store/ba0p7n38ncj0gx55yj2vddx189nkljs0-nixpkgs-migrated/lib/fileset/default.nix", + "line": 672 + } + } + }, + "path": ["lib", "fileset", "traceVal"] + } +]