-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
refactor autoderef to avoid prematurely registering obligations #33852
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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,210 @@ | ||
// 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. | ||
|
||
use astconv::AstConv; | ||
|
||
use super::FnCtxt; | ||
|
||
use rustc::traits; | ||
use rustc::ty::{self, Ty, TraitRef}; | ||
use rustc::ty::{ToPredicate, TypeFoldable}; | ||
use rustc::ty::{MethodCall, MethodCallee}; | ||
use rustc::ty::subst::Substs; | ||
use rustc::ty::{LvaluePreference, NoPreference, PreferMutLvalue}; | ||
use rustc::hir; | ||
|
||
use syntax::codemap::Span; | ||
use syntax::parse::token; | ||
|
||
#[derive(Copy, Clone, Debug)] | ||
enum AutoderefKind { | ||
Builtin, | ||
Overloaded | ||
} | ||
|
||
pub struct Autoderef<'a, 'gcx: 'tcx, 'tcx: 'a> { | ||
fcx: &'a FnCtxt<'a, 'gcx, 'tcx>, | ||
steps: Vec<(Ty<'tcx>, AutoderefKind)>, | ||
cur_ty: Ty<'tcx>, | ||
obligations: Vec<traits::PredicateObligation<'tcx>>, | ||
at_start: bool, | ||
span: Span | ||
} | ||
|
||
impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> { | ||
type Item = (Ty<'tcx>, usize); | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let tcx = self.fcx.tcx; | ||
|
||
debug!("autoderef: steps={:?}, cur_ty={:?}", | ||
self.steps, self.cur_ty); | ||
if self.at_start { | ||
self.at_start = false; | ||
debug!("autoderef stage #0 is {:?}", self.cur_ty); | ||
return Some((self.cur_ty, 0)); | ||
} | ||
|
||
if self.steps.len() == tcx.sess.recursion_limit.get() { | ||
// We've reached the recursion limit, error gracefully. | ||
span_err!(tcx.sess, self.span, E0055, | ||
"reached the recursion limit while auto-dereferencing {:?}", | ||
self.cur_ty); | ||
return None; | ||
} | ||
|
||
if self.cur_ty.is_ty_var() { | ||
return None; | ||
} | ||
|
||
// Otherwise, deref if type is derefable: | ||
let (kind, new_ty) = if let Some(mt) = self.cur_ty.builtin_deref(false, NoPreference) { | ||
(AutoderefKind::Builtin, mt.ty) | ||
} else { | ||
match self.overloaded_deref_ty(self.cur_ty) { | ||
Some(ty) => (AutoderefKind::Overloaded, ty), | ||
_ => return None | ||
} | ||
}; | ||
|
||
if new_ty.references_error() { | ||
return None; | ||
} | ||
|
||
self.steps.push((self.cur_ty, kind)); | ||
debug!("autoderef stage #{:?} is {:?} from {:?}", self.steps.len(), | ||
new_ty, (self.cur_ty, kind)); | ||
self.cur_ty = new_ty; | ||
|
||
Some((self.cur_ty, self.steps.len())) | ||
} | ||
} | ||
|
||
impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> { | ||
fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> { | ||
debug!("overloaded_deref_ty({:?})", ty); | ||
|
||
let tcx = self.fcx.tcx(); | ||
|
||
// <cur_ty as Deref> | ||
let trait_ref = TraitRef { | ||
def_id: match tcx.lang_items.deref_trait() { | ||
Some(f) => f, | ||
None => return None | ||
}, | ||
substs: tcx.mk_substs(Substs::new_trait(vec![], vec![], self.cur_ty)) | ||
}; | ||
|
||
let cause = traits::ObligationCause::misc(self.span, self.fcx.body_id); | ||
|
||
let mut selcx = traits::SelectionContext::new(self.fcx); | ||
let obligation = traits::Obligation::new(cause.clone(), trait_ref.to_predicate()); | ||
if !selcx.evaluate_obligation(&obligation) { | ||
debug!("overloaded_deref_ty: cannot match obligation"); | ||
return None; | ||
} | ||
|
||
let normalized = traits::normalize_projection_type( | ||
&mut selcx, | ||
ty::ProjectionTy { | ||
trait_ref: trait_ref, | ||
item_name: token::intern("Target") | ||
}, | ||
cause, | ||
0 | ||
); | ||
|
||
debug!("overloaded_deref_ty({:?}) = {:?}", ty, normalized); | ||
self.obligations.extend(normalized.obligations); | ||
|
||
Some(self.fcx.resolve_type_vars_if_possible(&normalized.value)) | ||
} | ||
|
||
pub fn unambiguous_final_ty(&self) -> Ty<'tcx> { | ||
self.fcx.structurally_resolved_type(self.span, self.cur_ty) | ||
} | ||
|
||
pub fn finalize<'b, I>(self, pref: LvaluePreference, exprs: I) | ||
where I: IntoIterator<Item=&'b hir::Expr> | ||
{ | ||
let methods : Vec<_> = self.steps.iter().map(|&(ty, kind)| { | ||
if let AutoderefKind::Overloaded = kind { | ||
self.fcx.try_overloaded_deref(self.span, None, ty, pref) | ||
} else { | ||
None | ||
} | ||
}).collect(); | ||
|
||
debug!("finalize({:?}) - {:?},{:?}", pref, methods, self.obligations); | ||
|
||
for expr in exprs { | ||
debug!("finalize - finalizing #{} - {:?}", expr.id, expr); | ||
for (n, method) in methods.iter().enumerate() { | ||
if let &Some(method) = method { | ||
let method_call = MethodCall::autoderef(expr.id, n as u32); | ||
self.fcx.tables.borrow_mut().method_map.insert(method_call, method); | ||
} | ||
} | ||
} | ||
|
||
for obligation in self.obligations { | ||
self.fcx.register_predicate(obligation); | ||
} | ||
} | ||
} | ||
|
||
impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | ||
pub fn autoderef(&'a self, | ||
span: Span, | ||
base_ty: Ty<'tcx>) | ||
-> Autoderef<'a, 'gcx, 'tcx> | ||
{ | ||
Autoderef { | ||
fcx: self, | ||
steps: vec![], | ||
cur_ty: self.resolve_type_vars_if_possible(&base_ty), | ||
obligations: vec![], | ||
at_start: true, | ||
span: span | ||
} | ||
} | ||
|
||
pub fn try_overloaded_deref(&self, | ||
span: Span, | ||
base_expr: Option<&hir::Expr>, | ||
base_ty: Ty<'tcx>, | ||
lvalue_pref: LvaluePreference) | ||
-> Option<MethodCallee<'tcx>> | ||
{ | ||
debug!("try_overloaded_deref({:?},{:?},{:?},{:?})", | ||
span, base_expr, base_ty, lvalue_pref); | ||
// Try DerefMut first, if preferred. | ||
let method = match (lvalue_pref, self.tcx.lang_items.deref_mut_trait()) { | ||
(PreferMutLvalue, Some(trait_did)) => { | ||
self.lookup_method_in_trait(span, base_expr, | ||
token::intern("deref_mut"), trait_did, | ||
base_ty, None) | ||
} | ||
_ => None | ||
}; | ||
|
||
// Otherwise, fall back to Deref. | ||
let method = match (method, self.tcx.lang_items.deref_trait()) { | ||
(None, Some(trait_did)) => { | ||
self.lookup_method_in_trait(span, base_expr, | ||
token::intern("deref"), trait_did, | ||
base_ty, None) | ||
} | ||
(method, _) => method | ||
}; | ||
|
||
method | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
// except according to those terms. | ||
|
||
use super::{DeferredCallResolution, Expectation, FnCtxt, | ||
TupleArgumentsFlag, UnresolvedTypeAction}; | ||
TupleArgumentsFlag}; | ||
|
||
use CrateCtxt; | ||
use middle::cstore::LOCAL_CRATE; | ||
|
@@ -72,15 +72,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { | |
{ | ||
self.check_expr(callee_expr); | ||
let original_callee_ty = self.expr_ty(callee_expr); | ||
let (callee_ty, _, result) = | ||
self.autoderef(callee_expr.span, | ||
original_callee_ty, | ||
|| Some(callee_expr), | ||
UnresolvedTypeAction::Error, | ||
LvaluePreference::NoPreference, | ||
|adj_ty, idx| { | ||
self.try_overloaded_call_step(call_expr, callee_expr, adj_ty, idx) | ||
}); | ||
|
||
let mut autoderef = self.autoderef(callee_expr.span, original_callee_ty); | ||
let result = autoderef.by_ref().flat_map(|(adj_ty, idx)| { | ||
self.try_overloaded_call_step(call_expr, callee_expr, adj_ty, idx) | ||
}).next(); | ||
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. Very nice pattern, I like it! |
||
let callee_ty = autoderef.unambiguous_final_ty(); | ||
autoderef.finalize(LvaluePreference::NoPreference, Some(callee_expr)); | ||
|
||
match result { | ||
None => { | ||
|
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 |
---|---|---|
|
@@ -60,7 +60,7 @@ | |
//! sort of a minor point so I've opted to leave it for later---after all | ||
//! we may want to adjust precisely when coercions occur. | ||
|
||
use check::{FnCtxt, UnresolvedTypeAction}; | ||
use check::{FnCtxt}; | ||
|
||
use rustc::hir; | ||
use rustc::infer::{Coercion, InferOk, TypeOrigin, TypeTrace}; | ||
|
@@ -220,7 +220,8 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> { | |
-> CoerceResult<'tcx> | ||
// FIXME(eddyb) use copyable iterators when that becomes ergonomic. | ||
where E: Fn() -> I, | ||
I: IntoIterator<Item=&'a hir::Expr> { | ||
I: IntoIterator<Item=&'a hir::Expr> | ||
{ | ||
|
||
debug!("coerce_borrowed_pointer(a={:?}, b={:?})", a, b); | ||
|
||
|
@@ -240,18 +241,16 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> { | |
|
||
let span = self.origin.span(); | ||
|
||
let lvalue_pref = LvaluePreference::from_mutbl(mt_b.mutbl); | ||
let mut first_error = None; | ||
let mut r_borrow_var = None; | ||
let (_, autoderefs, success) = self.autoderef(span, a, exprs, | ||
UnresolvedTypeAction::Ignore, | ||
lvalue_pref, | ||
|referent_ty, autoderef| | ||
{ | ||
if autoderef == 0 { | ||
let mut autoderef = self.autoderef(span, a); | ||
let mut success = None; | ||
|
||
for (referent_ty, autoderefs) in autoderef.by_ref() { | ||
if autoderefs == 0 { | ||
// Don't let this pass, otherwise it would cause | ||
// &T to autoref to &&T. | ||
return None; | ||
continue | ||
} | ||
|
||
// At this point, we have deref'd `a` to `referent_ty`. So | ||
|
@@ -326,7 +325,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> { | |
// and let regionck figure it out. | ||
let r = if !self.use_lub { | ||
r_b // [2] above | ||
} else if autoderef == 1 { | ||
} else if autoderefs == 1 { | ||
r_a // [3] above | ||
} else { | ||
if r_borrow_var.is_none() { // create var lazilly, at most once | ||
|
@@ -341,30 +340,31 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> { | |
mutbl: mt_b.mutbl // [1] above | ||
}); | ||
match self.unify(derefd_ty_a, b) { | ||
Ok(ty) => Some(ty), | ||
Ok(ty) => { success = Some((ty, autoderefs)); break }, | ||
Err(err) => { | ||
if first_error.is_none() { | ||
first_error = Some(err); | ||
} | ||
None | ||
} | ||
} | ||
}); | ||
} | ||
|
||
// Extract type or return an error. We return the first error | ||
// we got, which should be from relating the "base" type | ||
// (e.g., in example above, the failure from relating `Vec<T>` | ||
// to the target type), since that should be the least | ||
// confusing. | ||
let ty = match success { | ||
Some(ty) => ty, | ||
let (ty, autoderefs) = match success { | ||
Some(d) => d, | ||
None => { | ||
let err = first_error.expect("coerce_borrowed_pointer had no error"); | ||
debug!("coerce_borrowed_pointer: failed with err = {:?}", err); | ||
return Err(err); | ||
} | ||
}; | ||
|
||
autoderef.finalize(LvaluePreference::from_mutbl(mt_b.mutbl), exprs()); | ||
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. Could use a comment here explaining that this is only correct because nothing can later fail, i.e. we're guaranteed a successful coercion at this point. |
||
|
||
// Now apply the autoref. We have to extract the region out of | ||
// the final ref type we got. | ||
if ty == a && mt_a.mutbl == hir::MutImmutable && autoderefs == 1 { | ||
|
Oops, something went wrong.
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.
Would it be possible to make rolling back a snapshot in which obligations have been registered, into an ICE?
To prevent future misuse, I mean, you seem to have covered the existing problems.