leanprover / leanprover/lean4

`deriving` does not handle `unsafe` (or `noncomputable`)

Open
#3,498 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug help wanted P-low
Dominant language
Lean
Stars
9.2k
Forks
990
Avg merge
1d 17h
Merged PRs (30d)
175

Description

Prerequisites
  • Put an X between the brackets on this line if you have done all of the following:
    • Check that your issue is not already filed.
    • Reduce the issue to a minimal, self-contained, reproducible test case. Avoid dependencies to mathlib4 or std4.
Description

The deriving mechanism does not copy across unsafe attributes, and so cannot be used in the presence of unsafe code.

Perhaps erroring here is deliberate to avoid accidental creation of unsafe code; in which case, deriving unsafe Repr, unsafe BEq might be an acceptable solution.

Note that the same problem also occurs for noncomputable, though that can be handled with noncomputable section

Context

Zulip message

Steps to Reproduce
  1. Run the following
    structure Foo
    
    unsafe instance : Repr Foo where
      reprPrec f n := "foo"
    
    unsafe instance : BEq Foo where
      beq f₁ f₂ := true
    
    structure Bar where
      foo : Foo
    deriving Repr, BEq
    

Expected behavior: unsafe instance : BEq Bar and unsafe instance : Repr Bar are created

Actual behavior: (kernel) invalid declaration, it uses unsafe declaration 'instReprFoo'

Versions

4.6.0-rc1

Impact

Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the minimal self-contained Lean reproduction from the issue on version 4.6.0-rc1, then trace the deriving mechanism that processes Repr and BEq instances. Done means the derived instances for Bar are created with the required unsafe status without the invalid-declaration error, with the analogous noncomputable behavior understood as well.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.