]>
git.proxmox.com Git - rustc.git/blob - src/librustc/infer/error_reporting/nice_region_error/util.rs
013c02f75b88315f1a7f776d2ed8d70c93e4522d
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Helper functions corresponding to lifetime errors due to
12 //! anonymous regions.
15 use infer
::error_reporting
::nice_region_error
::NiceRegionError
;
16 use ty
::{self, Region, Ty}
;
17 use hir
::def_id
::DefId
;
20 // The struct contains the information about the anonymous region
21 // we are searching for.
23 pub(super) struct AnonymousArgInfo
<'tcx
> {
24 // the argument corresponding to the anonymous region
25 pub arg
: &'tcx hir
::Arg
,
26 // the type corresponding to the anonymopus region argument
28 // the ty::BoundRegion corresponding to the anonymous region
29 pub bound_region
: ty
::BoundRegion
,
30 // arg_ty_span contains span of argument type
31 pub arg_ty_span
: Span
,
32 // corresponds to id the argument is the first parameter
37 impl<'a
, 'gcx
, 'tcx
> NiceRegionError
<'a
, 'gcx
, 'tcx
> {
38 // This method walks the Type of the function body arguments using
39 // `fold_regions()` function and returns the
40 // &hir::Arg of the function argument corresponding to the anonymous
41 // region and the Ty corresponding to the named region.
42 // Currently only the case where the function declaration consists of
43 // one named region and one anonymous region is handled.
44 // Consider the example `fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32`
45 // Here, we would return the hir::Arg for y, we return the type &'a
46 // i32, which is the type of y but with the anonymous region replaced
47 // with 'a, the corresponding bound region and is_first which is true if
48 // the hir::Arg is the first argument in the function declaration.
49 pub(super) fn find_arg_with_region(
51 anon_region
: Region
<'tcx
>,
52 replace_region
: Region
<'tcx
>,
53 ) -> Option
<AnonymousArgInfo
<'_
>> {
54 let (id
, bound_region
) = match *anon_region
{
55 ty
::ReFree(ref free_region
) => (free_region
.scope
, free_region
.bound_region
),
56 ty
::ReEarlyBound(ref ebr
) => (
57 self.tcx
.parent_def_id(ebr
.def_id
).unwrap(),
58 ty
::BoundRegion
::BrNamed(ebr
.def_id
, ebr
.name
),
60 _
=> return None
, // not a free region
63 let hir
= &self.tcx
.hir
;
64 if let Some(node_id
) = hir
.as_local_node_id(id
) {
65 if let Some(body_id
) = hir
.maybe_body_owned_by(node_id
) {
66 let body
= hir
.body(body_id
);
67 let owner_id
= hir
.body_owner(body_id
);
68 let fn_decl
= hir
.fn_decl(owner_id
).unwrap();
69 if let Some(tables
) = self.tables
{
73 .filter_map(|(index
, arg
)| {
74 // May return None; sometimes the tables are not yet populated.
75 let ty_hir_id
= fn_decl
.inputs
[index
].hir_id
;
76 let arg_ty_span
= hir
.span(hir
.hir_to_node_id(ty_hir_id
));
77 let ty
= tables
.node_id_to_type_opt(arg
.hir_id
)?
;
78 let mut found_anon_region
= false;
79 let new_arg_ty
= self.tcx
.fold_regions(&ty
, &mut false, |r
, _
| {
80 if *r
== *anon_region
{
81 found_anon_region
= true;
87 if found_anon_region
{
88 let is_first
= index
== 0;
89 Some(AnonymousArgInfo
{
92 arg_ty_span
: arg_ty_span
,
93 bound_region
: bound_region
,
112 // Here, we check for the case where the anonymous region
113 // is in the return type.
114 // FIXME(#42703) - Need to handle certain cases here.
115 pub(super) fn is_return_type_anon(
121 let ret_ty
= self.tcx
.type_of(scope_def_id
);
122 if let ty
::FnDef(_
, _
) = ret_ty
.sty
{
123 let sig
= ret_ty
.fn_sig(self.tcx
);
124 let late_bound_regions
= self.tcx
125 .collect_referenced_late_bound_regions(&sig
.output());
126 if late_bound_regions
.iter().any(|r
| *r
== br
) {
127 return Some(decl
.output
.span());
133 // Here we check for the case where anonymous region
134 // corresponds to self and if yes, we display E0312.
135 // FIXME(#42700) - Need to format self properly to
136 // enable E0621 for it.
137 pub(super) fn is_self_anon(&self, is_first
: bool
, scope_def_id
: DefId
) -> bool
{
140 .opt_associated_item(scope_def_id
)
141 .map(|i
| i
.method_has_self_argument
) == Some(true)