Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mostafarezaei opened this issue Dec 11, 2024 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-needs-investigation Call for partcipation: This issues needs some investigation to determine current status T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@mostafarezaei
Copy link

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

@mostafarezaei mostafarezaei added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 11, 2024
@hkBst
Copy link
Contributor

hkBst commented Dec 11, 2024

Does SciterHandler have a Default impl?

@mostafarezaei
Copy link
Author

mostafarezaei commented Dec 11, 2024

Definitely, yes.

The ..Default::default() initialization worked correctly until I introduced private fields to the struct, even though I implemented a custom Default trait, but the mentioned error happen.

Thanks for your attention!

@jieyouxu jieyouxu added the E-needs-investigation Call for partcipation: This issues needs some investigation to determine current status label Dec 12, 2024
@bjorn3
Copy link
Member

bjorn3 commented Dec 12, 2024

This is expected. Foo { a: 0, b: 1, ..bar } desugars to Foo { a: 0, b:1, c: bar.c, d: bar.d } (assuming Foo has the fields a, b, c and d, which requires all fields to be public. It would need to desugar to { let mut tmp = bar; tmp.a = 0; tmp.b = 1; tmp } to allow private fields. You may want to read the discussion on the RFC for #[non_exhaustive]: rust-lang/rfcs#2008 Duplicate of #130891.

Edit: This issue was about regular private fields, not #[non_exhaustive]. Still same rationale for this behavior.

@bjorn3 bjorn3 closed this as completed Dec 12, 2024
@bjorn3 bjorn3 closed this as not planned Won't fix, can't repro, duplicate, stale Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints E-needs-investigation Call for partcipation: This issues needs some investigation to determine current status T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants