Skip to content

Commit 855b22b

Browse files
authored
Update tests to solc v0.8.22 and fix a few tests (hyperledger-solang#1579)
- Do not modify `ns.diagnostics` directly - ever. We may want to resolve an expression and drop all the diagnostics for it. - Ensure all variable errors are surfaced - Update solc testdata to `v0.8.22` Signed-off-by: Sean Young <[email protected]>
1 parent 5293181 commit 855b22b

21 files changed

+117
-90
lines changed

.gitmodules

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[submodule "solang-parser/testdata/solidity"]
1+
[submodule "testdata/solidity"]
22
path = testdata/solidity
33
url = https://github.com/ethereum/solidity
44
[submodule "integration/polkadot/tornado/tornado-cli"]

solang-parser/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
This crate is part of [Hyperledger Solang](https://solang.readthedocs.io/). It contains the
44
parser for Solidity, including the dialects used by Solang for Solana and Polkadot.
55

6-
This parser is compatible with Ethereum Solidity v0.8.21.
6+
This parser is compatible with Ethereum Solidity v0.8.22.
77

88
`solang-parser` is still `0.*.*`, so breaking changes [may occur at any time](https://semver.org/#spec-item-4). If you must depend on `solang-parser`, we recommend pinning to a specific version, i.e., `=0.y.z`.
99

src/abi/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub fn generate_abi(
1313
ns: &Namespace,
1414
code: &[u8],
1515
verbose: bool,
16-
default_authors: &Vec<String>,
16+
default_authors: &[String],
1717
version: &str,
1818
) -> (String, &'static str) {
1919
match ns.target {

src/abi/polkadot.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ pub fn metadata(
544544
contract_no: usize,
545545
code: &[u8],
546546
ns: &ast::Namespace,
547-
default_authors: &Vec<String>,
547+
default_authors: &[String],
548548
contract_version: &str,
549549
) -> Value {
550550
let hash = blake2_rfc::blake2b::blake2b(32, &[], code);

src/bin/solang.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use solang::{
1212
file_resolver::FileResolver,
1313
sema::{ast::Namespace, file::PathDisplay},
1414
standard_json::{EwasmContract, JsonContract, JsonResult},
15-
Target,
1615
};
1716
use std::{
1817
collections::{HashMap, HashSet},
@@ -243,8 +242,11 @@ fn compile(compile_args: &Compile) {
243242
let mut seen_contracts = HashMap::new();
244243

245244
let authors = if let Some(authors) = &compile_args.package.authors {
246-
if target == Target::Solana {
247-
eprintln!("warning: the `authors` flag will be ignored for solana target")
245+
if !target.is_polkadot() {
246+
eprintln!(
247+
"warning: the `authors` flag will be ignored for {} target",
248+
target
249+
)
248250
}
249251
authors.clone()
250252
} else {
@@ -348,7 +350,7 @@ fn contract_results(
348350
json_contracts: &mut HashMap<String, JsonContract>,
349351
seen_contracts: &mut HashMap<String, String>,
350352
opt: &Options,
351-
default_authors: &Vec<String>,
353+
default_authors: &[String],
352354
version: &str,
353355
) {
354356
let verbose = compiler_output.verbose;

src/sema/eval.rs

+28-21
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use super::{
44
ast::{Diagnostic, Expression, Namespace, Type},
55
diagnostics::Diagnostics,
6+
Recurse,
67
};
78
use num_bigint::BigInt;
89
use num_bigint::Sign;
@@ -295,9 +296,16 @@ pub fn eval_const_rational(
295296
}
296297
}
297298

299+
impl Expression {
300+
/// Check the expression for constant overflows, e.g. `uint8 a = 100 + 200;`.
301+
pub fn check_constant_overflow(&self, diagnostics: &mut Diagnostics) {
302+
self.recurse(diagnostics, check_term_for_constant_overflow);
303+
}
304+
}
305+
298306
/// Function that recurses the expression and folds number literals by calling 'eval_constants_in_expression'.
299307
/// If the expression is an arithmetic operation of two number literals, overflow_check() will be called on the result.
300-
pub(super) fn check_term_for_constant_overflow(expr: &Expression, ns: &mut Namespace) -> bool {
308+
fn check_term_for_constant_overflow(expr: &Expression, diagnostics: &mut Diagnostics) -> bool {
301309
match expr {
302310
Expression::Add { .. }
303311
| Expression::Subtract { .. }
@@ -310,28 +318,27 @@ pub(super) fn check_term_for_constant_overflow(expr: &Expression, ns: &mut Names
310318
| Expression::BitwiseAnd { .. }
311319
| Expression::BitwiseOr { .. }
312320
| Expression::BitwiseXor { .. }
313-
| Expression::NumberLiteral { .. } => {
314-
match eval_constants_in_expression(expr, &mut ns.diagnostics) {
315-
(
316-
Some(Expression::NumberLiteral {
317-
loc,
318-
ty,
319-
value: result,
320-
}),
321-
_,
322-
) => {
323-
if let Some(diagnostic) = overflow_diagnostic(&result, &ty, &loc) {
324-
ns.diagnostics.push(diagnostic);
325-
}
326-
327-
return false;
328-
}
329-
(None, false) => {
330-
return false;
321+
| Expression::NumberLiteral { .. } => match eval_constants_in_expression(expr, diagnostics)
322+
{
323+
(
324+
Some(Expression::NumberLiteral {
325+
loc,
326+
ty,
327+
value: result,
328+
}),
329+
_,
330+
) => {
331+
if let Some(diagnostic) = overflow_diagnostic(&result, &ty, &loc) {
332+
diagnostics.push(diagnostic);
331333
}
332-
_ => {}
334+
335+
return false;
333336
}
334-
}
337+
(None, false) => {
338+
return false;
339+
}
340+
_ => {}
341+
},
335342
_ => {}
336343
}
337344

src/sema/expression/assign.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
use crate::sema::ast::{Expression, Namespace, RetrieveType, Type};
44
use crate::sema::diagnostics::Diagnostics;
5-
use crate::sema::eval::check_term_for_constant_overflow;
65
use crate::sema::expression::integers::type_bits_and_sign;
76
use crate::sema::expression::resolve_expression::expression;
87
use crate::sema::expression::{ExprContext, ResolveTo};
98
use crate::sema::symtable::Symtable;
109
use crate::sema::unused_variable::{assigned_variable, used_variable};
11-
use crate::sema::Recurse;
1210
use solang_parser::diagnostics::Diagnostic;
1311
use solang_parser::pt;
1412
use solang_parser::pt::CodeLocation;
@@ -46,7 +44,7 @@ pub(super) fn assign_single(
4644
ResolveTo::Type(var_ty.deref_any()),
4745
)?;
4846

49-
val.recurse(ns, check_term_for_constant_overflow);
47+
val.check_constant_overflow(diagnostics);
5048

5149
used_variable(ns, &val, symtable);
5250
match &var {

src/sema/expression/literals.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ pub(super) fn hex_literal(
125125
pub(crate) fn hex_number_literal(
126126
loc: &pt::Loc,
127127
n: &str,
128-
ns: &mut Namespace,
128+
ns: &Namespace,
129129
diagnostics: &mut Diagnostics,
130130
resolve_to: ResolveTo,
131131
) -> Result<Expression, ()> {
@@ -200,7 +200,7 @@ pub(crate) fn hex_number_literal(
200200
pub(super) fn address_literal(
201201
loc: &pt::Loc,
202202
address: &str,
203-
ns: &mut Namespace,
203+
ns: &Namespace,
204204
diagnostics: &mut Diagnostics,
205205
) -> Result<Expression, ()> {
206206
if ns.target.is_polkadot() {
@@ -516,7 +516,7 @@ pub(super) fn struct_literal(
516516
pub(crate) fn unit_literal(
517517
loc: &pt::Loc,
518518
unit: &Option<pt::Identifier>,
519-
ns: &mut Namespace,
519+
ns: &Namespace,
520520
diagnostics: &mut Diagnostics,
521521
) -> BigInt {
522522
if let Some(unit) = unit {

src/sema/expression/resolve_expression.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,9 @@ use crate::sema::expression::{
2121
use crate::sema::{
2222
symtable::Symtable,
2323
unused_variable::{check_function_call, check_var_usage_expression, used_variable},
24-
Recurse,
2524
{
2625
ast::{Expression, Namespace, RetrieveType, Type},
2726
diagnostics::Diagnostics,
28-
eval::check_term_for_constant_overflow,
2927
},
3028
};
3129
use num_bigint::BigInt;
@@ -249,7 +247,7 @@ pub fn expression(
249247
};
250248
let expr = assign_expr(loc, var, expr, e, context, ns, symtable, diagnostics);
251249
if let Ok(expression) = &expr {
252-
expression.recurse(ns, check_term_for_constant_overflow);
250+
expression.check_constant_overflow(diagnostics);
253251
}
254252
expr
255253
}

src/sema/expression/subscript.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
use crate::sema::ast::{Expression, Mapping, Namespace, RetrieveType, Type};
44
use crate::sema::diagnostics::Diagnostics;
5-
use crate::sema::eval::check_term_for_constant_overflow;
65
use crate::sema::expression::resolve_expression::expression;
76
use crate::sema::expression::{ExprContext, ResolveTo};
87
use crate::sema::symtable::Symtable;
9-
use crate::sema::Recurse;
108
use solang_parser::diagnostics::Diagnostic;
119
use solang_parser::pt;
1210
use solang_parser::pt::CodeLocation;
@@ -52,7 +50,7 @@ pub(super) fn array_subscript(
5250

5351
let index_ty = index.ty();
5452

55-
index.recurse(ns, check_term_for_constant_overflow);
53+
index.check_constant_overflow(diagnostics);
5654

5755
match index_ty.deref_any() {
5856
Type::Uint(_) => (),

src/sema/expression/variable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use solang_parser::pt;
1212
pub(super) fn variable(
1313
id: &pt::Identifier,
1414
context: &ExprContext,
15-
ns: &mut Namespace,
15+
ns: &Namespace,
1616
symtable: &mut Symtable,
1717
diagnostics: &mut Diagnostics,
1818
resolve_to: ResolveTo,

src/sema/functions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,7 @@ pub fn resolve_params(
904904
"parameter of type '{}' not alowed in public or external functions",
905905
ty.to_string(ns)
906906
);
907-
ns.diagnostics.push(Diagnostic::error(p.ty.loc(), message));
907+
diagnostics.push(Diagnostic::error(p.ty.loc(), message));
908908
success = false
909909
}
910910
}
@@ -1032,7 +1032,7 @@ pub fn resolve_returns(
10321032
"return type '{}' not allowed in public or external functions",
10331033
ty.to_string(ns)
10341034
);
1035-
ns.diagnostics.push(Diagnostic::error(r.ty.loc(), message));
1035+
diagnostics.push(Diagnostic::error(r.ty.loc(), message));
10361036
success = false
10371037
}
10381038
}

src/sema/namespace.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ impl Namespace {
365365

366366
/// Resolve a free function name with namespace
367367
pub(super) fn resolve_function_with_namespace(
368-
&mut self,
368+
&self,
369369
file_no: usize,
370370
contract_no: Option<usize>,
371371
name: &pt::IdentifierPath,

src/sema/statements.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use super::ast::*;
44
use super::contracts::is_base;
55
use super::diagnostics::Diagnostics;
6-
use super::eval::check_term_for_constant_overflow;
76
use super::expression::{
87
function_call::{available_functions, call_expr, named_call_expr},
98
ExprContext, ResolveTo,
@@ -368,7 +367,8 @@ fn statement(
368367
ResolveTo::Type(&var_ty),
369368
)?;
370369

371-
expr.recurse(ns, check_term_for_constant_overflow);
370+
expr.check_constant_overflow(diagnostics);
371+
372372
used_variable(ns, &expr, symtable);
373373

374374
Some(Arc::new(expr.cast(
@@ -761,7 +761,7 @@ fn statement(
761761
pt::Statement::Return(loc, Some(returns)) => {
762762
let expr = return_with_values(returns, loc, context, symtable, ns, diagnostics)?;
763763

764-
expr.recurse(ns, check_term_for_constant_overflow);
764+
expr.check_constant_overflow(diagnostics);
765765

766766
for offset in symtable.returns.iter() {
767767
let elem = symtable.vars.get_mut(offset).unwrap();
@@ -821,7 +821,7 @@ fn statement(
821821
ResolveTo::Discard,
822822
)?;
823823

824-
ret.recurse(ns, check_term_for_constant_overflow);
824+
ret.check_constant_overflow(diagnostics);
825825
ret
826826
}
827827
pt::Expression::NamedFunctionCall(loc, ty, args) => {
@@ -836,7 +836,7 @@ fn statement(
836836
diagnostics,
837837
ResolveTo::Discard,
838838
)?;
839-
ret.recurse(ns, check_term_for_constant_overflow);
839+
ret.check_constant_overflow(diagnostics);
840840
ret
841841
}
842842
_ => {

src/sema/variables.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ use super::{
1414
tags::resolve_tags,
1515
ContractDefinition,
1616
};
17-
use crate::sema::eval::check_term_for_constant_overflow;
1817
use crate::sema::expression::resolve_expression::expression;
1918
use crate::sema::namespace::ResolveTypeContext;
20-
use crate::sema::Recurse;
2119
use solang_parser::{
2220
doccomment::DocComment,
2321
pt::{self, CodeLocation, OptionalCodeLocation},
@@ -322,9 +320,10 @@ pub fn variable_decl<'a>(
322320
return None;
323321
}
324322

323+
let mut diagnostics = Diagnostics::default();
324+
325325
let initializer = if constant {
326326
if let Some(initializer) = &def.initializer {
327-
let mut diagnostics = Diagnostics::default();
328327
let context = ExprContext {
329328
file_no,
330329
unchecked: false,
@@ -347,22 +346,16 @@ pub fn variable_decl<'a>(
347346
// implicitly conversion to correct ty
348347
match res.cast(&def.loc, &ty, true, ns, &mut diagnostics) {
349348
Ok(res) => {
350-
res.recurse(ns, check_term_for_constant_overflow);
349+
res.check_constant_overflow(&mut diagnostics);
351350
Some(res)
352351
}
353-
Err(_) => {
354-
ns.diagnostics.extend(diagnostics);
355-
None
356-
}
352+
Err(_) => None,
357353
}
358354
}
359-
Err(()) => {
360-
ns.diagnostics.extend(diagnostics);
361-
None
362-
}
355+
Err(()) => None,
363356
}
364357
} else {
365-
ns.diagnostics.push(Diagnostic::decl_error(
358+
diagnostics.push(Diagnostic::decl_error(
366359
def.loc,
367360
"missing initializer for constant".to_string(),
368361
));
@@ -373,6 +366,8 @@ pub fn variable_decl<'a>(
373366
None
374367
};
375368

369+
ns.diagnostics.extend(diagnostics);
370+
376371
let bases = contract_no.map(|contract_no| ns.contract_bases(contract_no));
377372

378373
let tags = resolve_tags(
@@ -803,7 +798,7 @@ pub fn resolve_initializers(
803798
ResolveTo::Type(&ty),
804799
) {
805800
if let Ok(res) = res.cast(&initializer.loc(), &ty, true, ns, &mut diagnostics) {
806-
res.recurse(ns, check_term_for_constant_overflow);
801+
res.check_constant_overflow(&mut diagnostics);
807802
ns.contracts[*contract_no].variables[*var_no].initializer = Some(res);
808803
}
809804
}

testdata/solidity

Submodule solidity updated 1046 files

0 commit comments

Comments
 (0)