cgen: fix "initializer element is not constant" for const fixed arrays with struct const elements#27852
Open
CG-SS wants to merge 1 commit into
Open
cgen: fix "initializer element is not constant" for const fixed arrays with struct const elements#27852CG-SS wants to merge 1 commit into
CG-SS wants to merge 1 commit into
Conversation
…s with struct const elements
Contributor
|
@codex review |
|
Codex Review: Didn't find any major issues. Hooray! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
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.
What this PR does
Fixes C compilation of fixed-array constants whose elements reference other
struct constants:
Before this change, cgen emits a static C initializer for
all:which fails to compile with gcc (and tcc):
because struct consts like
_const_main__firstare runtime-initialized Cglobals, not C constant expressions.
Why it happens
check_expr_is_constinvlib/v/gen/c/cgen.vdecides whether a constfixed-array initializer can be emitted as a static C initializer. Its
ast.Identarm classifies any ident naming a V const as C-constant,excluding only consts that are themselves fixed arrays:
The
!= .array_fixedexclusion patches one symptom of the underlyingmisclassification. Struct consts (and string consts) hit exactly the same
hole: they are also runtime-initialized globals and are equally invalid in
static initializers.
The fix
Replace the type-based special case with the correct general predicate that
the AST already provides:
An ident naming a V const is a valid C constant expression only when that
const is emitted as a simple
#define'd literal (char/float/int literalconsts). Everything else routes to the existing runtime-init fallback
(
const_decl_init_later_msvc_string_fixed_array), which already handlesident elements via element-wise assignment in
_vinitwith dependency-ordered initialization (
dep_names), so no other changes are needed. Theemitted C for the example becomes:
This also closes the sibling latent hole where a struct literal element
containing a string-const ident field would pass the
ast.StructInitcheck and hit the same C error.
Behavior is preserved for the cases that work today:
const arr = [a, b]!with int/float literal consts still compiles to a static initializer, since
those consts are
#define'd literals (covered by the added test).Test
Adds
vlib/v/tests/consts/const_fixed_array_of_struct_const_refs_test.vcovering:
this PR, passes after),
Validation done
one, with both tcc and gcc (no cc fallback needed).
v test vlib/v/tests/consts/passes (the only failure in my environmentis
const_resolution_test.v, which needs X11 headers forsokol.sappand fails identically on the unpatched compiler).