Skip to content

Note message after error recommends using inaccessible private fields. #27654

Closed
@Nashenas88

Description

@Nashenas88

The following code:

pub mod animal {
    pub struct Dog {
        pub age: usize,
        dog_age: usize,
    }

    impl Dog {
        pub fn new(age: usize) -> Dog {
            Dog { age: age, dog_age: age * 7 }
        }
    }
}

fn main() {
    let dog = animal::Dog::new(3);
    let dog_age = dog.dog_age();
    //let dog_age = dog.dog_age;
    println!("{}", dog_age);
}

Outputs the following message:

<anon>:16:23: 16:32 error: no method named `dog_age` found for type `animal::Dog` in the current scope
<anon>:16     let dog_age = dog.dog_age();
                                ^~~~~~~~~
<anon>:16:23: 16:32 note: did you mean to write `dog.dog_age`?
<anon>:16     let dog_age = dog.dog_age();
                                ^~~~~~~~~
error: aborting due to previous error

However, correcting the code by following the note message causes the following output:

<anon>:17:19: 17:30 error: field `dog_age` of struct `animal::Dog` is private
<anon>:17     let dog_age = dog.dog_age;
                            ^~~~~~~~~~~
error: aborting due to previous error

I would expect the private field to not be recommended.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions