-
Notifications
You must be signed in to change notification settings - Fork 200
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Implement format strings in the comptime interpreter (#5596)
# Description ## Problem\* Resolves #5482 ## Summary\* Implements format strings in the interpreter. These are a bit weird since we immediately interpolate them and thus have no need to actually distinguish them from regular strings. They are also lowered into runtime code as normal strings. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: Michael J Klein <[email protected]>
- Loading branch information
1 parent
4c3bf97
commit fd7002c
Showing
5 changed files
with
88 additions
and
16 deletions.
There are no files selected for viewing
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
7 changes: 7 additions & 0 deletions
7
test_programs/compile_success_empty/comptime_fmt_strings/Nargo.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,7 @@ | ||
[package] | ||
name = "comptime_fmt_strings" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.32.0" | ||
|
||
[dependencies] |
15 changes: 15 additions & 0 deletions
15
test_programs/compile_success_empty/comptime_fmt_strings/src/main.nr
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,15 @@ | ||
fn main() { | ||
// format strings are lowered as normal strings | ||
let (s1, s2): (str<39>, str<4>) = comptime { | ||
let x = 4; | ||
let y = 5; | ||
|
||
// Can't print these at compile-time here since printing to stdout while | ||
// compiling breaks the test runner. | ||
let s1 = f"x is {x}, fake interpolation: \{y}, y is {y}"; | ||
let s2 = std::unsafe::zeroed::<fmtstr<4, ()>>(); | ||
(s1, s2) | ||
}; | ||
assert_eq(s1, "x is 4, fake interpolation: {y}, y is 5"); | ||
assert_eq(s2, "\0\0\0\0"); | ||
} |