-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
librustc: Implement lifetime elision. #15767
Closed
+303
−168
Closed
Changes from all commits
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
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
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 |
---|---|---|
|
@@ -55,6 +55,7 @@ use middle::lang_items::FnMutTraitLangItem; | |
use middle::subst::{FnSpace, TypeSpace, SelfSpace, Subst, Substs}; | ||
use middle::ty; | ||
use middle::ty_fold::TypeFolder; | ||
use middle::typeck::rscope::{ExplicitRscope, ImpliedSingleRscope}; | ||
use middle::typeck::rscope::RegionScope; | ||
use middle::typeck::{TypeAndSubsts, infer, lookup_def_tcx, rscope}; | ||
use middle::typeck; | ||
|
@@ -931,31 +932,45 @@ fn ty_of_method_or_bare_fn<AC:AstConv>( | |
Option<ty::ExplicitSelfCategory>) { | ||
debug!("ty_of_method_or_bare_fn"); | ||
|
||
// new region names that appear inside of the fn decl are bound to | ||
// that function type | ||
// New region names that appear inside of the arguments of the function | ||
// declaration are bound to that function type. | ||
let rb = rscope::BindingRscope::new(id); | ||
|
||
// `implied_output_region` is the region that will be assumed for any | ||
// region parameters in the return type. In accordance with the rules for | ||
// lifetime elision, we can determine it in two ways. First (determined | ||
// here), if self is by-reference, then the implied output region is the | ||
// region of the self parameter. | ||
let mut explicit_self_category_result = None; | ||
let self_ty = opt_self_info.and_then(|self_info| { | ||
// Figure out and record the explicit self category. | ||
let explicit_self_category = | ||
determine_explicit_self_category(this, &rb, &self_info); | ||
explicit_self_category_result = Some(explicit_self_category); | ||
match explicit_self_category { | ||
ty::StaticExplicitSelfCategory => None, | ||
ty::ByValueExplicitSelfCategory => { | ||
Some(self_info.untransformed_self_ty) | ||
} | ||
ty::ByReferenceExplicitSelfCategory(region, mutability) => { | ||
Some(ty::mk_rptr(this.tcx(), region, | ||
ty::mt {ty: self_info.untransformed_self_ty, | ||
mutbl: mutability})) | ||
} | ||
ty::ByBoxExplicitSelfCategory => { | ||
Some(ty::mk_uniq(this.tcx(), self_info.untransformed_self_ty)) | ||
let (self_ty, mut implied_output_region) = match opt_self_info { | ||
None => (None, None), | ||
Some(self_info) => { | ||
// Figure out and record the explicit self category. | ||
let explicit_self_category = | ||
determine_explicit_self_category(this, &rb, &self_info); | ||
explicit_self_category_result = Some(explicit_self_category); | ||
match explicit_self_category { | ||
ty::StaticExplicitSelfCategory => (None, None), | ||
ty::ByValueExplicitSelfCategory => { | ||
(Some(self_info.untransformed_self_ty), None) | ||
} | ||
ty::ByReferenceExplicitSelfCategory(region, mutability) => { | ||
(Some(ty::mk_rptr(this.tcx(), | ||
region, | ||
ty::mt { | ||
ty: self_info.untransformed_self_ty, | ||
mutbl: mutability | ||
})), | ||
Some(region)) | ||
} | ||
ty::ByBoxExplicitSelfCategory => { | ||
(Some(ty::mk_uniq(this.tcx(), | ||
self_info.untransformed_self_ty)), | ||
None) | ||
} | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
// HACK(eddyb) replace the fake self type in the AST with the actual type. | ||
let input_tys = if self_ty.is_some() { | ||
|
@@ -964,12 +979,47 @@ fn ty_of_method_or_bare_fn<AC:AstConv>( | |
decl.inputs.as_slice() | ||
}; | ||
let input_tys = input_tys.iter().map(|a| ty_of_arg(this, &rb, a, None)); | ||
let self_and_input_tys: Vec<_> = | ||
self_ty.move_iter().chain(input_tys).collect(); | ||
|
||
// Second, if there was exactly one lifetime (either a substitution or a | ||
// reference) in the arguments, then any anonymous regions in the output | ||
// have that lifetime. | ||
if implied_output_region.is_none() { | ||
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. Oh, here it is, never mind... |
||
let mut self_and_input_tys_iter = self_and_input_tys.iter(); | ||
if self_ty.is_some() { | ||
// Skip the first argument if `self` is present. | ||
drop(self_and_input_tys_iter.next()) | ||
} | ||
|
||
let self_and_input_tys = self_ty.move_iter().chain(input_tys).collect(); | ||
let mut accumulator = Vec::new(); | ||
for input_type in self_and_input_tys_iter { | ||
ty::accumulate_lifetimes_in_type(&mut accumulator, *input_type) | ||
} | ||
if accumulator.len() == 1 { | ||
implied_output_region = Some(*accumulator.get(0)); | ||
} | ||
} | ||
|
||
let output_ty = match decl.output.node { | ||
ast::TyInfer => this.ty_infer(decl.output.span), | ||
_ => ast_ty_to_ty(this, &rb, &*decl.output) | ||
_ => { | ||
match implied_output_region { | ||
Some(implied_output_region) => { | ||
let rb = ImpliedSingleRscope { | ||
region: implied_output_region, | ||
}; | ||
ast_ty_to_ty(this, &rb, &*decl.output) | ||
} | ||
None => { | ||
// All regions must be explicitly specified in the output | ||
// if the lifetime elision rules do not apply. This saves | ||
// the user from potentially-confusing errors. | ||
let rb = ExplicitRscope; | ||
ast_ty_to_ty(this, &rb, &*decl.output) | ||
} | ||
} | ||
} | ||
}; | ||
|
||
(ty::BareFnTy { | ||
|
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
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
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
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.
nit: this comment should have a "Second, ..." clause which states where the other rule is applied.