Skip to content

Conversation

bricknerb
Copy link
Contributor

@bricknerb bricknerb commented Jul 3, 2025

We avoid using canonical type before knowing it's not a pointer because we need the nullability attribute.
No support for pointers to pointers, yet.

C++ Interop Demo:

// hello_world.h

auto hello_world_param(int* _Nonnull i) -> void;
auto hello_world_return() -> int* _Nonnull;
// hello_world.cpp

#include "hello_world.h"
#include <cstdio>

auto hello_world_param(int* _Nonnull i) -> void {
  printf("hello_world: %d\n", *i);
}

static int x = 5;
auto hello_world_return() -> int* _Nonnull { return &x; }
// main.carbon

library "Main";

import Core library "io";
import Cpp library "hello_world.h";

fn Run() -> i32 {
  var i: i32 = 10;
  Cpp.hello_world_param(&i);

  let p: i32* = Cpp.hello_world_return();
  Core.Print(*p);

  return 0;
}
$ clang -c hello_world.cpp
$ ./bazel-bin/toolchain/install/prefix_root/bin/carbon compile main.carbon
$ ./bazel-bin/toolchain/install/prefix_root/bin/carbon link hello_world.o main.o --output=demo
$ ./demo
hello_world: 10
5

Part of #5772.

…eturn values

We avoid using canonical type before knowing it's not a pointer because we need the nullability attribute.
No support for pointers to pointers, yet.

C++ Interop Demo:

```c++
// hello_world.h

auto hello_world_param(int* _Nonnull i) -> void;
auto hello_world_return() -> int* _Nonnull;
```

```c++
// hello_world.cpp

#include "hello_world.h"
#include <cstdio>

auto hello_world_param(int* _Nonnull i) -> void {
  printf("hello_world: %d\n", *i);
}

static int x = 5;
auto hello_world_return() -> int* _Nonnull { return &x; }
```

```carbon
// main.carbon

library "Main";

import Core library "io";
import Cpp library "hello_world.h";

fn Run() -> i32 {
  var i: i32 = 10;
  Cpp.hello_world_param(&i);

  let p: i32* = Cpp.hello_world_return();
  Core.Print(*p);

  return 0;
}
```

```shell
$ clang -c hello_world.cpp
$ ./bazel-bin/toolchain/install/prefix_root/bin/carbon compile main.carbon
$ ./bazel-bin/toolchain/install/prefix_root/bin/carbon link hello_world.o main.o --output=demo
$ ./demo
hello_world: 10
5
```

Part of carbon-language#5772.
@bricknerb bricknerb reopened this Jul 8, 2025
@bricknerb bricknerb marked this pull request as ready for review July 8, 2025 13:42
@github-actions github-actions bot requested a review from danakj July 8, 2025 13:42
.type_id = pointer_type_id};
}

// Maps a C++ type to a Carbon type.
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a comment about this type not being canonicalized before calling MapType and why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Comment on lines 1102 to 1108
// CHECK:STDOUT: constants {
// CHECK:STDOUT: %int_32: Core.IntLiteral = int_value 32 [concrete]
// CHECK:STDOUT: %i32: type = class_type @Int, @Int(%int_32) [concrete]
// CHECK:STDOUT: %ptr.235: type = ptr_type %i32 [concrete]
// CHECK:STDOUT: %foo.type: type = fn_type @foo [concrete]
// CHECK:STDOUT: %foo: %foo.type = struct_value () [concrete]
// CHECK:STDOUT: }
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the const lost?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I believe so.
Very nice catch!
I think we never create const types regardless of pointers, so I'm not sure whether we need to fix this issue first.
I've added a test for that, so it's explicit and we can follow up on that.
WDYT?

@bricknerb bricknerb requested a review from danakj July 9, 2025 18:07
Copy link
Contributor

@danakj danakj left a comment

Choose a reason for hiding this comment

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

LGTM

fn F() {
//@dump-sem-ir-begin
var s: const Cpp.S = G();
// CHECK:STDERR: fail_todo_import_const_pointer_param.carbon:[[@LINE+5]]:11: error: name `Core.ImplicitAs` implicitly referenced here, but not found [CoreNameNotFound]
Copy link
Contributor

Choose a reason for hiding this comment

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

This error means we should switch the min prelude from destroy.carbon to convert.carbon so we can get the actual conversion error instead of a prelude error

Copy link
Contributor Author

@bricknerb bricknerb Jul 10, 2025

Choose a reason for hiding this comment

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

Done.
FWIW, I did this while debugging this but I assumed that since we should eventually not have an error we shouldn't change includes.
However, I guess we would like to test more use cases here anyways, which might require conversions.


struct S {};

auto foo(const S* _Nonnull) -> void;
Copy link
Contributor

Choose a reason for hiding this comment

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

we could have this return const S* as well, and then try to store it in a const S* in Carbon, to test more things.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we want to test multiple use cases I prefer to have separate tests if possible.
Added a test for returning a const pointer.

@bricknerb bricknerb enabled auto-merge July 10, 2025 07:50
@bricknerb
Copy link
Contributor Author

Created #5788 to add const support.

@bricknerb bricknerb added this pull request to the merge queue Jul 10, 2025
Merged via the queue into carbon-language:trunk with commit a5ddc3e Jul 10, 2025
9 checks passed
@bricknerb bricknerb deleted the pointer branch July 10, 2025 08:12
github-merge-queue bot pushed a commit that referenced this pull request Jul 30, 2025
…king the return type pointer `_Nonnull` (#5878)

Followup of #5773.
Part of #5772.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants