`lean --deps` panics in `HeaderSyntax.imports` on a header that does not parse

Open
#16,464 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
Half a day
Newbie friendliness
62/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Domain
cli, compilers

Research direction

Start with Lean.Elab.parseImports, which passes the result of Parser.parseHeader to headerToImports without checking its messages, then with HeaderSyntax.imports in Lean/Elab/Import.lean, where the unreachable! at line 40 is hit. Reproduce with printf 'import\n' > W.lean && lean --deps W.lean. Done when the header's parse error is reported with a non-zero exit and no panic, for import, import Init., import 3, a tab before import, and a lone carriage return.

Written by the indexing model from the issue text.

Description

Description

lean --deps panics on any file whose header doesn't parse, then goes on to look up a module named [anonymous]. lean on the same file reports the parse error as a normal message.

Steps to Reproduce
printf 'import\n' > W.lean
lean --deps W.lean

Expected behavior: the header's parse error, as lean W.lean reports it (W.lean:2:0: error: unexpected end of input; expected identifier), with a non-zero exit and no panic.

Actual behavior:

PANIC at Lean.Elab.HeaderSyntax.imports Lean.Elab.Import:40:13: unreachable code has been reached
backtrace:
…
<toolchain>/lib/lean/Init.olean
<toolchain>/lib/lean/Init.olean
unknown module prefix '[anonymous]'

No directory '[anonymous]' or file '[anonymous].olean' in the search path entries:
<toolchain>/lib/lean

The same happens for every header error I tried: import Init., import 3, a tab before an import ("tabs are not allowed"), and an isolated carriage return.

Cause

Lean.Elab.parseImports passes the header that Parser.parseHeader returns to headerToImports without checking the returned messages for errors. On the error-recovered syntax, neither pattern in HeaderSyntax.imports matches, so it reaches unreachable!. printImports (--deps) and printImportSrcs (--src-deps) both go through it. So does any program that calls parseImports to read a header's imports and its end position.

Versions

Lean (version 4.34.1, aarch64-unknown-linux-gnu, commit 5045d0056413266e57c625dcd7c365b10e377c52, Release), on Linux.

Impact

A tool that reads a module's imports with parseImports or lean --deps, as a build driver does before passing them through --setup, has to check the header's messages itself first.

Dominant language
Lean
Stars
10.3k
Forks
1.1k
Avg merge
19h 8m
Merged PRs (30d)
421

Getting set up

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.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More CLI issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.