8000 feat: underscores and literals validation by TilakMaddy · Pull Request #165 · paradigmxyz/solar · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: underscores and literals validation #165

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

Merged
merged 8 commits into from
Dec 12, 2024
Merged
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
45 changes: 38 additions & 7 deletions crates/sema/src/ast_passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,38 @@ impl<'sess> AstValidator<'sess, '_> {
.emit();
}
}

fn check_underscores_in_number_literals(&self, lit: &ast::Lit) {
let ast::LitKind::Number(_) = lit.kind else {
return;
};
let literal_span = lit.span;
let literal_str = lit.symbol.as_str();

let error_help_msg = {
if literal_str.ends_with('_') {
Some("remove trailing underscores")
} else if literal_str.contains("__") {
Some("only 1 consecutive underscore `_` is allowed between digits")
} else if literal_str.contains("._") || literal_str.contains("_.") {
Some("remove underscores in front of the fraction part")
} else if literal_str.contains("_e") {
Some("remove underscores at the end of the mantissa")
} else if literal_str.contains("e_") {
Some("remove underscores in front of the exponent")
} else {
None
}
};

if let Some(error_help_msg) = error_help_msg {
self.dcx()
.err("invalid use of underscores in number literal")
.span(literal_span)
.help(error_help_msg)
.emit();
}
}
}

impl<'ast> Visit<'ast> for AstValidator<'_, 'ast> {
Expand Down Expand Up @@ -305,12 +337,11 @@ impl<'ast> Visit<'ast> for AstValidator<'_, 'ast> {
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(())
}

fn visit_ty(&mut self, _ty: &'ast ast::Type<'ast>) -> ControlFlow<Self::BreakValue> {
ControlFlow::Continue(())
fn visit_expr(&mut self, expr: &'ast ast::Expr<'ast>) -> ControlFlow<Self::BreakValue> {
let ast::Expr { kind, .. } = expr;
if let ast::ExprKind::Lit(lit, _) = kind {
self.check_underscores_in_number_literals(lit);
}
self.walk_expr(expr)
}
}
14 changes: 14 additions & 0 deletions tests/ui/resolve/literals_underscores.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
contract LT {
uint256 a = 1000_; //~ERROR: invalid use of underscores in number literal
uint256 b = 100__0; //~ERROR: invalid use of underscores in number literal
uint256 c = 1_.4e10; //~ERROR: invalid use of underscores in number literal
uint256 d = 3.4_e10; //~ERROR: invalid use of underscores in number literal
uint256 e = 3.4e_10; //~ERROR: invalid use of underscores in number literal

// exception
uint256 f = 3._4e10; // Does not show up

uint256 g = 1_.4e10 + 3.4e_10; //~ERROR: invalid use of underscores in number literal
//~^ERROR: invalid use of underscores in number literal
Comment on lines +11 to +12
Copy link
Contributor Author
@TilakMaddy TilakMaddy Dec 12, 2024

Choose a reason for hiding this comment

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

I've added this to prove that expression recursion works for this error message


}
58 changes: 58 additions & 0 deletions tests/ui/resolve/literals_underscores.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
error: invalid use of underscores in number literal
--> ROOT/tests/ui/resolve/literals_underscores.sol:LL:CC
|
LL | uint256 a = 1000_;
| ^^^^^
|
= help: remove trailing underscores

error: invalid use of underscores in number literal
--> ROOT/tests/ui/resolve/literals_underscores.sol:LL:CC
|
LL | uint256 b = 100__0;
| ^^^^^^
|
= help: only 1 consecutive underscore `_` is allowed between digits

error: invalid use of underscores in number literal
--> ROOT/tests/ui/resolve/literals_underscores.sol:LL:CC
|
LL | uint256 c = 1_.4e10;
| ^^^^^^^
|
= help: remove underscores in front of the fraction part

error: invalid use of underscores in number literal
--> ROOT/tests/ui/resolve/literals_underscores.sol:LL:CC
|
LL | uint256 d = 3.4_e10;
| ^^^^^^^
|
= help: remove underscores at the end of the mantissa

error: invalid use of underscores in number literal
--> ROOT/tests/ui/resolve/literals_underscores.sol:LL:CC
|
LL | uint256 e = 3.4e_10;
| ^^^^^^^
|
= help: remove underscores in front of the exponent

error: invalid use of underscores in number literal
--> ROOT/tests/ui/resolve/literals_underscores.sol:LL:CC
|
LL | uint256 g = 1_.4e10 + 3.4e_10;
| ^^^^^^^
|
= help: remove underscores in front of the fraction part

error: invalid use of underscores in number literal
--> ROOT/tests/ui/resolve/literals_underscores.sol:LL:CC
|
LL | uint256 g = 1_.4e10 + 3.4e_10;
| ^^^^^^^
|
= help: remove underscores in front of the exponent

error: aborting due to 7 previous errors

Loading
0