Skip to content

Commit c381346

Browse files
fix: better error message on missing declaration name for docstring
When elaborating partial syntax, the elaborator sometimes attempts to add a docstring for a declaration that it didn't parse a name for. The name defaults to anonymous, but inserting the docs for the anonymous name throws a panic about being on the wrong async branch. With this change, the reported error is the expected parser error instead, which is much friendlier.
1 parent 19f6c16 commit c381346

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

src/Lean/DocString/Add.lean

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,10 +206,12 @@ Adds an elaborated Verso docstring to the environment.
206206
-/
207207
def addVersoDocStringCore [Monad m] [MonadEnv m] [MonadLiftT BaseIO m] [MonadError m]
208208
(declName : Name) (docs : VersoDocString) : m Unit := do
209-
let throwImported {α} : m α :=
210-
throwError s!"invalid doc string, declaration '{declName}' is in an imported module"
209+
-- The decl name can be anonymous due to attempts to elaborate incomplete syntax. If the name is
210+
-- anonymous, the `MapDeclarationExtension.insert` panics due to not being on the right async
211+
-- branch. Better to just do nothing.
212+
if declName.isAnonymous then return
211213
unless (← getEnv).getModuleIdxFor? declName |>.isNone do
212-
throwImported
214+
throwError s!"invalid doc string, declaration '{declName}' is in an imported module"
213215
modifyEnv fun env =>
214216
versoDocStringExt.insert env declName docs
215217

tests/lean/versoDocMissing.lean

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
set_option doc.verso true
2+
3+
/-!
4+
This test checks that the error produced for a docstring without a corresponding declaration is a parser error instead of an internal error or panic from elaborating partial syntax.
5+
-/
6+
7+
/--
8+
foo
9+
-/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
versoDocMissing.lean:10:0: error: unexpected end of input; expected '#guard_msgs', 'abbrev', 'add_decl_doc', 'axiom', 'binder_predicate', 'builtin_dsimproc', 'builtin_dsimproc_decl', 'builtin_grind_propagator', 'builtin_initialize', 'builtin_simproc', 'builtin_simproc_decl', 'class', 'declare_simp_like_tactic', 'declare_syntax_cat', 'def', 'dsimproc', 'dsimproc_decl', 'elab', 'elab_rules', 'example', 'grind_propagator', 'inductive', 'infix', 'infixl', 'infixr', 'initialize', 'instance', 'macro', 'macro_rules', 'notation', 'opaque', 'postfix', 'prefix', 'recommended_spelling', 'register_error_explanation', 'register_tactic_tag', 'simproc', 'simproc_decl', 'structure', 'syntax', 'tactic_extension', 'theorem' or 'unif_hint'

0 commit comments

Comments
 (0)