]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_infer/src/infer/error_reporting/nice_region_error/util.rs
Move `{core,std}::stream::Stream` to `{core,std}::async_iter::AsyncIterator`.
[rust.git] / compiler / rustc_infer / src / infer / error_reporting / nice_region_error / util.rs
1 //! Helper functions corresponding to lifetime errors due to
2 //! anonymous regions.
3
4 use crate::infer::error_reporting::nice_region_error::NiceRegionError;
5 use rustc_hir as hir;
6 use rustc_hir::def_id::LocalDefId;
7 use rustc_middle::ty::{self, Binder, DefIdTree, Region, Ty, TypeFoldable};
8 use rustc_span::Span;
9
10 /// Information about the anonymous region we are searching for.
11 #[derive(Debug)]
12 pub(super) struct AnonymousParamInfo<'tcx> {
13     /// The parameter corresponding to the anonymous region.
14     pub param: &'tcx hir::Param<'tcx>,
15     /// The type corresponding to the anonymous region parameter.
16     pub param_ty: Ty<'tcx>,
17     /// The ty::BoundRegionKind corresponding to the anonymous region.
18     pub bound_region: ty::BoundRegionKind,
19     /// The `Span` of the parameter type.
20     pub param_ty_span: Span,
21     /// Signals that the argument is the first parameter in the declaration.
22     pub is_first: bool,
23 }
24
25 impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
26     // This method walks the Type of the function body parameters using
27     // `fold_regions()` function and returns the
28     // &hir::Param of the function parameter corresponding to the anonymous
29     // region and the Ty corresponding to the named region.
30     // Currently only the case where the function declaration consists of
31     // one named region and one anonymous region is handled.
32     // Consider the example `fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32`
33     // Here, we would return the hir::Param for y, we return the type &'a
34     // i32, which is the type of y but with the anonymous region replaced
35     // with 'a, the corresponding bound region and is_first which is true if
36     // the hir::Param is the first parameter in the function declaration.
37     pub(super) fn find_param_with_region(
38         &self,
39         anon_region: Region<'tcx>,
40         replace_region: Region<'tcx>,
41     ) -> Option<AnonymousParamInfo<'_>> {
42         let (id, bound_region) = match *anon_region {
43             ty::ReFree(ref free_region) => (free_region.scope, free_region.bound_region),
44             ty::ReEarlyBound(ebr) => (
45                 self.tcx().parent(ebr.def_id).unwrap(),
46                 ty::BoundRegionKind::BrNamed(ebr.def_id, ebr.name),
47             ),
48             _ => return None, // not a free region
49         };
50
51         let hir = &self.tcx().hir();
52         let hir_id = hir.local_def_id_to_hir_id(id.as_local()?);
53         let body_id = hir.maybe_body_owned_by(hir_id)?;
54         let body = hir.body(body_id);
55         let owner_id = hir.body_owner(body_id);
56         let fn_decl = hir.fn_decl_by_hir_id(owner_id).unwrap();
57         let poly_fn_sig = self.tcx().fn_sig(id);
58         let fn_sig = self.tcx().liberate_late_bound_regions(id, poly_fn_sig);
59         body.params
60             .iter()
61             .take(if fn_sig.c_variadic {
62                 fn_sig.inputs().len()
63             } else {
64                 assert_eq!(fn_sig.inputs().len(), body.params.len());
65                 body.params.len()
66             })
67             .enumerate()
68             .find_map(|(index, param)| {
69                 // May return None; sometimes the tables are not yet populated.
70                 let ty = fn_sig.inputs()[index];
71                 let mut found_anon_region = false;
72                 let new_param_ty = self.tcx().fold_regions(ty, &mut false, |r, _| {
73                     if *r == *anon_region {
74                         found_anon_region = true;
75                         replace_region
76                     } else {
77                         r
78                     }
79                 });
80                 if found_anon_region {
81                     let ty_hir_id = fn_decl.inputs[index].hir_id;
82                     let param_ty_span = hir.span(ty_hir_id);
83                     let is_first = index == 0;
84                     Some(AnonymousParamInfo {
85                         param,
86                         param_ty: new_param_ty,
87                         param_ty_span,
88                         bound_region,
89                         is_first,
90                     })
91                 } else {
92                     None
93                 }
94             })
95     }
96
97     // Here, we check for the case where the anonymous region
98     // is in the return type as written by the user.
99     // FIXME(#42703) - Need to handle certain cases here.
100     pub(super) fn is_return_type_anon(
101         &self,
102         scope_def_id: LocalDefId,
103         br: ty::BoundRegionKind,
104         hir_sig: &hir::FnSig<'_>,
105     ) -> Option<Span> {
106         let fn_ty = self.tcx().type_of(scope_def_id);
107         if let ty::FnDef(_, _) = fn_ty.kind() {
108             let ret_ty = fn_ty.fn_sig(self.tcx()).output();
109             let span = hir_sig.decl.output.span();
110             let future_output = if hir_sig.header.is_async() {
111                 ret_ty.map_bound(|ty| self.infcx.get_impl_future_output_ty(ty)).transpose()
112             } else {
113                 None
114             };
115             return match future_output {
116                 Some(output) if self.includes_region(output, br) => Some(span),
117                 None if self.includes_region(ret_ty, br) => Some(span),
118                 _ => None,
119             };
120         }
121         None
122     }
123
124     fn includes_region(
125         &self,
126         ty: Binder<'tcx, impl TypeFoldable<'tcx>>,
127         region: ty::BoundRegionKind,
128     ) -> bool {
129         let late_bound_regions = self.tcx().collect_referenced_late_bound_regions(&ty);
130         late_bound_regions.iter().any(|r| *r == region)
131     }
132
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: LocalDefId) -> bool {
138         is_first
139             && self
140                 .tcx()
141                 .opt_associated_item(scope_def_id.to_def_id())
142                 .map(|i| i.fn_has_self_parameter)
143                 == Some(true)
144     }
145 }