Skip to content

RFC: Encode zero kind_id semantic in return type - #689

Open
foxidokun wants to merge 1 commit into
TheDan64:masterfrom
foxidokun:master
Open

RFC: Encode zero kind_id semantic in return type#689
foxidokun wants to merge 1 commit into
TheDan64:masterfrom
foxidokun:master

Conversation

@foxidokun

Copy link
Copy Markdown
Contributor

Description

LLVM returns id=0 for unknown attribute ids. This patch encode this semantic via zero-cost Option, which will provide .unwrap() and other convenient methods.

Reasoning

I'd like to have convenient .unwrap(), .ok_or() and other methods to validate that attribute exist. However, this patch is pure cosmetics, so feel free to drop it

Considerations

  • NonZeroU32 has kinda ugly conversion API
  • Should we refactor functions that accept kind_id to NonZeroU32 too? Like Context::create_enum_attribute

Checklist

LLVM returns id=0 for unknown attribute ids. We should encode this
semantic via zero-cost Option<NonZeroU32>, which will provide .unwrap()
and other convinient methods.
@TheDan64
TheDan64 self-requested a review April 22, 2026 18:58

@TheDan64 TheDan64 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Change looks good but tests need to be updated to pass the suite

@ErisianArchitect

Copy link
Copy Markdown
Contributor

This is a breaking change, is it not? What's the need for Option here?

@TheDan64

TheDan64 commented Apr 22, 2026

Copy link
Copy Markdown
Owner

That's a good point that it's a breaking change and should have been documented as such. But we do make breaking changes from time to time since we don't have a stable API due to LLVM version changes. So I'm not necessarily opposed to the change... I do think that Option makes it clearer that the attribute doesn't exist rather than relying on implicit C semantics which an inkwell user may not be familiar with. Perhaps it should just be Option<u32> for simplicity?

@TheDan64

Copy link
Copy Markdown
Owner

It seems like it'd be nicer to make the whole thing a bit more typesafe, with KindId transparent wrappers, and const attribute values for reference, then you could get one dynamically, or use the const and provide it to create_enum_attribute as OP mentioned

@foxidokun

foxidokun commented Apr 23, 2026

Copy link
Copy Markdown
Contributor Author

This is a breaking change, is it not?

Yes, it is, but not very big. Wasn't documented because it's just RFC

Option for simplicity

Yes, this will be more usable, but will lose"zero-costness" (memory layout optimizations), but it's not a significant loss.

the whole thing a bit more typesafe

I agree, I'll try to play with this idea of wrappers. TBH I'd really like to distinguish Attribute::get_named_enum_kind and Context::get_kind_id, they has similar names and sometimes passing one's id to another works (sad experience...)

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.

3 participants