Skip to content

Commit

Permalink
Rollup merge of rust-lang#33342 - birkenfeld:issue-26472, r=arielb1
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
steveklabnik committed May 5, 2016
2 parents 04097c6 + fcebf52 commit e94a722
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/librustc_typeck/check/method/mod.rs
Expand Up @@ -84,7 +84,8 @@ pub fn exists<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
span: Span,
method_name: ast::Name,
self_ty: ty::Ty<'tcx>,
call_expr_id: ast::NodeId)
call_expr_id: ast::NodeId,
allow_private: bool)
-> bool
{
let mode = probe::Mode::MethodCall;
Expand All @@ -93,7 +94,7 @@ pub fn exists<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
Err(NoMatch(..)) => false,
Err(Ambiguity(..)) => true,
Err(ClosureAmbiguity(..)) => true,
Err(PrivateMatch(..)) => true,
Err(PrivateMatch(..)) => allow_private,
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -2943,12 +2943,19 @@ fn check_expr_with_expectation_and_lvalue_pref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,

if let Some((did, field_ty)) = private_candidate {
let struct_path = fcx.tcx().item_path_str(did);
let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
fcx.tcx().sess.span_err(expr.span, &msg);
fcx.write_ty(expr.id, field_ty);
let msg = format!("field `{}` of struct `{}` is private", field.node, struct_path);
let mut err = fcx.tcx().sess.struct_span_err(expr.span, &msg);
// Also check if an accessible method exists, which is often what is meant.
if method::exists(fcx, field.span, field.node, expr_t, expr.id, false) {
err.fileline_note(field.span, &format!("a method called `{}` also exists, \
maybe a `()` to call it is missing?",
field.node));
}
err.emit();
} else if field.node == keywords::Invalid.name() {
fcx.write_error(expr.id);
} else if method::exists(fcx, field.span, field.node, expr_t, expr.id) {
} else if method::exists(fcx, field.span, field.node, expr_t, expr.id, true) {
fcx.type_error_struct(field.span,
|actual| {
format!("attempted to take value of method `{}` on type \
Expand Down
24 changes: 24 additions & 0 deletions src/test/compile-fail/issue-26472.rs
@@ -0,0 +1,24 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

mod sub {
pub struct S { len: usize }
impl S {
pub fn new() -> S { S { len: 0 } }
pub fn len(&self) -> usize { self.len }
}
}

fn main() {
let s = sub::S::new();
let v = s.len;
//~^ ERROR field `len` of struct `sub::S` is private
//~| NOTE a method called `len` also exists, maybe a `()` to call it is missing
}

0 comments on commit e94a722

Please sign in to comment.