Skip to content

Commit e745713

Browse files
JovanGerbthorimur
andcommitted
feat: overlapping instances linter (#38126)
This PR implements the overlapping instances linter, with better performance than the previous attempt. The overlapping instances linter warns on data-carrying overlaps between instances in the local context of a declaration, as well as on redundant instances of `Prop` classes (i.e. those which can be synthesized from another instance in the context). An annoyance is the need for `withSetBoolOptionIn`. This is a private declaration in the `unusedInstancesInType` linter, with a link to leanprover/lean4#13133, which has been merged. When that commit lands in mathlib, we should move to `withSetOptionIn`. See #35095 and #33677 for previous iterations of this linter. See #14731 for an old attempt at a weaker form of this linter. Co-authored-by: @thorimur Co-authored-by: thorimur <68410468+thorimur@users.noreply.github.com>
1 parent 1407e6c commit e745713

68 files changed

Lines changed: 777 additions & 51 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Mathlib.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7239,6 +7239,7 @@ public import Mathlib.Tactic.Linter.Lint
72397239
public import Mathlib.Tactic.Linter.MinImports
72407240
public import Mathlib.Tactic.Linter.Multigoal
72417241
public import Mathlib.Tactic.Linter.OldObtain
7242+
public import Mathlib.Tactic.Linter.OverlappingInstances
72427243
public import Mathlib.Tactic.Linter.PPRoundtrip
72437244
public import Mathlib.Tactic.Linter.PrivateModule
72447245
public import Mathlib.Tactic.Linter.Style

Mathlib/Algebra/Category/Ring/FilteredColimits.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@ protected lemma nontrivial {F : J ⥤ CommRingCat.{v}} [IsFilteredOrEmpty J]
376376
(Types.FilteredColimit.isColimit_eq_iff' (isColimitOfPreserves (forget _) hc) _ _).mp h
377377
exact zero_ne_one (((F.map f).hom.map_zero.symm.trans e).trans (F.map f).hom.map_one)
378378

379+
set_option linter.overlappingInstances false in
379380
omit [IsFiltered J] in
380381
instance {F : J ⥤ CommRingCat.{v}} [IsFilteredOrEmpty J]
381382
[HasColimit F] [∀ i, Nontrivial (F.obj i)] : Nontrivial ↑(Limits.colimit F) :=

Mathlib/Algebra/Colimit/DirectLimit.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -833,8 +833,8 @@ end NonUnitalStarRing
833833
namespace Algebra
834834

835835
variable [CommSemiring R]
836-
variable [∀ i, Semiring (G i)] [∀ i j h, RingHomClass (T h) (G i) (G j)]
837-
variable [∀ i, Algebra R (G i)] [∀ i j h, AlgHomClass (T h) R (G i) (G j)]
836+
variable [∀ i, Semiring (G i)] [∀ i, Algebra R (G i)]
837+
variable [∀ i j h, AlgHomClass (T h) R (G i) (G j)]
838838
variable [Nonempty ι]
839839

840840
variable (G f) in

Mathlib/Algebra/Homology/HomotopyFiber.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class HasHomotopyFiber (φ : F ⟶ G) : Prop where
4242
instance [HasBinaryBiproducts C] : HasHomotopyFiber φ where
4343
hasBinaryBiproduct _ _ _ := inferInstance
4444

45-
variable [HasHomotopyFiber φ] [DecidableRel c.Rel]
45+
variable [HasHomotopyFiber φ]
4646

4747
instance : HasHomotopyCofiber ((opFunctor C c).map φ.op) where
4848
hasBinaryBiproduct i j hij := by

Mathlib/Algebra/Module/DedekindDomain.lean

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@ public section
2222

2323
universe u v
2424

25-
variable {R : Type u} [CommRing R] [IsDomain R] {M : Type v} [AddCommGroup M] [Module R M]
25+
variable {R : Type u} [CommRing R] [IsDedekindDomain R] {M : Type v} [AddCommGroup M] [Module R M]
2626

2727
open scoped DirectSum
2828

2929
namespace Submodule
3030

31-
variable [IsDedekindDomain R]
32-
3331
open UniqueFactorizationMonoid
3432

3533
/-- Over a Dedekind domain, an `I`-torsion module is the internal direct sum of its `p i ^ e i`-

Mathlib/Algebra/Module/Equiv/Defs.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ variable [RingHomCompTriple σ₁₃ σ₃₄ σ₁₄] [RingHomCompTriple σ₄
296296
variable [RingHomCompTriple σ₂₃ σ₃₄ σ₂₄] [RingHomCompTriple σ₄₃ σ₃₂ σ₄₂]
297297
variable (e₁₂ : M₁ ≃ₛₗ[σ₁₂] M₂) (e₂₃ : M₂ ≃ₛₗ[σ₂₃] M₃)
298298

299+
set_option linter.overlappingInstances false in
299300
/-- Linear equivalences are transitive. -/
300301
-- Note: the `RingHomCompTriple σ₃₂ σ₂₁ σ₃₁` is unused, but is convenient to carry around
301302
-- implicitly for lemmas like `LinearEquiv.self_trans_symm`.

Mathlib/Algebra/Module/GradedModule.lean

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,20 +191,21 @@ end SetLike
191191
namespace GradedModule
192192

193193
variable [AddCommMonoid M] [Module A M] [SetLike σ M] [AddSubmonoidClass σ' A]
194-
[AddSubmonoidClass σ M] [SetLike.GradedMonoid 𝓐] [SetLike.GradedSMul 𝓐 𝓜]
194+
[AddSubmonoidClass σ M] [SetLike.GradedSMul 𝓐 𝓜]
195+
[DecidableEq ιA] [DecidableEq ιM] [GradedRing 𝓐]
195196

196197
/-- The smul multiplication of `A` on `⨁ i, 𝓜 i` from `(⨁ i, 𝓐 i) →+ (⨁ i, 𝓜 i) →+ ⨁ i, 𝓜 i`
197198
turns `⨁ i, 𝓜 i` into an `A`-module
198199
-/
199200
@[implicit_reducible]
200-
def isModule [DecidableEq ιA] [DecidableEq ιM] [GradedRing 𝓐] : Module A (⨁ i, 𝓜 i) :=
201+
def isModule : Module A (⨁ i, 𝓜 i) :=
201202
{ Module.compHom _ (DirectSum.decomposeRingEquiv 𝓐 : A ≃+* ⨁ i, 𝓐 i).toRingHom with
202203
smul := fun a b => DirectSum.decompose 𝓐 a • b }
203204

204205
/-- `⨁ i, 𝓜 i` and `M` are isomorphic as `A`-modules.
205206
"The internal version" and "the external version" are isomorphism as `A`-modules.
206207
-/
207-
def linearEquiv [DecidableEq ιA] [DecidableEq ιM] [GradedRing 𝓐] [DirectSum.Decomposition 𝓜] :
208+
def linearEquiv [DirectSum.Decomposition 𝓜] :
208209
@LinearEquiv A A _ _ (RingHom.id A) (RingHom.id A) _ _ M (⨁ i, 𝓜 i) _
209210
_ _ (by letI := isModule 𝓐 𝓜; infer_instance) := by
210211
letI h := isModule 𝓐 𝓜

Mathlib/Algebra/Order/Antidiag/Prod.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ instance [AddMonoid A] : Subsingleton (HasAntidiagonal A) where
7777

7878
-- The goal of this lemma is to allow to rewrite antidiagonal
7979
-- when the decidability instances obfuscate Lean
80+
set_option linter.overlappingInstances false in
8081
lemma hasAntidiagonal_congr (A : Type*) [AddMonoid A]
8182
[H1 : HasAntidiagonal A] [H2 : HasAntidiagonal A] :
8283
H1.antidiagonal = H2.antidiagonal := by congr!; subsingleton

Mathlib/Algebra/Order/Invertible.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,5 @@ theorem invOf_le_one [Invertible a] (h : 1 ≤ a) : ⅟a ≤ 1 :=
4141
theorem invOf_lt_one [Invertible a] (h : 1 < a) : ⅟a < 1 :=
4242
mul_invOf_self a ▸ lt_mul_of_one_lt_left (invOf_pos.2 <| one_pos.trans h) h
4343

44-
theorem pos_invOf_of_invertible_cast [Nontrivial R] (n : ℕ)
45-
[Invertible (n : R)] : 0 < ⅟(n : R) :=
44+
theorem pos_invOf_of_invertible_cast (n : ℕ) [Invertible (n : R)] : 0 < ⅟(n : R) :=
4645
invOf_pos.2 <| Nat.cast_pos.2 <| pos_of_invertible_cast (R := R) n

Mathlib/Algebra/Polynomial/Div.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -789,7 +789,7 @@ lemma _root_.Irreducible.isRoot_eq_bot_of_natDegree_ne_one
789789
(hi : Irreducible p) (hdeg : p.natDegree ≠ 1) : p.IsRoot = ⊥ :=
790790
le_bot_iff.mp fun _ ↦ hi.not_isRoot_of_natDegree_ne_one hdeg
791791

792-
lemma _root_.Irreducible.subsingleton_isRoot [IsLeftCancelMulZero R]
792+
lemma _root_.Irreducible.subsingleton_isRoot
793793
(hi : Irreducible p) : { x | p.IsRoot x }.Subsingleton :=
794794
fun _ hx ↦ (subsingleton_isRoot_of_natDegree_eq_one <| natDegree_eq_of_degree_eq_some <|
795795
degree_eq_one_of_irreducible_of_root hi hx) hx

0 commit comments

Comments
 (0)