leanprover / leanprover/lean4

`extractMacroScopes` incorrectly parses context component of names

Open
#13,197 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Prerequisites
Description

extractMacroScopes has not been updated to reflect the current syntax for the context part of macro scopes.

Old grammar:
<actual name>._@.(<ctx>.<scopes>)*.<ctx>._hyg.<scopes>

New grammar (for just the last <ctx>? for all of them?):
<main module>.<uniq>._hygCtx

It ends up breaking the name into parts incorrectly due to the presence of the <uniq> number in the context portion of the name.

Context

Discovered while reading the MacroM code at the end of Prelude.lean.

Steps to Reproduce
  1. Create an ident Syntax using a syntax quotation.
  2. Extract the Name from the ident, which should have macro scopes inserted.
  3. Call Lean.extractMacroScopes on the Name value.
  4. Inspect the ctx and imported fields of the resulting MacroScopesView value.
import Lean

open Lean Elab Command

#eval show CommandElabM _ from do
  let stx ← `(foo)
  let generatedName := stx.raw.getId
  logInfo s!"generatedName: {generatedName}"

  let parts := extractMacroScopes generatedName
  logInfo <|
    s!"Extracted MacroScopesView:" ++
    s!"\n  name:     {parts.name}" ++
    s!"\n  imported: {parts.imported}" ++
    s!"\n  ctx:      {parts.ctx}" ++
    s!"\n  scopes:   {parts.scopes}"

  let expectedCtx ← liftMacroM MonadQuotation.getContext
  if parts.ctx ≠ expectedCtx then
    logError s!"{parts.ctx} should be {expectedCtx}"
  if parts.imported ≠ .anonymous then
    logError s!"{parts.imported} should be Name.anonymous"

Expected behavior:
parts.imported should be Name.anonymous
parts.ctx should be <main module>.<uniq>._hygCtx

Actual behavior:
parts.imported is <main module>.<uniq>
parts.ctx is _hygCtx

Versions

4.29.0
4.30.0-nightly-2026-03-30

Additional Information
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 reading extractMacroScopes and the MacroM code at the end of Prelude.lean, then run the minimal Lean example from the issue to inspect the parsed fields. The fix is complete when the generated name yields Name.anonymous for imported and the full <main module>.<uniq>._hygCtx value for ctx.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.