Skip to content

std.debug.assert should not be special-cased in test mode #1304

Closed

Description

@andrewrk

Here's the current implementation of std.debug.assert:

/// This function invokes undefined behavior when `ok` is `false`.
/// In Debug and ReleaseSafe modes, calls to this function are always
/// generated, and the `unreachable` statement triggers a panic.
/// In ReleaseFast and ReleaseSmall modes, calls to this function are
/// optimized away, and in fact the `unreachable` gives extra information to
/// the optimizer to work with.
pub fn assert(ok: bool) void {
    if (!ok) {
        // In ReleaseFast test mode, we still want assert(false) to crash, so
        // we insert an explicit call to @panic instead of unreachable.
        // TODO we should use `assertOrPanic` in tests and remove this logic.
        if (builtin.is_test) {
            @panic("assertion failure");
        } else {
            unreachable; // assertion failure
        }
    }
}

We turn asserts into calls to @panic in test mode so that assert() in tests will be guaranteed to panic in ReleaseFast and ReleaseSmall modes.

@thejoshwolfe pointed out that the point of testing in ReleaseFast mode and ReleaseSmall mode is to test the actual code that will be run in these modes. So instead of changing the behavior of assert in these modes, we should have a different function to call during testing, and keep asserts in the implementation code.

Activity

  1. added
    proposal
    on Jul 30, 2018
  2. added this to the 0.4.0 milestone on Jul 30, 2018
  3. PavelVozenilek commented on Jul 30, 2018

    @PavelVozenilek

    If tests are compiled in (regardless of debug/release mode), then make all asserts active. If they are not, make them no-op. No exceptions*.

    Then there is one special case, when you intentionally stress-test the code in a way that triggers reasonably placed useful assert. For this I made special version of assert (called verify), which does nothing if it gets false inside a test. If it is triggered from a normal (non-test) code it fires as expected. If tests are not compiled in, it is no-op.


    * If benchmarking support/PGO/etc is implemented via the testing mechanism (#1010 (comment)), then there will be exceptions, however this is advanced feature and whoever does it should be expected to understand its consequences.

  4. added
    standard library
    accepted
    on Nov 21, 2018
  5. andrewrk commented on Nov 21, 2018

    @andrewrk
    MemberAuthor

    We're going to have to edit a lot of tests to use std.debug.assertOrPanic before implementing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    acceptedproposalstandard library

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions