Skip to content

"Fill struct fields" assist shouldn't be applicable if the struct has inaccesible fields #10618

@agluszak

Description

@agluszak

As @matklad mentioned in ERA08, rust-analyzer should not suggest to fill struct fields if they are inaccessible.

Example:

use std::rc::Rc;

struct Struct {
    field: String
}

type Data = Rc<Struct>;

fn main() {
    let data = Data {};
}

It fails to compile with the following error:

error: cannot construct `Rc<Struct>` with struct literal syntax due to inaccessible fields
  --> src/main.rs:10:16
   |
10 |     let data = Data {};
   |      

Currently rust-analyzer offers to fill the fields, which results in following code:

let data = Data { ptr: todo!(), phantom: todo!() };

Of course it fails to compile:

error[E0451]: field `ptr` of struct `Rc` is private
  --> src/main.rs:10:23
   |
10 |     let data = Data { ptr: todo!(), phantom: todo!() };
   |                       ^^^^^^^^^^^^ private field

error[E0451]: field `phantom` of struct `Rc` is private
  --> src/main.rs:10:37
   |
10 |     let data = Data { ptr: todo!(), phantom: todo!() };
   |       

I'd like to fix this issue :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-assistsC-bugCategory: bugS-actionableSomeone could pick this issue up and work on it right now

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions