Skip to content

Commit 7ffe7da

Browse files
authored
Clarify @main error message for invalid return type (#58984)
When the `@main` function returns a value that is not nothing or convertible to `Cint`, the current error message may be a bit unclear or unhelpful. For example, running the script below results in an error message that doesn’t clearly point to the underlying cause: ```julia #!/usr/bin/env julia # Lengthy, potentially flawed code function @main(args) # Lengthy, potentially flawed code [] # Valid in regular functions, but not in `@main` end ``` In this case, returning an empty array (`[]`) is perfectly fine in a normal function, but it’s not allowed in a `@main` context. The current error message doesn’t clearly emphasize this, which can make it tricky for users to understand and resolve the issue: ```julia ERROR: MethodError: no method matching Int32(::Vector{Any}) The type `Int32` exists, but no method is defined for this combination of argument types when trying to construct it. Closest candidates are: Int32(::Float64) @ Base float.jl:895 Int32(::Float32) @ Base float.jl:919 Int32(::Float16) @ Base float.jl:919 ... Stacktrace: [1] _start() @ Base ./client.jl:568 ```
1 parent 8190fe8 commit 7ffe7da

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

base/client.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,7 +574,12 @@ function _start()
574574
ret = repl_main(ARGS)
575575
end
576576
ret === nothing && (ret = 0)
577-
ret = Cint(ret)
577+
ret = try
578+
Cint(ret)
579+
catch
580+
@error "The return value of `main` should be `nothing` or convertible to `Cint`"
581+
Cint(1)
582+
end
578583
catch
579584
ret = Cint(1)
580585
invokelatest(display_error, scrub_repl_backtrace(current_exceptions()))

0 commit comments

Comments
 (0)