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

Issue 22300 - Allow shared <-> unshared reinterpreting casts during CTFE #13203

Merged
merged 1 commit into from Oct 20, 2021

Conversation

MoonlightSentinel
Copy link
Contributor

@MoonlightSentinel MoonlightSentinel commented Oct 19, 2021

CTFE evaluation is single threaded only, hence it's safe to discard the
shared qualifier in a reinterpreting cast.

This change allows -checkaction=context to use a reinterpreting cast
for compile and runtime - which removes the problematic workaround
that caused the regression.

CC @Geod24

CTFE evaluation is single threaded only, hence it's safe to discard the
`shared` qualifier in a reinterpreting cast.

This change allows `-checkaction=context` to use a reinterpreting cast
for compile and runtime - which removes the problematic workaround
that caused the regression.
@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @MoonlightSentinel! 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
22300 regression [REG 2.098-rc.2] `-checkaction=context` of a `shared` type with an `opCast` fails to compile

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 "stable + dmd#13203"

@MoonlightSentinel MoonlightSentinel marked this pull request as draft October 19, 2021 13:02
@thewilsonator
Copy link
Contributor

cc @UplinkCoder

@MoonlightSentinel MoonlightSentinel marked this pull request as ready for review October 19, 2021 13:08
@UplinkCoder
Copy link
Member

@MoonlightSentinel CTFE must not expose the undefined behavior.

@UplinkCoder
Copy link
Member

@MoonlightSentinel I'd like to see the usecase first before coming to a decision is this is okay to do.

@MoonlightSentinel
Copy link
Contributor Author

There's no undefined behavior involved. Casting away shared is simply a way for the programmer to declare that there won't be any race conditions when accessing the underlying storage (e.g. because it's guarded by proper synchronization primitives).

@MoonlightSentinel
Copy link
Contributor Author

@MoonlightSentinel I'd like to see the usecase first before coming to a decision is this is okay to do.

It's intended as a fallback for code that would normally use atomicLoad but should also work during CTFE.

@UplinkCoder
Copy link
Member

@MoonlightSentinel If you intended to have core.atomic "just work" at compile time I'd prefer to register them as builit-ins.
That way have the option of faulting if we statically detect a possible race.

@MoonlightSentinel
Copy link
Contributor Author

@MoonlightSentinel If you intended to have core.atomic "just work" at compile time I'd prefer to register them as builit-ins.

That doesn't work for types not supported by atomicLoad.

@UplinkCoder
Copy link
Member

I dislike the idea of allowing shared to be cast away at CTFE without having had time to think about it properly.
Can we -preview-flag this?

@MoonlightSentinel
Copy link
Contributor Author

MoonlightSentinel commented Oct 19, 2021

Certain constructs not working in CTFE is good if the results are not portable (rely on UB as you mentioned before). But removing shared in an environment known to be single threaded cannot result in UB.

Note that this PR retains the restriction for const / immutable

@UplinkCoder
Copy link
Member

@MoonlightSentinel I'll merge it if it's behind a preview.

@MoonlightSentinel
Copy link
Contributor Author

(Also note that this is already supported for basic integral types)

@UplinkCoder
Copy link
Member

(Also note that this is already supported for basic integral types)

That's a concerning.

@ljmf00
Copy link
Member

ljmf00 commented Oct 19, 2021

I don't think this is a problem. CTFE variables are evaluated in a single-threaded environment. Also, I think resolving this and other reinterpret cast issues are a good way forward to make CTFE less restrictive.

Copy link
Member

@Geod24 Geod24 left a comment

Choose a reason for hiding this comment

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

I hate special cases, but it does have proven utility and no foreseeable downside.

@Geod24 Geod24 merged commit 7f9ce45 into dlang:stable Oct 20, 2021
@Geod24
Copy link
Member

Geod24 commented Oct 20, 2021

Oh, I just realized why you CCed me. Forgot about that issue. And yeah, we disabled ldc-master testing in our project over it.

@MoonlightSentinel MoonlightSentinel deleted the ctfe-shared-reinterpret branch October 20, 2021 08:27
MoonlightSentinel added a commit to MoonlightSentinel/druntime that referenced this pull request Oct 20, 2021
Using the same method for compile- and runtime was enabled by
dlang/dmd#13203. Using a reinterpreting cast avoids any potentially
calling into user-defined `opCast` methods which might reject the cast.
@MoonlightSentinel
Copy link
Contributor Author

I hate special cases, but it does have proven utility and no foreseeable downside.

Me neither. CTFE could even safely support mutable <-> immutable because it keeps track whether the referenced value is actually mutable and hence rejects invalid modifications.

But that's out of scope for this PR.

dlang-bot pushed a commit to dlang/druntime that referenced this pull request Oct 20, 2021
Using the same method for compile- and runtime was enabled by
dlang/dmd#13203. Using a reinterpreting cast avoids any potentially
calling into user-defined `opCast` methods which might reject the cast.
kinke pushed a commit to ldc-developers/dmd-testsuite that referenced this pull request Jul 11, 2022
Using the same method for compile- and runtime was enabled by
dlang/dmd#13203. Using a reinterpreting cast avoids any potentially
calling into user-defined `opCast` methods which might reject the cast.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
6 participants