-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Autoderef when suggesting to call (self.field)
#32712
Merged
bors
merged 3 commits into
rust-lang:master
from
jonas-schievink:autoderef-fields-for-fn-ty
Apr 5, 2016
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,15 @@ | |
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}; | ||
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); | ||
|
||
return selcx.evaluate_obligation(&obligation) | ||
}) | ||
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); | ||
|
||
if selcx.evaluate_obligation(&obligation) { | ||
Some(true) | ||
} else { | ||
None | ||
} | ||
}) | ||
}); | ||
|
||
match opt_is_fn { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gah, of course. Fixed. |
||
Some(result) => result, | ||
None => false, | ||
} | ||
} else { | ||
false | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <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 Example { | ||
example: Box<Fn(i32) -> 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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I can’t find
Some(false)
ever getting emitted. Couldn’t you just returnSome(())
and then interpret the discriminant as boolean with.is_some()
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sure. I just got caught up in a mess of
bool
s I think :-)This should be better.