-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Debug
trait and its auto implementation
#7015
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
Conversation
CodSpeed Performance ReportMerging #7015 will degrade performances by 58.31%Comparing Summary
Benchmarks breakdown
|
9122060
to
efa2227
Compare
76a644c
to
d49c3c8
Compare
sway-core/src/semantic_analysis/ast_node/declaration/auto_impl/abi_encoding.rs
Outdated
Show resolved
Hide resolved
e05eeee
to
b810972
Compare
Before my optimisations/Now: |
test/src/e2e_vm_tests/test_programs/should_fail/dbg_wrong_args_count/snapshot.toml
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work here; much needed functionality; LGTM 🚀
## Description This PR is a continuation of #7015. It is fixing the number printing function, which was not working correctly for zero. It also fixes a problem when printing `enum` variants. To improve `__dbg`, we are also now printing the argument span. An improved test now tests if all relevant `strucs` and `enums` from the `std` library are debug and show how they are printed. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [ ] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [ ] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [ ] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers. --------- Co-authored-by: kaya <[email protected]>
Description
This PR implements the
__dbg(...)
intrinsic, which is very similar to Rustdbg!(...)
macro.Up until now, it has being the norm to use
__log
to debug values. Given that this is NOT the first use case for log, we have always found some issues with it: log does not work on predicates, log does not show "private" fields likeVec::capacity
and others.To solve these problems
__dbg
is being introduced:1 - it will work on all program types, including predicates;
2 - it also prints the file name, line and column;
3 - All types will have an automatic implementation of Debug if possible, which can still be customized.
4 - Even
raw_ptr
and other non "loggable" types, haveDebug
impls.5 -
__dbg
will be completely stripped in the release build by default. It can be turned on again if needed.So this:
will generate this:
How does this work?
__dbg(value)
intrinsic is desugared into{ let f = Formatter{}; f.print_str(...); let value = value; value.fmt(f); value }
.Formatter
is similar to Rust's one. The difference is that we still do not support string formatting, so theFormatter
has a lot ofprint_*
functions.And each
print
function calls a "syscall". Thissyscall
usesecal
under the hood and it follows unix write syscall schema.For that to work, the VM interpreter must have its
EcalState
setup and interpret syscall number 1000 aswrite
. This PR does this forforc test
and oure2e test suite
.Each test in
forc test
will capture these calls and only print to the terminal when requested with the--log
flag.Garbage Collector and auto generated
Before, we were associating all auto-generated code with a pseudo file called "<autogenerated>.sw" that was never garbage collected.
This generated a problem inside the LSP when the
auto_impl.rs
ran a second time because of a collision in the "shareable type" map. When we try to solve this collision, choosing to keep the old value or to insert the new, the type inside the map points to already collected types and the compiler panics. This is a known problem.The workaround for this is to break the auto-generated code into multiple files. Now they are named "main.autogenerated.sw", for example. We create one pseudo-file for each real file that needs one.
When we garbage collect one file,
main.sw
, for example, we also collect its associated auto-generated file.Checklist
Breaking*
orNew Feature
labels where relevant.