Skip to content

Fix "... must be defined only once in the type namespace of this module" and NonFixedWidthArray serialisation#11

Merged
alexlarsson merged 4 commits intomasterfrom
deduplicate-struct-definitions
Jul 3, 2025
Merged

Fix "... must be defined only once in the type namespace of this module" and NonFixedWidthArray serialisation#11
alexlarsson merged 4 commits intomasterfrom
deduplicate-struct-definitions

Conversation

@wmanley
Copy link
Member

@wmanley wmanley commented Jul 3, 2025

For types where a struct is repeated. Example:

let data = gv!("(a{sv}tayay(a{sv}aya(say)sstayay)aya(uayttay)a(yaytt))").cast(&superblock);

Fails with:

|         let data = gv!("(a{sv}tayay(a{sv}aya(say)sstayay)aya(uayttay)a(yaytt))").cast(&superblock);
|                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `StructureFsv3` redefined here
|
= note: `StructureFsv3` must be defined only once in the type namespace of this module
= note: this error originates in the macro `_define_gv` which comes from the expansion of the macro `gv` (in Nightly builds, run with -Z macro-backtrace for more info)

Reported by @alexlarsson in #10

Fixes: #10

wmanley added 3 commits July 3, 2025 15:56
For types where a struct is repeated.  Example:

    let data = gv!("(a{sv}tayay(a{sv}aya(say)sstayay)aya(uayttay)a(yaytt))").cast(&superblock);

Fails with:

    |         let data = gv!("(a{sv}tayay(a{sv}aya(say)sstayay)aya(uayttay)a(yaytt))").cast(&superblock);
    |                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `StructureFsv3` redefined here
    |
    = note: `StructureFsv3` must be defined only once in the type namespace of this module
    = note: this error originates in the macro `_define_gv` which comes from the expansion of the macro `gv` (in Nightly builds, run with -Z macro-backtrace for more info)

Reported by @alexlarsson in #10.
...We want to align the current element, not add padding after each element
for the next one.  The offsets that come after the data must not be padded.
This lead to incorrect serialisation.
This test would fail if either of the previous commits are missing.
Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @wmanley, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses two distinct but related issues: a Rust compiler error that occurs when the gv! macro attempts to redefine types for repeated GVariant structures, and an issue with the serialization of NonFixedWidthArray types. The changes ensure that complex GVariant type definitions are correctly generated and serialized, improving the robustness of the gvariant and gvariant-macro crates.

Highlights

  • Macro Type Generation Fix: Implemented a mechanism within the gv! macro's type generation logic to track and prevent redundant type definitions. This resolves the Rust compiler error "must be defined only once in the type namespace of this module" when GVariant types are repeated within a complex structure.
  • NonFixedWidthArray Serialization Correction: Adjusted the serialization order for NonFixedWidthArray types to ensure padding is applied correctly before the item's data is written, leading to accurate offset calculations and proper serialization.
  • New Test Case: Added a new test (test_repeated_types) to validate the correct handling of repeated GVariant types during both serialization and deserialization, specifically for dictionary arrays.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses two separate issues. First, it resolves a compiler error about type redefinition when using GVariant types with repeated structures by tracking defined types within a macro expansion. Second, it corrects a critical serialization bug in NonFixedWidthArray where elements were not being properly aligned. The changes are logical, and the new test case provides good coverage for both fixes. Overall, this is a solid contribution that improves both the usability and correctness of the library.

Comment on lines 1631 to 1637
for x in self.into_iter() {
bytes_written += x.serialize(f)?;
offsets.push(bytes_written);
let padding = align_offset::<GvT::AlignOf>(bytes_written).to_usize() - bytes_written;
f.write_all(&b"\0\0\0\0\0\0\0"[..padding])?;
bytes_written += padding;
bytes_written += x.serialize(f)?;
offsets.push(bytes_written);
}

Choose a reason for hiding this comment

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

critical

This is an excellent and critical fix for NonFixedWidthArray serialization.

The previous implementation had a logic error where padding was calculated and written after the element was serialized. This meant that the element itself was not guaranteed to be correctly aligned, which is a violation of the GVariant specification.

The new implementation correctly calculates and writes the necessary padding before serializing the element, ensuring that each element in the array is properly aligned. This resolves a significant correctness issue.

Well done for spotting and fixing this!

Copy link
Member Author

Choose a reason for hiding this comment

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

Well gee wiz and wowzers! This is an excellent review. Thank you very much @gemini-code-assist. You upbeat regurgitation of my commit message has made my day.

Well done!

@alexlarsson
Copy link
Member

Yay!

@wmanley wmanley force-pushed the deduplicate-struct-definitions branch from 206e9f0 to e0bb8d6 Compare July 3, 2025 15:16
Was failing with:

> The system library `glib-2.0` required by crate `glib-sys` was not found.
@wmanley wmanley force-pushed the deduplicate-struct-definitions branch from e0bb8d6 to 687dd2d Compare July 3, 2025 15:19
@wmanley wmanley requested a review from alexlarsson July 3, 2025 15:22
@wmanley
Copy link
Member Author

wmanley commented Jul 3, 2025

@alexlarsson: Please review and merge if you're happy.

Once it's merged rebase #9 for the CI fix and I'll merge it.

@alexlarsson
Copy link
Member

LGTM, and was able to (what it looks like) deserialize an ostree delta superblock correctly.

@alexlarsson alexlarsson merged commit 7ab5dd3 into master Jul 3, 2025
1 check passed
wmanley added a commit that referenced this pull request Jul 7, 2025
Fixes
=====

* Soundness: Fix aligment downcast check in TryAsAligned #9
  57d66be
* Correctness: NonFixedWidthArray serialisation: Write padding before data #11
  0396d80
* Fix: Fix "... must be defined only once in the type namespace of this module"
  for types where a struct is repeated #11 42165a3
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.

gv fails to handle repeated sub.types

2 participants