Skip to content

E0609's "one of the expressions' fields has a field of the same name" suggestion does not consider privacy #84443

Closed
@ash2x3zb9cy

Description

@ash2x3zb9cy

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=d65d484826fe6e0d2d490ee1fa63052e
Given the following code:

mod lib {
    #[derive(Default)]
    pub struct Xyz { x: f32, y: f32, z: f32 }
    #[derive(Default)]
    pub struct Vec3(Xyz);
}

fn main() {
    let a: lib::Vec3 = Default::default();
    println!("{}", a.x);
}

The current output is:

   Compiling playground v0.0.1 (/playground)
error[E0609]: no field `x` on type `Vec3`
  --> src/main.rs:10:22
   |
10 |     println!("{}", a.x);
   |                      ^
   |
help: one of the expressions' fields has a field of the same name
   |
10 |     println!("{}", a.0.x);
   |                      ^^
help: a field with a similar name exists
   |
10 |     println!("{}", a.0);
   |                      ^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0609`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

The suggested fixes, a.0 and a.0.x both refer to a private field, so they do not compile..

Given the following code, having implemented the suggested change:

mod lib {
    #[derive(Default)]
    pub struct Xyz { x: f32, y: f32, z: f32 }
    #[derive(Default)]
    pub struct Vec3(Xyz);
}

fn main() {
    let a: lib::Vec3 = Default::default();
    println!("{}", a.0.x);
}

The output is, correctly:

   Compiling playground v0.0.1 (/playground)
error[E0616]: field `0` of struct `Vec3` is private
  --> src/main.rs:10:22
   |
10 |     println!("{}", a.0.x);
   |                      ^ private field

error[E0616]: field `x` of struct `Xyz` is private
  --> src/main.rs:10:24
   |
10 |     println!("{}", a.0.x);
   |                        ^ private field

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0616`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-incorrectDiagnostics: A diagnostic that is giving misleading or incorrect information.T-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