Skip to content

Commit fd0f0cd

Browse files
committed
feat(Tactic/ToFun): warn if the provided name matches the autogenerated one
1 parent 1fb6b28 commit fd0f0cd

2 files changed

Lines changed: 15 additions & 3 deletions

File tree

Mathlib/Tactic/ToFun.lean

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@ def toFunImpl (src : Name) (stx : Syntax) (kind : AttributeKind) : AttrM Name :=
4747
if (kind != AttributeKind.global) then
4848
throwError "`to_fun` can only be used as a global attribute"
4949
let name := match id with
50-
| some name =>
51-
(src.splitAt name.getId.getNumParts).1 ++ name.getId
50+
| some name => (src.splitAt name.getId.getNumParts).1 ++ name.getId
5251
| none => src.appendBefore "fun_"
52+
if id.isSome && name == src.appendBefore "fun_" then
53+
logWarningAt tk "the autogenerated name `name` matches the one provided"
5354
MetaM.run' <| addRelatedDecl src name tk optAttr
5455
(docstringPrefix? := s!"Eta-expanded form of `{src}`") (hoverInfo := true)
5556
fun value levels => do

MathlibTest/ToFun.lean

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
2+
13
import Mathlib.Tactic.ToFun
24
import Mathlib.Analysis.Normed.Ring.Basic
35

46
set_option pp.unicode.fun true
57

6-
variable {R : Type*}
8+
/-- warning: the autogenerated name `name` matches the one provided -/
9+
#guard_msgs in
10+
@[to_fun fun_id_eq']
11+
lemma id_eq' {R : Type*} : (@id R) = id := id_eq _
712

813
@[to_fun baz]
914
lemma Foo.bar (f g : ℝ → ℝ) : f * g = g * f := mul_comm f g
15+
1016
/-- info: Foo.baz (f g : ℝ → ℝ) : (fun i ↦ f i * g i) = fun i ↦ g i * f i -/
1117
#guard_msgs in #check Foo.baz
1218

@@ -52,6 +58,11 @@ theorem Function.mul_comm (f g : ℝ → ℝ) : f * g = g * f := _root_.mul_comm
5258
#guard_msgs in
5359
#check Function.fun_mul_comm
5460

61+
/-- warning: the autogenerated name `name` matches the one provided -/
62+
#guard_msgs in
63+
@[to_fun fun_bar]
64+
theorem bar (f g : ℝ → ℝ) : f * g = g * f := _root_.mul_comm f g
65+
5566
/-- Look I am the doc-string of `foo`. -/
5667
@[to_fun]
5768
theorem foo : (1 : Nat → Nat) = 1 := rfl

0 commit comments

Comments
 (0)