Skip to content

Commit

Permalink
Fix equality implementation of module UID.
Browse files Browse the repository at this point in the history
We already computed a `unique` `ID` value for each module to allow
declaring the same `ID` name multiple times; we however did not
consistently use that value in the implementation of `module::UID`
equality and hash operators which is addressed by this patch.

Closes #1813.
  • Loading branch information
bbannier committed Jul 31, 2024
1 parent 4c5c26b commit 4d577e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
14 changes: 8 additions & 6 deletions hilti/toolchain/include/ast/declarations/module-uid.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ struct UID {
in_memory(true) {
assert(this->id && ! parse_extension.empty() && ! process_extension.empty());
// just make up a path
path = util::fmt("/tmp/hilti/%s.%" PRIu64 ".%s.%s", id, ++_no_file_counter, process_extension, parse_extension);
path = util::fmt("/tmp/hilti/%s.%" PRIu64 ".%s.%s", unique, ++_no_file_counter, process_extension,
parse_extension);
}

UID(const UID& other) = default;
Expand All @@ -73,7 +74,8 @@ struct UID {

/** Hashes the UID. */
size_t hash() const {
return rt::hashCombine(std::hash<std::string_view>{}(id.str()), std::hash<std::string>{}(path.native()),
return rt::hashCombine(std::hash<std::string>{}(id.str()), std::hash<std::string_view>{}(unique.str()),
std::hash<std::string>{}(path.native()),
std::hash<std::string>{}(parse_extension.native()),
std::hash<std::string>{}(process_extension.native()));
}
Expand All @@ -82,14 +84,14 @@ struct UID {
UID& operator=(UID&& other) = default;

bool operator==(const UID& other) const {
return id == other.id && path == other.path && parse_extension == other.parse_extension &&
process_extension == other.process_extension;
return std::tie(id, unique, path, parse_extension, process_extension) ==
std::tie(other.id, other.unique, other.path, other.parse_extension, other.process_extension);
}

bool operator!=(const UID& other) const { return ! (*this == other); }
bool operator<(const UID& other) const {
return std::make_tuple(id, path, parse_extension, process_extension) <
std::make_tuple(other.id, other.path, other.parse_extension, other.process_extension);
return std::tie(id, unique, path, parse_extension, process_extension) <
std::tie(other.id, other.unique, other.path, other.parse_extension, other.process_extension);
}

/** Returns the module's globally uniqued name. */
Expand Down
7 changes: 7 additions & 0 deletions tests/hilti/ast/imported-twice.hlt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# @TEST-DOC: Test that we handle the same module declared twice fine, regression test for #1813.
#
# @TEST-EXEC: hiltic -dj %INPUT %INPUT
# @TEST-EXEC: hiltic -dj %INPUT $(basename %INPUT)
# @TEST-EXEC: hiltic -dj $(basename %INPUT) %INPUT

module foo {}

0 comments on commit 4d577e9

Please sign in to comment.