Fix deferred init narrowing #1446#1449
Open
dima-starosud wants to merge 1 commit into
Open
Conversation
dima-starosud
force-pushed
the
fix/deferred-init-narrowing
branch
4 times, most recently
from
July 16, 2026 07:45
039712a to
8041163
Compare
…ler context. Use construct_from(lambda) so constexpr initializers are not lost across a call boundary, matching immediate initialization behavior. Note, change out::construct to use brace-init in all paths to be consistent and to align with deferred init. Add regression tests for literals (negative too), constexpr, aggregates. Update snapshots affected by the change.
dima-starosud
force-pushed
the
fix/deferred-init-narrowing
branch
from
July 17, 2026 10:13
8041163 to
2361de1
Compare
dima-starosud
commented
Jul 17, 2026
| construct_from([&]() -> T { return T{CPP2_FORWARD(args)...}; }); | ||
| } | ||
|
|
||
| constexpr auto construct_from(auto&& factory) -> void |
Author
There was a problem hiding this comment.
With the factory, all the values are still on the caller context, which means their constexpr-ness isn't lost during crossing the call boundaries.
dima-starosud
commented
Jul 17, 2026
| if constexpr (requires { *t = CPP2_FORWARD(factory)(); }) { | ||
| cpp2_default.enforce( t ); | ||
| *t = T(CPP2_FORWARD(args)...); | ||
| *t = CPP2_FORWARD(factory)(); |
Author
There was a problem hiding this comment.
Here, instead of parens-init we switched to braces-init to align with deferred init, which is also used in out down below.
dima-starosud
commented
Jul 17, 2026
| y0.construct(sqrt(x0)); | ||
| y.construct(CPP2_UFCS(sqrt)(x, x0)); | ||
| y0.construct_from([&]() -> typename CPP2_TYPEOF(y0)::value_type { return typename CPP2_TYPEOF(y0)::value_type{sqrt(x0)}; }); | ||
| y.construct_from([&]() -> typename CPP2_TYPEOF(y)::value_type { return typename CPP2_TYPEOF(y)::value_type{CPP2_UFCS(sqrt)(x, x0)}; }); |
Author
There was a problem hiding this comment.
We could introduce a macro for this. If we decide to, I can bring it in this PR.
dima-starosud
commented
Jul 17, 2026
| @@ -0,0 +1,57 @@ | |||
| pure2-bugfix-for-deferred-init-narrowing-matrix-error.cpp2:19:125: error: non-constant-expression cannot be narrowed from type 'cpp2::impl::in<cpp2::i16>' (aka 'const short') to 'signed char' in initializer list [-Wc++11-narrowing] | |||
Author
There was a problem hiding this comment.
This tests errors, i.e. failing cases, when we do want the compilation to fail.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix deferred init narrowing #1446 by keeping brace-init in caller context.