Skip to content

Commit 216f3f4

Browse files
committed
Rollup merge of rust-lang#33342 - birkenfeld:issue-26472, r=jseyfried
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: rust-lang#26472 NOTE: I added the parameter `allow_private` to `method::exists` since I don't want to suggest inaccessible methods. For the second case, where only the method exists, I think it would make sense to set it to `false` as well, but I wanted to preserve compatibility for this case.
2 parents f9966df + 84034d4 commit 216f3f4

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
@@ -83,7 +83,8 @@ pub fn exists<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
8383
span: Span,
8484
method_name: ast::Name,
8585
self_ty: ty::Ty<'tcx>,
86-
call_expr_id: ast::NodeId)
86+
call_expr_id: ast::NodeId,
87+
allow_private: bool)
8788
-> bool
8889
{
8990
let mode = probe::Mode::MethodCall;
@@ -92,7 +93,7 @@ pub fn exists<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
9293
Err(NoMatch(..)) => false,
9394
Err(Ambiguity(..)) => true,
9495
Err(ClosureAmbiguity(..)) => true,
95-
Err(PrivateMatch(..)) => true,
96+
Err(PrivateMatch(..)) => allow_private,
9697
}
9798
}
9899

src/librustc_typeck/check/mod.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -2947,12 +2947,18 @@ fn check_expr_with_expectation_and_lvalue_pref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
29472947

29482948
if let Some((did, field_ty)) = private_candidate {
29492949
let struct_path = fcx.tcx().item_path_str(did);
2950-
let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
2951-
fcx.tcx().sess.span_err(expr.span, &msg);
29522950
fcx.write_ty(expr.id, field_ty);
2951+
let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
2952+
let mut err = fcx.tcx().sess.struct_span_err(expr.span, &msg);
2953+
// Also check if an accessible method exists, which is often what is meant.
2954+
if method::exists(fcx, field.span, field.node, expr_t, expr.id, false) {
2955+
err.note(&format!("a method `{}` also exists, \
2956+
perhaps you wish to call it", field.node));
2957+
}
2958+
err.emit();
29532959
} else if field.node == keywords::Invalid.name() {
29542960
fcx.write_error(expr.id);
2955-
} else if method::exists(fcx, field.span, field.node, expr_t, expr.id) {
2961+
} else if method::exists(fcx, field.span, field.node, expr_t, expr.id, true) {
29562962
fcx.type_error_struct(field.span,
29572963
|actual| {
29582964
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 `len` also exists, perhaps you wish to call it
24+
}

0 commit comments

Comments
 (0)