struct compound literal initialization - #302
Draft
lorettayao wants to merge 7 commits into
Draft
Conversation
jserv
requested changes
Sep 28, 2025
jserv
left a comment
Collaborator
There was a problem hiding this comment.
- Consolidate git commit messages by enforcing the rules specified by How to Write a Git Commit Message carefully
- Don't leave dead code
- Don't mention your name (loretta) in code
visitorckw
reviewed
Sep 28, 2025
visitorckw
reviewed
Sep 28, 2025
DrXiao
reviewed
Sep 28, 2025
jserv
reviewed
Sep 30, 2025
jserv
reviewed
Sep 30, 2025
jserv
reviewed
Sep 30, 2025
jserv
requested changes
Oct 5, 2025
jserv
left a comment
Collaborator
There was a problem hiding this comment.
Rebase the latest master branch.
jserv
requested changes
Oct 5, 2025
jserv
left a comment
Collaborator
There was a problem hiding this comment.
Always minimize the necessary changes.
jserv
requested changes
Oct 5, 2025
jserv
left a comment
Collaborator
There was a problem hiding this comment.
Minimize the necessary changes.
jserv
requested changes
Oct 5, 2025
jserv
left a comment
Collaborator
There was a problem hiding this comment.
Check https://github.com/sysprog21/shecc/blob/master/CONTRIBUTING.md carefully and enforce the rules.
Investigated the bug where compound literals like
(struct point){10, 20, 30} produced shifted field values
(e.g., x got garbage, y=10, z=20). Added a temporary fix
by adjusting the source address +4 bytes in parser.c.
Tested with simple cases: x=10, y=20, z=30. This is an
experimental patch; needs review for design correctness.
Refs: sysprog21#299
Previously, array compound literals such as (int[]){100, 200, 300}
failed with an "Unexpected token" error. Struct compound literals
worked correctly, but array syntax [] was unhandled in the parser.
This change adds proper array compound literal handling, including
scalar and pointer contexts. In scalar context, the literal returns
its first element (e.g. int x = (int[]){100,200}; yields 100). In
pointer context, it allocates and initializes backing storage and
returns the address (e.g. int *p = (int[]){100,200};). Arithmetic
expressions using literals (e.g. 50 + (int[]){100}) also evaluate
correctly.
Additional fixes include:
- Consume missing '{' token for int/char compound literals
- Add return statements to prevent control flow fall-through
- Prevent segfaults in pointer assignments by allocating memory
As a result, shecc now supports array compound literals alongside
existing struct compound literals, improving C99 compatibility and
preserving self-hosting capability.
Known limitations remain: designated initializers and complex
expression combinations are still unsupported.
lorettayao
force-pushed
the
fix-compound-literals
branch
from
October 6, 2025 08:42
c215684 to
6c8154b
Compare
jserv
requested changes
Oct 6, 2025
jserv
left a comment
Collaborator
There was a problem hiding this comment.
Check https://github.com/sysprog21/shecc/blob/master/CONTRIBUTING.md carefully and indent C source files with clang-format version 18.
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.
This PR is a work-in-progress for issue #299.
adjusting the source address by +4 bytes in parser.c.
Summary by cubic
Fix struct compound literal initialization in the parser so fields are assigned correctly instead of being shifted. Adds explicit brace-initializer emission and corrects assignment handling, addressing issue #299.