1//! Helper functions corresponding to lifetime errors due to
2//! anonymous regions.
34use rustc_hiras hir;
5use rustc_hir::def_id::{DefId, LocalDefId};
6use rustc_middle::ty::{self, Binder, Region, Ty, TyCtxt, TypeFoldable, fold_regions};
7use rustc_span::Span;
8use tracing::instrument;
910use crate::error_reporting::infer::nice_region_error::NiceRegionError;
1112/// Information about the anonymous region we are searching for.
13#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for AnonymousParamInfo<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
let names: &'static _ =
&["param", "param_ty", "orig_param_ty", "kind", "param_ty_span",
"is_first"];
let values: &[&dyn ::core::fmt::Debug] =
&[&self.param, &self.param_ty, &self.orig_param_ty, &self.kind,
&self.param_ty_span, &&self.is_first];
::core::fmt::Formatter::debug_struct_fields_finish(f,
"AnonymousParamInfo", names, values)
}
}Debug)]
14pub struct AnonymousParamInfo<'tcx> {
15/// The parameter corresponding to the anonymous region.
16pub param: &'tcx hir::Param<'tcx>,
17/// The type corresponding to the anonymous region parameter.
18pub param_ty: Ty<'tcx>,
19/// The original type before region replacement.
20pub orig_param_ty: Ty<'tcx>,
21/// The `ty::LateParamRegionKind` corresponding to the anonymous region.
22pub kind: ty::LateParamRegionKind,
23/// The `Span` of the parameter type.
24pub param_ty_span: Span,
25/// Signals that the argument is the first parameter in the declaration.
26pub is_first: bool,
27}
2829// This method walks the Type of the function body parameters using
30// `fold_regions()` function and returns the
31// &hir::Param of the function parameter corresponding to the anonymous
32// region and the Ty corresponding to the named region.
33// Currently only the case where the function declaration consists of
34// one named region and one anonymous region is handled.
35// Consider the example `fn foo<'a>(x: &'a i32, y: &i32) -> &'a i32`
36// Here, we would return the hir::Param for y, we return the type &'a
37// i32, which is the type of y but with the anonymous region replaced
38// with 'a, the corresponding bound region and is_first which is true if
39// the hir::Param is the first parameter in the function declaration.
40#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("find_param_with_region",
"rustc_trait_selection::error_reporting::infer::nice_region_error::util",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/nice_region_error/util.rs"),
::tracing_core::__macro_support::Option::Some(40u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::nice_region_error::util"),
::tracing_core::field::FieldSet::new(&["generic_param_scope",
"anon_region", "replace_region"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&generic_param_scope)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&anon_region)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&replace_region)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: Option<AnonymousParamInfo<'tcx>> =
loop {};
return __tracing_attr_fake_return;
}
{
let (id, kind) =
match anon_region.kind() {
ty::ReLateParam(late_param) =>
(late_param.scope, late_param.kind),
ty::ReEarlyParam(ebr) => {
let region_def =
tcx.generics_of(generic_param_scope).region_param(ebr,
tcx).def_id;
(tcx.parent(region_def),
ty::LateParamRegionKind::Named(region_def))
}
_ => return None,
};
let def_id = id.as_local()?;
match tcx.hir_node_by_def_id(generic_param_scope) {
hir::Node::Expr(&hir::Expr {
kind: hir::ExprKind::Closure { .. }, .. }) => {
return None;
}
_ => {}
}
let body = tcx.hir_maybe_body_owned_by(def_id)?;
let owner_id = tcx.hir_body_owner(body.id());
let fn_decl = tcx.hir_fn_decl_by_hir_id(owner_id)?;
let poly_fn_sig =
tcx.fn_sig(id).instantiate_identity().skip_norm_wip();
let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
body.params.iter().take(if fn_sig.c_variadic() {
fn_sig.inputs().len()
} else {
match (&fn_sig.inputs().len(), &body.params.len()) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
body.params.len()
}).enumerate().find_map(|(index, param)|
{
let ty = fn_sig.inputs()[index];
let mut found_anon_region = false;
let new_param_ty =
fold_regions(tcx, ty,
|r, _|
{
if r == anon_region {
found_anon_region = true;
replace_region
} else { r }
});
found_anon_region.then(||
{
let ty_hir_id = fn_decl.inputs[index].hir_id;
let param_ty_span = tcx.hir_span(ty_hir_id);
let is_first = index == 0;
AnonymousParamInfo {
param,
param_ty: new_param_ty,
orig_param_ty: ty,
param_ty_span,
kind,
is_first,
}
})
})
}
}
}#[instrument(skip(tcx), level = "debug")]41pub fn find_param_with_region<'tcx>(
42 tcx: TyCtxt<'tcx>,
43 generic_param_scope: LocalDefId,
44 anon_region: Region<'tcx>,
45 replace_region: Region<'tcx>,
46) -> Option<AnonymousParamInfo<'tcx>> {
47let (id, kind) = match anon_region.kind() {
48 ty::ReLateParam(late_param) => (late_param.scope, late_param.kind),
49 ty::ReEarlyParam(ebr) => {
50let region_def = tcx.generics_of(generic_param_scope).region_param(ebr, tcx).def_id;
51 (tcx.parent(region_def), ty::LateParamRegionKind::Named(region_def))
52 }
53_ => return None, // not a free region
54};
5556let def_id = id.as_local()?;
5758// FIXME: use def_kind
59 // Don't perform this on closures
60match tcx.hir_node_by_def_id(generic_param_scope) {
61 hir::Node::Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
62return None;
63 }
64_ => {}
65 }
6667let body = tcx.hir_maybe_body_owned_by(def_id)?;
6869let owner_id = tcx.hir_body_owner(body.id());
70let fn_decl = tcx.hir_fn_decl_by_hir_id(owner_id)?;
71let poly_fn_sig = tcx.fn_sig(id).instantiate_identity().skip_norm_wip();
7273let fn_sig = tcx.liberate_late_bound_regions(id, poly_fn_sig);
74 body.params
75 .iter()
76 .take(if fn_sig.c_variadic() {
77 fn_sig.inputs().len()
78 } else {
79assert_eq!(fn_sig.inputs().len(), body.params.len());
80 body.params.len()
81 })
82 .enumerate()
83 .find_map(|(index, param)| {
84// May return None; sometimes the tables are not yet populated.
85let ty = fn_sig.inputs()[index];
86let mut found_anon_region = false;
87let new_param_ty = fold_regions(tcx, ty, |r, _| {
88if r == anon_region {
89 found_anon_region = true;
90 replace_region
91 } else {
92 r
93 }
94 });
95 found_anon_region.then(|| {
96let ty_hir_id = fn_decl.inputs[index].hir_id;
97let param_ty_span = tcx.hir_span(ty_hir_id);
98let is_first = index == 0;
99 AnonymousParamInfo {
100 param,
101 param_ty: new_param_ty,
102 orig_param_ty: ty,
103 param_ty_span,
104 kind,
105 is_first,
106 }
107 })
108 })
109}
110111impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
112pub(super) fn find_param_with_region(
113&self,
114 anon_region: Region<'tcx>,
115 replace_region: Region<'tcx>,
116 ) -> Option<AnonymousParamInfo<'tcx>> {
117find_param_with_region(self.tcx(), self.generic_param_scope, anon_region, replace_region)
118 }
119120// Here, we check for the case where the anonymous region
121 // is in the return type as written by the user.
122 // FIXME(#42703) - Need to handle certain cases here.
123pub(super) fn is_return_type_anon(
124&self,
125 scope_def_id: LocalDefId,
126 region_def_id: DefId,
127 hir_sig: &hir::FnSig<'_>,
128 ) -> Option<Span> {
129let fn_ty = self.tcx().type_of(scope_def_id).instantiate_identity().skip_norm_wip();
130if let ty::FnDef(_, _) = fn_ty.kind() {
131let ret_ty = fn_ty.fn_sig(self.tcx()).output();
132let span = hir_sig.decl.output.span();
133let future_output = if hir_sig.header.is_async() {
134ret_ty.map_bound(|ty| self.cx.get_impl_future_output_ty(ty)).transpose()
135 } else {
136None137 };
138return match future_output {
139Some(output) if self.includes_region(output, region_def_id) => Some(span),
140Noneif self.includes_region(ret_ty, region_def_id) => Some(span),
141_ => None,
142 };
143 }
144None145 }
146147fn includes_region(
148&self,
149 ty: Binder<'tcx, impl TypeFoldable<TyCtxt<'tcx>>>,
150 region_def_id: DefId,
151 ) -> bool {
152let late_bound_regions = self.tcx().collect_referenced_late_bound_regions(ty);
153// We are only checking is any region meets the condition so order doesn't matter
154#[allow(rustc::potential_query_instability)]
155late_bound_regions.iter().any(|r| match *r {
156 ty::BoundRegionKind::Named(def_id) => def_id == region_def_id,
157_ => false,
158 })
159 }
160161// Here we check for the case where anonymous region
162 // corresponds to self and if yes, we display E0312.
163 // FIXME(#42700) - Need to format self properly to
164 // enable E0621 for it.
165pub(super) fn is_self_anon(&self, is_first: bool, scope_def_id: LocalDefId) -> bool {
166is_first167 && self168 .tcx()
169 .opt_associated_item(scope_def_id.to_def_id())
170 .is_some_and(|i| i.is_method())
171 }
172}