Skip to content

Commit 3d566ad

Browse files
authored
Rollup merge of rust-lang#41062 - estebank:private-field, r=arielb1
Do not recommend private fields called as method ```rust error: no method named `dog_age` found for type `animal::Dog` in the current scope --> $DIR/private-field.rs:26:23 | 26 | let dog_age = dog.dog_age(); | ^^^^^^^ private field, not a method ``` Fix rust-lang#27654.
2 parents 8c29876 + 3409f8d commit 3d566ad

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

src/librustc_typeck/check/method/suggest.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -196,19 +196,23 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
196196

197197
let field_ty = field.ty(tcx, substs);
198198

199-
if self.is_fn_ty(&field_ty, span) {
200-
err.help(&format!("use `({0}.{1})(...)` if you \
201-
meant to call the function \
202-
stored in the `{1}` field",
203-
expr_string,
204-
item_name));
199+
if tcx.vis_is_accessible_from(field.vis, self.body_id) {
200+
if self.is_fn_ty(&field_ty, span) {
201+
err.help(&format!("use `({0}.{1})(...)` if you \
202+
meant to call the function \
203+
stored in the `{1}` field",
204+
expr_string,
205+
item_name));
206+
} else {
207+
err.help(&format!("did you mean to write `{0}.{1}` \
208+
instead of `{0}.{1}(...)`?",
209+
expr_string,
210+
item_name));
211+
}
212+
err.span_label(span, &"field, not a method");
205213
} else {
206-
err.help(&format!("did you mean to write `{0}.{1}` \
207-
instead of `{0}.{1}(...)`?",
208-
expr_string,
209-
item_name));
214+
err.span_label(span, &"private field, not a method");
210215
}
211-
err.span_label(span, &"field, not a method");
212216
break;
213217
}
214218
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
pub mod animal {
12+
pub struct Dog {
13+
pub age: usize,
14+
dog_age: usize,
15+
}
16+
17+
impl Dog {
18+
pub fn new(age: usize) -> Dog {
19+
Dog { age: age, dog_age: age * 7 }
20+
}
21+
}
22+
}
23+
24+
fn main() {
25+
let dog = animal::Dog::new(3);
26+
let dog_age = dog.dog_age();
27+
//let dog_age = dog.dog_age;
28+
println!("{}", dog_age);
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: no method named `dog_age` found for type `animal::Dog` in the current scope
2+
--> $DIR/private-field.rs:26:23
3+
|
4+
26 | let dog_age = dog.dog_age();
5+
| ^^^^^^^ private field, not a method
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)