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

Initial AST validation for using-for #119

Open
wants to merge 2 commits 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
51 changes: 50 additions & 1 deletion crates/sema/src/ast_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@ struct AstValidator<'sess> {
span: Span,
dcx: &'sess DiagCtxt,
in_loop_depth: u64,
contract_kind: Option<ast::ContractKind>,
in_unchecked_block: bool,
}

impl<'sess> AstValidator<'sess> {
fn new(sess: &'sess Session) -> Self {
Self { span: Span::DUMMY, dcx: &sess.dcx, in_loop_depth: 0, in_unchecked_block: false }
Self {
span: Span::DUMMY,
dcx: &sess.dcx,
in_loop_depth: 0,
in_unchecked_block: false,
contract_kind: None,
}
}

/// Returns the diagnostics context.
Expand Down Expand Up @@ -123,6 +130,48 @@ impl<'ast> Visit<'ast> for AstValidator<'_> {
self.walk_stmt(stmt)
}

fn visit_item_contract(
&mut self,
contract: &'ast ast::ItemContract<'ast>,
) -> ControlFlow<Self::BreakValue> {
self.contract_kind = Some(contract.kind);
let r = self.walk_item_contract(contract);
self.contract_kind = None;
r
}

fn visit_using_directive(
&mut self,
using: &'ast ast::UsingDirective<'ast>,
) -> ControlFlow<Self::BreakValue> {
let ast::UsingDirective { list: _, ty, global } = using;
let with_typ = ty.is_some();
if self.contract_kind.is_none() && !with_typ {
self.dcx()
.err("the type has to be specified explicitly at file level (cannot use `*`)")
.span(self.span)
.emit();
}
if *global && !with_typ {
self.dcx()
.err("can only globally attach functions to specific types")
.span(self.span)
.emit();
}
if *global && self.contract_kind.is_some() {
self.dcx().err("`global` can only be used at file level").span(self.span).emit();
}
if let Some(contract_kind) = self.contract_kind {
if contract_kind == ast::ContractKind::Interface {
self.dcx()
.err("the `using for` directive is not allowed inside interfaces")
.span(self.span)
.emit();
}
}
self.walk_using_directive(using)
}

// Intentionally override unused default implementations to reduce bloat.
fn visit_expr(&mut self, _expr: &'ast ast::Expr<'ast>) -> ControlFlow<Self::BreakValue> {
ControlFlow::Continue(())
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/parser/using.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using {f} for * global; //~ ERROR: can only globally attach functions to specific types
//~^ ERROR: the type has to be specified explicitly at file level (cannot use `*`)
function f(uint) pure {}

contract C {
using {f} for uint global; //~ ERROR: `global` can only be used at file level
}
function f(uint) pure {}

interface I2 {
using L for int; //~ ERROR: the `using for` directive is not allowed inside interfaces
function g() external;
}
30 changes: 30 additions & 0 deletions tests/ui/parser/using.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
error: the type has to be specified explicitly at file level (cannot use `*`)
--> ROOT/tests/ui/parser/using.sol:LL:CC
|
LL | using {f} for * global;
| ^^^^^^^^^^^^^^^^^^^^^^^
|

error: can only globally attach functions to specific types
--> ROOT/tests/ui/parser/using.sol:LL:CC
|
LL | using {f} for * global;
| ^^^^^^^^^^^^^^^^^^^^^^^
|

error: `global` can only be used at file level
--> ROOT/tests/ui/parser/using.sol:LL:CC
|
LL | using {f} for uint global;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
|

error: the `using for` directive is not allowed inside interfaces
--> ROOT/tests/ui/parser/using.sol:LL:CC
|
LL | using L for int;
| ^^^^^^^^^^^^^^^^
|

error: aborting due to 4 previous errors