Skip to content

Commit fb281e9

Browse files
authored
Add GHC-67120 main not defined (#555)
1 parent 423843b commit fb281e9

File tree

7 files changed

+75
-0
lines changed

7 files changed

+75
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module Lib where
2+
3+
factorial :: Int -> Int
4+
factorial n = product [1..n]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
factorial :: Int -> Int
2+
factorial n = product [1..n]
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: Missing module header
3+
---
4+
5+
In this example, the file `Lib.hs` is intended to contain a module called `Lib`
6+
which is not supposed to be the main module. However, the file is missing a
7+
module header, so GHC defaults to the module name `Main` and expects the `main`
8+
function. Adding an explicit module header with the name `Lib` solves this
9+
issue.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
main :: IO ()
2+
main = putStrLn "Hello, World!"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
notMain :: IO ()
2+
notMain = putStrLn "Hello, World!"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
title: main has wrong name
3+
---
4+
5+
In this example, the `notMain` value is the intended entry point of the program,
6+
but it is not called `main` so GHC does not find it. Renaming `notMain` to
7+
`main` solves the issue.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: Missing main
3+
summary: The IO action ‘main’ is not defined in module ‘Main’.
4+
introduced: 9.8.1
5+
severity: error
6+
---
7+
8+
GHC expects the `Main` module to define a function called `main` which it can
9+
use as the entry point of your program.
10+
11+
This error can also occur in unnamed modules, because GHC will default to the
12+
module name `Main` for such modules.
13+
14+
If you just want GHC to produce an object file without an entry point, then you
15+
can give your module a name other than `Main` by putting a module header at the
16+
top of your file (below language pragmas and compiler options), for example as
17+
follows:
18+
19+
```
20+
module Foo where
21+
```
22+
23+
The conventions around `main` are defined in the second paragraph of Chapter 5
24+
of [The Haskell 2010 Report](https://www.haskell.org/onlinereport/haskell2010/haskellch5.html#x11-980005),
25+
26+
> A Haskell program is a collection of modules, one of which, by convention,
27+
> must be called `Main` and must export the value `main`. The value of the
28+
> program is the value of the identifier `main` in module `Main`, which must be
29+
> a computation of type `IO t` for some type `t` (see Chapter 7). When the
30+
> program is executed, the computation `main` is performed, and its result (of
31+
> type `t`) is discarded.
32+
33+
## Example error text
34+
35+
```
36+
example1/before/Lib.hs:1:1: error: [GHC-67120]
37+
The IO action ‘main’ is not defined in module ‘Main’
38+
|
39+
1 | factorial :: Int -> Int
40+
| ^
41+
```
42+
43+
```
44+
example2/before/Main.hs:1:1: error: [GHC-67120]
45+
The IO action ‘main’ is not defined in module ‘Main’
46+
|
47+
1 | notMain :: IO ()
48+
| ^
49+
```

0 commit comments

Comments
 (0)