Hannes Siebenhandl pushed to branch wip/romes/27461 at Glasgow Haskell Compiler / GHC

Commits:

10 changed files:

Changes:

  • compiler/GHC/Driver/Downsweep.hs
    ... ... @@ -1555,7 +1555,23 @@ summariseModuleWithSource home_unit summ_cache_ref is_boot maybe_buf hsc_env loc
    1555 1555
         -- hi file, object file, when is_boot says so
    
    1556 1556
         let src_fn = expectJust (ml_hs_file location)
    
    1557 1557
         summ_cache <- readIORef summ_cache_ref
    
    1558
    -    case ml_hs_file_ospath location >>= \p -> M.lookup (moduleUnitId mod, p) summ_cache of
    
    1558
    +    -- Reject the cache result if the module name doesn't match the inferred
    
    1559
    +    -- module name based on the file name.
    
    1560
    +    -- This happens if we `summariseFile` for a root file target (which has a different module name)
    
    1561
    +    -- and then we try to import it.
    
    1562
    +    --
    
    1563
    +    -- Fall through to `new_summary` if this happens to reject this module.
    
    1564
    +    let cached = do
    
    1565
    +          p   <- ml_hs_file_ospath location
    
    1566
    +          res <- M.lookup (moduleUnitId mod, p) summ_cache
    
    1567
    +          case res of
    
    1568
    +            Right (ms, _) | msKey ms /= moduleToMnk mod is_boot ->
    
    1569
    +              -- Module name doesn't match the file path name.
    
    1570
    +              -- We fall through to @new_summary@, where this will be
    
    1571
    +              -- discovered and the correct error message will be thrown.
    
    1572
    +              Nothing
    
    1573
    +            _ -> Just res
    
    1574
    +    case cached of
    
    1559 1575
           Just (Right (chd_summary, SummFresh)) ->
    
    1560 1576
             -- Fresh! just return it
    
    1561 1577
             pure $ FoundHome (ModuleNodeCompile chd_summary)
    
    ... ... @@ -1858,6 +1874,15 @@ twice).
    1858 1874
        Note that (2) can't guarantee this alone: Two ModuleName imports in
    
    1859 1875
        separate units can (and likely do) map to the same `Module`.
    
    1860 1876
     
    
    1877
    +   Note:
    
    1878
    +   There is a special case where the module name doesn't have to match the file name.
    
    1879
    +   For example, the `Main` module is sometimes not defined in a file
    
    1880
    +   named `Main.hs`. We still want to compile such file targets, but reject the module
    
    1881
    +   if it is used as a module target.
    
    1882
    +   Thus, we accept this difference while `summariseFile`, but reject if we encounter expand
    
    1883
    +   this module node during `summariseModuleWithSource`.
    
    1884
    +   See tests T27461a and T27461b.
    
    1885
    +
    
    1861 1886
     See also Note [Downsweep: building and maintaining the module graph] and
    
    1862 1887
     Note [The ModuleGraph].
    
    1863 1888
     -}

  • testsuite/tests/driver/T27461/Main1.hs
    1
    +module Main where
    
    2
    +
    
    3
    +import Bar () -- resolves to src/Bar.hs, which declares module Foo
    
    4
    +
    
    5
    +main :: IO ()
    
    6
    +main = return ()

  • testsuite/tests/driver/T27461/Main2.hs
    1
    +module Main where
    
    2
    +
    
    3
    +main :: IO ()
    
    4
    +main = return ()

  • testsuite/tests/driver/T27461/Makefile
    1
    +TOP=../../..
    
    2
    +include $(TOP)/mk/boilerplate.mk
    
    3
    +include $(TOP)/mk/test.mk
    
    4
    +
    
    5
    +# src/Bar.hs declares module Foo, which is fine for a file target, but Main's
    
    6
    +# `import Bar` resolves to that same file and must be rejected.
    
    7
    +T27461a :
    
    8
    +	cp Main1.hs src/Main.hs
    
    9
    +	! '$(TEST_HC)' $(TEST_HC_OPTS) --make -fno-code -v0 -isrc src/Main.hs src/Bar.hs

  • testsuite/tests/driver/T27461/T27461a.stderr
    1
    +src/Bar.hs:1:8: error: [GHC-28623]
    
    2
    +    File name does not match module name:
    
    3
    +    Saw     : ‘Foo’
    
    4
    +    Expected: ‘Bar’

  • testsuite/tests/driver/T27461/T27461b.script
    1
    +"-- Successfully load modules if file target is not imported"
    
    2
    +:! cp Main2.hs src/Main.hs
    
    3
    +:load src/Main.hs src/Bar.hs
    
    4
    +main
    
    5
    +:! cp Main1.hs src/Main.hs
    
    6
    +"-- Crash on reload as we import a file target that has the wrong module name"
    
    7
    +:reload

  • testsuite/tests/driver/T27461/T27461b.stderr
    1
    +src/Bar.hs:1:8: error: [GHC-28623]
    
    2
    +    File name does not match module name:
    
    3
    +    Saw     : ‘Foo’
    
    4
    +    Expected: ‘Bar’
    
    5
    +

  • testsuite/tests/driver/T27461/T27461b.stdout
    1
    +"-- Successfully load modules if file target is not imported"
    
    2
    +"-- Crash on reload as we import a file target that has the wrong module name"

  • testsuite/tests/driver/T27461/all.T
    1
    +test('T27461a', extra_files(['src/', 'Main1.hs']), makefile_test, [])
    
    2
    +test('T27461b', [extra_files(['src/', 'Main1.hs', 'Main2.hs']), extra_hc_opts('-isrc')],
    
    3
    +     ghci_script, ['T27461b.script'])

  • testsuite/tests/driver/T27461/src/Bar.hs
    1
    +module Foo where
    
    2
    +-- Named Bar.hs but declares module Foo: allowed for a file target.
    
    3
    +
    
    4
    +foo :: Int
    
    5
    +foo = 1