Skip to content

Commit

Permalink
Remove some explicit (Mutable)ArrayRef constructions (carbon-language…
Browse files Browse the repository at this point in the history
…#4238)

Rely on implicit conversion in call sites and initialization.

Removing the explicit conversions is only code simplication.
Moving from `auto x = Y(z)` to `Y x = z;` helps ensure that only
implicit constructors/conversions are happening (whereas the prior
syntax allows explicit conversions) which can help with readability
since implicit conversions are generally "less
complex"/risky/attention-requiring.
  • Loading branch information
dwblaikie committed Aug 22, 2024
1 parent ea8ad22 commit 5a11048
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
15 changes: 7 additions & 8 deletions common/hashing_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,12 +286,11 @@ TEST(HashingTest, BasicStrings) {

TEST(HashingTest, ArrayLike) {
int c_array[] = {1, 2, 3, 4};
llvm::ArrayRef arr = c_array;
EXPECT_THAT(HashValue(c_array), Eq(HashValue(arr)));
EXPECT_THAT(HashValue(std::array{1, 2, 3, 4}), Eq(HashValue(arr)));
EXPECT_THAT(HashValue(std::vector{1, 2, 3, 4}), Eq(HashValue(arr)));
EXPECT_THAT(HashValue(c_array), Eq(HashValue(c_array)));
EXPECT_THAT(HashValue(std::array{1, 2, 3, 4}), Eq(HashValue(c_array)));
EXPECT_THAT(HashValue(std::vector{1, 2, 3, 4}), Eq(HashValue(c_array)));
EXPECT_THAT(HashValue(llvm::SmallVector<int>{1, 2, 3, 4}),
Eq(HashValue(arr)));
Eq(HashValue(c_array)));
}

TEST(HashingTest, HashAPInt) {
Expand Down Expand Up @@ -700,7 +699,7 @@ auto ExpectNoHashCollisions(llvm::ArrayRef<HashedString> hashes) -> void {

TEST(HashingTest, Collisions1ByteSized) {
auto hashes_storage = AllByteStringsHashedAndSorted<1>();
auto hashes = llvm::ArrayRef(hashes_storage);
llvm::ArrayRef hashes = hashes_storage;
ExpectNoHashCollisions(hashes);

auto low_32bit_collisions = FindBitRangeCollisions<0, 32>(hashes);
Expand All @@ -725,7 +724,7 @@ TEST(HashingTest, Collisions1ByteSized) {

TEST(HashingTest, Collisions2ByteSized) {
auto hashes_storage = AllByteStringsHashedAndSorted<2>();
auto hashes = llvm::ArrayRef(hashes_storage);
llvm::ArrayRef hashes = hashes_storage;
ExpectNoHashCollisions(hashes);

auto low_32bit_collisions = FindBitRangeCollisions<0, 32>(hashes);
Expand Down Expand Up @@ -855,7 +854,7 @@ TYPED_TEST_SUITE(SparseHashTest, SparseHashTestParams);

TYPED_TEST(SparseHashTest, Collisions) {
auto hashes_storage = this->GetHashedByteStrings();
auto hashes = llvm::ArrayRef(hashes_storage);
llvm::ArrayRef hashes = hashes_storage;
ExpectNoHashCollisions(hashes);

int min_7bit_collisions = llvm::NextPowerOf2(hashes.size() - 1) / (1 << 7);
Expand Down
2 changes: 1 addition & 1 deletion common/raw_hashtable_metadata_group_benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ static auto BuildBenchMetadata() -> llvm::ArrayRef<BenchMetadata> {

for (ssize_t g_index : llvm::seq<ssize_t>(0, BenchSize)) {
// Start by filling the group with random bytes.
auto group_bytes = llvm::MutableArrayRef(
llvm::MutableArrayRef group_bytes(
&metadata_storage[bm_index][g_index * GroupSize], GroupSize);
for (uint8_t& b : group_bytes) {
b = absl::Uniform<uint8_t>(gen) | MetadataGroup::PresentMask;
Expand Down
3 changes: 1 addition & 2 deletions toolchain/driver/driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,8 +945,7 @@ auto Driver::Compile(const CompileOptions& options,
}
}
CARBON_VLOG() << "*** Check::CheckParseTrees ***\n";
Check::CheckParseTrees(llvm::MutableArrayRef(check_units),
options.prelude_import, vlog_stream_);
Check::CheckParseTrees(check_units, options.prelude_import, vlog_stream_);
CARBON_VLOG() << "*** Check::CheckParseTrees done ***\n";
for (auto& unit : units) {
if (unit->has_source()) {
Expand Down

0 comments on commit 5a11048

Please sign in to comment.