leanprover / leanprover/lean4

Incorrect parsing of non-indented `where` declaration followed by `declModifiers`

Open
#13,934 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Prerequisites
Description

Good afternoon! I have run into some strange behavior where an unindented where declaration will cause the next command to not be parsed correctly if it has declaration modifiers above it. Folks on Zulip believe that this may be caused by the modifier initially being parsed as belonging to another declaration in the current where, causing a def or similar keyword to be rejected due to it not backtracking.

Context

Zulip thread: https://leanprover.zulipchat.com/#narrow/channel/239415-metaprogramming-.2F-tactics/topic/Strange.20declaration.20parsing.20behavior/with/599627787

Context: I was working on some unrelated metaprogramming stuff.

Steps to Reproduce
def fun1 (x : Nat) : Nat :=
  aux1 x
where
aux1 (y : Nat) : Nat := y; -- Problem only if this isn't indented

/-- Everything works ok without a declModifier (doc comment, attribute, etc.) -/
def fun2 (x : Nat) : Nat := x -- unexpected token 'def'; expected '(', ':=', '|' or term

Expected behavior: fun2 should have been correctly parsed and elaborated.

Actual behavior: The parser throws an error when reaching the def keyword ("unexpected token 'def'; expected '(', ':=', '|' or term")

Versions

v4.31.0-rc1 (I used live.lean-lang.org)

Additional Information

N/A

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 example from the issue in live.lean-lang.org and confirm that the unindented where declaration followed by the documented modifier reproduces the parse error. Then trace the parser handling of where, declaration modifiers, and the following def. Done means the example parses and fun2 is correctly elaborated without regressing the existing syntax.

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
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.