Skip to content
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

Fix Bugzilla 22293: opCast!bool for Nullable #9039

Merged
merged 1 commit into from
Aug 1, 2024

Conversation

pbackus
Copy link
Contributor

@pbackus pbackus commented Jul 31, 2024

The second opCast overload is necessary to allow built-in implicit conversions (e.g., mutable to const) to be performed explicitly via a cast expression. Nullable.opEquals depends on this behavior.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @pbackus! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

Bugzilla references

Auto-close Bugzilla Severity Description
22293 enhancement Nullable should define opCast!bool

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + phobos#9039"

@pbackus
Copy link
Contributor Author

pbackus commented Jul 31, 2024

src/text/json/ParserMarker.d(31,29): Error: none of the overloads of template `std.typecons.Nullable!(const(JSONParserRange!(JSONLexerRange!(string, LexOptions.noTrackLocation)))).Nullable.opCast` are callable using argument types `!(immutable(Nullable!(JSONParserRange!(JSONLexerRange!(string, LexOptions.noTrackLocation)))))()`
/buildkite/builds/zaphod-3/dlang/phobos/buildkite-ci-build/distribution/bin/../imports/std/typecons.d(3558,10):        Candidates are: `opCast(T : bool)() const`
/buildkite/builds/zaphod-3/dlang/phobos/buildkite-ci-build/distribution/bin/../imports/std/typecons.d(3564,16):                        `opCast(T, this This)()`
  with `T = immutable(Nullable!(JSONParserRange!(JSONLexerRange!(string, LexOptions.noTrackLocation)))),
       This = Nullable!(const(JSONParserRange!(JSONLexerRange!(string, LexOptions.noTrackLocation))))`
  must satisfy the following constraint:
`       is(This : T)`

If I'm reading this right, the cast that's failing is Nullable!(const(T)) to immutable(Nullable!(T)), which is an unsafe type coercion.

I think maybe the right solution here is for the compiler to fall back to built-in cast behavior when no suitable opCast overload is present. Forcing every library author who overloads opCast to handle edge cases like this one is not really reasonable.

@pbackus
Copy link
Contributor Author

pbackus commented Jul 31, 2024

Further examination reveals that the D compiler will let you cast between more or less any two types as long as they're the same size:

struct A { ubyte[8] x; }
struct B { int* p; }

static assert(A.sizeof == B.sizeof);

void main()
{
    A a;
    auto b = cast(B) a;
}

So I guess the correct template constraint for the second opCast overload here would be is(This : T) || (This.sizeof == T.sizeof)...as crazy as that sounds.

The second opCast overload is necessary to allow built-in implicit
conversions (e.g., mutable to const) to be performed explicitly via a
cast expression. Nullable.opEquals depends on this behavior.
@pbackus
Copy link
Contributor Author

pbackus commented Jul 31, 2024

LDC build error appears to be transient (didn't happen the first time); force-pushing to re-run.

Copy link
Member

@schveiguy schveiguy left a comment

Choose a reason for hiding this comment

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

As much as I hate this opCast boiler plate, it appears to be necessary.

@thewilsonator thewilsonator merged commit ebd24da into dlang:master Aug 1, 2024
10 checks passed
RubyTheRoobster pushed a commit to RubyTheRoobster/phobos that referenced this pull request Aug 6, 2024
The second opCast overload is necessary to allow built-in implicit
conversions (e.g., mutable to const) to be performed explicitly via a
cast expression. Nullable.opEquals depends on this behavior.
RubyTheRoobster pushed a commit to RubyTheRoobster/phobos that referenced this pull request Aug 21, 2024
The second opCast overload is necessary to allow built-in implicit
conversions (e.g., mutable to const) to be performed explicitly via a
cast expression. Nullable.opEquals depends on this behavior.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants