Skip to content

Commit 7b837e0

Browse files
committed
Don't suggest adding parentheses to call an inaccessible method.
Previously, the test code would emit E0615, thus revealing the existence of private methods that the programmer probably does not care about. Now it ignores their existence instead, producing error E0609 (no field). The motivating example is: ```rust let x = std::rc::Rc::new(()); x.inner; ``` which would previously mention the private method `Rc::inner()`, even though `Rc<T>` intentionally has no public methods so that it can be a transparent smart pointer for any `T`.
1 parent bb90f81 commit 7b837e0

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2314,7 +2314,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
23142314
field,
23152315
base_ty,
23162316
expr.hir_id,
2317-
true,
2317+
false, // don't mention or suggest non-accessible methods
23182318
expected.only_has_type(self),
23192319
) {
23202320
self.ban_take_value_of_method(expr, base_ty, field)
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// This error is an E0609 and *not* an E0615 because the fact that the method exists is not
2+
// relevant.
3+
mod foo {
4+
pub struct Foo {
5+
x: u32,
6+
}
7+
8+
impl Foo {
9+
fn method(&self) {}
10+
}
11+
}
12+
13+
fn main() {
14+
let f = foo::Foo { x: 0 };
15+
f.method; //~ ERROR E0609
16+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0609]: no field `method` on type `Foo`
2+
--> $DIR/E0609-private-method.rs:15:7
3+
|
4+
LL | f.method;
5+
| ^^^^^^ unknown field
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0609`.

0 commit comments

Comments
 (0)