Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add note if method is called on a function object #32053

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler-rt
2 changes: 1 addition & 1 deletion src/jemalloc
Submodule jemalloc updated 134 files
2 changes: 1 addition & 1 deletion src/liblibc
Submodule liblibc updated 43 files
+1 −2 .travis.yml
+24 −8 ci/run-travis.sh
+0 −204 ci/style.rs
+8 −14 libc-test/build.rs
+1 −1 src/lib.rs
+1 −1 src/macros.rs
+4 −4 src/unix/bsd/apple/b32.rs
+4 −4 src/unix/bsd/apple/b64.rs
+11 −0 src/unix/bsd/freebsdlike/dragonfly.rs
+0 −103 src/unix/bsd/freebsdlike/dragonfly/mod.rs
+13 −0 src/unix/bsd/freebsdlike/freebsd.rs
+0 −91 src/unix/bsd/freebsdlike/freebsd/mod.rs
+73 −12 src/unix/bsd/freebsdlike/mod.rs
+0 −0 src/unix/bsd/freebsdlike/x86.rs
+0 −0 src/unix/bsd/freebsdlike/x86_64.rs
+2 −17 src/unix/bsd/mod.rs
+0 −4 src/unix/bsd/openbsdlike/bitrig.rs
+6 −8 src/unix/bsd/openbsdlike/mod.rs
+0 −6 src/unix/bsd/openbsdlike/netbsd.rs
+0 −4 src/unix/bsd/openbsdlike/openbsd.rs
+20 −43 src/unix/mod.rs
+0 −2 src/unix/notbsd/android/b32.rs
+0 −2 src/unix/notbsd/android/b64.rs
+4 −4 src/unix/notbsd/android/mod.rs
+2 −8 src/unix/notbsd/linux/mips.rs
+33 −41 src/unix/notbsd/linux/mod.rs
+76 −84 src/unix/notbsd/linux/musl/b32/arm.rs
+76 −84 src/unix/notbsd/linux/musl/b32/asmjs.rs
+0 −2 src/unix/notbsd/linux/musl/b32/mips.rs
+3 −3 src/unix/notbsd/linux/musl/b32/mod.rs
+76 −85 src/unix/notbsd/linux/musl/b32/x86.rs
+97 −100 src/unix/notbsd/linux/musl/b64/mod.rs
+0 −2 src/unix/notbsd/linux/other/b32/arm.rs
+11 −11 src/unix/notbsd/linux/other/b32/mod.rs
+0 −2 src/unix/notbsd/linux/other/b32/powerpc.rs
+0 −28 src/unix/notbsd/linux/other/b32/x86.rs
+21 −23 src/unix/notbsd/linux/other/b64/aarch64.rs
+21 −23 src/unix/notbsd/linux/other/b64/powerpc64.rs
+28 −54 src/unix/notbsd/linux/other/b64/x86_64.rs
+38 −36 src/unix/notbsd/linux/other/mod.rs
+7 −21 src/unix/notbsd/mod.rs
+0 −1 src/unix/solaris/mod.rs
+2 −4 src/windows.rs
119 changes: 69 additions & 50 deletions src/librustc_typeck/check/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,56 @@ use middle::subst::Substs;
use middle::traits::{Obligation, SelectionContext};
use util::nodemap::{FnvHashSet};


use syntax::ast;
use syntax::codemap::Span;
use syntax::errors::DiagnosticBuilder;
use rustc_front::print::pprust;
use rustc_front::hir;
use rustc_front::hir::Expr_;

use std::cell;
use std::cmp::Ordering;

use super::{MethodError, NoMatchData, CandidateSource, impl_item, trait_item};
use super::probe::Mode;

fn is_fn_ty<'a, 'tcx>(ty: &Ty<'tcx>, fcx: &FnCtxt<'a, 'tcx>, span: Span) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hooray, DRY!

let cx = fcx.tcx();
println!("{:?}", ty);
match ty.sty {
// Not all of these (e.g. unsafe fns) implement FnOnce
// so we look for these beforehand
ty::TyClosure(..) | ty::TyFnDef(..) | ty::TyFnPtr(_) => true,
// If it's not a simple function, look for things which implement FnOnce
_ => {
if let Ok(fn_once_trait_did) =
cx.lang_items.require(FnOnceTraitLangItem) {
let infcx = fcx.infcx();
infcx.probe(|_| {
let fn_once_substs =
Substs::new_trait(vec![infcx.next_ty_var()],
Vec::new(),
ty);
let trait_ref =
ty::TraitRef::new(fn_once_trait_did,
cx.mk_substs(fn_once_substs));
let poly_trait_ref = trait_ref.to_poly_trait_ref();
let obligation = Obligation::misc(span,
fcx.body_id,
poly_trait_ref
.to_predicate());
let mut selcx = SelectionContext::new(infcx);

return selcx.evaluate_obligation(&obligation)
})
} else {
false
}
}
}
}

pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
span: Span,
rcvr_ty: Ty<'tcx>,
Expand Down Expand Up @@ -79,60 +117,41 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
// snippet
};

macro_rules! span_stored_function {
() => {
err.span_note(span,
&format!("use `({0}.{1})(...)` if you meant to call \
the function stored in the `{1}` field",
expr_string, item_name));
}
}
let field_ty = field.ty(cx, substs);

macro_rules! span_did_you_mean {
() => {
err.span_note(span, &format!("did you mean to write `{0}.{1}`?",
expr_string, item_name));
}
if is_fn_ty(&field_ty, &fcx, span) {
err.span_note(span,
&format!("use `({0}.{1})(...)` if you meant to call \
the function stored in the `{1}` field",
expr_string, item_name));
} else {
err.span_note(span, &format!("did you mean to write `{0}.{1}`?",
expr_string, item_name));
}
}
}

// Determine if the field can be used as a function in some way
let field_ty = field.ty(cx, substs);
if is_fn_ty(&rcvr_ty, &fcx, span) {
macro_rules! report_function {
($span:expr, $name:expr) => {
err.fileline_note(
$span,
&format!("{} is a function, perhaps you wish to call it",
$name));
}
}

match field_ty.sty {
// Not all of these (e.g. unsafe fns) implement FnOnce
// so we look for these beforehand
ty::TyClosure(..) | ty::TyFnDef(..) | ty::TyFnPtr(_) => {
span_stored_function!();
}
// If it's not a simple function, look for things which implement FnOnce
_ => {
if let Ok(fn_once_trait_did) =
cx.lang_items.require(FnOnceTraitLangItem) {
let infcx = fcx.infcx();
infcx.probe(|_| {
let fn_once_substs =
Substs::new_trait(vec![infcx.next_ty_var()],
Vec::new(),
field_ty);
let trait_ref =
ty::TraitRef::new(fn_once_trait_did,
cx.mk_substs(fn_once_substs));
let poly_trait_ref = trait_ref.to_poly_trait_ref();
let obligation = Obligation::misc(span,
fcx.body_id,
poly_trait_ref
.to_predicate());
let mut selcx = SelectionContext::new(infcx);

if selcx.evaluate_obligation(&obligation) {
span_stored_function!();
} else {
span_did_you_mean!();
}
});
} else {
span_did_you_mean!();
}
if let Some(expr) = rcvr_expr {
if let Ok (expr_string) = cx.sess.codemap().span_to_snippet(expr.span) {
report_function!(expr.span, expr_string);
err.span_suggestion(expr.span,
"try calling the base function:",
format!("{}()",
expr_string));
}
else if let Expr_::ExprPath(_, path) = expr.node.clone() {
if let Some(segment) = path.segments.last() {
report_function!(expr.span, segment.identifier.name);
}
}
}
Expand Down
35 changes: 35 additions & 0 deletions src/test/compile-fail/issue-29124.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2015 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.

struct ret;
struct obj;

impl obj {
fn func() -> ret {
ret
}
}

fn func() -> ret {
ret
}

fn main() {
obj::func.x();
//~^ ERROR no method named `x` found for type `fn() -> ret {obj::func}` in the current scope
//~^^ NOTE obj::func is a function, perhaps you wish to call it
//~^^^ HELP try calling the base function:
//~| SUGGESTION obj::func().x();
func.x();
//~^ ERROR no method named `x` found for type `fn() -> ret {func}` in the current scope
//~^^ NOTE func is a function, perhaps you wish to call it
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For some reason these pass without referencing the help note in the comments. The help note does show when this file is manually compiled though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compiletest only errors for unexpected errors and warnings, if it gets a help that wasn't expected it silently pushes ahead. It only errors for helps when there is an expected help which doesn't match the found help.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should add //~| SUGGESTION as well (see other instances of this in the tests)

//~^^^ HELP try calling the base function:
//~| SUGGESTION func().x();
}