Skip to content

Conversation

de-vri-es
Copy link
Contributor

@de-vri-es de-vri-es commented Jul 8, 2025

This PR makes sure that disabled log statements are still typechecked.

Essentially, it just substitutes false for the log filter instead of outputting different code for build-time disabled logs.

I've done some quick verification in a sample project to ensure that the code is still completely removed from the binary: the size of the binary code did not change with or without this commit applied, tested both with the log statements enabled and disabled.

It will almost certainly increase compile times a little for projects with many normally disabled logs, but I believe the trade-off of type-checking disabled logs is worth it.

@de-vri-es de-vri-es force-pushed the typecheck-disabled-logs branch 2 times, most recently from c3da86d to 58ae56e Compare July 8, 2025 08:30
Without this change, the body of log statements is completely removed
from the code if the log statement is disabled by the `DEFMT_LOG` env
variable. This means that even invalid log statements will compile.

This change ensures that the code logging code is still visible to the
compiler, but inside an `if false { ... }` block, so it can easily be
thrown away by the compiler after type checking.
@de-vri-es de-vri-es force-pushed the typecheck-disabled-logs branch from 58ae56e to 8beb74a Compare July 8, 2025 08:34
@de-vri-es
Copy link
Contributor Author

de-vri-es commented Jul 8, 2025

CI is now picking up an error in the hints.md book. I think this is a nice example why I think we need this 😄

@de-vri-es
Copy link
Contributor Author

de-vri-es commented Jul 8, 2025

Added a UI test to verify that disabled logs are type checked, and run UI tests twice: once with DEFMT_LOG=trace and once with DEFMT_LOG=off (in separate files, so they run in their own process and their environment variables can't influence each-other).

@de-vri-es
Copy link
Contributor Author

Let me know if you think this needs more tests regarding binary size or compile times!

@thejpster
Copy link

This is very interesting! I hope to find time to look at it soon.

@de-vri-es
Copy link
Contributor Author

Polite ping :)

Copy link
Member

@Hoverbear Hoverbear left a comment

Choose a reason for hiding this comment

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

My initial impression is that this makes sense. Checking things even when the logs are disabled means the code stays correct, and that's always good.

I'm very glad you tested binary size impact, thank you. I believe @thejpster and I discussed spending a bit of time checking this a tad more before a merge.

This change doesn't appear to break any code that was not already subtly broken. I'm not sure how this interacts with the stability promises of https://ferrous-systems.com/blog/defmt-1-0-released/. Technically, making this change would only reveal already existing bugs in tested with some logs disabled, so we need to ask ourselves if our stability promise gives us permission to break code that already would break, provided the right build time arguments...

@jonathanpallant
Copy link
Contributor

Perhaps one path forward is to merge this, plan the next release, and do a blog post telling everyone what we're about to release. Then leave it some period of time for anyone to object, and we can then do the release with people having been warned.

@Hoverbear
Copy link
Member

Circling back, I did some testing with https://github.com/ferrous-systems/rust-exercises/tree/main/nrf52-code/radio-app and noted that the binary sizes did not change, debug or release, or because of DEFMT_LOG setting between this branch and main. So I think it's fine.

@de-vri-es
Copy link
Contributor Author

Thanks for testing! <3

@jonathanpallant
Copy link
Contributor

Perhaps one path forward is to merge this, plan the next release, and do a blog post telling everyone what we're about to release. Then leave it some period of time for anyone to object, and we can then do the release with people having been warned.

Absent any other ideas, let's do this.

@jonathanpallant
Copy link
Contributor

Thank you for this PR!

@jonathanpallant jonathanpallant added this pull request to the merge queue Sep 8, 2025
Merged via the queue into knurling-rs:main with commit 82d590e Sep 8, 2025
26 checks passed
@de-vri-es de-vri-es deleted the typecheck-disabled-logs branch September 8, 2025 09:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants