Skip to content

Fix deferred init narrowing #1446#1449

Open
dima-starosud wants to merge 1 commit into
hsutter:mainfrom
dima-starosud:fix/deferred-init-narrowing
Open

Fix deferred init narrowing #1446#1449
dima-starosud wants to merge 1 commit into
hsutter:mainfrom
dima-starosud:fix/deferred-init-narrowing

Conversation

@dima-starosud

@dima-starosud dima-starosud commented Jul 15, 2026

Copy link
Copy Markdown

Fix deferred init narrowing #1446 by keeping brace-init in caller 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 dima-starosud changed the title Fix deferred init narrowing #1446 Draft: Fix deferred init narrowing #1446 Jul 15, 2026
@dima-starosud
dima-starosud force-pushed the fix/deferred-init-narrowing branch 4 times, most recently from 039712a to 8041163 Compare July 16, 2026 07:45
…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
dima-starosud force-pushed the fix/deferred-init-narrowing branch from 8041163 to 2361de1 Compare July 17, 2026 10:13
@dima-starosud dima-starosud changed the title Draft: Fix deferred init narrowing #1446 Fix deferred init narrowing #1446 Jul 17, 2026
Comment thread include/cpp2util.h
construct_from([&]() -> T { return T{CPP2_FORWARD(args)...}; });
}

constexpr auto construct_from(auto&& factory) -> void

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

Comment thread include/cpp2util.h
if constexpr (requires { *t = CPP2_FORWARD(factory)(); }) {
cpp2_default.enforce( t );
*t = T(CPP2_FORWARD(args)...);
*t = CPP2_FORWARD(factory)();

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Here, instead of parens-init we switched to braces-init to align with deferred init, which is also used in out down below.

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)}; });

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

We could introduce a macro for this. If we decide to, I can bring it in this PR.

@@ -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]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This tests errors, i.e. failing cases, when we do want the compilation to fail.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant