Skip to content

Commit

Permalink
Merge pull request #15610 from ethereum/fix-code-generation-error-rep…
Browse files Browse the repository at this point in the history
…orting-in-yul

Fix code generation error reporting in Yul
  • Loading branch information
cameel authored Dec 13, 2024
2 parents 87b76ad + 47545d3 commit 03e2739
Show file tree
Hide file tree
Showing 14 changed files with 75 additions and 5 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Compiler Features:
Bugfixes:
* General: Fix internal compiler error when requesting IR AST outputs for interfaces and abstract contracts.
* Standard JSON Interface: Fix ``generatedSources`` and ``sourceMap`` being generated internally even when not requested.
* Yul: Fix internal compiler error when a code generation error should be reported instead.


### 0.8.28 (2024-10-09)
Expand Down
17 changes: 17 additions & 0 deletions liblangutil/ErrorReporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,23 @@ void ErrorReporter::unimplementedFeatureError(ErrorId _error, SourceLocation con
);
}

void ErrorReporter::codeGenerationError(ErrorId _error, SourceLocation const& _location, std::string const& _description)
{
error(_error, Error::Type::CodeGenerationError, _location, _description);
}

void ErrorReporter::codeGenerationError(Error const& _error)
{
solAssert(_error.type() == Error::Type::CodeGenerationError);
solAssert(_error.comment(), "Errors must include a message for the user.");
solUnimplementedAssert(!_error.secondarySourceLocation(), "Secondary locations not supported yet.");
codeGenerationError(
_error.errorId(),
_error.sourceLocation() ? *_error.sourceLocation() : SourceLocation{},
*_error.comment()
);
}

void ErrorReporter::info(
ErrorId _error,
SourceLocation const& _location,
Expand Down
3 changes: 3 additions & 0 deletions liblangutil/ErrorReporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ class ErrorReporter

void unimplementedFeatureError(ErrorId _error, SourceLocation const& _location, std::string const& _description);

void codeGenerationError(ErrorId _error, SourceLocation const& _location, std::string const& _description);
void codeGenerationError(Error const& _error);

ErrorList const& errors() const;

void clear();
Expand Down
6 changes: 1 addition & 5 deletions libsolidity/interface/CompilerStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -779,11 +779,7 @@ bool CompilerStack::compile(State _stopAfter)
}
catch (Error const& _error)
{
// Since codegen has no access to the error reporter, the only way for it to
// report an error is to throw. In most cases it uses dedicated exceptions,
// but CodeGenerationError is one case where someone decided to just throw Error.
solAssert(_error.type() == Error::Type::CodeGenerationError);
m_errorReporter.error(_error.errorId(), _error.type(), SourceLocation(), _error.what());
m_errorReporter.codeGenerationError(_error);
}
catch (UnimplementedFeatureError const& _error)
{
Expand Down
10 changes: 10 additions & 0 deletions libyul/YulStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ YulStack::assembleWithDeployed(std::optional<std::string_view> _deployName)
);
}
}
catch (Error const& _error)
{
m_errorReporter.codeGenerationError(_error);
return {MachineAssemblyObject{}, MachineAssemblyObject{}};
}
catch (UnimplementedFeatureError const& _error)
{
reportUnimplementedFeatureError(_error);
Expand Down Expand Up @@ -344,6 +349,11 @@ YulStack::assembleEVMWithDeployed(std::optional<std::string_view> _deployName)
return {std::make_shared<evmasm::Assembly>(assembly), std::make_shared<evmasm::Assembly>(runtimeAssembly)};
}
}
catch (Error const& _error)
{
m_errorReporter.codeGenerationError(_error);
return {nullptr, nullptr};
}
catch (UnimplementedFeatureError const& _error)
{
reportUnimplementedFeatureError(_error);
Expand Down
2 changes: 2 additions & 0 deletions libyul/YulStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <liblangutil/DebugInfoSelection.h>
#include <liblangutil/ErrorReporter.h>
#include <liblangutil/EVMVersion.h>
#include <liblangutil/Exceptions.h>

#include <libsolutil/JSON.h>

#include <libyul/Object.h>
Expand Down
1 change: 1 addition & 0 deletions test/cmdlineTests/standard_yul_code_generation_error/args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--allow-paths .
9 changes: 9 additions & 0 deletions test/cmdlineTests/standard_yul_code_generation_error/in.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object "C" {
code {}

object "C_deployed" {
code {
sstore(0, loadimmutable("1"))
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"language": "Yul",
"sources": {
"C": {"urls": ["standard_yul_code_generation_error/in.yul"]}
}
}
13 changes: 13 additions & 0 deletions test/cmdlineTests/standard_yul_code_generation_error/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"errors": [
{
"component": "general",
"formattedMessage": "CodeGenerationError: Some immutables were read from but never assigned, possibly because of optimization.

",
"message": "Some immutables were read from but never assigned, possibly because of optimization.",
"severity": "error",
"type": "CodeGenerationError"
}
]
}
1 change: 1 addition & 0 deletions test/cmdlineTests/strict_asm_code_generation_error/args
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--strict-assembly --bin
1 change: 1 addition & 0 deletions test/cmdlineTests/strict_asm_code_generation_error/err
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Error: Some immutables were read from but never assigned, possibly because of optimization.
1 change: 1 addition & 0 deletions test/cmdlineTests/strict_asm_code_generation_error/exit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
9 changes: 9 additions & 0 deletions test/cmdlineTests/strict_asm_code_generation_error/input.yul
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object "C" {
code {}

object "C_deployed" {
code {
sstore(0, loadimmutable("1"))
}
}
}

0 comments on commit 03e2739

Please sign in to comment.