Opened 5 years ago

Closed 5 years ago

#10031 closed bug (fixed)

coerce can cause compiler to loop

Reported by: glguy Owned by: goldfire
Priority: normal Milestone: 7.10.1
Component: Compiler Version: 7.10.1-rc1
Keywords: Cc: goldfiere
Operating System: Unknown/Multiple Architecture: x86_64 (amd64)
Type of failure: Compile-time crash Test Case: typecheck/should_compile/T10031
Blocked By: Blocking:
Related Tickets: Differential Rev(s): Phab:D630
Wiki Page:

Description

This program causes GHC to loop at compile time

{-# LANGUAGE ScopedTypeVariables #-}
import Data.Coerce
coerce' :: Coercible b a => a -> b
coerce' = coerce (\x -> x :: b) :: forall a b. Coercible b a => a -> b

I encountered this trying to use a similar construction in

https://github.com/glguy/profunctors/blob/coerce/src/Data/Profunctor/Unsafe.hs

I'm not absolutely certain about what aspect of this snippet is looping the compiler, but it's pretty small.

Change History (8)

comment:1 Changed 5 years ago by nomeata

Cc: goldfiere added

JFTR, this does not loop with 7.8.4, so it is likely related to the Coercible solver rewrite.

comment:2 Changed 5 years ago by simonpj

Owner: set to goldfire

Richard, might you look at this?

comment:3 Changed 5 years ago by nomeata

Confirmed with GHC HEAD.

Here is the repeating tail of -ddump-tc-trace (not sure if it is useful)

updWorkList
  WL {Eqs = [G] cobox_am7 :: Coercible
                               b_am6[sk] a_am5[sk] (CTyEqCan)}
Start solver pipeline {
  work item =  [G] cobox_am7 :: Coercible
                                  b_am6[sk] a_am5[sk] (CTyEqCan)
  inerts    =  Equalities: [G] cobox_amr :: Coercible
                                              b_amq[sk] a_amp[sk] (CTyEqCan)
               Type-function equalities:
               Dictionaries:
               Irreds:
               Insolubles = {}
               Solved dicts
runStage canonicalization {
  workitem   =  [G] cobox_am7 :: Coercible
                                   b_am6[sk] a_am5[sk] (CTyEqCan)
canEqTyVar
  b_am6[sk]
  a_am5[sk]
  Not-swapped
flattenTyVar1
  a_am5[sk]
  *
canEqTyVar flat LHS
  b_am6[sk]
  b_am6[sk]
  a_am5[sk]
  Not-swapped
  a_am5[sk]
end stage canonicalization }
runStage interact with inerts {
  workitem   =  [G] cobox_am7 :: Coercible
                                   b_am6[sk] a_am5[sk] (CTyEqCan)
Can't solve tyvar equality
  LHS: b_am6[sk] :: *
  RHS: a_am5[sk] :: *
  TcLevel = 3
Kick out, tv = b_am6[sk]
  n-kicked = 1
  n-kept fun-eqs = 0
  WL {Eqs = [G] cobox_amr :: Coercible
                               b_amq[sk] a_amp[sk] (CTyEqCan)}
end stage interact with inerts }
1832[U:3<C:0,F:0>] Kept as inert (1 kicked out):
    [G] cobox_am7 :: Coercible b_am6[sk] a_am5[sk]
End solver pipeline (discharged) }
  inerts = Equalities: [G] cobox_am7 :: Coercible
                                          b_am6[sk] a_am5[sk] (CTyEqCan)
           Type-function equalities:
           Dictionaries:
           Irreds:
           Insolubles = {}
           Solved dicts
updWorkList
  WL {Eqs = [G] cobox_amr :: Coercible
                               b_amq[sk] a_amp[sk] (CTyEqCan)}

comment:4 Changed 5 years ago by goldfire

Thanks to glguy for such a nice, tiny test case and to Joachim (nomeata) for the dump. I'm able to solve this without testing! Just add a critical missing not in the bowels of kick_out. Will validate and push shortly.

comment:5 Changed 5 years ago by goldfire

Differential Rev(s): Phab:D630

Just using Phab to validate... I don't think this needs external eyes to review.

comment:6 Changed 5 years ago by Richard Eisenberg <eir@…>

In cecf036fa10830a5e9861d1e6a2f8c22059fb4bb/ghc:

Fix #10031 by inverting a critical test in kick_out.

Summary:
The documentation (Note [The flattening story] in TcFlatten) was
correct; it's just the implementation that was not.

Test in typecheck/should_compile/T10031

Test Plan: validate

Reviewers: austin

Subscribers: thomie

Differential Revision: https://phabricator.haskell.org/D630

GHC Trac Issues: #10031

comment:7 Changed 5 years ago by goldfire

Status: newmerge
Test Case: typecheck/should_compile/T10031

Please merge for 7.10.1. This is a clear error and should be dead simple to merge. Thanks!

comment:8 Changed 5 years ago by thoughtpolice

Milestone: 7.10.1
Resolution: fixed
Status: mergeclosed

Merged to ghc-7.10 (via 2387369c498f631bd3f4be2d6efe71773353acc5).

Note: See TracTickets for help on using tickets.