From da9c43a723a8bfe920483a550666f90de64830d5 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Sun, 3 Apr 2016 22:58:44 +0200 Subject: [PATCH 1/3] Autoderef when suggesting to call `(self.field)` Fixes #32128 --- src/librustc_typeck/check/method/suggest.rs | 52 ++++++++++++++------- src/test/compile-fail/issue-32128.rs | 25 ++++++++++ 2 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 src/test/compile-fail/issue-32128.rs diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 4d806bda484..5b46880d5cf 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -14,7 +14,7 @@ use CrateCtxt; use astconv::AstConv; -use check::{self, FnCtxt}; +use check::{self, FnCtxt, UnresolvedTypeAction, autoderef}; use front::map as hir_map; use rustc::ty::{self, Ty, ToPolyTraitRef, ToPredicate, TypeFoldable}; use middle::cstore::{self, CrateStore}; @@ -22,6 +22,7 @@ use middle::def::Def; use middle::def_id::DefId; use middle::lang_items::FnOnceTraitLangItem; use rustc::ty::subst::Substs; +use rustc::ty::LvaluePreference; use rustc::traits::{Obligation, SelectionContext}; use util::nodemap::{FnvHashSet}; @@ -50,23 +51,40 @@ fn is_fn_ty<'a, 'tcx>(ty: &Ty<'tcx>, fcx: &FnCtxt<'a, 'tcx>, span: Span) -> bool 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); + let (_, _, opt_is_fn) = autoderef(fcx, + span, + ty, + || None, + UnresolvedTypeAction::Ignore, + LvaluePreference::NoPreference, + |ty, _| { + 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) - }) + if selcx.evaluate_obligation(&obligation) { + Some(true) + } else { + None + } + }) + }); + + match opt_is_fn { + Some(result) => result, + None => false, + } } else { false } diff --git a/src/test/compile-fail/issue-32128.rs b/src/test/compile-fail/issue-32128.rs new file mode 100644 index 00000000000..fe7e66a2116 --- /dev/null +++ b/src/test/compile-fail/issue-32128.rs @@ -0,0 +1,25 @@ +// 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +struct Example { + example: Box i32> +} + +fn main() { + let demo = Example { + example: Box::new(|x| { + x + 1 + }) + }; + + demo.example(1); //~ ERROR no method named `example` + //~^ NOTE use `(demo.example)(...)` + // (demo.example)(1); +} From 580c5f92d1050543f5e69f842ef8c4899ab2540e Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 4 Apr 2016 14:10:03 +0200 Subject: [PATCH 2/3] use `unwrap_or` --- src/librustc_typeck/check/method/suggest.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 5b46880d5cf..fcd5c4ea94f 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -81,10 +81,7 @@ fn is_fn_ty<'a, 'tcx>(ty: &Ty<'tcx>, fcx: &FnCtxt<'a, 'tcx>, span: Span) -> bool }) }); - match opt_is_fn { - Some(result) => result, - None => false, - } + opt_is_fn.unwrap_or(false) } else { false } From 1ea98a8b7060579e69c6269cdfb1cc527093d561 Mon Sep 17 00:00:00 2001 From: Jonas Schievink Date: Mon, 4 Apr 2016 15:08:38 +0200 Subject: [PATCH 3/3] Just use Some(()) instead --- src/librustc_typeck/check/method/suggest.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index fcd5c4ea94f..32b5a63817e 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -74,14 +74,14 @@ fn is_fn_ty<'a, 'tcx>(ty: &Ty<'tcx>, fcx: &FnCtxt<'a, 'tcx>, span: Span) -> bool let mut selcx = SelectionContext::new(infcx); if selcx.evaluate_obligation(&obligation) { - Some(true) + Some(()) } else { None } }) }); - opt_is_fn.unwrap_or(false) + opt_is_fn.is_some() } else { false }