Skip to content

Cannot use ..Default::default() with structs having private fields despite implementing a custom Default trait #134167

@mostafarezaei

Description

@mostafarezaei

Code

#[derive(Clone)]
pub struct Session<T: InvokeUiSession> {
    pub server_clipboard_enabled: Arc<RwLock<bool>>,
    pub last_change_display: Arc<Mutex<ChangeDisplayRecord>>,
    pub connection_round_state: Arc<Mutex<ConnectionRoundState>>,
    #[cfg(target_os = "windows")]
    pub cursor_confinement: crate::cursor_confinement::CursorConfinement,
    #[cfg(target_os = "windows")]
    left_ctrl_pressed: bool,
    #[cfg(target_os = "windows")]
     left_alt_pressed: bool,
}

impl<T: InvokeUiSession + Default> Default for Session<T> {
    fn default() -> Self {
        Self {
            server_clipboard_enabled: Default::default(),
            last_change_display: Default::default(),
            connection_round_state: Default::default(),
            #[cfg(target_os = "windows")]
            cursor_confinement: Default::default(),
            #[cfg(target_os = "windows")]
            left_ctrl_pressed: false,
            #[cfg(target_os = "windows")]
            left_alt_pressed: false,
        }
    }
}


And in some other code in another module, I have the following piece of code:
        let session: Session<SciterHandler> = Session {
            server_clipboard_enabled: Arc::new(RwLock::new(true)),
            ..Default::default()
        };

Current output

error[E0451]: field `left_ctrl_pressed` of struct `ui_session_interface::Session` is privat
e
   --> src\ui\remote.rs:516:15
    |
516 |             ..Default::default()
    |               ^^^^^^^^^^^^^^^^^^ field `left_ctrl_pressed` is private

error[E0451]: field `left_alt_pressed` of struct `ui_session_interface::Session` is private
   --> src\ui\remote.rs:516:15
    |
516 |             ..Default::default()
    |               ^^^^^^^^^^^^^^^^^^ field `left_alt_pressed` is private

Desired output

As you see, I've implemented the Default trait myself to support private field, though the error has happen, but I expected to pass without any issue.

Rationale and extra context

No response

Other cases

Rust Version

rustc --version --verbose
rustc 1.77.2 (25ef9e3d8 2024-04-09)
binary: rustc
commit-hash: 25ef9e3d85d934b27d9dada2f9dd52b1dc63bb04
commit-date: 2024-04-09
host: x86_64-pc-windows-msvc
release: 1.77.2
LLVM version: 17.0.6

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsE-needs-investigationCall for partcipation: This issues needs some investigation to determine current statusT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions