Skip to content

Conversation

huangyxi
Copy link
Contributor

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:

#!/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:

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

@huangyxi
Copy link
Contributor Author

This PR would fix #56328.

@LilithHafner
Copy link
Member

Thanks for the PR! I like the change to the error message. Why move it into an else block?

@huangyxi
Copy link
Contributor Author

Thanks for your response.

It's fine to keep it as before. The reason I moved the new try-catch into the else block is that I want the original try-catch to handle only the code that is prone to failure.

@LilithHafner
Copy link
Member

I recommend nesting the inner try-catch in the try block of the outer try-catch instead of nesting the inner try-catch in the else block of the outer try-catch. Either way there are nested try-catchs.

@LilithHafner LilithHafner merged commit 7ffe7da into JuliaLang:master Aug 19, 2025
5 of 7 checks passed
@LilithHafner
Copy link
Member

Thanks, @huangyxi

Cint(ret)
catch
@error "The return value of `main` should be `nothing` or convertible to `Cint`"
Cint(1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line is unreachable?

Suggested change
Cint(1)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your comment. Theoretically, @error only prints a message to stderr without throwing an error, so it does not skip the subsequent statements. In practice, changing Cint(1) to Cint(2) makes the program return 2 instead of 1, which proves that Cint(1) is indeed reachable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants