Skip to content

fix: handle flattened inheritance in wrapInstance#13302

Merged
Kha merged 5 commits intoleanprover:masterfrom
Kha:push-ltwqsqwnksoq
Apr 9, 2026
Merged

fix: handle flattened inheritance in wrapInstance#13302
Kha merged 5 commits intoleanprover:masterfrom
Kha:push-ltwqsqwnksoq

Conversation

@Kha
Copy link
Copy Markdown
Member

@Kha Kha commented Apr 7, 2026

Instead of unconditionally wrapping value of fields that were copied from flattened parent structures, try finding an existing instance and projecting it first.

@Kha Kha force-pushed the push-ltwqsqwnksoq branch from aaf578e to b8cb743 Compare April 7, 2026 12:09
@github-actions github-actions bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Apr 7, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 7, 2026
@github-actions github-actions bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Apr 7, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 7, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Apr 7, 2026
@leanprover-bot leanprover-bot added the builds-manual CI has verified that the Lean Language Reference builds against this PR label Apr 7, 2026
@leanprover-bot
Copy link
Copy Markdown
Collaborator

leanprover-bot commented Apr 7, 2026

Reference manual CI status:

  • ✅ Reference manual branch lean-pr-testing-13302 has successfully built against this PR. (2026-04-07 13:22:01) View Log
  • 🟡 Reference manual branch lean-pr-testing-13302 build against this PR didn't complete normally. (2026-04-07 13:23:56) View Log
  • ✅ Reference manual branch lean-pr-testing-13302 has successfully built against this PR. (2026-04-08 10:50:32) View Log
  • 🟡 Reference manual branch lean-pr-testing-13302 build against this PR didn't complete normally. (2026-04-08 10:51:41) View Log
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-04-07 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-04-09 14:25:33)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 1d1c5c6e309ee8ac6b92be4c14853ba3b146e20d --onto 861bc19e0c1c45b5766cc5c9ef0488568368b236. You can force reference manual CI using the force-manual-ci label. (2026-04-09 16:02:08)

@mathlib-lean-pr-testing mathlib-lean-pr-testing bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Apr 7, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

mathlib-lean-pr-testing bot commented Apr 7, 2026

Mathlib CI status (docs):

  • ✅ Mathlib branch lean-pr-testing-13302 has successfully built against this PR. (2026-04-07 14:10:09) View Log
  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 1d1c5c6e309ee8ac6b92be4c14853ba3b146e20d --onto 4f6bcc5adac8d6234a9e3def3675402cb89823c6. You can force Mathlib CI using the force-mathlib-ci label. (2026-04-09 16:02:06)

try
if let .some new ← trySynthInstance argExpectedType then
trace[Meta.wrapInstance] "using existing instance {new}"
mvarId.assign new
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I think there ought to be a check that new is defeq to arg, since it's possible for a class argument to be non-canonical, and in any case you'd want to be sure the existing instance is defeq at the correct transparency, right?

I know that sometimes there are explicit fields in structures that are instances, but not instance implicit, and we wouldn't want to replace those.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We should assume new and arg are not defeq at instances transparency because their types aren't - if they were defeq, we wouldn't have to replace anything in the first place, no? But maybe you mean we should test if they are equal at least at the default level and if that's not the case just skip this case and continue with the ones below?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Yeah, I'd hope they're defeq at default transparency, as a consistency check.

Rather than skipping, it might even be worth a "synthesized instance is not definitionally equal to provided instance" error — not sure!

return ← getFieldOrigin parent.structName field
let some fi := getFieldInfo? env structName field
| throwError "no such field {field} in {structName}"
return (structName, fi)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

getFieldOrigin was meant to be a #print heuristic, and I'm not sure about using it for this purpose. There can be multiple paths to a field, and not every parent needs to be a class. At least it appears to give the parent that mkProjection would pass through.

To copy the behavior of StructInst, what you'd do instead is take getAllParentStructures structName, filter by those that are classes, and then filter by those that have field as a field. Then you take the first that has a synthesizable instance (comments about that below).

@Kha Kha force-pushed the push-ltwqsqwnksoq branch from b8cb743 to 4bf6ea0 Compare April 8, 2026 09:35
@Kha Kha force-pushed the push-ltwqsqwnksoq branch from 4bf6ea0 to 5fb808a Compare April 8, 2026 09:40
mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 8, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 8, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Apr 8, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@Kha Kha force-pushed the push-ltwqsqwnksoq branch from f4564e3 to ab877ca Compare April 9, 2026 13:50
@Kha Kha changed the title fix: handle flattened inheritance in wrapInstance [REBASED] fix: handle flattened inheritance in wrapInstance Apr 9, 2026
@Kha Kha changed the base branch from master to nightly-with-mathlib April 9, 2026 13:51
@Kha Kha added the fast-ci Forces the use of large runners so that e.g. PR releases are created faster label Apr 9, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 9, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 9, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@Kha Kha changed the base branch from nightly-with-mathlib to master April 9, 2026 15:34
@Kha Kha changed the title [REBASED] fix: handle flattened inheritance in wrapInstance [REBASED] Apr 9, 2026
@Kha Kha added changelog-no Do not include this PR in the release changelog backport releases/v4.30.0 labels Apr 9, 2026
@Kha Kha marked this pull request as ready for review April 9, 2026 15:35
@Kha Kha changed the title [REBASED] fix: handle flattened inheritance in wrapInstance Apr 9, 2026
@Kha Kha enabled auto-merge April 9, 2026 15:36
@Kha Kha added this pull request to the merge queue Apr 9, 2026
Merged via the queue into leanprover:master with commit 031bfa5 Apr 9, 2026
20 of 21 checks passed
github-actions bot pushed a commit that referenced this pull request Apr 9, 2026
Instead of unconditionally wrapping value of fields that were copied
from flattened parent structures, try finding an existing instance and
projecting it first.

(cherry picked from commit 031bfa5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport releases/v4.30.0 builds-manual CI has verified that the Lean Language Reference builds against this PR builds-mathlib CI has verified that Mathlib builds against this PR changelog-no Do not include this PR in the release changelog fast-ci Forces the use of large runners so that e.g. PR releases are created faster mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants