E4202

E4202#

Compiler diagnostic name: update_struct_with_priv_field.

Struct update syntax copies every field from the original value before applying the explicit field changes. If the struct has private fields from another package, code outside that package cannot perform the copy directly.

Erroneous example#

The imported package defines a struct with a private field:

///|
pub(all) struct User {
  name : String
  priv id : Int
}

///|
pub let default_user : User = { name: "old", id: 1 }

The caller cannot use update syntax on that struct:

///|
fn rename(user : @lib.User) -> @lib.User {
  { ..user, name: "new" }
}

///|
test {
  ignore(rename(@lib.default_user))
}

Suggestion#

Move the update into the package that owns the private fields, or expose a helper that performs the update.

///|
pub(all) struct User {
  name : String
  priv id : Int
}

///|
pub let default_user : User = { name: "old", id: 1 }

///|
pub fn rename(user : User) -> User {
  { name: "new", id: user.id }
}
///|
test {
  inspect(@lib.rename(@lib.default_user).name, content="new")
}