8000 feat: `opaque_repr` attribute to suppress "trivial structure" opt by digama0 · Pull Request #2292 · leanprover/lean4 · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: opaque_repr attribute to suppress "trivial structure" opt #2292

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

digama0
Copy link
Collaborator
@digama0 digama0 commented Jun 27, 2023

This adds an attribute 8000 @[opaque_repr] which is used by both the old and new compilers to suppress the "trivial structure" optimization (which deletes mk and proj applications for newtype structures), replacing the existing is_runtime_builtin_type() function. (There is a TODO(Leo): use an attribute? comment in the code, so I am hopeful that this is a desirable direction.)

I have renamed the function to has_opaque_repr_attr though because it's not actually about builtin types, and the provided test case shows where it is useful in user code: when you are making an opaque type (i.e. a type where the mk / val / casesOn will be overridden using implemented_by), the compiler's trivial structure optimization can cause calls to the overridden functions to be removed.

The attribute name is slightly more generic than "suppress trivial structure optimization", and is intended as a general purpose attribute to put on types to direct the compiler that it should not make any assumptions about the type layout, with the default (non-overridden) behavior being that of a boxed inductive type.

@kim-em
Copy link
Collaborator
kim-em commented Oct 16, 2024

@digama0 sent me the following explanation of the underlying issue here, which I'll quote here in the hope it is useful:

Right now the compiler has what can only be described as a bug where it deletes constructors and projections of "trivial wrapper structures" in the style of ULift or Inhabited even if those constructors have an @[extern] or @[implemented_by] implementation, resulting in unsound behavior. The only reason this works for core data types like Array and String is because they are explicitly exempted in a hardcoded list in the compiler. The PR replaces this list with an attribute so that user types can also avoid the issue.

@leanprover-bot leanprover-bot added the P-low We are not planning to work on this issue label Feb 4, 2025
@kim-em kim-em requested a review from zwarich as a code owner June 4, 2025 09:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review Waiting for someone to review the PR low priority P-low We are not planning to work on this issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0