Skip to content

Conversation

@bricknerb
Copy link
Contributor

@bricknerb bricknerb commented Nov 6, 2025

This proposal introduces an import Cpp; directive. This directive makes C++ built-in types, such as long, available in Carbon through a clear and explicit mechanism.

@bricknerb bricknerb added proposal A proposal proposal draft Proposal in draft, not ready for review labels Nov 6, 2025
@bricknerb bricknerb force-pushed the proposal-import-cpp branch from fc27fc7 to e5235a3 Compare November 6, 2025 08:58
…akes C++ built-in types, such as `long`, available in Carbon through a clear and explicit mechanism.
@bricknerb bricknerb changed the title Import Cpp; Add import Cpp; for C++ built-in entities Nov 6, 2025
@bricknerb bricknerb marked this pull request as ready for review November 6, 2025 11:15
@bricknerb bricknerb requested a review from a team as a code owner November 6, 2025 11:15
@bricknerb bricknerb requested review from chandlerc and removed request for a team November 6, 2025 11:15
@github-actions github-actions bot added proposal rfc Proposal with request-for-comment sent out and removed proposal draft Proposal in draft, not ready for review labels Nov 6, 2025
Comment on lines 105 to 106
in all files would be unexpected and would violate Carbon's design
principles.
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to be explicit about which design principles are relevant here. For example, it could be about adding overhead to compiles that don't need it which falls under "Fast and scalable development".

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!
I think the overhead depends on the implementation and we could avoid any significant impact.
I was actually thinking about "principle of least surprise". Clarified.

Comment on lines 104 to 106
2. **Implicit Activation:** Rejected. Automatically making `Cpp.long` available
in all files would be unexpected and would violate Carbon's design
principles.
Copy link
Contributor

Choose a reason for hiding this comment

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

I wonder if there has been much discussion about this option?

These seem awfully like what we would want in a "prelude" like construct for C++. We already have a prelude that brings in names in Core, and the Cpp package name is going to be similarly special (or more special) to Core due to it enabling the C++ importing. For example, it isn't reasonable to define a Carbon file with the package name Cpp.

So maybe that argues that this should be part of Carbon's prelude, and that prelude should introduce names both in Core and Cpp?

(If this is controversial or we want a longer discussion, maybe this should be factored into a leads issue.)

Copy link
Contributor

@zygoloid zygoloid Nov 7, 2025

Choose a reason for hiding this comment

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

I think I'd be happy with this if we make Cpp a keyword (see https://docs.carbon-lang.dev/docs/project/principles/namespace_cleanliness.html#exceptions). Though in another leads issue we did just suggest adding names Core.Cpp.Long32 etc, which would presumably add another special case if Cpp is a keyword.

Notably we made Cpp a keyword in the toolchain already because it simplifies the implementation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FTR, the suggestion to add Core.Cpp.Long32 is here.

I think Cpp is currently only special when used as a top namespace, and it can be used by users elsewhere.
I guess if it's an official keyword, we should not allow using it elsewhere.

I think introducing Cpp as part of Carbon language by default means we don't just support great interop with C++, we incorporate C++ names as part of Carbon.
Future users of pure Carbon might find having Cpp.long in pure Carbon a bit surprising and legacy of C++.

I agree Core is somewhat similar, but Core is probably necessary for almost any Carbon based project, and Cpp is not necessary for future pure Carbon projects.

I think we could make a decision between the proposed solution here and alternative 2. If we decided on alternative 2, I'll be happy to create a new proposal for that and close this one. I can also create a leads question, but I feel like this would just split the discussion unnecessarily.

Copy link
Contributor

Choose a reason for hiding this comment

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

Chatted a bit with @zygoloid and I think we're both increasingly comfortable with this alternative, and making Cpp a keyword and refer to the special Cpp package.

That will force us to use a different namespace inside of Core for types like Long32, but that's probably good -- these aren't ways of naming the C++ name, but the Carbon type that is used for compatibility with a specific C++ name. Basic suggestion for #6275 names is Core.CppCompat.*.

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!
To clarify, we want to not support import Cpp; and instead, when there's no import Cpp ... (library or inline), and the Cpp keyword is used (e.g. Cpp.long or even Cpp.MyType), we will trigger AST creation so we can query it.
In other words, Cpp and Cpp.<builtin_type> are always found, regardless of whether we explicitly imported Cpp.
Correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

proposal rfc Proposal with request-for-comment sent out proposal A proposal

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants