Do not use defaulting in ambiguity check
authorSimon Peyton Jones <simonpj@microsoft.com>
Thu, 21 Apr 2016 12:03:29 +0000 (13:03 +0100)
committerSimon Peyton Jones <simonpj@microsoft.com>
Fri, 22 Apr 2016 10:32:24 +0000 (11:32 +0100)
This fixes Trac #11947.  See TcSimplify
Note [No defaulting in the ambiguity check]

compiler/typecheck/TcSimplify.hs
testsuite/tests/typecheck/should_compile/T11947.hs [new file with mode: 0644]
testsuite/tests/typecheck/should_compile/all.T
testsuite/tests/typecheck/should_fail/T11947a.hs [new file with mode: 0644]
testsuite/tests/typecheck/should_fail/T11947a.stderr [new file with mode: 0644]
testsuite/tests/typecheck/should_fail/all.T

index 1f7c984..8cd7bf4 100644 (file)
@@ -384,13 +384,25 @@ How is this implemented? It's complicated! So we'll step through it all:
  7) `HscMain.tcRnModule'` -- Reads `tcg_safeInfer` after type-checking, calling
     `HscMain.markUnsafeInfer` (passing the reason along) when safe-inferrence
     failed.
+
+Note [No defaulting in the ambiguity check]
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+When simplifying constraints for the ambiguity check, we use
+solveWantedsAndDrop, not simpl_top, so that we do no defaulting.
+Trac #11947 was an example:
+   f :: Num a => Int -> Int
+This is ambiguous of course, but we don't want to default the
+(Num alpha) constraint to (Num Int)!  Doing so gives a defaulting
+warning, but no error.
 -}
 
 ------------------
 simplifyAmbiguityCheck :: Type -> WantedConstraints -> TcM ()
 simplifyAmbiguityCheck ty wanteds
   = do { traceTc "simplifyAmbiguityCheck {" (text "type = " <+> ppr ty $$ text "wanted = " <+> ppr wanteds)
-       ; (final_wc, _) <- runTcS $ simpl_top wanteds
+       ; (final_wc, _) <- runTcS $ solveWantedsAndDrop wanteds
+             -- NB: no defaulting!  See Note [No defaulting in the ambiguity check]
+
        ; traceTc "End simplifyAmbiguityCheck }" empty
 
        -- Normally report all errors; but with -XAllowAmbiguousTypes
diff --git a/testsuite/tests/typecheck/should_compile/T11947.hs b/testsuite/tests/typecheck/should_compile/T11947.hs
new file mode 100644 (file)
index 0000000..75817c8
--- /dev/null
@@ -0,0 +1,10 @@
+{-# LANGUAGE TypeApplications, ScopedTypeVariables, AllowAmbiguousTypes #-}
+module T11947 where
+
+theFloatDigits :: forall a. RealFloat a => Int
+-- The type is ambiguous
+theFloatDigits = floatDigits (undefined @_ @a)
+
+foo :: IO ()
+foo = print (theFloatDigits @Double, theFloatDigits @Float)
+-- But the applications are not
index ebc68eb..e58feae 100644 (file)
@@ -514,3 +514,4 @@ test('T11754', normal, compile, [''])
 test('T11811', normal, compile, [''])
 test('T11793', normal, compile, [''])
 test('T11348', normal, compile, [''])
+test('T11947', normal, compile, [''])
diff --git a/testsuite/tests/typecheck/should_fail/T11947a.hs b/testsuite/tests/typecheck/should_fail/T11947a.hs
new file mode 100644 (file)
index 0000000..0d8a0d9
--- /dev/null
@@ -0,0 +1,6 @@
+{-# LANGUAGE TypeApplications, ScopedTypeVariables #-}
+module T11947 where
+
+theFloatDigits :: forall a. RealFloat a => Int
+-- The type is ambiguous, despite potential defaulting
+theFloatDigits = floatDigits (undefined @_ @a)
diff --git a/testsuite/tests/typecheck/should_fail/T11947a.stderr b/testsuite/tests/typecheck/should_fail/T11947a.stderr
new file mode 100644 (file)
index 0000000..4f6a4a7
--- /dev/null
@@ -0,0 +1,12 @@
+
+T11947a.hs:4:19: error:
+    • Could not deduce (RealFloat a0)
+      from the context: RealFloat a
+        bound by the type signature for:
+                   theFloatDigits :: RealFloat a => Int
+        at T11947a.hs:4:19-46
+      The type variable ‘a0’ is ambiguous
+    • In the ambiguity check for ‘theFloatDigits’
+      To defer the ambiguity check to use sites, enable AllowAmbiguousTypes
+      In the type signature:
+        theFloatDigits :: forall a. RealFloat a => Int
index fe40ca2..3903f4b 100644 (file)
@@ -414,3 +414,4 @@ test('T11723', normal, compile_fail, [''])
 test('T11724', normal, compile_fail, [''])
 test('BadUnboxedTuple', normal, compile_fail, [''])
 test('T11698', normal, compile_fail, [''])
+test('T11947a', normal, compile_fail, [''])