Serge S. Gulin pushed to branch wip/T25974 at Glasgow Haskell Compiler / GHC

Commits:

21 changed files:

Changes:

  • .gitignore
    ... ... @@ -256,3 +256,6 @@ ghc.nix/
    256 256
     # clangd
    
    257 257
     .clangd
    
    258 258
     dist-newstyle/
    
    259
    +
    
    260
    +# .gitlab/ci.sh for HERMETIC=1
    
    261
    +cabal/*

  • .gitlab/ci.sh
    ... ... @@ -75,6 +75,15 @@ Environment variables affecting both build systems:
    75 75
       NIX_SYSTEM        On Darwin, the target platform of the desired toolchain
    
    76 76
                         (either "x86-64-darwin" or "aarch-darwin")
    
    77 77
       NO_BOOT           Whether to run ./boot or not, used when testing the source dist
    
    78
    +  TOOLCHAIN_SOURCE  Select a source of toolchain. Possible values:
    
    79
    +                    - "env": Toolchains are included in the Docker image via environment
    
    80
    +                             variables. Default for Linux.
    
    81
    +                    - "nix": Toolchains are provided via .gitlab/darwin/toolchain.nix.
    
    82
    +                             Default for Darwin.
    
    83
    +                    - "extracted":
    
    84
    +                             Toolchains will be downloaded and extracted through the
    
    85
    +                             CI process. Default for other systems. Windows and FreeBSD
    
    86
    +                             are included.
    
    78 87
     
    
    79 88
     Environment variables determining build configuration of Hadrian system:
    
    80 89
     
    
    ... ... @@ -83,14 +92,14 @@ Environment variables determining build configuration of Hadrian system:
    83 92
                         This tests the "reinstall" configuration
    
    84 93
       CROSS_EMULATOR    The emulator to use for testing of cross-compilers.
    
    85 94
     
    
    86
    -Environment variables determining bootstrap toolchain (Linux):
    
    95
    +Environment variables determining bootstrap toolchain (TOOLCHAIN_SOURCE=env):
    
    87 96
     
    
    88 97
       GHC           Path of GHC executable to use for bootstrapping.
    
    89 98
       CABAL         Path of cabal-install executable to use for bootstrapping.
    
    90 99
       ALEX          Path of alex executable to use for bootstrapping.
    
    91 100
       HAPPY         Path of alex executable to use for bootstrapping.
    
    92 101
     
    
    93
    -Environment variables determining bootstrap toolchain (non-Linux):
    
    102
    +Environment variables determining bootstrap toolchain (TOOLCHAIN_SOURCE=extracted):
    
    94 103
     
    
    95 104
       GHC_VERSION   Which GHC version to fetch for bootstrapping.
    
    96 105
       CABAL_INSTALL_VERSION
    
    ... ... @@ -135,7 +144,9 @@ function mingw_init() {
    135 144
       case "$MSYSTEM" in
    
    136 145
         CLANG64)
    
    137 146
           target_triple="x86_64-unknown-mingw32"
    
    138
    -      boot_triple="x86_64-unknown-mingw32" # triple of bootstrap GHC
    
    147
    +      ;;
    
    148
    +    CLANGARM64)
    
    149
    +      target_triple="aarch64-unknown-mingw32"
    
    139 150
           ;;
    
    140 151
         *)
    
    141 152
           fail "win32-init: Unknown MSYSTEM $MSYSTEM"
    
    ... ... @@ -150,10 +161,19 @@ function mingw_init() {
    150 161
       MINGW_MOUNT_POINT="${MINGW_PREFIX}"
    
    151 162
       PATH="$MINGW_MOUNT_POINT/bin:$PATH"
    
    152 163
     
    
    153
    -  # We always use mingw64 Python to avoid path length issues like #17483.
    
    154
    -  export PYTHON="/mingw64/bin/python3"
    
    155
    -  # And need to use sphinx-build from the environment
    
    156
    -  export SPHINXBUILD="/mingw64/bin/sphinx-build.exe"
    
    164
    +  case "$MSYSTEM" in
    
    165
    +    CLANGARM64)
    
    166
    +      # At MSYS for ARM64 we force to use their special versions to speedup the compiler step
    
    167
    +      export PYTHON="/clangarm64/bin/python3"
    
    168
    +      export SPHINXBUILD="/clangarm64/bin/sphinx-build.exe"
    
    169
    +      ;;
    
    170
    +    *)
    
    171
    +      # We always use mingw64 Python to avoid path length issues like #17483.
    
    172
    +      export PYTHON="/mingw64/bin/python3"
    
    173
    +      # And need to use sphinx-build from the environment
    
    174
    +      export SPHINXBUILD="/mingw64/bin/sphinx-build.exe"
    
    175
    +      ;;
    
    176
    +  esac
    
    157 177
     }
    
    158 178
     
    
    159 179
     # This will contain GHC's local native toolchain
    
    ... ... @@ -178,15 +198,21 @@ function show_tool() {
    178 198
     }
    
    179 199
     
    
    180 200
     function set_toolchain_paths() {
    
    181
    -  case "$(uname -m)-$(uname)" in
    
    182
    -    # Linux toolchains are included in the Docker image
    
    183
    -    *-Linux) toolchain_source="env" ;;
    
    184
    -    # Darwin toolchains are provided via .gitlab/darwin/toolchain.nix
    
    185
    -    *-Darwin) toolchain_source="nix" ;;
    
    186
    -    *) toolchain_source="extracted" ;;
    
    187
    -  esac
    
    201
    +  if [ -z "${TOOLCHAIN_SOURCE:-}" ]
    
    202
    +  then
    
    203
    +    # Fallback to automatic detection which could not work for cases
    
    204
    +    # when cross compiler will be build at Windows environment
    
    205
    +    # and requires a special mingw compiler (not bundled)
    
    206
    +    case "$(uname -m)-$(uname)" in
    
    207
    +      # Linux toolchains are included in the Docker image
    
    208
    +      *-Linux) TOOLCHAIN_SOURCE="env" ;;
    
    209
    +      # Darwin toolchains are provided via .gitlab/darwin/toolchain.nix
    
    210
    +      *-Darwin) TOOLCHAIN_SOURCE="nix" ;;
    
    211
    +      *) TOOLCHAIN_SOURCE="extracted" ;;
    
    212
    +    esac
    
    213
    +  fi
    
    188 214
     
    
    189
    -  case "$toolchain_source" in
    
    215
    +  case "$TOOLCHAIN_SOURCE" in
    
    190 216
         extracted)
    
    191 217
           # These are populated by setup_toolchain
    
    192 218
           GHC="$toolchain/bin/ghc$exe"
    
    ... ... @@ -217,7 +243,7 @@ function set_toolchain_paths() {
    217 243
           : ${HAPPY:=$(which happy)}
    
    218 244
           : ${ALEX:=$(which alex)}
    
    219 245
           ;;
    
    220
    -    *) fail "bad toolchain_source"
    
    246
    +    *) fail "bad TOOLCHAIN_SOURCE"
    
    221 247
       esac
    
    222 248
     
    
    223 249
       export GHC
    
    ... ... @@ -247,7 +273,7 @@ function setup() {
    247 273
           cp -Rf "$CABAL_CACHE"/* "$CABAL_DIR"
    
    248 274
       fi
    
    249 275
     
    
    250
    -  case $toolchain_source in
    
    276
    +  case $TOOLCHAIN_SOURCE in
    
    251 277
         extracted) time_it "setup" setup_toolchain ;;
    
    252 278
         *) ;;
    
    253 279
       esac
    
    ... ... @@ -273,14 +299,37 @@ function setup() {
    273 299
     }
    
    274 300
     
    
    275 301
     function fetch_ghc() {
    
    276
    -  if [ ! -e "$GHC" ]; then
    
    277
    -      local v="$GHC_VERSION"
    
    302
    +  local boot_triple_to_fetch
    
    303
    +  case "$(uname)" in
    
    304
    +    MSYS_*|MINGW*)
    
    305
    +      case "$MSYSTEM" in
    
    306
    +        CLANG64)
    
    307
    +          boot_triple_to_fetch="x86_64-unknown-mingw32" # triple of bootstrap GHC
    
    308
    +          ;;
    
    309
    +        *)
    
    310
    +          fail "win32-init: Unknown MSYSTEM $MSYSTEM"
    
    311
    +          ;;
    
    312
    +      esac
    
    313
    +      ;;
    
    314
    +    Darwin)
    
    315
    +      boot_triple_to_fetch="x86_64-apple-darwin"
    
    316
    +      ;;
    
    317
    +    FreeBSD)
    
    318
    +      boot_triple_to_fetch="x86_64-portbld-freebsd"
    
    319
    +      ;;
    
    320
    +    Linux)
    
    321
    +      ;;
    
    322
    +    *) fail "uname $(uname) is not supported by ghc boot fetch" ;;
    
    323
    +  esac
    
    324
    +  readonly boot_triple_to_fetch
    
    325
    +
    
    326
    +  local -r v="$GHC_VERSION"
    
    278 327
           if [[ -z "$v" ]]; then
    
    279 328
               fail "neither GHC nor GHC_VERSION are not set"
    
    280 329
           fi
    
    281 330
     
    
    282 331
           start_section "fetch GHC"
    
    283
    -      url="https://downloads.haskell.org/~ghc/${GHC_VERSION}/ghc-${GHC_VERSION}-${boot_triple}.tar.xz"
    
    332
    +  url="https://downloads.haskell.org/~ghc/${GHC_VERSION}/ghc-${GHC_VERSION}-${boot_triple_to_fetch}.tar.xz"
    
    284 333
           info "Fetching GHC binary distribution from $url..."
    
    285 334
           curl "$url" > ghc.tar.xz || fail "failed to fetch GHC binary distribution"
    
    286 335
           $TAR -xJf ghc.tar.xz || fail "failed to extract GHC binary distribution"
    
    ... ... @@ -297,8 +346,6 @@ function fetch_ghc() {
    297 346
           esac
    
    298 347
           rm -Rf "ghc-${GHC_VERSION}" ghc.tar.xz
    
    299 348
           end_section "fetch GHC"
    
    300
    -  fi
    
    301
    -
    
    302 349
     }
    
    303 350
     
    
    304 351
     function fetch_cabal() {
    
    ... ... @@ -349,7 +396,10 @@ function fetch_cabal() {
    349 396
     # here. For Docker platforms this is done in the Docker image
    
    350 397
     # build.
    
    351 398
     function setup_toolchain() {
    
    399
    +  if [ ! -e "$GHC" ]; then
    
    352 400
       fetch_ghc
    
    401
    +  fi
    
    402
    +
    
    353 403
       fetch_cabal
    
    354 404
       cabal_update
    
    355 405
     
    
    ... ... @@ -405,6 +455,17 @@ function configure() {
    405 455
       if [[ -n "${target_triple:-}" ]]; then
    
    406 456
         args+=("--target=$target_triple")
    
    407 457
       fi
    
    458
    +  if [[ "${TOOLCHAIN_SOURCE:-}" =~ "extracted" ]]; then
    
    459
    +    # To extract something need download something first.
    
    460
    +    args+=("--enable-tarballs-autodownload")
    
    461
    +  else
    
    462
    +    # For Windows we should explicitly --enable-distro-toolchain
    
    463
    +    # if i.e. we decided to use TOOLCHAIN_SOURCE = env
    
    464
    +    case "$(uname)" in
    
    465
    +      MSYS_*|MINGW*) args+=("--enable-distro-toolchain") ;;
    
    466
    +      *) ;;
    
    467
    +    esac
    
    468
    +  fi
    
    408 469
       if [[ -n "${ENABLE_NUMA:-}" ]]; then
    
    409 470
         args+=("--enable-numa")
    
    410 471
         else
    
    ... ... @@ -421,7 +482,6 @@ function configure() {
    421 482
       # See https://stackoverflow.com/questions/7577052 for a rationale for the
    
    422 483
       # args[@] symbol-soup below.
    
    423 484
       run ${CONFIGURE_WRAPPER:-} ./configure \
    
    424
    -    --enable-tarballs-autodownload \
    
    425 485
         "${args[@]+"${args[@]}"}" \
    
    426 486
         GHC="$GHC" \
    
    427 487
         || ( cat config.log; fail "configure failed" )
    
    ... ... @@ -562,12 +622,35 @@ function install_bindist() {
    562 622
           read -r -a args <<< "${INSTALL_CONFIGURE_ARGS:-}"
    
    563 623
     
    
    564 624
           if [[ "${CROSS_TARGET:-no_cross_target}" =~ "mingw" ]]; then
    
    565
    -          # We suppose that host target = build target.
    
    625
    +          # We assume that BUILD=HOST.
    
    566 626
               # By the fact above it is clearly turning out which host value is
    
    567 627
               # for currently built compiler.
    
    568 628
               # The fix for #21970 will probably remove this if-branch.
    
    569
    -          local -r CROSS_HOST_GUESS=$($SHELL ./config.guess)
    
    570
    -          args+=( "--target=$CROSS_TARGET" "--host=$CROSS_HOST_GUESS" )
    
    629
    +          # Modifications are needed due of reasons like See Note [Wide Triple Windows].
    
    630
    +
    
    631
    +          local -r cross_host_triple_guess_origin=$($SHELL ./config.guess)
    
    632
    +
    
    633
    +          # We expect here to have (x86_64|aarch64)
    
    634
    +          local -r cross_host_triple_guess_arch=$(echo "${cross_host_triple_guess_origin}" | cut -d'-' -f1)
    
    635
    +
    
    636
    +          # Expect to have (apple|unknown)
    
    637
    +          local -r cross_host_triple_guess_vendor=$(echo "${cross_host_triple_guess_origin}" \
    
    638
    +            `# "pc" should be converted to unknown for all supported platforms by GHC` \
    
    639
    +            | sed -e "s/-pc-/-unknown-/" | cut -d'-' -f2)
    
    640
    +
    
    641
    +          # 3,4 because it might contain a dash, expect to have (linux-gnu|mingw32|darwin)
    
    642
    +          local -r cross_host_triple_guess_os=$(echo "${cross_host_triple_guess_origin}" | cut -d'-' -f3,4 \
    
    643
    +            `# GHC treats mingw64 as mingw32, so, we need hide this difference` \
    
    644
    +            | sed -e "s/mingw.*/mingw32/" \
    
    645
    +            `# config.guess may return triple with a release number, i.e. for darwin: aarch64-apple-darwin24.4.0` \
    
    646
    +            | sed -e "s/darwin.*/darwin/" \
    
    647
    +            | sed -e "s/freebsd.*/freebsd/" \
    
    648
    +            )
    
    649
    +
    
    650
    +          local -r cross_host_triple_guess="$cross_host_triple_guess_arch-$cross_host_triple_guess_vendor-$cross_host_triple_guess_os"
    
    651
    +          echo "Convert guessed triple ${cross_host_triple_guess_origin} to GHC-compatible: ${cross_host_triple_guess}"
    
    652
    +
    
    653
    +          args+=( "--target=$CROSS_TARGET" "--host=$cross_host_triple_guess" )
    
    571 654
     
    
    572 655
           # FIXME: The bindist configure script shouldn't need to be reminded of
    
    573 656
           # the target platform. See #21970.
    
    ... ... @@ -946,10 +1029,12 @@ esac
    946 1029
     MAKE="make"
    
    947 1030
     TAR="tar"
    
    948 1031
     case "$(uname)" in
    
    949
    -  MSYS_*|MINGW*) mingw_init ;;
    
    950
    -  Darwin) boot_triple="x86_64-apple-darwin" ;;
    
    1032
    +  MSYS_*|MINGW*)
    
    1033
    +    mingw_init
    
    1034
    +    ;;
    
    1035
    +  Darwin)
    
    1036
    +    ;;
    
    951 1037
       FreeBSD)
    
    952
    -    boot_triple="x86_64-portbld-freebsd"
    
    953 1038
         MAKE="gmake"
    
    954 1039
         TAR="gtar"
    
    955 1040
         ;;
    

  • .gitlab/generate-ci/gen_ci.hs
    ... ... @@ -1302,17 +1302,21 @@ cross_jobs = [
    1302 1302
             . setVariable "WindresCmd" (llvm_prefix ++ "windres")
    
    1303 1303
             . setVariable "LLVMAS" (llvm_prefix ++ "clang")
    
    1304 1304
             . setVariable "LD" (llvm_prefix ++ "ld")
    
    1305
    +          -- See Note [Empty MergeObjsCmd]
    
    1305 1306
               -- Windows target require to make linker merge feature check disabled.
    
    1306 1307
             . setVariable "MergeObjsCmd" ""
    
    1308
    +          -- Note [Wide Triple Windows]
    
    1309
    +          -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1307 1310
               -- LLVM MinGW Linux Toolchain expects to recieve "aarch64-w64-mingw32"
    
    1308 1311
               -- as a triple but we use more common "aarch64-unknown-mingw32".
    
    1309
    -          -- Due of this we need configure ld manually for clang beacause
    
    1312
    +          -- Due of this we need configure ld manually for clang because
    
    1310 1313
               -- it will use system's ld otherwise when --target will be specified to
    
    1311 1314
               -- unexpected triple.
    
    1312 1315
             . setVariable "CFLAGS" cflags
    
    1313 1316
             . setVariable "CONF_CC_OPTS_STAGE2" cflags
    
    1314 1317
             ) where
    
    1315 1318
                 llvm_prefix = "/opt/llvm-mingw-linux/bin/aarch64-w64-mingw32-"
    
    1319
    +            -- See Note [Windows Toolchain Standard Library Options]
    
    1316 1320
                 cflags = "-fuse-ld=" ++ llvm_prefix ++ "ld --rtlib=compiler-rt"
    
    1317 1321
     
    
    1318 1322
         winAarch64Config = (crossConfig "aarch64-unknown-mingw32" (Emulator "/opt/wine-arm64ec-msys2-deb12/bin/wine") Nothing)
    

  • boot
    ... ... @@ -52,6 +52,8 @@ def autoreconf():
    52 52
         # Run autoreconf on everything that needs it.
    
    53 53
         processes = {}
    
    54 54
         if os.name == 'nt':
    
    55
    +        # Note [ACLOCAL_PATH for Windows]
    
    56
    +        # ~~~~~~~~~~~~~~~~~~~~~~~~~
    
    55 57
             # Get the normalized ACLOCAL_PATH for Windows
    
    56 58
             # This is necessary since on Windows this will be a Windows
    
    57 59
             # path, which autoreconf doesn't know doesn't know how to handle.
    

  • compiler/GHC/HsToCore/Pmc/Solver/Types.hs
    ... ... @@ -64,7 +64,7 @@ import GHC.Builtin.Names
    64 64
     import GHC.Builtin.Types
    
    65 65
     import GHC.Builtin.Types.Prim
    
    66 66
     import GHC.Tc.Solver.InertSet (InertSet, emptyInert)
    
    67
    -import GHC.Tc.Utils.TcType (isStringTy)
    
    67
    +import GHC.Tc.Utils.TcType (isStringTy, topTcLevel)
    
    68 68
     import GHC.Types.CompleteMatch
    
    69 69
     import GHC.Types.SourceText (SourceText(..), mkFractionalLit, FractionalLit
    
    70 70
                                 , fractionalLitFromRational
    
    ... ... @@ -129,7 +129,7 @@ instance Outputable TyState where
    129 129
       ppr (TySt n inert) = ppr n <+> ppr inert
    
    130 130
     
    
    131 131
     initTyState :: TyState
    
    132
    -initTyState = TySt 0 emptyInert
    
    132
    +initTyState = TySt 0 (emptyInert topTcLevel)
    
    133 133
     
    
    134 134
     -- | The term oracle state. Stores 'VarInfo' for encountered 'Id's. These
    
    135 135
     -- entries are possibly shared when we figure out that two variables must be
    

  • compiler/GHC/Runtime/Eval.hs
    ... ... @@ -23,7 +23,7 @@ module GHC.Runtime.Eval (
    23 23
             setupBreakpoint,
    
    24 24
             back, forward,
    
    25 25
             setContext, getContext,
    
    26
    -        mkTopLevEnv,
    
    26
    +        mkTopLevEnv, mkTopLevImportedEnv,
    
    27 27
             getNamesInScope,
    
    28 28
             getRdrNamesInScope,
    
    29 29
             moduleIsInterpreted,
    
    ... ... @@ -836,29 +836,36 @@ mkTopLevEnv hsc_env modl
    836 836
           Nothing -> pure $ Left "not a home module"
    
    837 837
           Just details ->
    
    838 838
              case mi_top_env (hm_iface details) of
    
    839
    -                (IfaceTopEnv exports imports) -> do
    
    840
    -                  imports_env <-
    
    841
    -                        runInteractiveHsc hsc_env
    
    842
    -                      $ ioMsgMaybe $ hoistTcRnMessage $ runTcInteractive hsc_env
    
    843
    -                      $ fmap (foldr plusGlobalRdrEnv emptyGlobalRdrEnv)
    
    844
    -                      $ forM imports $ \iface_import -> do
    
    845
    -                        let ImpUserSpec spec details = tcIfaceImport iface_import
    
    846
    -                        iface <- loadInterfaceForModule (text "imported by GHCi") (is_mod spec)
    
    847
    -                        pure $ case details of
    
    848
    -                          ImpUserAll -> importsFromIface hsc_env iface spec Nothing
    
    849
    -                          ImpUserEverythingBut ns -> importsFromIface hsc_env iface spec (Just ns)
    
    850
    -                          ImpUserExplicit x _parents_of_implicits ->
    
    851
    -                            -- TODO: Not quite right, is_explicit should refer to whether the user wrote A(..) or A(x,y).
    
    852
    -                            -- It is only used for error messages. It seems dubious even to add an import context to these GREs as
    
    853
    -                            -- they are not "imported" into the top-level scope of the REPL. I changed this for now so that
    
    854
    -                            -- the test case produce the same output as before.
    
    855
    -                            let spec' = ImpSpec { is_decl = spec, is_item = ImpSome { is_explicit = True, is_iloc = noSrcSpan } }
    
    856
    -                            in mkGlobalRdrEnv $ gresFromAvails hsc_env (Just spec') x
    
    839
    +                (IfaceTopEnv exports _imports) -> do
    
    840
    +                  imports_env <- mkTopLevImportedEnv hsc_env details
    
    857 841
                       let exports_env = mkGlobalRdrEnv $ gresFromAvails hsc_env Nothing (getDetOrdAvails exports)
    
    858 842
                       pure $ Right $ plusGlobalRdrEnv imports_env exports_env
    
    859 843
       where
    
    860 844
         hpt = hsc_HPT hsc_env
    
    861 845
     
    
    846
    +-- | Make the top-level environment with all bindings imported by this module.
    
    847
    +-- Exported bindings from this module are not included in the result.
    
    848
    +mkTopLevImportedEnv :: HscEnv -> HomeModInfo -> IO GlobalRdrEnv
    
    849
    +mkTopLevImportedEnv hsc_env details = do
    
    850
    +    runInteractiveHsc hsc_env
    
    851
    +  $ ioMsgMaybe $ hoistTcRnMessage $ runTcInteractive hsc_env
    
    852
    +  $ fmap (foldr plusGlobalRdrEnv emptyGlobalRdrEnv)
    
    853
    +  $ forM imports $ \iface_import -> do
    
    854
    +    let ImpUserSpec spec details = tcIfaceImport iface_import
    
    855
    +    iface <- loadInterfaceForModule (text "imported by GHCi") (is_mod spec)
    
    856
    +    pure $ case details of
    
    857
    +      ImpUserAll -> importsFromIface hsc_env iface spec Nothing
    
    858
    +      ImpUserEverythingBut ns -> importsFromIface hsc_env iface spec (Just ns)
    
    859
    +      ImpUserExplicit x _parents_of_implicits ->
    
    860
    +        -- TODO: Not quite right, is_explicit should refer to whether the user wrote A(..) or A(x,y).
    
    861
    +        -- It is only used for error messages. It seems dubious even to add an import context to these GREs as
    
    862
    +        -- they are not "imported" into the top-level scope of the REPL. I changed this for now so that
    
    863
    +        -- the test case produce the same output as before.
    
    864
    +        let spec' = ImpSpec { is_decl = spec, is_item = ImpSome { is_explicit = True, is_iloc = noSrcSpan } }
    
    865
    +        in mkGlobalRdrEnv $ gresFromAvails hsc_env (Just spec') x
    
    866
    +  where
    
    867
    +    IfaceTopEnv _ imports = mi_top_env (hm_iface details)
    
    868
    +
    
    862 869
     -- | Get the interactive evaluation context, consisting of a pair of the
    
    863 870
     -- set of modules from which we take the full top-level scope, and the set
    
    864 871
     -- of modules from which we take just the exports respectively.
    

  • compiler/GHC/Tc/Solver.hs
    ... ... @@ -915,21 +915,22 @@ simplifyInfer top_lvl rhs_tclvl infer_mode sigs name_taus wanteds
    915 915
            ; let psig_theta = concatMap sig_inst_theta partial_sigs
    
    916 916
     
    
    917 917
            -- First do full-blown solving
    
    918
    -       -- NB: we must gather up all the bindings from doing
    
    919
    -       -- this solving; hence (runTcSWithEvBinds ev_binds_var).
    
    920
    -       -- And note that since there are nested implications,
    
    921
    -       -- calling solveWanteds will side-effect their evidence
    
    922
    -       -- bindings, so we can't just revert to the input
    
    923
    -       -- constraint.
    
    924
    -
    
    918
    +       -- NB: we must gather up all the bindings from doing this solving; hence
    
    919
    +       -- (runTcSWithEvBinds ev_binds_var).  And note that since there are
    
    920
    +       -- nested implications, calling solveWanteds will side-effect their
    
    921
    +       -- evidence bindings, so we can't just revert to the input constraint.
    
    922
    +       --
    
    923
    +       -- See also Note [Inferring principal types]
    
    925 924
            ; ev_binds_var <- TcM.newTcEvBinds
    
    926 925
            ; psig_evs     <- newWanteds AnnOrigin psig_theta
    
    927 926
            ; wanted_transformed
    
    928
    -            <- setTcLevel rhs_tclvl $
    
    929
    -               runTcSWithEvBinds ev_binds_var $
    
    927
    +            <- runTcSWithEvBinds ev_binds_var $
    
    928
    +               setTcLevelTcS rhs_tclvl        $
    
    930 929
                    solveWanteds (mkSimpleWC psig_evs `andWC` wanteds)
    
    930
    +               -- setLevelTcS: we do setLevel /inside/ the runTcS, so that
    
    931
    +               --              we initialise the InertSet inert_given_eq_lvl as far
    
    932
    +               --              out as possible, maximising oppportunities to unify
    
    931 933
                    -- psig_evs : see Note [Add signature contexts as wanteds]
    
    932
    -               -- See Note [Inferring principal types]
    
    933 934
     
    
    934 935
            -- Find quant_pred_candidates, the predicates that
    
    935 936
            -- we'll consider quantifying over
    
    ... ... @@ -1430,13 +1431,15 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted
    1430 1431
     
    
    1431 1432
                  -- Step 1 of Note [decideAndPromoteTyVars]
    
    1432 1433
                  -- Get candidate constraints, decide which we can potentially quantify
    
    1433
    -             (can_quant_cts, no_quant_cts) = approximateWCX wanted
    
    1434
    +             -- The `no_quant_tvs` are free in constraints we can't quantify.
    
    1435
    +             (can_quant_cts, no_quant_tvs) = approximateWCX False wanted
    
    1434 1436
                  can_quant = ctsPreds can_quant_cts
    
    1435
    -             no_quant  = ctsPreds no_quant_cts
    
    1437
    +             can_quant_tvs = tyCoVarsOfTypes can_quant
    
    1436 1438
     
    
    1437 1439
                  -- Step 2 of Note [decideAndPromoteTyVars]
    
    1438 1440
                  -- Apply the monomorphism restriction
    
    1439 1441
                  (post_mr_quant, mr_no_quant) = applyMR dflags infer_mode can_quant
    
    1442
    +             mr_no_quant_tvs              = tyCoVarsOfTypes mr_no_quant
    
    1440 1443
     
    
    1441 1444
                  -- The co_var_tvs are tvs mentioned in the types of covars or
    
    1442 1445
                  -- coercion holes. We can't quantify over these covars, so we
    
    ... ... @@ -1448,30 +1451,33 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted
    1448 1451
                                              ++ tau_tys ++ post_mr_quant)
    
    1449 1452
                  co_var_tvs = closeOverKinds co_vars
    
    1450 1453
     
    
    1451
    -             -- outer_tvs are mentioned in `wanted, and belong to some outer level.
    
    1454
    +             -- outer_tvs are mentioned in `wanted`, and belong to some outer level.
    
    1452 1455
                  -- We definitely can't quantify over them
    
    1453 1456
                  outer_tvs = outerLevelTyVars rhs_tclvl $
    
    1454
    -                         tyCoVarsOfTypes can_quant `unionVarSet` tyCoVarsOfTypes no_quant
    
    1457
    +                         can_quant_tvs `unionVarSet` no_quant_tvs
    
    1455 1458
     
    
    1456
    -             -- Step 3 of Note [decideAndPromoteTyVars]
    
    1459
    +             -- Step 3 of Note [decideAndPromoteTyVars], (a-c)
    
    1457 1460
                  -- Identify mono_tvs: the type variables that we must not quantify over
    
    1461
    +             -- At top level we are much less keen to create mono tyvars, to avoid
    
    1462
    +             -- spooky action at a distance.
    
    1458 1463
                  mono_tvs_without_mr
    
    1459
    -               | is_top_level = outer_tvs
    
    1460
    -               | otherwise    = outer_tvs                                 -- (a)
    
    1461
    -                                `unionVarSet` tyCoVarsOfTypes no_quant    -- (b)
    
    1462
    -                                `unionVarSet` co_var_tvs                  -- (c)
    
    1464
    +               | is_top_level = outer_tvs    -- See (DP2)
    
    1465
    +               | otherwise    = outer_tvs                    -- (a)
    
    1466
    +                                `unionVarSet` no_quant_tvs   -- (b)
    
    1467
    +                                `unionVarSet` co_var_tvs     -- (c)
    
    1463 1468
     
    
    1469
    +             -- Step 3 of Note [decideAndPromoteTyVars], (d)
    
    1464 1470
                  mono_tvs_with_mr
    
    1465 1471
                    = -- Even at top level, we don't quantify over type variables
    
    1466 1472
                      -- mentioned in constraints that the MR tells us not to quantify
    
    1467 1473
                      -- See Note [decideAndPromoteTyVars] (DP2)
    
    1468
    -                 mono_tvs_without_mr `unionVarSet` tyCoVarsOfTypes mr_no_quant
    
    1474
    +                 mono_tvs_without_mr `unionVarSet` mr_no_quant_tvs
    
    1469 1475
     
    
    1470 1476
                  --------------------------------------------------------------------
    
    1471 1477
                  -- Step 4 of Note [decideAndPromoteTyVars]
    
    1472 1478
                  -- Use closeWrtFunDeps to find any other variables that are determined by mono_tvs
    
    1473
    -             add_determined tvs = closeWrtFunDeps post_mr_quant tvs
    
    1474
    -                                  `delVarSetList` psig_qtvs
    
    1479
    +             add_determined tvs preds = closeWrtFunDeps preds tvs
    
    1480
    +                                        `delVarSetList` psig_qtvs
    
    1475 1481
                      -- Why delVarSetList psig_qtvs?
    
    1476 1482
                      -- If the user has explicitly asked for quantification, then that
    
    1477 1483
                      -- request "wins" over the MR.
    
    ... ... @@ -1480,8 +1486,8 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted
    1480 1486
                      -- (i.e. says "no" to isQuantifiableTv)? That's OK: explanation
    
    1481 1487
                      -- in Step 2 of Note [Deciding quantification].
    
    1482 1488
     
    
    1483
    -             mono_tvs_with_mr_det    = add_determined mono_tvs_with_mr
    
    1484
    -             mono_tvs_without_mr_det = add_determined mono_tvs_without_mr
    
    1489
    +             mono_tvs_with_mr_det    = add_determined mono_tvs_with_mr    post_mr_quant
    
    1490
    +             mono_tvs_without_mr_det = add_determined mono_tvs_without_mr can_quant
    
    1485 1491
     
    
    1486 1492
                  --------------------------------------------------------------------
    
    1487 1493
                  -- Step 5 of Note [decideAndPromoteTyVars]
    
    ... ... @@ -1518,7 +1524,7 @@ decideAndPromoteTyVars top_lvl rhs_tclvl infer_mode name_taus psigs wanted
    1518 1524
                , text "newly_mono_tvs =" <+> ppr newly_mono_tvs
    
    1519 1525
                , text "can_quant =" <+> ppr can_quant
    
    1520 1526
                , text "post_mr_quant =" <+> ppr post_mr_quant
    
    1521
    -           , text "no_quant =" <+> ppr no_quant
    
    1527
    +           , text "no_quant_tvs =" <+> ppr no_quant_tvs
    
    1522 1528
                , text "mr_no_quant =" <+> ppr mr_no_quant
    
    1523 1529
                , text "final_quant =" <+> ppr final_quant
    
    1524 1530
                , text "co_vars =" <+> ppr co_vars ]
    
    ... ... @@ -1605,8 +1611,8 @@ The plan
    1605 1611
       The body of z tries to unify the type of x (call it alpha[1]) with
    
    1606 1612
       (beta[2] -> gamma[2]). This unification fails because alpha is untouchable, leaving
    
    1607 1613
            [W] alpha[1] ~ (beta[2] -> gamma[2])
    
    1608
    -  We need to know not to quantify over beta or gamma, because they are in the
    
    1609
    -  equality constraint with alpha. Actual test case:   typecheck/should_compile/tc213
    
    1614
    +  We don't want to quantify over beta or gamma because they are fixed by alpha,
    
    1615
    +  which is monomorphic. Actual test case:   typecheck/should_compile/tc213
    
    1610 1616
     
    
    1611 1617
       Another example. Suppose we have
    
    1612 1618
           class C a b | a -> b
    
    ... ... @@ -1643,9 +1649,22 @@ Wrinkles
    1643 1649
       promote type variables.  But for bindings affected by the MR we have no choice
    
    1644 1650
       but to promote.
    
    1645 1651
     
    
    1652
    +  An example is in #26004.
    
    1653
    +      f w e = case e of
    
    1654
    +        T1 -> let y = not w in False
    
    1655
    +        T2 -> True
    
    1656
    +  When generalising `f` we have a constraint
    
    1657
    +      forall. (a ~ Bool) => alpha ~ Bool
    
    1658
    +  where our provisional type for `f` is `f :: T alpha -> blah`.
    
    1659
    +  In a /nested/ setting, we might simply not-generalise `f`, hoping to learn
    
    1660
    +  about `alpha` from f's call sites (test T5266b is an example).  But at top
    
    1661
    +  level, to avoid spooky action at a distance.
    
    1662
    +
    
    1646 1663
     Note [The top-level Any principle]
    
    1647 1664
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    1648
    -Key principle: we never want to show the programmer a type with `Any` in it.
    
    1665
    +Key principles:
    
    1666
    +  * we never want to show the programmer a type with `Any` in it.
    
    1667
    +  * avoid "spooky action at a distance" and silent defaulting
    
    1649 1668
     
    
    1650 1669
     Most /top level/ bindings have a type signature, so none of this arises.  But
    
    1651 1670
     where a top-level binding lacks a signature, we don't want to infer a type like
    
    ... ... @@ -1654,11 +1673,18 @@ and then subsequently default alpha[0]:=Any. Exposing `Any` to the user is bad
    1654 1673
     bad bad.  Better to report an error, which is what may well happen if we
    
    1655 1674
     quantify over alpha instead.
    
    1656 1675
     
    
    1676
    +Moreover,
    
    1677
    + * If (elsewhere in this module) we add a call to `f`, say (f True), then
    
    1678
    +   `f` will get the type `Bool -> Int`
    
    1679
    + * If we add /another/ call, say (f 'x'), we will then get a type error.
    
    1680
    + * If we have no calls, the final exported type of `f` may get set by
    
    1681
    +   defaulting, and might not be principal (#26004).
    
    1682
    +
    
    1657 1683
     For /nested/ bindings, a monomorphic type like `f :: alpha[0] -> Int` is fine,
    
    1658 1684
     because we can see all the call sites of `f`, and they will probably fix
    
    1659 1685
     `alpha`.  In contrast, we can't see all of (or perhaps any of) the calls of
    
    1660 1686
     top-level (exported) functions, reducing the worries about "spooky action at a
    
    1661
    -distance".
    
    1687
    +distance".  This also moves in the direction of `MonoLocalBinds`, which we like.
    
    1662 1688
     
    
    1663 1689
     Note [Do not quantify over constraints that determine a variable]
    
    1664 1690
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    

  • compiler/GHC/Tc/Solver/InertSet.hs
    ... ... @@ -374,20 +374,20 @@ instance Outputable InertSet where
    374 374
              where
    
    375 375
                dicts = bagToList (dictsToBag solved_dicts)
    
    376 376
     
    
    377
    -emptyInertCans :: InertCans
    
    378
    -emptyInertCans
    
    377
    +emptyInertCans :: TcLevel -> InertCans
    
    378
    +emptyInertCans given_eq_lvl
    
    379 379
       = IC { inert_eqs          = emptyTyEqs
    
    380 380
            , inert_funeqs       = emptyFunEqs
    
    381
    -       , inert_given_eq_lvl = topTcLevel
    
    381
    +       , inert_given_eq_lvl = given_eq_lvl
    
    382 382
            , inert_given_eqs    = False
    
    383 383
            , inert_dicts        = emptyDictMap
    
    384 384
            , inert_safehask     = emptyDictMap
    
    385 385
            , inert_insts        = []
    
    386 386
            , inert_irreds       = emptyBag }
    
    387 387
     
    
    388
    -emptyInert :: InertSet
    
    389
    -emptyInert
    
    390
    -  = IS { inert_cans           = emptyInertCans
    
    388
    +emptyInert :: TcLevel -> InertSet
    
    389
    +emptyInert given_eq_lvl
    
    390
    +  = IS { inert_cans           = emptyInertCans given_eq_lvl
    
    391 391
            , inert_cycle_breakers = emptyBag :| []
    
    392 392
            , inert_famapp_cache   = emptyFunEqs
    
    393 393
            , inert_solved_dicts   = emptyDictMap }
    
    ... ... @@ -678,6 +678,23 @@ should update inert_given_eq_lvl?
    678 678
        imply nominal ones. For example, if (G a ~R G b) and G's argument's
    
    679 679
        role is nominal, then we can deduce a ~N b.
    
    680 680
     
    
    681
    +(TGE6) A subtle point is this: when initialising the solver, giving it
    
    682
    +   an empty InertSet, we must conservatively initialise `inert_given_lvl`
    
    683
    +   to the /current/ TcLevel.  This matters when doing let-generalisation.
    
    684
    +   Consider #26004:
    
    685
    +      f w e = case e of
    
    686
    +                  T1 -> let y = not w in False   -- T1 is a GADT
    
    687
    +                  T2 -> True
    
    688
    +   When let-generalising `y`, we will have (w :: alpha[1]) in the type
    
    689
    +   envt; and we are under GADT pattern match.  So when we solve the
    
    690
    +   constraints from y's RHS, in simplifyInfer, we must NOT unify
    
    691
    +       alpha[1] := Bool
    
    692
    +   Since we don't know what enclosing equalities there are, we just
    
    693
    +   conservatively assume that there are some.
    
    694
    +
    
    695
    +   This initialisation in done in `runTcSWithEvBinds`, which passes
    
    696
    +   the current TcLevl to `emptyInert`.
    
    697
    +
    
    681 698
     Historical note: prior to #24938 we also ignored Given equalities that
    
    682 699
     did not mention an "outer" type variable.  But that is wrong, as #24938
    
    683 700
     showed. Another example is immortalised in test LocalGivenEqs2
    

  • compiler/GHC/Tc/Solver/Monad.hs
    ... ... @@ -20,7 +20,7 @@ module GHC.Tc.Solver.Monad (
    20 20
         runTcSSpecPrag,
    
    21 21
         failTcS, warnTcS, addErrTcS, wrapTcS, ctLocWarnTcS,
    
    22 22
         runTcSEqualities,
    
    23
    -    nestTcS, nestImplicTcS, setEvBindsTcS,
    
    23
    +    nestTcS, nestImplicTcS, setEvBindsTcS, setTcLevelTcS,
    
    24 24
         emitImplicationTcS, emitTvImplicationTcS,
    
    25 25
         emitImplication,
    
    26 26
         emitFunDepWanteds,
    
    ... ... @@ -947,8 +947,9 @@ added. This is initialised from the innermost implication constraint.
    947 947
     -- | See Note [TcSMode]
    
    948 948
     data TcSMode
    
    949 949
       = TcSVanilla    -- ^ Normal constraint solving
    
    950
    +  | TcSPMCheck    -- ^ Used when doing patterm match overlap checks
    
    950 951
       | TcSEarlyAbort -- ^ Abort early on insoluble constraints
    
    951
    -  | TcSSpecPrag -- ^ Fully solve all constraints
    
    952
    +  | TcSSpecPrag   -- ^ Fully solve all constraints
    
    952 953
       deriving (Eq)
    
    953 954
     
    
    954 955
     {- Note [TcSMode]
    
    ... ... @@ -957,6 +958,11 @@ The constraint solver can operate in different modes:
    957 958
     
    
    958 959
     * TcSVanilla: Normal constraint solving mode. This is the default.
    
    959 960
     
    
    961
    +* TcSPMCheck: Used by the pattern match overlap checker.
    
    962
    +      Like TcSVanilla, but the idea is that the returned InertSet will
    
    963
    +      later be resumed, so we do not want to restore type-equality cycles
    
    964
    +      See also Note [Type equality cycles] in GHC.Tc.Solver.Equality
    
    965
    +
    
    960 966
     * TcSEarlyAbort: Abort (fail in the monad) as soon as we come across an
    
    961 967
       insoluble constraint. This is used to fail-fast when checking for hole-fits.
    
    962 968
       See Note [Speeding up valid hole-fits].
    
    ... ... @@ -1135,7 +1141,7 @@ runTcS tcs
    1135 1141
     runTcSEarlyAbort :: TcS a -> TcM a
    
    1136 1142
     runTcSEarlyAbort tcs
    
    1137 1143
       = do { ev_binds_var <- TcM.newTcEvBinds
    
    1138
    -       ; runTcSWithEvBinds' True TcSEarlyAbort ev_binds_var tcs }
    
    1144
    +       ; runTcSWithEvBinds' TcSEarlyAbort ev_binds_var tcs }
    
    1139 1145
     
    
    1140 1146
     -- | Run the 'TcS' monad in 'TcSSpecPrag' mode, which either fully solves
    
    1141 1147
     -- individual Wanted quantified constraints or leaves them alone.
    
    ... ... @@ -1143,7 +1149,7 @@ runTcSEarlyAbort tcs
    1143 1149
     -- See Note [TcSSpecPrag].
    
    1144 1150
     runTcSSpecPrag :: EvBindsVar -> TcS a -> TcM a
    
    1145 1151
     runTcSSpecPrag ev_binds_var tcs
    
    1146
    -  = runTcSWithEvBinds' True TcSSpecPrag ev_binds_var tcs
    
    1152
    +  = runTcSWithEvBinds' TcSSpecPrag ev_binds_var tcs
    
    1147 1153
     
    
    1148 1154
     {- Note [TcSSpecPrag]
    
    1149 1155
     ~~~~~~~~~~~~~~~~~~~~~
    
    ... ... @@ -1200,7 +1206,7 @@ runTcSEqualities thing_inside
    1200 1206
     runTcSInerts :: InertSet -> TcS a -> TcM (a, InertSet)
    
    1201 1207
     runTcSInerts inerts tcs = do
    
    1202 1208
       ev_binds_var <- TcM.newTcEvBinds
    
    1203
    -  runTcSWithEvBinds' False TcSVanilla ev_binds_var $ do
    
    1209
    +  runTcSWithEvBinds' TcSPMCheck ev_binds_var $ do
    
    1204 1210
         setInertSet inerts
    
    1205 1211
         a <- tcs
    
    1206 1212
         new_inerts <- getInertSet
    
    ... ... @@ -1209,21 +1215,23 @@ runTcSInerts inerts tcs = do
    1209 1215
     runTcSWithEvBinds :: EvBindsVar
    
    1210 1216
                       -> TcS a
    
    1211 1217
                       -> TcM a
    
    1212
    -runTcSWithEvBinds = runTcSWithEvBinds' True TcSVanilla
    
    1218
    +runTcSWithEvBinds = runTcSWithEvBinds' TcSVanilla
    
    1213 1219
     
    
    1214
    -runTcSWithEvBinds' :: Bool  -- True <=> restore type equality cycles
    
    1215
    -                           -- Don't if you want to reuse the InertSet.
    
    1216
    -                           -- See also Note [Type equality cycles]
    
    1217
    -                           -- in GHC.Tc.Solver.Equality
    
    1218
    -                   -> TcSMode
    
    1220
    +runTcSWithEvBinds' :: TcSMode
    
    1219 1221
                        -> EvBindsVar
    
    1220 1222
                        -> TcS a
    
    1221 1223
                        -> TcM a
    
    1222
    -runTcSWithEvBinds' restore_cycles mode ev_binds_var tcs
    
    1224
    +runTcSWithEvBinds' mode ev_binds_var tcs
    
    1223 1225
       = do { unified_var <- TcM.newTcRef 0
    
    1224
    -       ; step_count <- TcM.newTcRef 0
    
    1225
    -       ; inert_var <- TcM.newTcRef emptyInert
    
    1226
    -       ; wl_var <- TcM.newTcRef emptyWorkList
    
    1226
    +       ; step_count  <- TcM.newTcRef 0
    
    1227
    +
    
    1228
    +       -- Make a fresh, empty inert set
    
    1229
    +       -- Subtle point: see (TGE6) in Note [Tracking Given equalities]
    
    1230
    +       --               in GHC.Tc.Solver.InertSet
    
    1231
    +       ; tc_lvl      <- TcM.getTcLevel
    
    1232
    +       ; inert_var   <- TcM.newTcRef (emptyInert tc_lvl)
    
    1233
    +
    
    1234
    +       ; wl_var      <- TcM.newTcRef emptyWorkList
    
    1227 1235
            ; unif_lvl_var <- TcM.newTcRef Nothing
    
    1228 1236
            ; let env = TcSEnv { tcs_ev_binds           = ev_binds_var
    
    1229 1237
                               , tcs_unified            = unified_var
    
    ... ... @@ -1240,9 +1248,13 @@ runTcSWithEvBinds' restore_cycles mode ev_binds_var tcs
    1240 1248
            ; when (count > 0) $
    
    1241 1249
              csTraceTcM $ return (text "Constraint solver steps =" <+> int count)
    
    1242 1250
     
    
    1243
    -       ; when restore_cycles $
    
    1244
    -         do { inert_set <- TcM.readTcRef inert_var
    
    1245
    -            ; restoreTyVarCycles inert_set }
    
    1251
    +       -- Restore tyvar cycles: see Note [Type equality cycles] in
    
    1252
    +       --                       GHC.Tc.Solver.Equality
    
    1253
    +       -- But /not/ in TCsPMCheck mode: see Note [TcSMode]
    
    1254
    +       ; case mode of
    
    1255
    +            TcSPMCheck -> return ()
    
    1256
    +            _ -> do { inert_set <- TcM.readTcRef inert_var
    
    1257
    +                    ; restoreTyVarCycles inert_set }
    
    1246 1258
     
    
    1247 1259
     #if defined(DEBUG)
    
    1248 1260
            ; ev_binds <- TcM.getTcEvBindsMap ev_binds_var
    
    ... ... @@ -1284,6 +1296,10 @@ setEvBindsTcS :: EvBindsVar -> TcS a -> TcS a
    1284 1296
     setEvBindsTcS ref (TcS thing_inside)
    
    1285 1297
      = TcS $ \ env -> thing_inside (env { tcs_ev_binds = ref })
    
    1286 1298
     
    
    1299
    +setTcLevelTcS :: TcLevel -> TcS a -> TcS a
    
    1300
    +setTcLevelTcS lvl (TcS thing_inside)
    
    1301
    + = TcS $ \ env -> TcM.setTcLevel lvl (thing_inside env)
    
    1302
    +
    
    1287 1303
     nestImplicTcS :: EvBindsVar
    
    1288 1304
                   -> TcLevel -> TcS a
    
    1289 1305
                   -> TcS a
    

  • compiler/GHC/Tc/Types/Constraint.hs
    ... ... @@ -1743,24 +1743,21 @@ will be able to report a more informative error:
    1743 1743
     ************************************************************************
    
    1744 1744
     -}
    
    1745 1745
     
    
    1746
    -type ApproxWC = ( Bag Ct    -- Free quantifiable constraints
    
    1747
    -                , Bag Ct )  -- Free non-quantifiable constraints
    
    1748
    -                            -- due to shape, or enclosing equality
    
    1746
    +type ApproxWC = ( Bag Ct          -- Free quantifiable constraints
    
    1747
    +                , TcTyCoVarSet )  -- Free vars of non-quantifiable constraints
    
    1748
    +                                  -- due to shape, or enclosing equality
    
    1749 1749
     
    
    1750 1750
     approximateWC :: Bool -> WantedConstraints -> Bag Ct
    
    1751 1751
     approximateWC include_non_quantifiable cts
    
    1752
    -  | include_non_quantifiable = quant `unionBags` no_quant
    
    1753
    -  | otherwise                = quant
    
    1754
    -  where
    
    1755
    -    (quant, no_quant) = approximateWCX cts
    
    1752
    +  = fst (approximateWCX include_non_quantifiable cts)
    
    1756 1753
     
    
    1757
    -approximateWCX :: WantedConstraints -> ApproxWC
    
    1754
    +approximateWCX :: Bool -> WantedConstraints -> ApproxWC
    
    1758 1755
     -- The "X" means "extended";
    
    1759 1756
     --    we return both quantifiable and non-quantifiable constraints
    
    1760 1757
     -- See Note [ApproximateWC]
    
    1761 1758
     -- See Note [floatKindEqualities vs approximateWC]
    
    1762
    -approximateWCX wc
    
    1763
    -  = float_wc False emptyVarSet wc (emptyBag, emptyBag)
    
    1759
    +approximateWCX include_non_quantifiable wc
    
    1760
    +  = float_wc False emptyVarSet wc (emptyBag, emptyVarSet)
    
    1764 1761
       where
    
    1765 1762
         float_wc :: Bool           -- True <=> there are enclosing equalities
    
    1766 1763
                  -> TcTyCoVarSet   -- Enclosing skolem binders
    
    ... ... @@ -1786,17 +1783,23 @@ approximateWCX wc
    1786 1783
                -- There can be (insoluble) Given constraints in wc_simple,
    
    1787 1784
                -- there so that we get error reports for unreachable code
    
    1788 1785
                -- See `given_insols` in GHC.Tc.Solver.Solve.solveImplication
    
    1789
    -       | insolubleCt ct                              = acc
    
    1790
    -       | tyCoVarsOfCt ct `intersectsVarSet` skol_tvs = acc
    
    1791
    -       | otherwise
    
    1792
    -       = case classifyPredType (ctPred ct) of
    
    1786
    +       | insolubleCt ct                       = acc
    
    1787
    +       | pred_tvs `intersectsVarSet` skol_tvs = acc
    
    1788
    +       | include_non_quantifiable             = add_to_quant
    
    1789
    +       | is_quantifiable encl_eqs (ctPred ct) = add_to_quant
    
    1790
    +       | otherwise                            = add_to_no_quant
    
    1791
    +       where
    
    1792
    +         pred     = ctPred ct
    
    1793
    +         pred_tvs = tyCoVarsOfType pred
    
    1794
    +         add_to_quant    = (ct `consBag` quant, no_quant)
    
    1795
    +         add_to_no_quant = (quant, no_quant `unionVarSet` pred_tvs)
    
    1796
    +
    
    1797
    +    is_quantifiable encl_eqs pred
    
    1798
    +       = case classifyPredType pred of
    
    1793 1799
                -- See the classification in Note [ApproximateWC]
    
    1794 1800
                EqPred eq_rel ty1 ty2
    
    1795
    -             | not encl_eqs      -- See Wrinkle (W1)
    
    1796
    -             , quantify_equality eq_rel ty1 ty2
    
    1797
    -             -> add_to_quant
    
    1798
    -             | otherwise
    
    1799
    -             -> add_to_no_quant
    
    1801
    +             | encl_eqs  -> False  -- encl_eqs: See Wrinkle (W1)
    
    1802
    +             | otherwise -> quantify_equality eq_rel ty1 ty2
    
    1800 1803
     
    
    1801 1804
                ClassPred cls tys
    
    1802 1805
                  | Just {} <- isCallStackPred cls tys
    
    ... ... @@ -1804,17 +1807,14 @@ approximateWCX wc
    1804 1807
                    -- the constraints bubble up to be solved from the outer
    
    1805 1808
                    -- context, or be defaulted when we reach the top-level.
    
    1806 1809
                    -- See Note [Overview of implicit CallStacks] in GHC.Tc.Types.Evidence
    
    1807
    -             -> add_to_no_quant
    
    1810
    +             -> False
    
    1808 1811
     
    
    1809 1812
                  | otherwise
    
    1810
    -             -> add_to_quant  -- See Wrinkle (W2)
    
    1813
    +             -> True  -- See Wrinkle (W2)
    
    1811 1814
     
    
    1812
    -           IrredPred {}  -> add_to_quant  -- See Wrinkle (W2)
    
    1815
    +           IrredPred {}  -> True  -- See Wrinkle (W2)
    
    1813 1816
     
    
    1814
    -           ForAllPred {} -> add_to_no_quant  -- Never quantify these
    
    1815
    -       where
    
    1816
    -         add_to_quant    = (ct `consBag` quant, no_quant)
    
    1817
    -         add_to_no_quant = (quant, ct `consBag` no_quant)
    
    1817
    +           ForAllPred {} -> False  -- Never quantify these
    
    1818 1818
     
    
    1819 1819
         -- See Note [Quantifying over equality constraints]
    
    1820 1820
         quantify_equality NomEq  ty1 ty2 = quant_fun ty1 || quant_fun ty2
    
    ... ... @@ -1852,7 +1852,7 @@ We proceed by classifying the constraint:
    1852 1852
     
    
    1853 1853
     Wrinkle (W1)
    
    1854 1854
       When inferring most-general types (in simplifyInfer), we
    
    1855
    -  do *not* float an equality constraint if the implication binds
    
    1855
    +  do *not* quantify over equality constraint if the implication binds
    
    1856 1856
       equality constraints, because that defeats the OutsideIn story.
    
    1857 1857
       Consider data T a where { TInt :: T Int; MkT :: T a }
    
    1858 1858
              f TInt = 3::Int
    

  • compiler/GHC/Types/Name/Occurrence.hs
    ... ... @@ -92,6 +92,7 @@ module GHC.Types.Name.Occurrence (
    92 92
             plusOccEnv, plusOccEnv_C,
    
    93 93
             extendOccEnv_Acc, filterOccEnv, delListFromOccEnv, delFromOccEnv,
    
    94 94
             alterOccEnv, minusOccEnv, minusOccEnv_C, minusOccEnv_C_Ns,
    
    95
    +        sizeOccEnv,
    
    95 96
             pprOccEnv, forceOccEnv,
    
    96 97
             intersectOccEnv_C,
    
    97 98
     
    
    ... ... @@ -803,6 +804,10 @@ minusOccEnv_C_Ns f (MkOccEnv as) (MkOccEnv bs) =
    803 804
                then Nothing
    
    804 805
                else Just m
    
    805 806
     
    
    807
    +sizeOccEnv :: OccEnv a -> Int
    
    808
    +sizeOccEnv (MkOccEnv as) =
    
    809
    +  nonDetStrictFoldUFM (\ m !acc -> acc + sizeUFM m) 0 as
    
    810
    +
    
    806 811
     instance Outputable a => Outputable (OccEnv a) where
    
    807 812
         ppr x = pprOccEnv ppr x
    
    808 813
     
    

  • configure.ac
    ... ... @@ -658,12 +658,13 @@ GHC_LLVM_TARGET_SET_VAR
    658 658
     AC_SUBST(LlvmTarget)
    
    659 659
     
    
    660 660
     dnl ** See whether cc supports --target=<triple> and set
    
    661
    -dnl CONF_CC_OPTS_STAGE[012] accordingly.
    
    662
    -FP_CC_SUPPORTS_TARGET([$CC_STAGE0], [CONF_CC_OPTS_STAGE0], [CONF_CXX_OPTS_STAGE0])
    
    661
    +dnl CONF_CC_OPTS_STAGE[12] accordingly.
    
    663 662
     FP_CC_SUPPORTS_TARGET([$CC], [CONF_CC_OPTS_STAGE1], [CONF_CXX_OPTS_STAGE1])
    
    664 663
     FP_CC_SUPPORTS_TARGET([$CC], [CONF_CC_OPTS_STAGE2], [CONF_CXX_OPTS_STAGE2])
    
    665 664
     
    
    666
    -FP_PROG_CC_LINKER_TARGET([$CC_STAGE0], [CONF_CC_OPTS_STAGE0], [CONF_GCC_LINKER_OPTS_STAGE0])
    
    665
    +# CONF_CC_OPTS_STAGE0 should be left as is because it is already configured
    
    666
    +# by bootstrap compiler settings
    
    667
    +
    
    667 668
     FP_PROG_CC_LINKER_TARGET([$CC], [CONF_CC_OPTS_STAGE1], [CONF_GCC_LINKER_OPTS_STAGE1])
    
    668 669
     FP_PROG_CC_LINKER_TARGET([$CC], [CONF_CC_OPTS_STAGE2], [CONF_GCC_LINKER_OPTS_STAGE2])
    
    669 670
     
    

  • hadrian/src/Builder.hs
    ... ... @@ -26,7 +26,7 @@ import Hadrian.Builder.Tar
    26 26
     import Hadrian.Oracles.Path
    
    27 27
     import Hadrian.Oracles.TextFile
    
    28 28
     import Hadrian.Utilities
    
    29
    -import Oracles.Setting (bashPath, targetStage)
    
    29
    +import Oracles.Setting (bashPath, targetStage, isWinHost)
    
    30 30
     import System.Exit
    
    31 31
     import System.IO (stderr)
    
    32 32
     
    
    ... ... @@ -327,8 +327,14 @@ instance H.Builder Builder where
    327 327
                     Ar Unpack _ -> cmd' [Cwd output] [path] buildArgs buildOptions
    
    328 328
     
    
    329 329
                     Autoreconf dir -> do
    
    330
    +                  isWin <- isWinHost
    
    331
    +                  let aclocal_env =
    
    332
    +                        -- It is generally assumed that you would use MinGW's compilers from within an MSYS shell.
    
    333
    +                        -- See Note [ACLOCAL_PATH for Windows]
    
    334
    +                        if isWin then [AddEnv "ACLOCAL_PATH" "/c/msys64/usr/share/aclocal/"]
    
    335
    +                        else []
    
    330 336
                       bash <- bashPath
    
    331
    -                  cmd' [Cwd dir] [bash, path] buildArgs buildOptions
    
    337
    +                  cmd' (Cwd dir `cons` aclocal_env) [bash, path] buildArgs buildOptions
    
    332 338
     
    
    333 339
                     Configure  dir -> do
    
    334 340
                         -- Inject /bin/bash into `libtool`, instead of /bin/sh,
    

  • hadrian/src/Rules/BinaryDist.hs
    ... ... @@ -115,7 +115,12 @@ installTo relocatable prefix = do
    115 115
         targetPlatform <- setting TargetPlatformFull
    
    116 116
         let ghcVersionPretty = "ghc-" ++ version ++ "-" ++ targetPlatform
    
    117 117
             bindistFilesDir  = root -/- "bindist" -/- ghcVersionPretty
    
    118
    -    runBuilder (Configure bindistFilesDir) ["--prefix="++prefix] [] []
    
    118
    +    win <- isWinTarget
    
    119
    +    -- See Note [Empty MergeObjsCmd]
    
    120
    +    let disabledMerge =
    
    121
    +          if win then ["MergeObjsCmd="]
    
    122
    +          else []
    
    123
    +    runBuilder (Configure bindistFilesDir) (["--prefix="++prefix] ++ disabledMerge) [] []
    
    119 124
         let env = case relocatable of
    
    120 125
                     Relocatable -> [AddEnv "RelocatableBuild" "YES"]
    
    121 126
                     NotRelocatable -> []
    
    ... ... @@ -232,7 +237,7 @@ bindistRules = do
    232 237
             -- N.B. the ghc-pkg executable may be prefixed with a target triple
    
    233 238
             -- (c.f. #20267).
    
    234 239
             ghcPkgName <- programName (vanillaContext Stage1 ghcPkg)
    
    235
    -        cmd_ (bindistFilesDir -/- "bin" -/- ghcPkgName) ["recache"]
    
    240
    +        cmd_ (bindistFilesDir -/- "bin" -/- ghcPkgName <.> exe) ["recache"]
    
    236 241
     
    
    237 242
     
    
    238 243
     
    

  • libraries/ghc-internal/src/GHC/Internal/Event/Windows.hsc
    ... ... @@ -861,7 +861,9 @@ expirationTime mgr us = do
    861 861
     -- The 'TimeoutCallback' will not be called more than once.
    
    862 862
     --
    
    863 863
     -- Be careful not to exceed @maxBound :: Int@, which on 32-bit machines is only
    
    864
    --- 2147483647 μs, less than 36 minutes.
    
    864
    +-- 2147483647 microseconds, less than 36 minutes.
    
    865
    +-- We can not use here utf/greek symbol due of:
    
    866
    +-- _build/stage1/libraries/ghc-internal/build/GHC/Internal/Event/Windows.hs: commitBuffer: invalid argument (cannot encode character '\206')
    
    865 867
     --
    
    866 868
     {-# NOINLINE registerTimeout #-}
    
    867 869
     registerTimeout :: Manager -> Int -> TimeoutCallback -> IO TimeoutKey
    
    ... ... @@ -878,7 +880,9 @@ registerTimeout mgr@Manager{..} uSrelTime cb = do
    878 880
     -- This has no effect if the timeout has already fired.
    
    879 881
     --
    
    880 882
     -- Be careful not to exceed @maxBound :: Int@, which on 32-bit machines is only
    
    881
    --- 2147483647 μs, less than 36 minutes.
    
    883
    +-- 2147483647 microseconds, less than 36 minutes.
    
    884
    +-- We can not use here utf/greek symbol due of:
    
    885
    +-- _build/stage1/libraries/ghc-internal/build/GHC/Internal/Event/Windows.hs: commitBuffer: invalid argument (cannot encode character '\206')
    
    882 886
     --
    
    883 887
     updateTimeout :: Manager -> TimeoutKey -> Seconds -> IO ()
    
    884 888
     updateTimeout mgr (TK key) relTime = do
    
    ... ... @@ -980,7 +984,7 @@ step maxDelay mgr@Manager{..} = do
    980 984
         -- There are some unusual edge cases you need to deal with. The
    
    981 985
         -- GetQueuedCompletionStatus function blocks a thread until there's
    
    982 986
         -- work for it to do. Based on the return value, the number of bytes
    
    983
    -    -- and the overlapped structure, theres a lot of possible "reasons"
    
    987
    +    -- and the overlapped structure, there's a lot of possible "reasons"
    
    984 988
         -- for the function to have returned. Deciphering all the possible
    
    985 989
         -- cases:
    
    986 990
         --
    

  • m4/find_merge_objects.m4
    ... ... @@ -33,6 +33,8 @@ AC_DEFUN([FIND_MERGE_OBJECTS],[
    33 33
         fi
    
    34 34
     
    
    35 35
     
    
    36
    +    # Note [Empty MergeObjsCmd]
    
    37
    +    # ~~~~~~~~~~~~~~~~~~~~~~~~~
    
    36 38
         # If MergeObjsCmd="" then we assume that the user is explicitly telling us that
    
    37 39
         # they do not want to configure the MergeObjsCmd, this is particularly important for
    
    38 40
         # the bundled windows toolchain.
    

  • m4/fp_setup_windows_toolchain.m4
    ... ... @@ -85,6 +85,8 @@ AC_DEFUN([FP_SETUP_WINDOWS_TOOLCHAIN],[
    85 85
         mingw_prefix="$1"
    
    86 86
         mingw_install_prefix="$2"
    
    87 87
     
    
    88
    +    # Note [Windows Toolchain Standard Library Options]
    
    89
    +    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    88 90
         # Our Windows toolchain is based around Clang and LLD. We use compiler-rt
    
    89 91
         # for the runtime, libc++ and libc++abi for the C++ standard library
    
    90 92
         # implementation, and libunwind for C++ unwinding.
    

  • testsuite/tests/typecheck/should_fail/T26004.hs
    1
    +{-# LANGUAGE GADTs #-}
    
    2
    +{-# LANGUAGE NoMonoLocalBinds #-}
    
    3
    +
    
    4
    +module T26004 where
    
    5
    +
    
    6
    +data T a where
    
    7
    +  T1 :: T Bool
    
    8
    +  T2 :: T a
    
    9
    +
    
    10
    +-- This funcion should be rejected:
    
    11
    +-- we should not infer a non-principal type for `f`
    
    12
    +f w e = case e of
    
    13
    +  T1 -> let y = not w in False
    
    14
    +  T2 -> True

  • testsuite/tests/typecheck/should_fail/T26004.stderr
    1
    +
    
    2
    +T26004.hs:13:21: error: [GHC-25897]
    
    3
    +    • Could not deduce ‘p ~ Bool’
    
    4
    +      from the context: a ~ Bool
    
    5
    +        bound by a pattern with constructor: T1 :: T Bool,
    
    6
    +                 in a case alternative
    
    7
    +        at T26004.hs:13:3-4
    
    8
    +      ‘p’ is a rigid type variable bound by
    
    9
    +        the inferred type of f :: p -> T a -> Bool
    
    10
    +        at T26004.hs:(12,1)-(14,12)
    
    11
    +    • In the first argument of ‘not’, namely ‘w’
    
    12
    +      In the expression: not w
    
    13
    +      In an equation for ‘y’: y = not w
    
    14
    +    • Relevant bindings include
    
    15
    +        w :: p (bound at T26004.hs:12:3)
    
    16
    +        f :: p -> T a -> Bool (bound at T26004.hs:12:1)
    
    17
    +    Suggested fix: Consider giving ‘f’ a type signature

  • testsuite/tests/typecheck/should_fail/T7453.stderr
    1
    -
    
    2
    -T7453.hs:9:15: error: [GHC-25897]
    
    3
    -    • Couldn't match type ‘t’ with ‘p’
    
    4
    -      Expected: Id t
    
    5
    -        Actual: Id p
    
    1
    +T7453.hs:10:30: error: [GHC-25897]
    
    2
    +    • Couldn't match expected type ‘t’ with actual type ‘p’
    
    6 3
           ‘t’ is a rigid type variable bound by
    
    7 4
             the type signature for:
    
    8 5
               z :: forall t. Id t
    
    ... ... @@ -10,29 +7,17 @@ T7453.hs:9:15: error: [GHC-25897]
    10 7
           ‘p’ is a rigid type variable bound by
    
    11 8
             the inferred type of cast1 :: p -> a
    
    12 9
             at T7453.hs:(7,1)-(10,30)
    
    13
    -    • In the expression: aux
    
    14
    -      In an equation for ‘z’:
    
    15
    -          z = aux
    
    16
    -            where
    
    17
    -                aux = Id v
    
    18
    -      In an equation for ‘cast1’:
    
    19
    -          cast1 v
    
    20
    -            = runId z
    
    21
    -            where
    
    22
    -                z :: Id t
    
    23
    -                z = aux
    
    24
    -                  where
    
    25
    -                      aux = Id v
    
    10
    +    • In the first argument of ‘Id’, namely ‘v’
    
    11
    +      In the expression: Id v
    
    12
    +      In an equation for ‘aux’: aux = Id v
    
    26 13
         • Relevant bindings include
    
    27
    -        aux :: Id p (bound at T7453.hs:10:21)
    
    14
    +        aux :: Id t (bound at T7453.hs:10:21)
    
    28 15
             z :: Id t (bound at T7453.hs:9:11)
    
    29 16
             v :: p (bound at T7453.hs:7:7)
    
    30 17
             cast1 :: p -> a (bound at T7453.hs:7:1)
    
    31 18
     
    
    32
    -T7453.hs:15:15: error: [GHC-25897]
    
    33
    -    • Couldn't match type ‘t1’ with ‘p’
    
    34
    -      Expected: () -> t1
    
    35
    -        Actual: () -> p
    
    19
    +T7453.hs:16:33: error: [GHC-25897]
    
    20
    +    • Couldn't match expected type ‘t1’ with actual type ‘p’
    
    36 21
           ‘t1’ is a rigid type variable bound by
    
    37 22
             the type signature for:
    
    38 23
               z :: forall t1. () -> t1
    
    ... ... @@ -40,21 +25,11 @@ T7453.hs:15:15: error: [GHC-25897]
    40 25
           ‘p’ is a rigid type variable bound by
    
    41 26
             the inferred type of cast2 :: p -> t
    
    42 27
             at T7453.hs:(13,1)-(16,33)
    
    43
    -    • In the expression: aux
    
    44
    -      In an equation for ‘z’:
    
    45
    -          z = aux
    
    46
    -            where
    
    47
    -                aux = const v
    
    48
    -      In an equation for ‘cast2’:
    
    49
    -          cast2 v
    
    50
    -            = z ()
    
    51
    -            where
    
    52
    -                z :: () -> t
    
    53
    -                z = aux
    
    54
    -                  where
    
    55
    -                      aux = const v
    
    28
    +    • In the first argument of ‘const’, namely ‘v’
    
    29
    +      In the expression: const v
    
    30
    +      In an equation for ‘aux’: aux = const v
    
    56 31
         • Relevant bindings include
    
    57
    -        aux :: forall {b}. b -> p (bound at T7453.hs:16:21)
    
    32
    +        aux :: b -> t1 (bound at T7453.hs:16:21)
    
    58 33
             z :: () -> t1 (bound at T7453.hs:15:11)
    
    59 34
             v :: p (bound at T7453.hs:13:7)
    
    60 35
             cast2 :: p -> t (bound at T7453.hs:13:1)
    
    ... ... @@ -86,3 +61,4 @@ T7453.hs:21:15: error: [GHC-25897]
    86 61
             z :: t1 (bound at T7453.hs:21:11)
    
    87 62
             v :: p (bound at T7453.hs:19:7)
    
    88 63
             cast3 :: p -> t (bound at T7453.hs:19:1)
    
    64
    +

  • testsuite/tests/typecheck/should_fail/all.T
    ... ... @@ -735,3 +735,4 @@ test('T24938', normal, compile_fail, [''])
    735 735
     test('T25325', normal, compile_fail, [''])
    
    736 736
     test('T25004', normal, compile_fail, [''])
    
    737 737
     test('T25004k', normal, compile_fail, [''])
    
    738
    +test('T26004', normal, compile_fail, [''])