feat: generalize panic to Sort (#6333)

This PR generalizes the panic functions to a type of `Sort u` rather
than `Type u`. This better supports universe polymorphic types and
avoids confusing errors.

An minimal (but somewhat contrived) example of such a confusing error
is:

```lean
/-
stuck at solving universe constraint
  ?u.59+1 =?= max 1 ?u.7
while trying to unify
  Subtype.{?u.7} P : Sort (max 1 ?u.7)
with
  Subtype.{?u.7} P : Sort (max 1 ?u.7)
-/
def assertSubtype! {P : α → Prop} [Inhabited (Subtype P)] (a : α) [Decidable (P a)] : Subtype P := -- errors on :=
  if h : P a then 
    ⟨a, h⟩ 
  else 
    panic! "Property not satisified"
```
This commit is contained in:
Mac Malone 2024-12-15 16:36:45 -05:00 committed by GitHub
parent a8dc619f8e
commit a8656c5812
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 4 additions and 4 deletions

View file

@ -2554,7 +2554,7 @@ When we reimplement the specializer, we may consider copying `inst` if it also
occurs outside binders or if it is an instance. occurs outside binders or if it is an instance.
-/ -/
@[never_extract, extern "lean_panic_fn"] @[never_extract, extern "lean_panic_fn"]
def panicCore {α : Type u} [Inhabited α] (msg : String) : α := default def panicCore {α : Sort u} [Inhabited α] (msg : String) : α := default
/-- /--
`(panic "msg" : α)` has a built-in implementation which prints `msg` to `(panic "msg" : α)` has a built-in implementation which prints `msg` to
@ -2568,7 +2568,7 @@ Because this is a pure function with side effects, it is marked as
elimination and other optimizations that assume that the expression is pure. elimination and other optimizations that assume that the expression is pure.
-/ -/
@[noinline, never_extract] @[noinline, never_extract]
def panic {α : Type u} [Inhabited α] (msg : String) : α := def panic {α : Sort u} [Inhabited α] (msg : String) : α :=
panicCore msg panicCore msg
-- TODO: this be applied directly to `Inhabited`'s definition when we remove the above workaround -- TODO: this be applied directly to `Inhabited`'s definition when we remove the above workaround

View file

@ -33,13 +33,13 @@ def dbgSleep {α : Type u} (ms : UInt32) (f : Unit → α) : α := f ()
@[noinline] private def mkPanicMessage (modName : String) (line col : Nat) (msg : String) : String := @[noinline] private def mkPanicMessage (modName : String) (line col : Nat) (msg : String) : String :=
"PANIC at " ++ modName ++ ":" ++ toString line ++ ":" ++ toString col ++ ": " ++ msg "PANIC at " ++ modName ++ ":" ++ toString line ++ ":" ++ toString col ++ ": " ++ msg
@[never_extract, inline] def panicWithPos {α : Type u} [Inhabited α] (modName : String) (line col : Nat) (msg : String) : α := @[never_extract, inline] def panicWithPos {α : Sort u} [Inhabited α] (modName : String) (line col : Nat) (msg : String) : α :=
panic (mkPanicMessage modName line col msg) panic (mkPanicMessage modName line col msg)
@[noinline] private def mkPanicMessageWithDecl (modName : String) (declName : String) (line col : Nat) (msg : String) : String := @[noinline] private def mkPanicMessageWithDecl (modName : String) (declName : String) (line col : Nat) (msg : String) : String :=
"PANIC at " ++ declName ++ " " ++ modName ++ ":" ++ toString line ++ ":" ++ toString col ++ ": " ++ msg "PANIC at " ++ declName ++ " " ++ modName ++ ":" ++ toString line ++ ":" ++ toString col ++ ": " ++ msg
@[never_extract, inline] def panicWithPosWithDecl {α : Type u} [Inhabited α] (modName : String) (declName : String) (line col : Nat) (msg : String) : α := @[never_extract, inline] def panicWithPosWithDecl {α : Sort u} [Inhabited α] (modName : String) (declName : String) (line col : Nat) (msg : String) : α :=
panic (mkPanicMessageWithDecl modName declName line col msg) panic (mkPanicMessageWithDecl modName declName line col msg)
@[extern "lean_ptr_addr"] @[extern "lean_ptr_addr"]