Skip to content

Commit

Permalink
Dedup keys when parsing shape maps in ELP
Browse files Browse the repository at this point in the history
Summary: The Erlang spec allows users to define map types with duplicate keys, wherte the leftmost definition takes precedence. This will be later disallowed in eqWAlizer, but for now, we need to ensure parsing deals with these cases correctly.

Reviewed By: ilya-klyuchnikov, VLanvin

Differential Revision: D59802368

fbshipit-source-id: f551fe2503b1a0e75e998bc732abefb1317bbffa
  • Loading branch information
ruippeixotog authored and facebook-github-bot committed Jul 17, 2024
1 parent dac7b06 commit 3791250
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/eqwalizer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ elp_types_db.workspace = true
anyhow.workspace = true
eetf.workspace = true
fxhash.workspace = true
itertools.workspace = true
lazy_static.workspace = true
log.workspace = true
parking_lot.workspace = true
Expand Down
7 changes: 7 additions & 0 deletions crates/eqwalizer/src/ast/convert_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ use elp_types_db::eqwalizer::types::Type;
use elp_types_db::eqwalizer::types::UnionType;
use elp_types_db::eqwalizer::types::VarType;
use fxhash::FxHashMap;
use itertools::Itertools;

use super::TypeConversionError;

Expand Down Expand Up @@ -353,6 +354,12 @@ impl TypeConverter {
let props = ty
.props
.into_iter()
.unique_by(|prop| {
let ExtType::AtomLitExtType(atom_ty) = prop.key() else {
panic!("Illegal state: is_shape implies key is always an atom")
};
atom_ty.atom.clone()
})
.map(|prop| self.to_shape_prop(sub, prop))
.collect::<Result<Result<Vec<_>, _>, _>>()?;
Ok(props.map(|props| Type::ShapeMap(ShapeMap { props })))
Expand Down
3 changes: 3 additions & 0 deletions test_projects/eqwalizer_tests/check/src/other.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ any_as_metadata_neg(M) -> M.
elab_var_scope(_) ->
(begin X = 3, X, fun () -> ok end end)(),
X.

-spec map_with_dup_keys(#{ak => av1, bk => bv1, ak => av2}) -> #{ak => av1, bk => bv1}.
map_with_dup_keys(M) -> M.

0 comments on commit 3791250

Please sign in to comment.