# How to mark a trait as applying to NOT register-passable?

**URL:** <https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265>\
**Category:** General\
**Created:** [September 13, 2025, 3:10pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265 "2025-09-13T15:10:20Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mseritan](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/mseritan/32/130_2.png) [@mseritan](https://forum.modular.com/u/mseritan)\
**Post date:** [September 13, 2025, 3:10pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/1 "2025-09-13T15:10:20Z")

</div>

I have been working for many weekends and evenings on FireBolt, an Mojo implementation of the Apache Arrow format started by Krisztián Szűcs. With recent Mojo releases it has come time to cleanup our memory story. My current thinking on the ownership structure is [here](https://github.com/winding-lines/firebolt/blob/mojo-25.6.0.dev2025091105/firebolt/MEMORY.md).

When defining a trait [code](https://github.com/winding-lines/firebolt/blob/mojo-25.6.0.dev2025091105/firebolt/arrays/base.mojo#L11)

```auto
trait Array(Movable, Representable, Sized, Stringable, Writable):

    fn as_data(var self) -> ref [self] ArrayData:
        """Return a read only reference to the ArrayData wrapped by self."""
        pass

```

the compiler fails with

```auto
firebolt/arrays/base.mojo|10 col 40 error| cannot return 'self's origin, because it might expand to a @register_passable type
|| fn as_data(var self) -> ref [self] ArrayData:

```

I do not intent to use this trait with `@registe_passable` types, how do I communicate this to the compiler?

Many thanks,

Marius

---

<div class="post-metadata">

**Author:** ![mseritan](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/mseritan/32/130_2.png) [@mseritan](https://forum.modular.com/u/mseritan)\
**Post date:** [September 13, 2025, 4:22pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/2 "2025-09-13T16:22:53Z")

</div>

There is also a separate error once I start to implement the trait. I am not sure if it’s possible to indicate I need any origin derived from `__origin_of(self)`?

```auto
firebolt/arrays/nested.mojo|68 col 20 error| cannot return reference with incompatible origin: 'self.data' vs 'self'
|| return self.data

```

---

<div class="post-metadata">

**Author:** ![Samuel](https://avatars.discourse-cdn.com/v4/letter/s/34f0e0/32.png) [@Samuel](https://forum.modular.com/u/Samuel)\
**Post date:** [September 13, 2025, 5:07pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/3 "2025-09-13T17:07:06Z")

</div>

Hi my friend, to solve the first issue, you can use fn func\_name(ref self, …) …: . Ref works with both register passable types and memory types. If you want a read only or immutable origin, you will need fn func\_name[self\_origin: ImmutableOrigin](ref [self\_origin] self, …) …:

The second issue is just your origin signature. Each field within your structure has its own origin. So your return type should be ref [self.data] instead of ref[self].

Happy to know you are working on the Apache arrow format for mojo. I was going to do the same but I’ll see if I can contribute to your repo.

---

<div class="post-metadata">

**Author:** ![mseritan](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/mseritan/32/130_2.png) [@mseritan](https://forum.modular.com/u/mseritan)\
**Post date:** [September 13, 2025, 6:35pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/4 "2025-09-13T18:35:12Z")

</div>

Many thanks Samuel!!!

The errors have changed now. With

```auto
    fn as_data[
        self_origin: ImmutableOrigin
    ](ref [self_origin]self) -> ref [self_origin.data] ArrayData:
        return self.data

```

I get the error

```auto
firebolt/arrays/primitive.mojo|116 col 49 error| cannot infer origin for a function result
|| ](ref [self_origin]self) -> ref [self_origin.data] ArrayData:

```

With the variant

```auto
    fn as_data[
        self_origin: ImmutableOrigin
    ](ref [self_origin]self) -> ref [self_origin] ArrayData:
        return self.data

```

I get the error

```auto
firebolt/arrays/primitive.mojo|117 col 20 error| cannot return reference with incompatible origin: 'self_origin._mlir_origin.data' vs 'self_origin._mlir_origin'
|| return self.data

```

These errors make sense to me, I just don’t know how to express what I need. Each of these objects passed in a `self` contain a `data` as an `ArrayData` and I want to extract it and return.

Thoughts?

---

<div class="post-metadata">

**Author:** ![clattner](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/clattner/32/201_2.png) [@clattner](https://forum.modular.com/u/clattner)\
**Post date:** [September 13, 2025, 6:51pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/5 "2025-09-13T18:51:30Z")

</div>

Samuel is right, using `ref self` is a good workaround for this.

Unfortunately, the origin system doesn’t have a good way to decouple origin references from variable accesses, and don’t support the ability to erase parts of the access pattern, which is what you need in a trait. This is something we need to build out, but isn’t in the short-term plan due to other priorities.

Your as\_data example seems like it should work though, could you post a small self-contained example that I can try out?

---

<div class="post-metadata">

**Author:** ![mseritan](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/mseritan/32/130_2.png) [@mseritan](https://forum.modular.com/u/mseritan)\
**Post date:** [September 13, 2025, 7:17pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/6 "2025-09-13T19:17:26Z")

</div>

Thanks Chris, here is an example

```auto
struct Graphite:
    var diameter: Float32

trait Inner:
    fn as_core[
        self_origin: ImmutableOrigin
    ](ref [self_origin]self) -> ref [self_origin] Graphite:
        pass

struct Pencil(Inner):
    var core: Graphite

    fn as_core[
        self_origin: ImmutableOrigin
    ](ref [self_origin]self) -> ref [self_origin] Graphite:
        return self.core

```

Ran `mojo sample.mojo` with output

````auto
❯ mojo sample.mojo 
.../sample.mojo:18:20: error: cannot return reference with incompatible origin: 'self_origin._mlir_origin.core' vs 'self_origin._mlir_origin'
        return self.core

Version

```
❯ mojo --version
Mojo 0.25.6.0.dev2025091105 (07937a3a)
```

````

---

<div class="post-metadata">

**Author:** ![clattner](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/clattner/32/201_2.png) [@clattner](https://forum.modular.com/u/clattner)\
**Post date:** [September 14, 2025, 4:58am UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/7 "2025-09-14T04:58:56Z")

</div>

Ah, yes, that is precisely what we can’t do right now. ☹ We need a way to “erase” field information instead of keeping it precise. We don’t have syntax or an implementation that is confirmed, but that would allow you to write something like:

```auto
trait Inner:
    fn as_core[
        o: ImmutableOrigin
    ](ref [o]self) -> ref [o*] Graphite:
        pass

```

^ saying that it is anything contained in `self_origin`.

Then implement it with something more specific, e.g.:

```auto
struct Pencil(Inner):
    var core: Graphite

    fn as_core[
        o: ImmutableOrigin
    ](ref [o] self) -> ref [self.core] Graphite:
        return self.core

```

Unfortunately, we don’t have an ETA for this and it isn’t going to be in the near term.

---

<div class="post-metadata">

**Author:** ![mseritan](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/mseritan/32/130_2.png) [@mseritan](https://forum.modular.com/u/mseritan)\
**Post date:** [September 14, 2025, 1:00pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/8 "2025-09-14T13:00:03Z")

</div>

Thanks for the fast response Chris. For now I will use this workaround.

```auto
trait Inner:
    fn as_core[
        o: ImmutableOrigin
    ](ref [o]self) -> UnsafePointer[Graphite, mut=False]:
        pass

```

---

<div class="post-metadata">

**Author:** ![owenhilyard](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/owenhilyard/32/41_2.png) [@owenhilyard](https://forum.modular.com/u/owenhilyard)\
**Post date:** [September 14, 2025, 1:40pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/9 "2025-09-14T13:40:37Z")

</div>

Are you also considering a way to “upcast” origins (`[self.core]` → `[self]`) in case someone wants the “compile-time RW lock” behavior that Rust has to uphold some invariants?

---

<div class="post-metadata">

**Author:** ![mseritan](https://sea1.discourse-cdn.com/flex001/user_avatar/forum.modular.com/mseritan/32/130_2.png) [@mseritan](https://forum.modular.com/u/mseritan)\
**Post date:** [September 14, 2025, 4:55pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/10 "2025-09-14T16:55:32Z")

</div>

Samuel,

I pushed a variation on your suggestion to the [main repo](https://github.com/kszucs/firebolt)

I also have a [client repo](https://github.com/winding-lines/herringbone) where I play with the integration with python/parquet using some earlier PyCapsule work. There is a lot of work to do here, the direction I am focussing on is to complete enough of the API to make it useful. I am sure that the internals can be improved, specially as Mojo goes towards 1.0

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex001/uploads/modular/original/1X/2751e0fbdc595a99718b216730957e9db4448cfd.jpeg) [@system](https://forum.modular.com/u/system)\
**Post date:** [September 21, 2025, 4:55pm UTC](https://forum.modular.com/t/how-to-mark-a-trait-as-applying-to-not-register-passable/2265/11 "2025-09-21T16:55:45Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
