Skip to content

Commit d59d8f5

Browse files
committed
fix(to_dual none): don't allow attributes in to_dual none (leanprover-community#33773)
This PR fixes the problem that the `existingAttributeWarning` linter fires on `to_dual none`. The generated declaration of `to_dual none` should never get attributes anyways, so we also turn off this option (just like in `to_dual self`)
1 parent 5ec8c3f commit d59d8f5

2 files changed

Lines changed: 22 additions & 19 deletions

File tree

Mathlib/Tactic/Translate/Core.lean

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1006,9 +1006,9 @@ def elabTranslationAttr (declName : Name) (stx : Syntax) : CoreM Config := do
10061006
| `(translationHint| self) => existing := true; self := true
10071007
| `(translationHint| none) => none := true
10081008
| _ => pure ()
1009-
if self && !attrs.isEmpty then
1010-
throwError "invalid `(attr := ...)` after `self`, \
1011-
as there is only one declaration for the attributes.\n\
1009+
if (self || none) && !attrs.isEmpty then
1010+
throwError "invalid `(attr := ...)` after `self` or `none`, \
1011+
as there is no other declaration for the attributes.\n\
10121012
Instead, you can write the attributes in the usual way."
10131013
trace[translate_detail] "attributes: {attrs}; reorder arguments: {reorder?}"
10141014
let doc ← doc.mapM fun
@@ -1050,35 +1050,35 @@ def elabTranslationAttr (declName : Name) (stx : Syntax) : CoreM Config := do
10501050

10511051
mutual
10521052
/-- Apply attributes to the original and translated declarations. -/
1053-
partial def applyAttributes (t : TranslateData) (stx : Syntax) (rawAttrs : Array Syntax)
1054-
(src tgt : Name) (argInfo : ArgInfo) : TermElabM (Array Name) := withoutExporting do
1053+
partial def applyAttributes (t : TranslateData) (cfg : Config) (src tgt : Name)
1054+
(argInfo : ArgInfo) : TermElabM (Array Name) := withoutExporting do
10551055
-- we only copy the `instance` attribute, since it is nice to directly tag `instance` declarations
10561056
copyInstanceAttribute src tgt
10571057
-- Warn users if the original declaration has an attributee
1058-
if src != tgt && linter.existingAttributeWarning.get (← getOptions) then
1058+
if !cfg.self && !cfg.none && linter.existingAttributeWarning.get (← getOptions) then
10591059
let appliedAttrs ← getAllSimpAttrs src
10601060
if appliedAttrs.size > 0 then
10611061
let appliedAttrs := ", ".intercalate (appliedAttrs.toList.map toString)
10621062
-- Note: we're not bothering to print the correct attribute arguments.
1063-
Linter.logLintIf linter.existingAttributeWarning stx m!"\
1063+
Linter.logLintIf linter.existingAttributeWarning cfg.ref m!"\
10641064
The source declaration {src} was given the simp-attribute(s) {appliedAttrs} before \
10651065
calling @[{t.attrName}].\nThe preferred method is to use something like \
10661066
`@[{t.attrName} (attr := {appliedAttrs})]`\nto apply the attribute to both \
10671067
{src} and the target declaration {tgt}."
1068-
warnAttr stx Lean.Meta.Ext.extExtension
1068+
warnAttr cfg.ref Lean.Meta.Ext.extExtension
10691069
(fun b n => (b.tree.values.any fun t => t.declName = n)) t.attrName `ext src tgt
1070-
warnAttr stx Lean.Meta.Rfl.reflExt (·.values.contains ·) t.attrName `refl src tgt
1071-
warnAttr stx Lean.Meta.Symm.symmExt (·.values.contains ·) t.attrName `symm src tgt
1072-
warnAttr stx Batteries.Tactic.transExt (·.values.contains ·) t.attrName `trans src tgt
1073-
warnAttr stx Lean.Meta.coeExt (·.contains ·) t.attrName `coe src tgt
1074-
warnParametricAttr stx Lean.Linter.deprecatedAttr t.attrName `deprecated src tgt
1070+
warnAttr cfg.ref Lean.Meta.Rfl.reflExt (·.values.contains ·) t.attrName `refl src tgt
1071+
warnAttr cfg.ref Lean.Meta.Symm.symmExt (·.values.contains ·) t.attrName `symm src tgt
1072+
warnAttr cfg.ref Batteries.Tactic.transExt (·.values.contains ·) t.attrName `trans src tgt
1073+
warnAttr cfg.ref Lean.Meta.coeExt (·.contains ·) t.attrName `coe src tgt
1074+
warnParametricAttr cfg.ref Lean.Linter.deprecatedAttr t.attrName `deprecated src tgt
10751075
-- the next line also warns for `@[to_additive, simps]`, because of the application times
1076-
warnParametricAttr stx simpsAttr t.attrName `simps src tgt
1077-
warnAttrCore stx Term.elabAsElim.hasTag t.attrName `elab_as_elim src tgt
1076+
warnParametricAttr cfg.ref simpsAttr t.attrName `simps src tgt
1077+
warnAttrCore cfg.ref Term.elabAsElim.hasTag t.attrName `elab_as_elim src tgt
10781078
-- add attributes
10791079
-- the following is similar to `Term.ApplyAttributesCore`, but we hijack the implementation of
10801080
-- `simps` and `to_additive`.
1081-
let attrs ← elabAttrs rawAttrs
1081+
let attrs ← elabAttrs cfg.attrs
10821082
let (additiveAttrs, attrs) := attrs.partition (·.name == t.attrName)
10831083
let nestedDecls ←
10841084
match h : additiveAttrs.size with
@@ -1134,8 +1134,7 @@ partial def copyMetaData (t : TranslateData) (cfg : Config) (src tgt : Name) (ar
11341134
when doing a `rw`, but it won't be generated for `tgt`. -/
11351135
translateLemmas t #[src, tgt] argInfo "equation lemmas" fun nm ↦
11361136
(·.getD #[]) <$> MetaM.run' (getEqnsFor? nm)
1137-
MetaM.run' <| Elab.Term.TermElabM.run' <|
1138-
applyAttributes t cfg.ref cfg.attrs src tgt argInfo
1137+
applyAttributes t cfg src tgt argInfo |>.run'.run'
11391138

11401139
/-- `addTranslationAttr src cfg` adds a translation attribute to `src` with configuration `cfg`.
11411140
See the attribute implementation for more details.

MathlibTest/toAdditive.lean

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,10 +560,14 @@ fun {α} [Add α] a => Add.add a
560560
@[simp, to_additive self]
561561
theorem test1 : 5 = 5 := rfl
562562

563+
/-! Test that the `existingAttributeWarning` linter doesn't fire for `to_additive none`. -/
564+
@[simp, to_additive none]
565+
theorem test1' : 5 = 5 := rfl
566+
563567
/-! Test that we can't write `to_additive self (attr := ..)`. -/
564568

565569
/--
566-
error: invalid `(attr := ...)` after `self`, as there is only one declaration for the attributes.
570+
error: invalid `(attr := ...)` after `self` or `none`, as there is no other declaration for the attributes.
567571
Instead, you can write the attributes in the usual way.
568572
-/
569573
#guard_msgs in

0 commit comments

Comments
 (0)