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

Error message shows type of private field #57320

Open
killercup opened this Issue Jan 3, 2019 · 1 comment

Comments

Projects
None yet
2 participants
@killercup
Copy link
Member

killercup commented Jan 3, 2019

The code below yields two errors:

  1. The field is private
  2. Type error in if expression because type is <type of private field> instead of bool

This means a user can observe the type of a private field of a dependency in their error messages. It doesn't mean they can actually use that information, but it seems weird at least.

Is this actually an issue? If you don't consider it one, feel free to close this.


fn main() {
    let x = foo::Foo::default();
    if x.len {
        println!("foo");
    }
}

mod foo {
    #[derive(Default)]
    pub struct Foo {
        len: String,
    }
    
    impl Foo {
        pub fn len(&self) -> usize {
            42
        }
    }
}

playground

(originally seen on reddit)

@estebank

This comment has been minimized.

Copy link
Contributor

estebank commented Jan 3, 2019

I won't close the ticket as this deserves a conversation to be had, but I personally don't consider it to
be a big problem.


Parsing x.len as x.len() when we suggest that would be nice, but in this case it would lead to confusing errors if we don't annotate the compiler state with something along the lines of

error[E0616]: field `len` of struct `foo::Foo` is private
 --> src/main.rs:3:8
  |
3 |     if x.len {
  |        ^^^^^ help: a method `len` also exists, perhaps you wish to call it: `x.len()`

error[E0308]: mismatched types
 --> src/main.rs:3:8
  |
3 |     if x.len {
  |        ^^^^^ expected bool, found `usize`
  |
  = note: expected type `bool`
             found type `usize`
  = note: found `usize` assuming that `x.len` has been rewritten to `x.len()`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment