Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

PSA: Renaming FIDL LLCPP mutable union accessors

3 views
Skip to first unread message

Zachary Stewart

unread,
Jan 22, 2022, 12:19:46 AM1/22/22
to anno...@fuchsia.dev

If you don’t write C++ code using FIDL low-level bindings, you can stop reading.

TL;DR

LLCPP generated code for unions will now use foo() instead of mutable_foo() for non-const access.

No Action Required

You do not need to update existing code. I will fix all existing code, however if you have a LLCPP CL in progress, you might need to manually fix code that uses the mutable_ union accessors.

Background

Currently, LLCPP generated unions contain different accessors for members:

// Setters:

// - For inline-envelope Unions:

void set_foo(FooType elem);

// - For non inline-envelope Unions

void set_foo(::fidl::ObjectView<FooType> elem);

template <typename... Args>

void set_foo(::fidl::AnyArena& allocator, Args&&... args);

// Getters:

FooType& mutable_foo();

const FooType& foo() const;


C++ allows overloading methods based on whether this is const, so using a different name for mutable_foo is not necessary and breaks developer expectations in some surprising ways:

FooUnion u = GetUnion();

if (u.is_foo()) {  // check if the variant holds foo

  if (u.foo().bar()) {  // still good

    zx::handle h = std::move(u.foo().bar().h);  // compile error! `foo` was const.

    zx::handle h = std::move(u.mutable_foo().bar().h);  // instead, the user has to reach for the "mutable_foo"

  }

}

After this change, mutable_foo will also be named foo, so the accessors will have these names:

// Setters:

// - For inline-envelope Unions:

void set_foo(FooType elem);

// - For non inline-envelope Unions

void set_foo(::fidl::ObjectView<FooType> elem);

template <typename... Args>

void set_foo(::fidl::AnyArena& allocator, Args&&... args);

// Getters:

FooType& foo();

const FooType& foo() const;

This is a multi-stage change, so both FooType& mutable_foo(); and FooType& foo(); will be defined during the migration.

Reply all
Reply to author
Forward
0 new messages