Skip to content

Keep generic types when translating trait alias in monomorphized mode#1087

Merged
Nadrieril merged 8 commits intoAeneasVerif:mainfrom
Sam-Ni:mono_trait_alias
May 5, 2026
Merged

Keep generic types when translating trait alias in monomorphized mode#1087
Nadrieril merged 8 commits intoAeneasVerif:mainfrom
Sam-Ni:mono_trait_alias

Conversation

@Sam-Ni
Copy link
Copy Markdown
Contributor

@Sam-Ni Sam-Ni commented Apr 6, 2026

This PR closes #1086 . We keep the generic types (such as Self) of trait aliases in their declarations and impl blocks to make it consistent with the "normal" trait declarations in mono mode.

@Nadrieril
Copy link
Copy Markdown
Member

Hm that feels wrong, the way trait aliases are translated is that they're translated twice: once as TraitDecl and once as a TraitImpl. The trait impl side should be translated as a normal RustcItem::Mono, can you justify why the special mono-trait exception is useful here?

@Sam-Ni
Copy link
Copy Markdown
Contributor Author

Sam-Ni commented Apr 7, 2026

Hm that feels wrong, the way trait aliases are translated is that they're translated twice: once as TraitDecl and once as a TraitImpl. The trait impl side should be translated as a normal RustcItem::Mono, can you justify why the special mono-trait exception is useful here?

I see. I misunderstood how impl blocks work for trait aliases. So we should keep a single trait declaration and provide separate impl blocks for different types, just like the normal traits. Is that correct?

@Nadrieril
Copy link
Copy Markdown
Member

Yep, that's what I had in mind so that trait aliases can be as close to normal traits as possible

Comment thread charon/src/bin/charon-driver/translate/translate_meta.rs Outdated
@Sam-Ni
Copy link
Copy Markdown
Contributor Author

Sam-Ni commented Apr 28, 2026

The remark is updated to include clearer explanations and examples on how trait implementations for aliases are translated in mono mode. If anything is still unclear or confusing, please let me know.

@Nadrieril Nadrieril added this pull request to the merge queue May 5, 2026
Comment on lines +444 to +446
// As an exception, we do not push generics for impls of trait aliases,
// since this information is already encoded in the impl block itself,
// just like impls of normal traits.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I still don't understand why this ever comes up, and you've not explained why there would ever be generics applied to an item which is monomorphic. But I'll accept this fix since it only applies to names

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I just expect there's a deeper bug with trait aliases + mono that will show up somewhere else, so let's keep that in mind

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I still don't understand why this ever comes up, and you've not explained why there would ever be generics applied to an item which is monomorphic. But I'll accept this fix since it only applies to names

This comes up from a difference between how normal trait impls and trait alias impls are represented in Hax. In Mono mode, the generic_args stored in the Hax item_ref for normal trait impls is empty. In contrast, for trait alias impls, the generic_args field still contains type arguments (including those for Self and other generic parameters).

At the same time, other monomorphic items (e.g., functions instantiated from generics) require an Instantiated suffix (e.g., f::<i32>) to record the concrete instantiation in their names. However, for impl blocks, the generic arguments are already encoded directly in the impl itself (e.g., impl A<i32> for u32). Therefore, I treat trait alias impls specially and avoid pushing generics for them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I just expect there's a deeper bug with trait aliases + mono that will show up somewhere else, so let's keep that in mind

I did encounter another issue related to trait aliases when translating the Rust standard library in Mono mode. I have not yet checked it in detail. I'll do that next.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In Mono mode, the generic_args stored in the Hax item_ref for normal trait impls is empty.

That doesn't seem right, an ItemRef always should have exactly the number of arguments that the item's ParamEnv expects

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I understand now: your code is right, and in fact should apply to all impls and not just trait alias impls. See the last commit of #1123.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see. The current code that deals with names is much more strucutured and general. Thank you!

Merged via the queue into AeneasVerif:main with commit 9d315ea May 5, 2026
5 checks passed
@Sam-Ni Sam-Ni deleted the mono_trait_alias branch May 5, 2026 12:10
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.

Bug: mismatched type generics appears when translating trait alias in monomorphized mode

2 participants