Skip to content

[std.checkedint] Saturate claims to support bitwise operators, but doesn't #11017

@ichordev

Description

@ichordev

Saturate's documentation claims that it supports &, |, ^, &=, |=, and ^=. However, attempting to actually use any of these triggers the static assert(false) on line 2597 in checkedint.d. For example:

import std.checkedint;

void main(){
	auto x = checked!Saturate(10);
	x &= 10;
}

When run through run.dlang.io produces this error:

/dlang/dmd/linux/bin64/../../src/phobos/std/checkedint.d(2597): Error: static assert:  `false` is false
            static assert(false);
            ^
/dlang/dmd/linux/bin64/../../src/phobos/std/checkedint.d(986):        instantiated from here: `onOverflow!("&", int, const(int))`
            if (overflow) r = hook.onOverflow!op(payload, rhs);
                                                ^
/dlang/dmd/linux/bin64/../../src/phobos/std/checkedint.d(952):        instantiated from here: `opBinaryImpl!("&", int, Checked!(int, Saturate))`
        return opBinaryImpl!(op, Rhs, typeof(this))(rhs);
                                                   ^
/dlang/dmd/linux/bin64/../../src/phobos/std/checkedint.d(1196):        instantiated from here: `opBinary!("&", int)`
            auto r = opBinary!op(rhs).get;
                                ^
onlineapp.d(5):        instantiated from here: `opOpAssign!("&", int)`
    x &= 10;
      ^

This is because Saturate.onOverflow simply lacks a case for &, |, or ^, and so it defaults to the static assert.
It shouldn't be very hard to either add cases for these operators (although I'm not sure what would be considered desirable behaviour for them, discuss in the comments if you want), or just remove the falsehoods from the documentation.

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions