Can assert! be fixed to be hygienic?

core::panic! and std::panic! are different, which forces the assert! macro to be unhygienic (see issues 56389 and 61567 which were closed as wont-fix).

I'm currently working on implementing improved messaging for assert! but while working on it I had the question: is it possible for assert! to expand to if ... { $crate::panic!(...) }? This would make core::assert! use core::panic! and std::assert! use std::panic!.

Normally this could be done by just having two different assert! implementations in each crate, but since assert! is a builtin I'm not sure if this is possible/feasible. To summarize:

  1. Is it possible/feasible for the builtin assert! to use $crate (or something like it) that expands to core if calling core::assert! (and expanding to std if calling std::assert!)? This might be simplest to implement by making assert! a real macro that forwards its $crate metavariable to the builtin (and renaming the builtin macro), assuming that can be done.
  2. Are there any drawbacks to doing this? This might result in someone using core::assert! in situations where std::assert! is available (and thus using core::panic! instead of std::panic!), but I'm not sure how bad that would be.

*Side question: what is the difference between core::panic! and std::panic!? It would be nice to document the difference because it's not obvious how core::panicking::panic and std::rt::begin_panic behave (I assume the difference is something like std printing to stderr but core not doing that?)

4 Likes

Is it built-in? I thought it was just part of the standard library. cc @petrochenkov

It was a part of the standard library, but it was moved to built-ins to implement "Tracking issue for RFC 2011: nicer assert messages". Besides the move itself there was no progress though.

1 Like

I'd recommend not to block the assert work on the panic hygiene issue and use what all other macros use - any panic that is currently in scope at call site.

I wasn't going to block my ongoing assert! work on the hygiene issue. Rather I was thinking of following up with the hygiene issue once the nicer assertion messages are done. I wanted to have this conversation in parallel though.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.