Skip to content

Commit fcebf52

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 855fb61 commit fcebf52

File tree

3 files changed

+37
-5
lines changed

3 files changed

+37
-5
lines changed

src/librustc_typeck/check/method/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ pub fn exists<'a, 'tcx>(fcx: &FnCtxt<'a, '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 @@ pub fn exists<'a, 'tcx>(fcx: &FnCtxt<'a, '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

+10-3
Original file line numberDiff line numberDiff line change
@@ -2943,12 +2943,19 @@ fn check_expr_with_expectation_and_lvalue_pref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
29432943

29442944
if let Some((did, field_ty)) = private_candidate {
29452945
let struct_path = fcx.tcx().item_path_str(did);
2946-
let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
2947-
fcx.tcx().sess.span_err(expr.span, &msg);
29482946
fcx.write_ty(expr.id, field_ty);
2947+
let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
2948+
let mut err = fcx.tcx().sess.struct_span_err(expr.span, &msg);
2949+
// Also check if an accessible method exists, which is often what is meant.
2950+
if method::exists(fcx, field.span, field.node, expr_t, expr.id, false) {
2951+
err.fileline_note(field.span, &format!("a method called `{}` also exists, \
2952+
maybe a `()` to call it is missing?",
2953+
field.node));
2954+
}
2955+
err.emit();
29492956
} else if field.node == keywords::Invalid.name() {
29502957
fcx.write_error(expr.id);
2951-
} else if method::exists(fcx, field.span, field.node, expr_t, expr.id) {
2958+
} else if method::exists(fcx, field.span, field.node, expr_t, expr.id, true) {
29522959
fcx.type_error_struct(field.span,
29532960
|actual| {
29542961
format!("attempted to take value of method `{}` on type \

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 called `len` also exists, maybe a `()` to call it is missing
24+
}

0 commit comments

Comments
 (0)