Skip to content

Commit cc5a05a

Browse files
committed
chore: deprecate := variants of inductive and structure
Deprecates `inductive ... :=` and `structure ... :=`. Currently this syntax produces a warning, controlled by the `linter.deprecated` option. Part of leanprover#5236
1 parent 3e75d8f commit cc5a05a

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

Diff for: src/Lean/Elab/Declaration.lean

+8-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ def elabAxiom (modifiers : Modifiers) (stx : Syntax) : CommandElabM Unit := do
136136
Term.applyAttributesAt declName modifiers.attrs AttributeApplicationTime.afterCompilation
137137

138138
/-
139-
leading_parser "inductive " >> declId >> optDeclSig >> optional ":=" >> many ctor
140-
leading_parser atomic (group ("class " >> "inductive ")) >> declId >> optDeclSig >> optional ":=" >> many ctor >> optDeriving
139+
leading_parser "inductive " >> declId >> optDeclSig >> optional ("where" <|> ":=") >> many ctor
140+
leading_parser atomic (group ("class " >> "inductive ")) >> declId >> optDeclSig >> optional ("where" <|> ":=") >> many ctor >> optDeriving
141141
-/
142142
private def inductiveSyntaxToView (modifiers : Modifiers) (decl : Syntax) : CommandElabM InductiveView := do
143143
checkValidInductiveModifier modifiers
@@ -167,6 +167,12 @@ private def inductiveSyntaxToView (modifiers : Modifiers) (decl : Syntax) : Comm
167167
let computedFields ← (decl[5].getOptional?.map (·[1].getArgs) |>.getD #[]).mapM fun cf => withRef cf do
168168
return { ref := cf, modifiers := cf[0], fieldId := cf[1].getId, type := ⟨cf[3]⟩, matchAlts := ⟨cf[4]⟩ }
169169
let classes ← liftCoreM <| getOptDerivingClasses decl[6]
170+
if decl[3][0].isToken ":=" then
171+
-- https://github.com/leanprover/lean4/issues/5236
172+
if Linter.getLinterValue Linter.linter.deprecated (← getOptions) then
173+
withRef decl[0] <| withRef decl[3] <| logWarning <| .tagged ``Linter.deprecatedAttr "\
174+
'inductive ... :=' has been deprecated in favor of 'inductive ... where'.\n\
175+
You can disable this warning with 'set_option linter.deprecated false'."
170176
return {
171177
ref := decl
172178
shortDeclName := name

Diff for: src/Lean/Elab/Structure.lean

+9-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,13 @@ def structSimpleBinder := leading_parser atomic (declModifiers true >> ident)
137137
def structFields := leading_parser many (structExplicitBinder <|> structImplicitBinder <|> structInstBinder)
138138
```
139139
-/
140-
private def expandFields (structStx : Syntax) (structModifiers : Modifiers) (structDeclName : Name) : TermElabM (Array StructFieldView) :=
140+
private def expandFields (structStx : Syntax) (structModifiers : Modifiers) (structDeclName : Name) : TermElabM (Array StructFieldView) := do
141+
if structStx[5][0].isToken ":=" then
142+
-- https://github.com/leanprover/lean4/issues/5236
143+
if Linter.getLinterValue Linter.linter.deprecated (← getOptions) then
144+
withRef structStx[0] <| withRef structStx[5][0] <| logWarning <| .tagged ``Linter.deprecatedAttr "\
145+
'structure ... :=' has been deprecated in favor of 'structure ... where'.\n\
146+
You can disable this warning with 'set_option linter.deprecated false'."
141147
let fieldBinders := if structStx[5].isNone then #[] else structStx[5][2][0].getArgs
142148
fieldBinders.foldlM (init := #[]) fun (views : Array StructFieldView) fieldBinder => withRef fieldBinder do
143149
let mut fieldBinder := fieldBinder
@@ -866,7 +872,8 @@ private def elabStructureView (view : StructView) : TermElabM Unit := do
866872
addDefaults lctx defaultAuxDecls
867873

868874
/-
869-
leading_parser (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >> " := " >> optional structCtor >> structFields >> optDeriving
875+
leading_parser (structureTk <|> classTk) >> declId >> many Term.bracketedBinder >> optional «extends» >> Term.optType >>
876+
optional (("where" <|> ":=") >> optional structCtor >> structFields) >> optDeriving
870877
871878
where
872879
def «extends» := leading_parser " extends " >> sepBy1 termParser ", "

Diff for: tests/lean/run/5236.lean

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/-!
2+
# Deprecate `:=` for `inductive` and `structure`
3+
-/
4+
5+
/--
6+
warning: 'inductive ... :=' has been deprecated in favor of 'inductive ... where'.
7+
You can disable this warning with 'set_option linter.deprecated false'.
8+
-/
9+
#guard_msgs in
10+
inductive DogSound' :=
11+
| woof
12+
| grr
13+
14+
/--
15+
warning: 'structure ... :=' has been deprecated in favor of 'structure ... where'.
16+
You can disable this warning with 'set_option linter.deprecated false'.
17+
-/
18+
#guard_msgs in
19+
structure S :=
20+
(n : Nat)

0 commit comments

Comments
 (0)