Skip to content

Incorrect suggestion of private struct members #92999

Description

@SadiinsoSnowfall

Lets consider the following code (playground) :

pub struct S {
   pub val: String
}

pub fn test(s: S) {
   dbg!(s.cap)
}

Here, we try to access a property cap on an object of type struct S.
Up to the 1.50 version, the error message was the following :

rror[E0609]: no field `cap` on type `S`
--> <source>:6:12
  |
6 |     dbg!(s.cap)
  |            ^^^ unknown field
  |
 = note: available fields are: `val`

However, since Rust 1.51 and onward, the diagnostic message changed to this:

rror[E0609]: no field `cap` on type `S`
--> <source>:6:12
  |
6 |     dbg!(s.cap)
  |            ^^^ unknown field
  |
 = note: available fields are: `val`
help: one of the expressions' fields has a field of the same name
  |
6 |     dbg!(s.val.vec.buf.cap)
  |            ^^^^^^^^^^^^

While there is indead a cap property on the RawVec struct, this can't be accessed like that, applying the compiler suggestion implies making the buf property of std::vec::Vec public, which is not possible.

The compiler should not look at non-pub properties when searching for properties with a matching name for diagnostic purposes.

Activity

  1. terrarier2111 commented on Jan 17, 2022

    @terrarier2111
    Contributor

    @rustbot claim

  2. pierwill commented on Jan 18, 2022

    @pierwill
    Contributor

    @rustbot label +A-diagnostics

  3. m-ou-se commented on Jan 22, 2022

    @m-ou-se
    Member

    Related issue: #93210

  4. added 2 commits that reference this issue on Jan 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsC-bugCategory: This is a bug.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions