Skip to content

Commit

Permalink
fix: error when return func has named param
Browse files Browse the repository at this point in the history
  • Loading branch information
TilakMaddy committed Dec 15, 2024
1 parent e321f62 commit 2ce3772
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/sema/src/ast_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,20 @@ impl<'sess> AstValidator<'sess, '_> {
.emit();
}
}

fn check_var_for_named_return_params(&self, var: &ast::VariableDefinition<'_>) {
if let ast::TypeKind::Function(f) = &var.ty.kind {
for param in f.returns.iter() {
if let Some(param_name) = param.name {
self.dcx()
.err("return parameters in function types may not be named")
.span(param.span)
.help(format!("remove `{param_name}`"))
.emit();
}
}
}
}
}

impl<'ast> Visit<'ast> for AstValidator<'_, 'ast> {
Expand Down Expand Up @@ -209,6 +223,14 @@ impl<'ast> Visit<'ast> for AstValidator<'_, 'ast> {
.emit();
}
}
ast::StmtKind::DeclSingle(var) => {
self.check_var_for_named_return_params(var);
}
ast::StmtKind::DeclMulti(vars, _) => {
for var in vars.iter().flatten() {
self.check_var_for_named_return_params(var);
}
}
_ => {}
}

Expand Down
11 changes: 11 additions & 0 deletions tests/ui/resolve/func_ty_named_params.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
contract A {
constructor() {
function(uint256) view returns (uint256) a;

function(uint256) view returns (uint256 j) c; //~ERROR: return parameters in function types may not be named
//~^WARN: named function type parameters are deprecated

function(uint256 k) view returns (uint256) d;
//~^WARN: named function type parameters are deprecated
}
}
24 changes: 24 additions & 0 deletions tests/ui/resolve/func_ty_named_params.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
warning[6162]: named function type parameters are deprecated
--> ROOT/tests/ui/resolve/func_ty_named_params.sol:LL:CC
|
LL | function(uint256) view returns (uint256 j) c;
| -
|

warning[6162]: named function type parameters are deprecated
--> ROOT/tests/ui/resolve/func_ty_named_params.sol:LL:CC
|
LL | function(uint256 k) view returns (uint256) d;
| -
|

error: return parameters in function types may not be named
--> ROOT/tests/ui/resolve/func_ty_named_params.sol:LL:CC
|
LL | function(uint256) view returns (uint256 j) c;
| ^^^^^^^^^
|
= help: remove `j`

error: aborting due to 1 previous error; 2 warnings emitted

0 comments on commit 2ce3772

Please sign in to comment.