Rust: Error message shows type of private field

Created on 3 Jan 2019  路  3Comments  路  Source: rust-lang/rust

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)

A-diagnostics C-enhancement P-low T-compiler

Most helpful comment

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()`

All 3 comments

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()`

Hi @rust-lang/wg-diagnostics! Do any of you have opinions on this matter? I lean towards closing as "wontfix" because I feel the current behavior is acceptable.

Hi @rust-lang/wg-diagnostics! Do any of you have opinions on this matter? I lean towards closing as "wontfix" because I feel the current behavior is acceptable.

I agree, like you mentioned in a previous comment, there are some improvements we could make, but I think the current behaviour is fine.

Was this page helpful?
0 / 5 - 0 ratings