Import compiler builtins

Is there a way to import builtins created in the compiler? I know they can be used in the standard library but, I cannot find a way to use them in a test program. For example, how could I get access to Builtin.cmp_eq_Int64 or even Builtin.Int64 for that matter?

Theres no way to access these unless you’re the standard library. You really shouldn’t need to use these at all in user facing code which is why they aren’t allowed.

The reason I want to access them is to tests builtins I have created. Maybe just writing a test would be better, though.

If you’re testing a new Builtin, you can use the -parse-stdlib flag to allow usage of Builtins.

1 Like

Great. That is very helpful. Thank you!

@Alejandro unfortunately that did not work for me. I still get this error:

error: use of unresolved identifier 'Builtin'

I am invoking the program with:

$ build/Ninja-DebugAssert/swift-macosx-x86_64/bin/swift test/test.swift -parse-stdlib

Am I missing something? Thanks again for the help.

It works when I use swiftc, maybe try that?

Aha worked like a charm. Thanks!

1 Like

I believe it's a frontend option, so -Xfrontend -parse-stdlib should also work

When you run swift myscript.swift, arguments after the filename become arguments to the script, available in CommandLine.arguments, rather than arguments to Swift.

(Ask Me How I Know™.)

The other gotcha you'll probably run into: If you use -parse-stdlib but you still want access to the real standard library, you'll need to import Swift.

1 Like

Thank you for pointing this out, I always forget that's the case! I filed SR-11304 to see if we can improve the UX in this area. It's difficult to guess whether an argument was intended for swift or the program being run in some cases, but for something like swift script.swift -parse-stdlib I think an "are you sure you didn't mean to pass this to swift" warning would be acceptable.

What if you want to pass -parse-stdlib to your script? In that case it will garble your output...

Terms of Service

Privacy Policy

Cookie Policy