Skip to content

Commit 4c8a81a

Browse files
authored
Merge pull request #17121 from walnuthq/build/json-explicit-conversions
Disable implicit conversions from JSON values
2 parents bbfadd4 + 0bb88b0 commit 4c8a81a

5 files changed

Lines changed: 16 additions & 6 deletions

File tree

‎CMakeLists.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ else ()
6868
find_package(nlohmann_json REQUIRED)
6969
find_package(range-v3 REQUIRED)
7070
endif()
71+
add_compile_definitions(JSON_USE_IMPLICIT_CONVERSIONS=0)
72+
# TODO: Remove once the emscripten build uses emscripten >= 4.0.0, whose standard library
73+
# defines __cpp_lib_three_way_comparison.
74+
add_compile_definitions(JSON_HAS_THREE_WAY_COMPARISON=1)
7175

7276
find_package(Threads)
7377

‎libsolidity/interface/StandardCompiler.cpp‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,6 @@ std::variant<StandardCompiler::InputsAndSettings, Json> StandardCompiler::parseI
710710
return formatFatalError(Error::Type::JSONError, "No input sources specified.");
711711

712712
ret.errors = Json::array();
713-
ret.sources = Json::object();
714713

715714
if (ret.language == "Solidity" || ret.language == "Yul")
716715
{

‎libsolutil/JSON.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@
2626
#include <libsolutil/Assertions.h>
2727
#include <nlohmann/json.hpp>
2828

29+
#include <compare>
30+
#include <concepts>
2931
#include <string>
3032
#include <string_view>
33+
#include <utility>
3134
#include <optional>
3235
#include <limits>
3336

@@ -36,6 +39,10 @@ namespace solidity
3639

3740
using Json = nlohmann::json;
3841

42+
// Comparisons of tuples, pairs and containers of JSON values go through operator<=>, which must
43+
// compare the values rather than only their types.
44+
static_assert(std::same_as<decltype(std::declval<Json const&>() <=> std::declval<Json const&>()), std::partial_ordering>);
45+
3946
} // namespace solidity
4047

4148
namespace solidity::util

‎test/libevmasm/Assembler.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -460,23 +460,23 @@ BOOST_AUTO_TEST_CASE(ethdebug_resources)
460460
},
461461
"version1"
462462
);
463-
std::string const compilationID = resources["compilation"]["id"];
463+
std::string const compilationID = resources["compilation"]["id"].get<std::string>();
464464
BOOST_REQUIRE(compilationID.substr(0, 5) == "solc-");
465465
std::string const repeatedCompilationID = ethdebug::resources(
466466
{
467467
{.id = 0, .path = "sourceA", .contents = "contentsA", .language = "Solidity"},
468468
{.id = 1, .path = "sourceB", .contents = "contentsB", .language = "Yul"}
469469
},
470470
"version1"
471-
)["compilation"]["id"];
471+
)["compilation"]["id"].get<std::string>();
472472
BOOST_REQUIRE(compilationID == repeatedCompilationID);
473473
std::string const changedCompilationID = ethdebug::resources(
474474
{
475475
{.id = 0, .path = "sourceA", .contents = "changedContentsA", .language = "Solidity"},
476476
{.id = 1, .path = "sourceB", .contents = "contentsB", .language = "Yul"}
477477
},
478478
"version1"
479-
)["compilation"]["id"];
479+
)["compilation"]["id"].get<std::string>();
480480
BOOST_REQUIRE(compilationID != changedCompilationID);
481481
BOOST_REQUIRE(resources["types"].is_object());
482482
BOOST_REQUIRE(resources["types"].empty());

‎test/libsolidity/StandardCompiler.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,7 +2294,7 @@ BOOST_DATA_TEST_CASE(ethdebug_output_instructions_smoketest, boost::unit_test::d
22942294
BOOST_REQUIRE(instruction["context"]["code"]["range"].contains("offset"));
22952295
BOOST_REQUIRE(instruction["context"]["code"]["source"].contains("id"));
22962296
}
2297-
std::string mnemonic = instruction["operation"]["mnemonic"];
2297+
std::string mnemonic = instruction["operation"]["mnemonic"].get<std::string>();
22982298
if (mnemonic.find("PUSH") != std::string::npos)
22992299
{
23002300
size_t bytesToPush = boost::lexical_cast<size_t>(mnemonic.substr(4));
@@ -2303,7 +2303,7 @@ BOOST_DATA_TEST_CASE(ethdebug_output_instructions_smoketest, boost::unit_test::d
23032303
BOOST_REQUIRE(instruction["operation"].contains("arguments"));
23042304
BOOST_REQUIRE(instruction["operation"]["arguments"].is_array());
23052305
BOOST_REQUIRE(instruction["operation"]["arguments"].size() == 1);
2306-
std::string argument = instruction["operation"]["arguments"][0];
2306+
std::string argument = instruction["operation"]["arguments"][0].get<std::string>();
23072307
BOOST_REQUIRE(argument.length() % 2 == 0);
23082308
BOOST_REQUIRE(bytesToPush == (argument.length() - 2) / 2); // remove "0x" and calculate actual byte size from hex.
23092309
}

0 commit comments

Comments
 (0)