lean4-htt/src/Lean/Elab/InheritDoc.lean
Leonardo de Moura 173b956961
feat: reserved names (#3675)
- Add support for reserved declaration names. We use them for theorems
generated on demand.
- Equation theorems are not private declarations anymore.
- Generate equation theorems on demand when resolving symbols.
- Prevent users from creating declarations using reserved names. Users
can bypass it using meta-programming.

See next test for examples.
2024-03-15 00:33:22 +00:00

30 lines
1.2 KiB
Text

/-
Copyright (c) 2022 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Authors: Mario Carneiro
-/
prelude
import Lean.Elab.InfoTree.Main
import Lean.DocString
namespace Lean
builtin_initialize
registerBuiltinAttribute {
name := `inherit_doc
descr := "inherit documentation from a specified declaration"
add := fun decl stx kind => do
unless kind == AttributeKind.global do
throwError "invalid `[inherit_doc]` attribute, must be global"
match stx with
| `(attr| inherit_doc $[$id?:ident]?) => withRef stx[0] do
let some id := id?
| throwError "invalid `[inherit_doc]` attribute, could not infer doc source"
let declName ← Elab.realizeGlobalConstNoOverloadWithInfo id
if (← findDocString? (← getEnv) decl).isSome then
logWarning m!"{← mkConstWithLevelParams decl} already has a doc string"
let some doc ← findDocString? (← getEnv) declName
| logWarningAt id m!"{← mkConstWithLevelParams declName} does not have a doc string"
addDocString decl doc
| _ => throwError "invalid `[inherit_doc]` attribute"
}