Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect error messages #72327

Open
dhoepfl opened this issue Mar 14, 2024 · 0 comments
Open

Incorrect error messages #72327

dhoepfl opened this issue Mar 14, 2024 · 0 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels

Comments

@dhoepfl
Copy link
Contributor

dhoepfl commented Mar 14, 2024

Description

The code shown below shows not helpful and incorrect errors:

xxx.swift:1:1: error: 'main' attribute cannot be used in a module that contains top-level code
@main
^
xxx.swift:1:1: note: top-level code defined in this source file
@main
^

Either @main as top-level code should work or the error should give a hint how to fix this.

xxx.swift:1:1: note: pass '-parse-as-library' to compiler invocation if this is intentional
@main
^

This message should not be shown when using swift:

$ swift -parse-as-library xxx.swift
error: option '-parse-as-library' is not supported by 'swift'; did you mean to use 'swiftc'?

The error related to URL is correct. Replacing the body of func main with print("hello world") does not change the other errors.

Reproduction

Copy the following code (from Hacking With Swift) into a new file, e.g. xxx.swift:

@main
struct UserFetcher {
    static func main() async throws {
        let url = URL(string: "https://hws.dev/users.csv")!

        for try await line in url.lines {
            print("Received user: \(line)")
        }
    }
}

Try to run the code using swift xxx.swift results in the following errors:

xxx.swift:1:1: error: 'main' attribute cannot be used in a module that contains top-level code
@main
^
xxx.swift:1:1: note: top-level code defined in this source file
@main
^
xxx.swift:1:1: note: pass '-parse-as-library' to compiler invocation if this is intentional
@main
^
xxx.swift:4:19: error: cannot find 'URL' in scope
        let url = URL(string: "https://hws.dev/users.csv")!
                  ^~~

Expected behavior

  • The code should compile and run (after removing URL/adding the required import)
  • The error should be more helpful than saying “You cannot use @main if you use @main”.
  • If the code is correct in a different context, the error messages should say what to do in the current context. (e.g. “@main is only supported for code compiled by swiftc. For swift, use … instead”)
  • If an option that is not supported by the command, it should not suggest to use it.

Environment

$ swiftc -version
Swift version 5.9 (swift-5.9-RELEASE)
Target: x86_64-unknown-linux-gnu

Additional information

No response

@dhoepfl dhoepfl added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Mar 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

1 participant