Skip to content

Commit

Permalink
Merge pull request #3095 from mgreter/bugfix/unordered-map-usage
Browse files Browse the repository at this point in the history
Fix issue with some std::unordered_map implementations
  • Loading branch information
mgreter authored May 4, 2020
2 parents c890f92 + 4c545f1 commit c3c1c0e
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ namespace Sass {
class Hashed {
private:
std::unordered_map<
K, T, ObjHash, ObjEquality
K, T, ObjHash, ObjHashEquality
> elements_;

sass::vector<K> _keys;
Expand Down Expand Up @@ -396,7 +396,7 @@ namespace Sass {
bool has_duplicate_key() const { return duplicate_key_ != nullptr; }
K get_duplicate_key() const { return duplicate_key_; }
const std::unordered_map<
K, T, ObjHash, ObjEquality
K, T, ObjHash, ObjHashEquality
>& elements() { return elements_; }
Hashed& operator<<(std::pair<K, T> p)
{
Expand Down Expand Up @@ -432,7 +432,7 @@ namespace Sass {
return *this;
}
const std::unordered_map<
K, T, ObjHash, ObjEquality
K, T, ObjHash, ObjHashEquality
>& pairs() const { return elements_; }

const sass::vector<K>& keys() const { return _keys; }
Expand Down
24 changes: 24 additions & 0 deletions src/ast_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,30 @@ namespace Sass {
}
};

// ###########################################################################
// Special compare function only for hashes.
// We need to make sure to not have objects equal that
// have different hashes. This is currently an issue,
// since `1px` is equal to `1` but have different hashes.
// This goes away once we remove unitless equality.
// ###########################################################################

template <class T>
// Compare the objects and its hashes
bool ObjHashEqualityFn(const T& lhs, const T& rhs) {
if (lhs == nullptr) return rhs == nullptr;
else if (rhs == nullptr) return false;
else return lhs->hash() == rhs->hash();
}
struct ObjHashEquality {
template <class T>
// Compare the objects and its contents and hashes
bool operator() (const T& lhs, const T& rhs) const {
return ObjEqualityFn<T>(lhs, rhs) &&
ObjHashEqualityFn(lhs, rhs);
}
};

// ###########################################################################
// Implement ordering operations for AST Nodes
// ###########################################################################
Expand Down

0 comments on commit c3c1c0e

Please # to comment.