Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error when func type has named return param #173

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<'_>) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a check in visit_ty

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

Loading