forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for raw identifiers and improve reserved keywords checking.
This commit deals with the usage and checking of reserved keywords as identifiers, for code like: ``` fn main() { let mut mut = 0; } It introduces a new error that checks if an identifier is a reserved keyword: ``` error --> /main.sw:4:13 | 2 | 3 | fn main() { 4 | let mut mut = 0; | ^^^ Identifiers cannot be a reserved keyword. 5 | } | ____ ``` There was an existing issue in the standard library, which has a library/module named `storage`. Instead of working around this by renaming it to something else, an alternative solution with raw identifiers is implemented. This raw identifier feature is implemented at the lexer level, and allows you to use keywords as identifiers in places that generally wouldn't be allowed. Rust and a bunch of other modern languages also provide this escape hatch, and it seemed the simplest solution for me to handle the issue. It activates by declaring an identifier prefixed with `r#`, just like Rust. The complexity on the codebase to support this feature is pretty minimal, but if there any objections to this, I can easily remove it, but some other solution to the issue above will need to be figured out. Closes FuelLabs#1996.
- Loading branch information
Showing
22 changed files
with
283 additions
and
11 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
library storage; | ||
library r#storage; | ||
|
||
use ::hash::sha256; | ||
use ::context::registers::stack_ptr; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
test/src/e2e_vm_tests/test_programs/should_fail/reserved_identifiers/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[[package]] | ||
name = 'reserved_identifiers' | ||
source = 'root' | ||
dependencies = [] |
6 changes: 6 additions & 0 deletions
6
test/src/e2e_vm_tests/test_programs/should_fail/reserved_identifiers/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[project] | ||
authors = ["Fuel Labs <[email protected]>"] | ||
license = "Apache-2.0" | ||
name = "reserved_identifiers" | ||
entry = "main.sw" | ||
implicit-std = false |
39 changes: 39 additions & 0 deletions
39
test/src/e2e_vm_tests/test_programs/should_fail/reserved_identifiers/src/main.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
script; | ||
|
||
// this should fail with an `Identifiers cannot be a reserved keyword.` error | ||
|
||
fn main() { | ||
let mut script = 0; | ||
let mut contract = 0; | ||
let mut predicate = 0; | ||
let mut library = 0; | ||
let mut dep = 0; | ||
let mut pub = 0; | ||
let mut use = 0; | ||
let mut as = 0; | ||
let mut struct = 0; | ||
let mut enum = 0; | ||
let mut self = 0; | ||
let mut fn = 0; | ||
let mut trait = 0; | ||
let mut impl = 0; | ||
let mut for = 0; | ||
let mut abi = 0; | ||
let mut const = 0; | ||
let mut storage = 0; | ||
let mut str = 0; | ||
let mut asm = 0; | ||
let mut return = 0; | ||
let mut if = 0; | ||
let mut else = 0; | ||
let mut match = 0; | ||
let mut mut = 0; | ||
let mut let = 0; | ||
let mut while = 0; | ||
let mut where = 0; | ||
let mut ref = 0; | ||
let mut deref = 0; | ||
let mut true = 0; | ||
let mut false = 0; | ||
} | ||
|
1 change: 1 addition & 0 deletions
1
test/src/e2e_vm_tests/test_programs/should_fail/reserved_identifiers/test.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
category = "fail" |
9 changes: 9 additions & 0 deletions
9
test/src/e2e_vm_tests/test_programs/should_pass/language/raw_identifiers/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[[package]] | ||
name = 'core' | ||
source = 'path+from-root-D97961F846794409' | ||
dependencies = [] | ||
|
||
[[package]] | ||
name = 'simple' | ||
source = 'root' | ||
dependencies = ['core'] |
Oops, something went wrong.