blob: e7ccc60f415df8bde0452304855563692c0431a1 [file]
/-!
Tests that the type class resolution cache persists across commands, that adding, erasing or
changing the scoped instances of a class invalidates the entries whose search consulted the class,
and only those, and that entries computed under `attribute [local instance]` do not outlive its
scope.
New persistent entries are environment modifications, so they are rolled back together with the
environment; in particular `example`s (which are elaborated inside `withoutModifyingEnv`) can
read the cache but do not contribute new entries to it.
-/
set_option trace.Meta.synthInstance.cache true
class Boo (α : Type) where
instance booNat : Boo Nat := ⟨⟩
/-- trace: [Meta.synthInstance.cache] new: Boo Nat -/
#guard_msgs in
def b1 : Unit := let _ : Boo Nat := inferInstance; ()
-- The result is cached across commands.
/-- trace: [Meta.synthInstance.cache] cached: Boo Nat -/
#guard_msgs in
def b2 : Unit := let _ : Boo Nat := inferInstance; ()
class Coo (α : Type) where
-- Failures are cached as well, including across commands.
/--
error: failed to synthesize instance of type class
Coo Nat
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
---
trace: [Meta.synthInstance.cache] new: Coo Nat
-/
#guard_msgs in
def c1 : Unit := let _ : Coo Nat := inferInstance; ()
/--
error: failed to synthesize instance of type class
Coo Nat
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
---
trace: [Meta.synthInstance.cache] cached: Coo Nat
-/
#guard_msgs in
def c2 : Unit := let _ : Coo Nat := inferInstance; ()
-- Adding an instance of `Coo` discards the cached failure above.
instance : Coo Nat := ⟨⟩
/-- trace: [Meta.synthInstance.cache] new: Coo Nat -/
#guard_msgs in
def c3 : Unit := let _ : Coo Nat := inferInstance; ()
-- The entries of other classes are kept.
/-- trace: [Meta.synthInstance.cache] cached: Boo Nat -/
#guard_msgs in
def b3 : Unit := let _ : Boo Nat := inferInstance; ()
-- Erasing an instance of `Boo` discards the cached success.
attribute [-instance] booNat
/--
error: failed to synthesize instance of type class
Boo Nat
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
---
trace: [Meta.synthInstance.cache] new: Boo Nat
-/
#guard_msgs in
def b5 : Unit := let _ : Boo Nat := inferInstance; ()
class Doo (α : Type) where
namespace N
scoped instance dooNat : Doo Nat := ⟨⟩
end N
/--
error: failed to synthesize instance of type class
Doo Nat
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
---
trace: [Meta.synthInstance.cache] new: Doo Nat
-/
#guard_msgs in
def d1 : Unit := let _ : Doo Nat := inferInstance; ()
-- Activating a scoped instance via `open` changes the instances in effect, which invalidates the
-- cached failure above, so synthesis succeeds.
open N in
/-- trace: [Meta.synthInstance.cache] new: Doo Nat -/
#guard_msgs in
def d2 : Unit := let _ : Doo Nat := inferInstance; ()
-- Ending the scope restores the instances in effect before the `open`, for which the failure cached
-- by `d1` holds again.
/--
error: failed to synthesize instance of type class
Doo Nat
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
---
trace: [Meta.synthInstance.cache] cached: Doo Nat
-/
#guard_msgs in
def d3 : Unit := let _ : Doo Nat := inferInstance; ()
-- Re-activating the scope brings back the instances in effect for `d2`, whose entry holds again.
open N
/-- trace: [Meta.synthInstance.cache] cached: Doo Nat -/
#guard_msgs in
def d4 : Unit := let _ : Doo Nat := inferInstance; ()
-- Local instances are part of the cache key, so entries computed with a local instance do not leak
-- out of its scope.
class Eoo (α : Type) where
@[instance_reducible] def eooNat : Eoo Nat := ⟨⟩
section
attribute [local instance] eooNat
/-- trace: [Meta.synthInstance.cache] new: Eoo Nat -/
#guard_msgs in
def e1 : Unit := let _ : Eoo Nat := inferInstance; ()
/-- trace: [Meta.synthInstance.cache] cached: Eoo Nat -/
#guard_msgs in
def e2 : Unit := let _ : Eoo Nat := inferInstance; ()
end
/--
error: failed to synthesize instance of type class
Eoo Nat
Hint: Type class instance resolution failures can be inspected with the `set_option trace.Meta.synthInstance true` command.
---
trace: [Meta.synthInstance.cache] new: Eoo Nat
-/
#guard_msgs in
def e3 : Unit := let _ : Eoo Nat := inferInstance; ()
-- `example`s do not contribute cache entries: their environment changes, including cache fills,
-- are reverted.
class Foo (α : Type) where
instance : Foo Nat := ⟨⟩
/-- trace: [Meta.synthInstance.cache] new: Foo Nat -/
#guard_msgs in
example : Foo Nat := inferInstance
/-- trace: [Meta.synthInstance.cache] new: Foo Nat -/
#guard_msgs in
def f1 : Unit := let _ : Foo Nat := inferInstance; ()
/-- trace: [Meta.synthInstance.cache] cached: Foo Nat -/
#guard_msgs in
def f2 : Unit := let _ : Foo Nat := inferInstance; ()
-- `synthInstance.maxSize` is part of the cache key, so cached results (in particular failures)
-- obtained under a different size limit are not reused.
/-- trace: [Meta.synthInstance.cache] cached: Coo Nat -/
#guard_msgs in
def c4 : Unit := let _ : Coo Nat := inferInstance; ()
set_option synthInstance.maxSize 100 in
/-- trace: [Meta.synthInstance.cache] new: Coo Nat -/
#guard_msgs in
def c6 : Unit := let _ : Coo Nat := inferInstance; ()