Add default values to compiler arguments (#451)

## Description

Adds default values to the compiler arguments below when parsed by
`clap`. This does not change previous user-facing behavior as these
defaults were implemented using different logic, but improves the UX and
the visibility of the exact defaults used.

**Example output from `resolc --help`:**
(Only affected arguments are shown here, and some of their descriptions
have been omitted here.)
```
Options:

  -O, --optimization <OPTIMIZATION>
          Set the optimization parameter -O[0 | 1 | 2 | 3 | s | z]. Use `3` for best performance and `z` for minimal size
          
          [default: z]

      --metadata-hash <METADATA_HASH>
          Set the metadata hash type. Available types: `none`, `ipfs`, `keccak256`
          
          [default: keccak256]

      --heap-size <HEAP_SIZE>
          The emulated EVM linear heap memory static buffer size in bytes.
          ...
          
          [default: 131072]

      --stack-size <STACK_SIZE>
          The contracts total stack size in bytes.
          ...
          
          [default: 131072]
```

## Resolved Issues

Closes #446
This commit is contained in:
LJ authored and GitHub committed 2026-01-14 19:22:03 +01:00
1 parent 3bcb62ac0b
commit f5fe3faa71
4 files changed
+166 -65

No files matched your search

+109 -3
View File
@@ -1,6 +1,8 @@
//! The tests for running resolc with standard JSON option.
use revive_solc_json_interface::SolcStandardJsonOutput;
use revive_solc_json_interface::{
PolkaVMDefaultHeapMemorySize, PolkaVMDefaultStackMemorySize, SolcStandardJsonOutput,
};
use crate::cli_utils::{
assert_command_success, assert_equal_exit_codes, execute_resolc_with_stdin_input,
@@ -10,12 +12,31 @@ use crate::cli_utils::{
const JSON_OPTION: &str = "--standard-json";
/// Asserts that the standard JSON output has at least one error with the given `error_message`.
fn assert_standard_json_errors_contain(output: &SolcStandardJsonOutput, error_message: &str) {
assert!(
output
.errors
.iter()
.any(|error| error.is_error() && error.message.contains(error_message)),
"the standard JSON output should contain the error message `{error_message}`"
);
}
/// Converts valid JSON text to `SolcStandardJsonOutput`.
fn to_solc_standard_json_output(json_text: &str) -> SolcStandardJsonOutput {
serde_json::from_str(json_text).unwrap()
}
#[test]
fn runs_with_valid_input_file() {
let arguments = &[JSON_OPTION];
let resolc_result = execute_resolc_with_stdin_input(arguments, STANDARD_JSON_CONTRACTS_PATH);
assert_command_success(&resolc_result, "Providing a valid input file to stdin");
let resolc_output = to_solc_standard_json_output(&resolc_result.stdout);
assert!(!resolc_output.errors.iter().any(|error| error.is_error()));
assert!(
resolc_result.stdout.contains("contracts"),
"Expected the output to contain a `contracts` field."
@@ -30,7 +51,7 @@ fn no_evm_codegen_requested() {
let result = execute_resolc_with_stdin_input(&[JSON_OPTION], STANDARD_JSON_NO_EVM_CODEGEN_PATH);
assert_command_success(&result, "EVM codegen std json input fixture should build");
let output: SolcStandardJsonOutput = serde_json::from_str(&result.stdout).unwrap();
let output = to_solc_standard_json_output(&result.stdout);
assert!(!output.errors.iter().any(|msg| msg.severity == "error"))
}
@@ -49,6 +70,91 @@ fn no_evm_codegen_requested_complex() {
"the EVM codegen std json complext input fixture should build fine",
);
let output: SolcStandardJsonOutput = serde_json::from_str(&result.stdout).unwrap();
let output = to_solc_standard_json_output(&result.stdout);
assert!(!output.errors.iter().any(|msg| msg.severity == "error"))
}
#[test]
fn invalid_extra_arguments() {
struct TestCase<'a> {
arguments: Vec<&'a str>,
error_message: &'static str,
}
let default_heap_size_string = PolkaVMDefaultHeapMemorySize.to_string();
let default_stack_size_string = PolkaVMDefaultStackMemorySize.to_string();
let cases = vec![
TestCase {
arguments: vec![JSON_OPTION, "--heap-size", &default_heap_size_string],
error_message:
"Heap size must be specified in standard JSON input polkavm memory settings",
},
TestCase {
arguments: vec![JSON_OPTION, "--stack-size", &default_stack_size_string],
error_message:
"Stack size must be specified in standard JSON input polkavm memory settings",
},
TestCase {
arguments: vec![JSON_OPTION, "--optimization", "z"],
error_message: "LLVM optimizations must be specified in standard JSON input settings",
},
TestCase {
arguments: vec![JSON_OPTION, "-Oz"],
error_message: "LLVM optimizations must be specified in standard JSON input settings",
},
TestCase {
arguments: vec![JSON_OPTION, "--metadata-hash", "ipfs"],
error_message:
"`ipfs` metadata hash type is not supported. Please use `keccak256` instead",
},
TestCase {
arguments: vec![JSON_OPTION, "--metadata-hash", "keccak256"],
error_message: "Metadata hash mode must be specified in standard JSON input settings",
},
TestCase {
arguments: vec![JSON_OPTION, "--asm"],
error_message: "Cannot output assembly or binary outside of JSON in standard JSON mode",
},
TestCase {
arguments: vec![JSON_OPTION, "--bin"],
error_message: "Cannot output assembly or binary outside of JSON in standard JSON mode",
},
TestCase {
arguments: vec![JSON_OPTION, "--libraries", "myLib=0x0000"],
error_message: "Libraries must be passed via standard JSON input",
},
TestCase {
arguments: vec![JSON_OPTION, "--evm-version", "osaka"],
error_message: "EVM version must be passed via standard JSON input",
},
TestCase {
arguments: vec![JSON_OPTION, "--output-dir", "tmp"],
error_message: "Output directory cannot be used in standard JSON mode",
},
TestCase {
arguments: vec![JSON_OPTION, "--overwrite"],
error_message: "Overwriting flag cannot be used in standard JSON mode",
},
TestCase {
arguments: vec![JSON_OPTION, "--disable-solc-optimizer"],
error_message:
"Disabling the solc optimizer must be specified in standard JSON input settings",
},
TestCase {
arguments: vec![JSON_OPTION, "-g"],
error_message: "Debug info must be requested in standard JSON input polkavm settings",
},
TestCase {
arguments: vec![JSON_OPTION, "--llvm-arg=-riscv"],
error_message:
"LLVM arguments must be configured in standard JSON input polkavm settings",
},
];
for case in cases {
let result = execute_resolc_with_stdin_input(&case.arguments, STANDARD_JSON_CONTRACTS_PATH);
let output = to_solc_standard_json_output(&result.stdout);
assert_standard_json_errors_contain(&output, case.error_message);
}
}