Skip to content

Commit 843b174

Browse files
committed
typeck: if a private field exists, also check for a public method
For example, `Vec::len` is both a field and a method, and usually encountering `vec.len` just means that the parens were forgotten. Fixes: #26472
1 parent 22ac88f commit 843b174

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/librustc_typeck/check/method/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
8484
span: Span,
8585
method_name: ast::Name,
8686
self_ty: ty::Ty<'tcx>,
87-
call_expr_id: ast::NodeId)
87+
call_expr_id: ast::NodeId,
88+
allow_private: bool)
8889
-> bool
8990
{
9091
let mode = probe::Mode::MethodCall;
@@ -93,7 +94,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
9394
Err(NoMatch(..)) => false,
9495
Err(Ambiguity(..)) => true,
9596
Err(ClosureAmbiguity(..)) => true,
96-
Err(PrivateMatch(..)) => true,
97+
Err(PrivateMatch(..)) => allow_private,
9798
}
9899
}
99100

src/librustc_typeck/check/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -3045,12 +3045,18 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
30453045

30463046
if let Some((did, field_ty)) = private_candidate {
30473047
let struct_path = self.tcx().item_path_str(did);
3048-
let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
3049-
self.tcx().sess.span_err(expr.span, &msg);
30503048
self.write_ty(expr.id, field_ty);
3049+
let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
3050+
let mut err = self.tcx().sess.struct_span_err(expr.span, &msg);
3051+
// Also check if an accessible method exists, which is often what is meant.
3052+
if self.method_exists(field.span, field.node, expr_t, expr.id, false) {
3053+
err.note(&format!("a method `{}` also exists, perhaps you wish to call it",
3054+
field.node));
3055+
}
3056+
err.emit();
30513057
} else if field.node == keywords::Invalid.name() {
30523058
self.write_error(expr.id);
3053-
} else if self.method_exists(field.span, field.node, expr_t, expr.id) {
3059+
} else if self.method_exists(field.span, field.node, expr_t, expr.id, true) {
30543060
self.type_error_struct(field.span, |actual| {
30553061
format!("attempted to take value of method `{}` on type \
30563062
`{}`", field.node, actual)

src/test/compile-fail/issue-26472.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2016 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+
mod sub {
12+
pub struct S { len: usize }
13+
impl S {
14+
pub fn new() -> S { S { len: 0 } }
15+
pub fn len(&self) -> usize { self.len }
16+
}
17+
}
18+
19+
fn main() {
20+
let s = sub::S::new();
21+
let v = s.len;
22+
//~^ ERROR field `len` of struct `sub::S` is private
23+
//~| NOTE a method `len` also exists, perhaps you wish to call it
24+
}

0 commit comments

Comments
 (0)