Marge Bot pushed to branch master at Glasgow Haskell Compiler / GHC

Commits:

4 changed files:

Changes:

  • compiler/GHC/Tc/Utils/Env.hs
    ... ... @@ -8,6 +8,7 @@
    8 8
                                           -- in module Language.Haskell.Syntax.Extension
    
    9 9
     {-# LANGUAGE TypeFamilies #-}
    
    10 10
     {-# LANGUAGE LambdaCase #-}
    
    11
    +{-# LANGUAGE MultiWayIf #-}
    
    11 12
     
    
    12 13
     module GHC.Tc.Utils.Env(
    
    13 14
             TyThing(..), TcTyThing(..), TcId,
    
    ... ... @@ -1213,6 +1214,20 @@ pprBinders bndrs = pprWithCommas ppr bndrs
    1213 1214
     notFound :: Name -> TcM TyThing
    
    1214 1215
     notFound name
    
    1215 1216
       = do { lcl_env <- getLclEnv
    
    1217
    +       ; lvls <- getCurrentAndBindLevel name
    
    1218
    +       ; if    -- See Note [Out of scope might be a staging error]
    
    1219
    +           | isUnboundName name -> failM  -- If the name really isn't in scope
    
    1220
    +                                          -- don't report it again (#11941)
    
    1221
    +                                          -- the
    
    1222
    +                                          -- the 'Nothing' case of 'getCurrentAndBindLevel'
    
    1223
    +                                          -- currently means 'isUnboundName' but to avoid
    
    1224
    +                                          -- introducing bugs after a refactoring of that
    
    1225
    +                                          -- function, we check this completely independently
    
    1226
    +                                          -- before scrutinizing lvls
    
    1227
    +           | Just (_top_lvl_flag, bind_lvls, lvl@Splice {}) <- lvls
    
    1228
    +               -> failWithTc (TcRnBadlyLevelled (LevelCheckSplice name Nothing) bind_lvls (thLevelIndex lvl) Nothing ErrorWithoutFlag)
    
    1229
    +           | otherwise  -> pure ()
    
    1230
    +
    
    1216 1231
            ; if isTermVarOrFieldNameSpace (nameNameSpace name)
    
    1217 1232
                then
    
    1218 1233
                    -- This code path is only reachable with RequiredTypeArguments enabled
    
    ... ... @@ -1243,14 +1258,23 @@ wrongThingErr expected thing name =
    1243 1258
     {- Note [Out of scope might be a staging error]
    
    1244 1259
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1245 1260
     Consider
    
    1246
    -  x = 3
    
    1247
    -  data T = MkT $(foo x)
    
    1261
    +  type T = Int
    
    1262
    +  foo = $(1 :: T)
    
    1263
    +
    
    1264
    +GHC currently leaves the user some liberty when it comes to using
    
    1265
    +types in a manner that is theoretically not well-staged.
    
    1266
    +E.g. if `T` here were to be a value, we would reject the program with
    
    1267
    +a staging error. Since it is a type though, we allow it for backwards
    
    1268
    +compatibility reasons.
    
    1269
    +
    
    1270
    +However, in this case, we're just in the process of renaming a splice
    
    1271
    +when trying to type check an expression involving a type, that hasn't
    
    1272
    +even been added to the (type checking) environment yet. That is, why
    
    1273
    +it is out of scope.
    
    1248 1274
     
    
    1249
    -where 'foo' is imported from somewhere.
    
    1275
    +The reason why we cannot recognise this issue earlier is, that if we
    
    1276
    +are not actually type checking the splice, i.e. if we're only using the
    
    1277
    +name of the type (e.g. ''T), the program should be accepted.
    
    1250 1278
     
    
    1251
    -This is really a staging error, because we can't run code involving 'x'.
    
    1252
    -But in fact the type checker processes types first, so 'x' won't even be
    
    1253
    -in the type envt when we look for it in $(foo x).  So inside splices we
    
    1254
    -report something missing from the type env as a staging error.
    
    1255
    -See #5752 and #5795.
    
    1279
    +We stop and report a staging error.
    
    1256 1280
     -}

  • testsuite/tests/th/T26099.hs
    1
    +{-# LANGUAGE TemplateHaskell #-}
    
    2
    +module M where
    
    3
    +
    
    4
    +type T = Int
    
    5
    +
    
    6
    +a = $(3 :: T)

  • testsuite/tests/th/T26099.stderr
    1
    +T26099.hs:6:12: error: [GHC-28914]
    
    2
    +    • Level error: ‘T’ is bound at level 0 but used at level -1
    
    3
    +    • In an expression type signature: T
    
    4
    +      In the expression: 3 :: T
    
    5
    +      In the untyped splice: $(3 :: T)
    
    6
    +

  • testsuite/tests/th/all.T
    ... ... @@ -642,3 +642,4 @@ test('QQInQuote', normal, compile, [''])
    642 642
     test('QQTopError', normal, compile_fail, ['-fdiagnostics-show-caret'])
    
    643 643
     test('GadtConSigs_th_pprint1', normal, compile, [''])
    
    644 644
     test('GadtConSigs_th_dump1', normal, compile, ['-v0 -ddump-splices -dsuppress-uniques'])
    
    645
    +test('T26099', normal, compile_fail, [''])