1use std::fmt::{self, Display};
2use std::iter;
3
4use rustc_data_structures::fx::IndexEntry;
5use rustc_errors::Diag;
6use rustc_hir as hir;
7use rustc_hir::def::{DefKind, Res};
8use rustc_middle::ty::print::RegionHighlightMode;
9use rustc_middle::ty::{self, GenericArgKind, GenericArgsRef, RegionVid, Ty, Unnormalized};
10use rustc_span::{DUMMY_SP, Span, Symbol, bug, kw, span_bug, sym};
11use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
12use tracing::{debug, instrument};
13
14use crate::MirBorrowckCtxt;
15use crate::universal_regions::DefiningTy;
16
17#[derive(#[automatically_derived]
impl ::core::fmt::Debug for RegionName {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "RegionName",
"name", &self.name, "source", &&self.source)
}
}Debug, #[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for RegionName { }
#[automatically_derived]
impl ::core::clone::Clone for RegionName {
#[inline]
fn clone(&self) -> RegionName {
let _: ::core::clone::AssertParamIsClone<Symbol>;
let _: ::core::clone::AssertParamIsClone<RegionNameSource>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for RegionName { }Copy)]
20pub(crate) struct RegionName {
21 pub(crate) name: Symbol,
23 pub(crate) source: RegionNameSource,
25}
26
27#[derive(#[automatically_derived]
impl ::core::fmt::Debug for RegionNameSource {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
RegionNameSource::NamedEarlyParamRegion(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"NamedEarlyParamRegion", &__self_0),
RegionNameSource::NamedLateParamRegion(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"NamedLateParamRegion", &__self_0),
RegionNameSource::Static =>
::core::fmt::Formatter::write_str(f, "Static"),
RegionNameSource::SynthesizedFreeEnvRegion(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"SynthesizedFreeEnvRegion", __self_0, &__self_1),
RegionNameSource::AnonRegionFromArgument(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"AnonRegionFromArgument", &__self_0),
RegionNameSource::AnonRegionFromUpvar(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"AnonRegionFromUpvar", __self_0, &__self_1),
RegionNameSource::AnonRegionFromOutput(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"AnonRegionFromOutput", __self_0, &__self_1),
RegionNameSource::AnonRegionFromYieldTy(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"AnonRegionFromYieldTy", __self_0, &__self_1),
RegionNameSource::AnonRegionFromAsyncFn(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"AnonRegionFromAsyncFn", &__self_0),
RegionNameSource::AnonRegionFromImplSignature(__self_0, __self_1)
=>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"AnonRegionFromImplSignature", __self_0, &__self_1),
}
}
}Debug, #[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for RegionNameSource { }
#[automatically_derived]
impl ::core::clone::Clone for RegionNameSource {
#[inline]
fn clone(&self) -> RegionNameSource {
let _: ::core::clone::AssertParamIsClone<Span>;
let _: ::core::clone::AssertParamIsClone<&'static str>;
let _: ::core::clone::AssertParamIsClone<RegionNameHighlight>;
let _: ::core::clone::AssertParamIsClone<Symbol>;
let _: ::core::clone::AssertParamIsClone<&'static str>;
let _: ::core::clone::AssertParamIsClone<&'static str>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for RegionNameSource { }Copy)]
31pub(crate) enum RegionNameSource {
32 NamedEarlyParamRegion(Span),
34 NamedLateParamRegion(Span),
36 Static,
38 SynthesizedFreeEnvRegion(Span, &'static str),
40 AnonRegionFromArgument(RegionNameHighlight),
42 AnonRegionFromUpvar(Span, Symbol),
44 AnonRegionFromOutput(RegionNameHighlight, &'static str),
46 AnonRegionFromYieldTy(Span, Symbol),
48 AnonRegionFromAsyncFn(Span),
50 AnonRegionFromImplSignature(Span, &'static str),
52}
53
54#[derive(#[automatically_derived]
impl ::core::fmt::Debug for RegionNameHighlight {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
RegionNameHighlight::MatchedHirTy(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"MatchedHirTy", &__self_0),
RegionNameHighlight::MatchedAdtAndSegment(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"MatchedAdtAndSegment", &__self_0),
RegionNameHighlight::CannotMatchHirTy(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"CannotMatchHirTy", __self_0, &__self_1),
RegionNameHighlight::Occluded(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"Occluded", __self_0, &__self_1),
}
}
}Debug, #[automatically_derived]
#[doc(hidden)]
unsafe impl ::core::clone::TrivialClone for RegionNameHighlight { }
#[automatically_derived]
impl ::core::clone::Clone for RegionNameHighlight {
#[inline]
fn clone(&self) -> RegionNameHighlight {
let _: ::core::clone::AssertParamIsClone<Span>;
let _: ::core::clone::AssertParamIsClone<Symbol>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for RegionNameHighlight { }Copy)]
57pub(crate) enum RegionNameHighlight {
58 MatchedHirTy(Span),
60 MatchedAdtAndSegment(Span),
62 CannotMatchHirTy(Span, Symbol),
65 Occluded(Span, Symbol),
69}
70
71impl RegionName {
72 pub(crate) fn was_named(&self) -> bool {
73 match self.source {
74 RegionNameSource::NamedEarlyParamRegion(..)
75 | RegionNameSource::NamedLateParamRegion(..)
76 | RegionNameSource::Static => true,
77 RegionNameSource::SynthesizedFreeEnvRegion(..)
78 | RegionNameSource::AnonRegionFromArgument(..)
79 | RegionNameSource::AnonRegionFromUpvar(..)
80 | RegionNameSource::AnonRegionFromOutput(..)
81 | RegionNameSource::AnonRegionFromYieldTy(..)
82 | RegionNameSource::AnonRegionFromAsyncFn(..)
83 | RegionNameSource::AnonRegionFromImplSignature(..) => false,
84 }
85 }
86
87 pub(crate) fn span(&self) -> Option<Span> {
88 match self.source {
89 RegionNameSource::Static => None,
90 RegionNameSource::NamedEarlyParamRegion(span)
91 | RegionNameSource::NamedLateParamRegion(span)
92 | RegionNameSource::SynthesizedFreeEnvRegion(span, _)
93 | RegionNameSource::AnonRegionFromUpvar(span, _)
94 | RegionNameSource::AnonRegionFromYieldTy(span, _)
95 | RegionNameSource::AnonRegionFromAsyncFn(span)
96 | RegionNameSource::AnonRegionFromImplSignature(span, _) => Some(span),
97 RegionNameSource::AnonRegionFromArgument(ref highlight)
98 | RegionNameSource::AnonRegionFromOutput(ref highlight, _) => match *highlight {
99 RegionNameHighlight::MatchedHirTy(span)
100 | RegionNameHighlight::MatchedAdtAndSegment(span)
101 | RegionNameHighlight::CannotMatchHirTy(span, _)
102 | RegionNameHighlight::Occluded(span, _) => Some(span),
103 },
104 }
105 }
106
107 pub(crate) fn highlight_region_name<G>(&self, diag: &mut Diag<'_, G>) {
108 match &self.source {
109 RegionNameSource::NamedLateParamRegion(span)
110 | RegionNameSource::NamedEarlyParamRegion(span) => {
111 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` defined here",
self))
})format!("lifetime `{self}` defined here"));
112 }
113 RegionNameSource::SynthesizedFreeEnvRegion(span, closure_trait) => {
114 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` represents this closure\'s body",
self))
})format!("lifetime `{self}` represents this closure's body"));
115 diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("closure implements `{0}`, so references to captured variables can\'t escape the closure",
closure_trait))
})format!(
116 "closure implements `{closure_trait}`, so references to captured variables \
117 can't escape the closure"
118 ));
119 }
120 RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::CannotMatchHirTy(
121 span,
122 type_name,
123 )) => {
124 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("has type `{0}`", type_name))
})format!("has type `{type_name}`"));
125 }
126 RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::MatchedHirTy(span))
127 | RegionNameSource::AnonRegionFromOutput(RegionNameHighlight::MatchedHirTy(span), _)
128 | RegionNameSource::AnonRegionFromAsyncFn(span) => {
129 diag.span_label(
130 *span,
131 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("let\'s call the lifetime of this reference `{0}`",
self))
})format!("let's call the lifetime of this reference `{self}`"),
132 );
133 }
134 RegionNameSource::AnonRegionFromArgument(
135 RegionNameHighlight::MatchedAdtAndSegment(span),
136 )
137 | RegionNameSource::AnonRegionFromOutput(
138 RegionNameHighlight::MatchedAdtAndSegment(span),
139 _,
140 ) => {
141 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("let\'s call this `{0}`", self))
})format!("let's call this `{self}`"));
142 }
143 RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::Occluded(
144 span,
145 type_name,
146 )) => {
147 diag.span_label(
148 *span,
149 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` appears in the type `{1}`",
self, type_name))
})format!("lifetime `{self}` appears in the type `{type_name}`"),
150 );
151 }
152 RegionNameSource::AnonRegionFromOutput(
153 RegionNameHighlight::Occluded(span, type_name),
154 mir_description,
155 ) => {
156 diag.span_label(
157 *span,
158 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("return type{0} `{1}` contains a lifetime `{2}`",
mir_description, type_name, self))
})format!(
159 "return type{mir_description} `{type_name}` contains a lifetime `{self}`"
160 ),
161 );
162 }
163 RegionNameSource::AnonRegionFromUpvar(span, upvar_name) => {
164 diag.span_label(
165 *span,
166 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` appears in the type of `{1}`",
self, upvar_name))
})format!("lifetime `{self}` appears in the type of `{upvar_name}`"),
167 );
168 }
169 RegionNameSource::AnonRegionFromOutput(
170 RegionNameHighlight::CannotMatchHirTy(span, type_name),
171 mir_description,
172 ) => {
173 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("return type{0} is {1}",
mir_description, type_name))
})format!("return type{mir_description} is {type_name}"));
174 }
175 RegionNameSource::AnonRegionFromYieldTy(span, type_name) => {
176 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("yield type is {0}", type_name))
})format!("yield type is {type_name}"));
177 }
178 RegionNameSource::AnonRegionFromImplSignature(span, location) => {
179 diag.span_label(
180 *span,
181 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` appears in the `impl`\'s {1}",
self, location))
})format!("lifetime `{self}` appears in the `impl`'s {location}"),
182 );
183 }
184 RegionNameSource::Static => {}
185 }
186 }
187}
188
189impl Display for RegionName {
190 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191 f.write_fmt(format_args!("{0}", self.name))write!(f, "{}", self.name)
192 }
193}
194
195impl rustc_errors::IntoDiagArg for RegionName {
196 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
197 self.to_string().into_diag_arg(path)
198 }
199}
200
201impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
202 pub(crate) fn mir_def_id(&self) -> hir::def_id::LocalDefId {
203 self.body.source.def_id().expect_local()
204 }
205
206 pub(crate) fn mir_hir_id(&self) -> hir::HirId {
207 self.infcx.tcx.local_def_id_to_hir_id(self.mir_def_id())
208 }
209
210 pub(crate) fn synthesize_region_name(&self) -> Symbol {
215 let c = self.next_region_name.replace_with(|counter| *counter + 1);
216 Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\'{0:?}", c))
})format!("'{c:?}"))
217 }
218
219 pub(crate) fn give_region_a_name(&self, fr: RegionVid) -> Option<RegionName> {
247 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs:247",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(247u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("give_region_a_name(fr={0:?}, counter={1:?})",
fr, self.next_region_name.try_borrow().unwrap()) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
248 "give_region_a_name(fr={:?}, counter={:?})",
249 fr,
250 self.next_region_name.try_borrow().unwrap()
251 );
252
253 if !self.regioncx.universal_regions().is_universal_region(fr) {
::core::panicking::panic("assertion failed: self.regioncx.universal_regions().is_universal_region(fr)")
};assert!(self.regioncx.universal_regions().is_universal_region(fr));
254
255 match self.region_names.borrow_mut().entry(fr) {
256 IndexEntry::Occupied(precomputed_name) => Some(*precomputed_name.get()),
257 IndexEntry::Vacant(slot) => {
258 let new_name = self
259 .give_name_from_error_region(fr)
260 .or_else(|| self.give_name_if_anonymous_region_appears_in_arguments(fr))
261 .or_else(|| self.give_name_if_anonymous_region_appears_in_upvars(fr))
262 .or_else(|| self.give_name_if_anonymous_region_appears_in_output(fr))
263 .or_else(|| self.give_name_if_anonymous_region_appears_in_yield_ty(fr))
264 .or_else(|| self.give_name_if_anonymous_region_appears_in_impl_signature(fr))
265 .or_else(|| {
266 self.give_name_if_anonymous_region_appears_in_arg_position_impl_trait(fr)
267 });
268
269 if let Some(new_name) = new_name {
270 slot.insert(new_name);
271 }
272 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs:272",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(272u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("give_region_a_name: gave name {0:?}",
new_name) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!("give_region_a_name: gave name {:?}", new_name);
273
274 new_name
275 }
276 }
277 }
278
279 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::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("give_name_from_error_region",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(283u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("fr")
}> =
::tracing::__macro_support::FieldName::new("fr");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&fr)
as &dyn ::tracing::field::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<RegionName> = loop {};
return __tracing_attr_fake_return;
}
{
let error_region = self.regioncx.to_error_region(fr)?;
let tcx = self.infcx.tcx;
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs:289",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(289u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("give_region_a_name: error_region = {0:?}",
error_region) as &dyn ::tracing::field::Value))])
});
} else { ; }
};
match error_region.kind() {
ty::ReEarlyParam(ebr) =>
ebr.is_named().then(||
{
let def_id =
tcx.generics_of(self.mir_def_id()).region_param(ebr,
tcx).def_id;
let span =
tcx.hir_span_if_local(def_id).unwrap_or(DUMMY_SP);
RegionName {
name: ebr.name,
source: RegionNameSource::NamedEarlyParamRegion(span),
}
}),
ty::ReStatic => {
Some(RegionName {
name: kw::StaticLifetime,
source: RegionNameSource::Static,
})
}
ty::ReLateParam(late_param) =>
match late_param.kind {
ty::LateParamRegionKind::Named(region_def_id) => {
let span =
tcx.hir_span_if_local(region_def_id).unwrap_or(DUMMY_SP);
if let Some(name) = late_param.kind.get_name(tcx) {
Some(RegionName {
name,
source: RegionNameSource::NamedLateParamRegion(span),
})
} else if tcx.asyncness(self.mir_hir_id().owner).is_async()
{
let name = self.synthesize_region_name();
Some(RegionName {
name,
source: RegionNameSource::AnonRegionFromAsyncFn(span),
})
} else { None }
}
ty::LateParamRegionKind::ClosureEnv => {
let def_ty = self.regioncx.universal_regions().defining_ty;
let (is_lending_coroutine_closure, closure_kind) =
match def_ty {
DefiningTy::Closure(_, args) =>
(false, args.as_closure().kind()),
DefiningTy::CoroutineClosure(_, args) => {
let args = args.as_coroutine_closure();
(!args.tupled_upvars_ty().is_ty_var() &&
args.has_self_borrows(), args.kind())
}
_ => {
bug_impl(None, format_args!("BrEnv outside of closure."),
Location::caller());
}
};
let hir::ExprKind::Closure(&hir::Closure { fn_decl_span, ..
}) =
tcx.hir_expect_expr(self.mir_hir_id()).kind else {
bug_impl(None,
format_args!("Closure is not defined by a closure expr"),
Location::caller());
};
let region_name = self.synthesize_region_name();
let closure_trait =
match (is_lending_coroutine_closure, closure_kind) {
(false, kind) => kind.as_str(),
(true, ty::ClosureKind::Fn) => "AsyncFn",
(true, ty::ClosureKind::FnMut) => "AsyncFnMut",
(true, ty::ClosureKind::FnOnce) => "AsyncFnOnce",
};
Some(RegionName {
name: region_name,
source: RegionNameSource::SynthesizedFreeEnvRegion(fn_decl_span,
closure_trait),
})
}
ty::LateParamRegionKind::Anon(_) => None,
ty::LateParamRegionKind::NamedAnon(_, _) =>
bug_impl(None,
format_args!("only used for pretty printing"),
Location::caller()),
},
ty::ReBound(..) | ty::ReVar(..) | ty::RePlaceholder(..) |
ty::ReErased | ty::ReError(_) => None,
}
}
}
}#[instrument(level = "trace", skip(self))]
284 fn give_name_from_error_region(&self, fr: RegionVid) -> Option<RegionName> {
285 let error_region = self.regioncx.to_error_region(fr)?;
286
287 let tcx = self.infcx.tcx;
288
289 debug!("give_region_a_name: error_region = {:?}", error_region);
290 match error_region.kind() {
291 ty::ReEarlyParam(ebr) => ebr.is_named().then(|| {
292 let def_id = tcx.generics_of(self.mir_def_id()).region_param(ebr, tcx).def_id;
293 let span = tcx.hir_span_if_local(def_id).unwrap_or(DUMMY_SP);
294 RegionName { name: ebr.name, source: RegionNameSource::NamedEarlyParamRegion(span) }
295 }),
296
297 ty::ReStatic => {
298 Some(RegionName { name: kw::StaticLifetime, source: RegionNameSource::Static })
299 }
300
301 ty::ReLateParam(late_param) => match late_param.kind {
302 ty::LateParamRegionKind::Named(region_def_id) => {
303 let span = tcx.hir_span_if_local(region_def_id).unwrap_or(DUMMY_SP);
305
306 if let Some(name) = late_param.kind.get_name(tcx) {
307 Some(RegionName {
309 name,
310 source: RegionNameSource::NamedLateParamRegion(span),
311 })
312 } else if tcx.asyncness(self.mir_hir_id().owner).is_async() {
313 let name = self.synthesize_region_name();
319 Some(RegionName {
320 name,
321 source: RegionNameSource::AnonRegionFromAsyncFn(span),
322 })
323 } else {
324 None
325 }
326 }
327
328 ty::LateParamRegionKind::ClosureEnv => {
329 let def_ty = self.regioncx.universal_regions().defining_ty;
330
331 let (is_lending_coroutine_closure, closure_kind) = match def_ty {
332 DefiningTy::Closure(_, args) => (false, args.as_closure().kind()),
333 DefiningTy::CoroutineClosure(_, args) => {
334 let args = args.as_coroutine_closure();
335 (
336 !args.tupled_upvars_ty().is_ty_var() && args.has_self_borrows(),
337 args.kind(),
338 )
339 }
340 _ => {
341 bug!("BrEnv outside of closure.");
343 }
344 };
345 let hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }) =
346 tcx.hir_expect_expr(self.mir_hir_id()).kind
347 else {
348 bug!("Closure is not defined by a closure expr");
349 };
350 let region_name = self.synthesize_region_name();
351 let closure_trait = match (is_lending_coroutine_closure, closure_kind) {
352 (false, kind) => kind.as_str(),
353 (true, ty::ClosureKind::Fn) => "AsyncFn",
354 (true, ty::ClosureKind::FnMut) => "AsyncFnMut",
355 (true, ty::ClosureKind::FnOnce) => "AsyncFnOnce",
356 };
357
358 Some(RegionName {
359 name: region_name,
360 source: RegionNameSource::SynthesizedFreeEnvRegion(
361 fn_decl_span,
362 closure_trait,
363 ),
364 })
365 }
366
367 ty::LateParamRegionKind::Anon(_) => None,
368 ty::LateParamRegionKind::NamedAnon(_, _) => bug!("only used for pretty printing"),
369 },
370
371 ty::ReBound(..)
372 | ty::ReVar(..)
373 | ty::RePlaceholder(..)
374 | ty::ReErased
375 | ty::ReError(_) => None,
376 }
377 }
378
379 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::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("give_name_if_we_can_match_upvar_args",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(389u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("fr")
}> =
::tracing::__macro_support::FieldName::new("fr");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("upvar_index")
}> =
::tracing::__macro_support::FieldName::new("upvar_index");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&fr)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&upvar_index as
&dyn ::tracing::field::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<RegionName> = loop {};
return __tracing_attr_fake_return;
}
{
let tcx = self.infcx.tcx;
let defining_ty = self.regioncx.universal_regions().defining_ty;
let closure_def_id =
match defining_ty {
DefiningTy::Closure(def_id, _) |
DefiningTy::Coroutine(def_id, _) |
DefiningTy::CoroutineClosure(def_id, _) => def_id,
_ => return None,
};
let parent_def_id = tcx.parent(closure_def_id);
if !#[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(parent_def_id)
{
DefKind::Fn | DefKind::AssocFn => true,
_ => false,
} {
return None;
}
let captured_place = self.upvars.get(upvar_index)?;
let upvar_hir_id = captured_place.get_root_variable();
let parent_local_def_id = parent_def_id.as_local()?;
let parent_body = tcx.hir_body_owned_by(parent_local_def_id);
let param_index =
parent_body.params.iter().position(|param|
param.pat.hir_id == upvar_hir_id)?;
let parent_fn_sig =
tcx.fn_sig(parent_def_id).instantiate_identity().skip_norm_wip();
let liberated_sig =
tcx.liberate_late_bound_regions(parent_def_id, parent_fn_sig);
let parent_param_ty = *liberated_sig.inputs().get(param_index)?;
let upvar_nll_ty = *defining_ty.upvar_tys().get(upvar_index)?;
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs:432",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(432u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("give_name_if_we_can_match_upvar_args: parent_param_ty={0:?}, upvar_nll_ty={1:?}",
parent_param_ty, upvar_nll_ty) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};
let mut parent_regions = ::alloc::vec::Vec::new();
tcx.for_each_free_region(&parent_param_ty,
|r| parent_regions.push(r));
let mut nll_regions = ::alloc::vec::Vec::new();
tcx.for_each_free_region(&upvar_nll_ty, |r| nll_regions.push(r));
if parent_regions.len() != nll_regions.len() {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs:449",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(449u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("give_name_if_we_can_match_upvar_args: region count mismatch ({0} vs {1})",
parent_regions.len(), nll_regions.len()) as
&dyn ::tracing::field::Value))])
});
} else { ; }
};
return None;
}
for (parent_r, nll_r) in iter::zip(&parent_regions, &nll_regions)
{
if nll_r.as_var() == fr {
match parent_r.kind() {
ty::ReLateParam(late_param) => {
if let Some(name) = late_param.kind.get_name(tcx) {
let span =
late_param.kind.get_id().and_then(|id|
tcx.hir_span_if_local(id)).unwrap_or(DUMMY_SP);
return Some(RegionName {
name,
source: RegionNameSource::NamedLateParamRegion(span),
});
}
}
ty::ReEarlyParam(ebr) => {
if ebr.is_named() {
let def_id =
tcx.generics_of(parent_def_id).region_param(ebr,
tcx).def_id;
let span =
tcx.hir_span_if_local(def_id).unwrap_or(DUMMY_SP);
return Some(RegionName {
name: ebr.name,
source: RegionNameSource::NamedEarlyParamRegion(span),
});
}
}
_ => {}
}
}
}
None
}
}
}#[instrument(level = "trace", skip(self))]
390 fn give_name_if_we_can_match_upvar_args(
391 &self,
392 fr: RegionVid,
393 upvar_index: usize,
394 ) -> Option<RegionName> {
395 let tcx = self.infcx.tcx;
396 let defining_ty = self.regioncx.universal_regions().defining_ty;
397
398 let closure_def_id = match defining_ty {
399 DefiningTy::Closure(def_id, _)
400 | DefiningTy::Coroutine(def_id, _)
401 | DefiningTy::CoroutineClosure(def_id, _) => def_id,
402 _ => return None,
403 };
404
405 let parent_def_id = tcx.parent(closure_def_id);
406
407 if !matches!(tcx.def_kind(parent_def_id), DefKind::Fn | DefKind::AssocFn) {
409 return None;
410 }
411
412 let captured_place = self.upvars.get(upvar_index)?;
417 let upvar_hir_id = captured_place.get_root_variable();
418 let parent_local_def_id = parent_def_id.as_local()?;
419 let parent_body = tcx.hir_body_owned_by(parent_local_def_id);
420 let param_index =
421 parent_body.params.iter().position(|param| param.pat.hir_id == upvar_hir_id)?;
422
423 let parent_fn_sig = tcx.fn_sig(parent_def_id).instantiate_identity().skip_norm_wip();
426 let liberated_sig = tcx.liberate_late_bound_regions(parent_def_id, parent_fn_sig);
427 let parent_param_ty = *liberated_sig.inputs().get(param_index)?;
428
429 let upvar_nll_ty = *defining_ty.upvar_tys().get(upvar_index)?;
431
432 debug!(
433 "give_name_if_we_can_match_upvar_args: parent_param_ty={:?}, upvar_nll_ty={:?}",
434 parent_param_ty, upvar_nll_ty
435 );
436
437 let mut parent_regions = vec![];
443 tcx.for_each_free_region(&parent_param_ty, |r| parent_regions.push(r));
444
445 let mut nll_regions = vec![];
446 tcx.for_each_free_region(&upvar_nll_ty, |r| nll_regions.push(r));
447
448 if parent_regions.len() != nll_regions.len() {
449 debug!(
450 "give_name_if_we_can_match_upvar_args: region count mismatch ({} vs {})",
451 parent_regions.len(),
452 nll_regions.len()
453 );
454 return None;
455 }
456
457 for (parent_r, nll_r) in iter::zip(&parent_regions, &nll_regions) {
458 if nll_r.as_var() == fr {
459 match parent_r.kind() {
460 ty::ReLateParam(late_param) => {
461 if let Some(name) = late_param.kind.get_name(tcx) {
462 let span = late_param
463 .kind
464 .get_id()
465 .and_then(|id| tcx.hir_span_if_local(id))
466 .unwrap_or(DUMMY_SP);
467 return Some(RegionName {
468 name,
469 source: RegionNameSource::NamedLateParamRegion(span),
470 });
471 }
472 }
473 ty::ReEarlyParam(ebr) => {
474 if ebr.is_named() {
475 let def_id =
476 tcx.generics_of(parent_def_id).region_param(ebr, tcx).def_id;
477 let span = tcx.hir_span_if_local(def_id).unwrap_or(DUMMY_SP);
478 return Some(RegionName {
479 name: ebr.name,
480 source: RegionNameSource::NamedEarlyParamRegion(span),
481 });
482 }
483 }
484 _ => {}
485 }
486 }
487 }
488
489 None
490 }
491
492 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::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("give_name_if_anonymous_region_appears_in_arguments",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(500u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("fr")
}> =
::tracing::__macro_support::FieldName::new("fr");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&fr)
as &dyn ::tracing::field::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<RegionName> = loop {};
return __tracing_attr_fake_return;
}
{
let implicit_inputs =
self.regioncx.universal_regions().defining_ty.implicit_inputs();
let user_arg_index =
self.regioncx.get_user_arg_index_for_region(self.infcx.tcx,
fr)?;
let arg_ty =
self.regioncx.universal_regions().unnormalized_input_tys[implicit_inputs
+ user_arg_index];
let (_, span) =
self.regioncx.get_argument_name_and_span_for_region(self.body,
self.local_names(), user_arg_index);
let highlight =
self.get_argument_hir_ty_for_highlighting(user_arg_index).and_then(|arg_hir_ty|
self.highlight_if_we_can_match_hir_ty(fr, arg_ty,
arg_hir_ty)).unwrap_or_else(||
{
let counter = *self.next_region_name.try_borrow().unwrap();
self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, span,
counter)
});
Some(RegionName {
name: self.synthesize_region_name(),
source: RegionNameSource::AnonRegionFromArgument(highlight),
})
}
}
}#[instrument(level = "trace", skip(self))]
501 fn give_name_if_anonymous_region_appears_in_arguments(
502 &self,
503 fr: RegionVid,
504 ) -> Option<RegionName> {
505 let implicit_inputs = self.regioncx.universal_regions().defining_ty.implicit_inputs();
506 let user_arg_index = self.regioncx.get_user_arg_index_for_region(self.infcx.tcx, fr)?;
507
508 let arg_ty = self.regioncx.universal_regions().unnormalized_input_tys
509 [implicit_inputs + user_arg_index];
510 let (_, span) = self.regioncx.get_argument_name_and_span_for_region(
511 self.body,
512 self.local_names(),
513 user_arg_index,
514 );
515
516 let highlight = self
517 .get_argument_hir_ty_for_highlighting(user_arg_index)
518 .and_then(|arg_hir_ty| self.highlight_if_we_can_match_hir_ty(fr, arg_ty, arg_hir_ty))
519 .unwrap_or_else(|| {
520 let counter = *self.next_region_name.try_borrow().unwrap();
524 self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, span, counter)
525 });
526
527 Some(RegionName {
528 name: self.synthesize_region_name(),
529 source: RegionNameSource::AnonRegionFromArgument(highlight),
530 })
531 }
532
533 fn get_argument_hir_ty_for_highlighting(
534 &self,
535 user_arg_index: usize,
536 ) -> Option<&hir::Ty<'tcx>> {
537 let fn_decl = self.infcx.tcx.hir_fn_decl_by_hir_id(self.mir_hir_id())?;
538 let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(user_arg_index)?;
540 match argument_hir_ty.kind {
541 hir::TyKind::Infer(()) => None,
547
548 _ => Some(argument_hir_ty),
549 }
550 }
551
552 fn highlight_if_we_cannot_match_hir_ty(
564 &self,
565 needle_fr: RegionVid,
566 ty: Ty<'tcx>,
567 span: Span,
568 counter: usize,
569 ) -> RegionNameHighlight {
570 let mut highlight = RegionHighlightMode::default();
571 highlight.highlighting_region_vid(self.infcx.tcx, needle_fr, counter);
572 let type_name =
573 self.infcx.err_ctxt().extract_inference_diagnostics_data(ty.into(), highlight).name;
574
575 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs:575",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(575u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("highlight_if_we_cannot_match_hir_ty: type_name={0:?} needle_fr={1:?}",
type_name, needle_fr) as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(
576 "highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
577 type_name, needle_fr
578 );
579 if type_name.contains(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\'{0}", counter))
})format!("'{counter}")) {
580 RegionNameHighlight::CannotMatchHirTy(span, Symbol::intern(&type_name))
582 } else {
583 RegionNameHighlight::Occluded(span, Symbol::intern(&type_name))
584 }
585 }
586
587 fn highlight_if_we_can_match_hir_ty(
609 &self,
610 needle_fr: RegionVid,
611 ty: Ty<'tcx>,
612 hir_ty: &hir::Ty<'_>,
613 ) -> Option<RegionNameHighlight> {
614 let search_stack: &mut Vec<(Ty<'tcx>, &hir::Ty<'_>)> = &mut ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[(ty, hir_ty)]))vec![(ty, hir_ty)];
615
616 while let Some((ty, hir_ty)) = search_stack.pop() {
617 match (ty.kind(), &hir_ty.kind) {
618 (ty::Ref(region, referent_ty, _), hir::TyKind::Ref(_lifetime, referent_hir_ty)) => {
625 if region.as_var() == needle_fr {
626 let source_map = self.infcx.tcx.sess.source_map();
628 let ampersand_span = source_map.start_point(hir_ty.span);
629
630 return Some(RegionNameHighlight::MatchedHirTy(ampersand_span));
631 }
632
633 search_stack.push((*referent_ty, referent_hir_ty.ty));
635 }
636
637 (ty::Adt(_adt_def, args), hir::TyKind::Path(hir::QPath::Resolved(None, path))) => {
639 match path.res {
640 Res::Def(DefKind::TyAlias, _) => (),
645 _ => {
646 if let Some(last_segment) = path.segments.last()
647 && let Some(highlight) = self.match_adt_and_segment(
648 args,
649 needle_fr,
650 last_segment,
651 search_stack,
652 )
653 {
654 return Some(highlight);
655 }
656 }
657 }
658 }
659
660 (&ty::Tuple(elem_tys), hir::TyKind::Tup(elem_hir_tys)) => {
664 search_stack.extend(iter::zip(elem_tys, *elem_hir_tys));
665 }
666
667 (ty::Slice(elem_ty), hir::TyKind::Slice(elem_hir_ty))
668 | (ty::Array(elem_ty, _), hir::TyKind::Array(elem_hir_ty, _)) => {
669 search_stack.push((*elem_ty, elem_hir_ty));
670 }
671
672 (ty::RawPtr(mut_ty, _), hir::TyKind::Ptr(mut_hir_ty)) => {
673 search_stack.push((*mut_ty, mut_hir_ty.ty));
674 }
675
676 _ => {
677 }
679 }
680 }
681
682 None
683 }
684
685 fn match_adt_and_segment<'hir>(
691 &self,
692 args: GenericArgsRef<'tcx>,
693 needle_fr: RegionVid,
694 last_segment: &'hir hir::PathSegment<'hir>,
695 search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
696 ) -> Option<RegionNameHighlight> {
697 let explicit_args = last_segment.args.as_ref()?;
699 let lifetime =
700 self.try_match_adt_and_generic_args(args, needle_fr, explicit_args, search_stack)?;
701 if lifetime.is_anonymous() {
702 None
703 } else {
704 Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime.ident.span))
705 }
706 }
707
708 fn try_match_adt_and_generic_args<'hir>(
714 &self,
715 args: GenericArgsRef<'tcx>,
716 needle_fr: RegionVid,
717 hir_args: &'hir hir::GenericArgs<'hir>,
718 search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
719 ) -> Option<&'hir hir::Lifetime> {
720 for (arg, hir_arg) in iter::zip(args, hir_args.args) {
721 match (arg.kind(), hir_arg) {
722 (GenericArgKind::Lifetime(r), hir::GenericArg::Lifetime(lt)) => {
723 if r.as_var() == needle_fr {
724 return Some(lt);
725 }
726 }
727
728 (GenericArgKind::Type(ty), hir::GenericArg::Type(hir_ty)) => {
729 search_stack.push((ty, hir_ty.as_unambig_ty()));
730 }
731
732 (GenericArgKind::Const(_ct), hir::GenericArg::Const(_hir_ct)) => {
733 }
736
737 (
738 GenericArgKind::Lifetime(_)
739 | GenericArgKind::Type(_)
740 | GenericArgKind::Const(_),
741 _,
742 ) => {
743 self.dcx().span_delayed_bug(
744 hir_arg.span(),
745 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("unmatched arg and hir arg: found {0:?} vs {1:?}",
arg, hir_arg))
})format!("unmatched arg and hir arg: found {arg:?} vs {hir_arg:?}"),
746 );
747 }
748 }
749 }
750
751 None
752 }
753
754 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::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("give_name_if_anonymous_region_appears_in_upvars",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(762u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("fr")
}> =
::tracing::__macro_support::FieldName::new("fr");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&fr)
as &dyn ::tracing::field::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<RegionName> = loop {};
return __tracing_attr_fake_return;
}
{
let upvar_index =
self.regioncx.get_upvar_index_for_region(self.infcx.tcx, fr)?;
if let Some(region_name) =
self.give_name_if_we_can_match_upvar_args(fr, upvar_index) {
return Some(region_name);
}
let (upvar_name, upvar_span) =
self.regioncx.get_upvar_name_and_span_for_region(self.infcx.tcx,
self.upvars, upvar_index);
let region_name = self.synthesize_region_name();
Some(RegionName {
name: region_name,
source: RegionNameSource::AnonRegionFromUpvar(upvar_span,
upvar_name),
})
}
}
}#[instrument(level = "trace", skip(self))]
763 fn give_name_if_anonymous_region_appears_in_upvars(&self, fr: RegionVid) -> Option<RegionName> {
764 let upvar_index = self.regioncx.get_upvar_index_for_region(self.infcx.tcx, fr)?;
765
766 if let Some(region_name) = self.give_name_if_we_can_match_upvar_args(fr, upvar_index) {
769 return Some(region_name);
770 }
771
772 let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
773 self.infcx.tcx,
774 self.upvars,
775 upvar_index,
776 );
777 let region_name = self.synthesize_region_name();
778
779 Some(RegionName {
780 name: region_name,
781 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name),
782 })
783 }
784
785 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::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("give_name_if_anonymous_region_appears_in_output",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(789u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("fr")
}> =
::tracing::__macro_support::FieldName::new("fr");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&fr)
as &dyn ::tracing::field::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<RegionName> = loop {};
return __tracing_attr_fake_return;
}
{
let tcx = self.infcx.tcx;
let mut return_ty =
self.regioncx.universal_regions().unnormalized_output_ty;
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs:794",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(794u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("give_name_if_anonymous_region_appears_in_output: return_ty = {0:?}",
return_ty) as &dyn ::tracing::field::Value))])
});
} else { ; }
};
if !tcx.any_free_region_meets(&return_ty, |r| r.as_var() == fr) {
return None;
}
if let ty::Coroutine(_, args) = return_ty.kind() {
return_ty = args.as_coroutine().return_ty();
}
let mir_hir_id = self.mir_hir_id();
let (return_span, mir_description, hir_ty) =
match tcx.hir_node(mir_hir_id) {
hir::Node::Expr(&hir::Expr {
kind: hir::ExprKind::Closure(&hir::Closure {
fn_decl, kind, fn_decl_span, .. }), .. }) => {
let (mut span, mut hir_ty) =
match fn_decl.output {
hir::FnRetTy::DefaultReturn(_) => {
(tcx.sess.source_map().end_point(fn_decl_span), None)
}
hir::FnRetTy::Return(hir_ty) =>
(fn_decl.output.span(), Some(hir_ty)),
};
let mir_description =
match kind {
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async,
hir::CoroutineSource::Block)) => " of async block",
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async,
hir::CoroutineSource::Closure)) |
hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async)
=> {
" of async closure"
}
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async,
hir::CoroutineSource::Fn)) => {
let parent_item =
tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
let output =
&parent_item.fn_decl().expect("coroutine lowered from async fn should be in fn").output;
span = output.span();
if let hir::FnRetTy::Return(ret) = output {
hir_ty = Some(self.get_future_inner_return_ty(ret));
}
" of async function"
}
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Block)) => " of gen block",
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Closure)) |
hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Gen)
=> {
" of gen closure"
}
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen,
hir::CoroutineSource::Fn)) => {
let parent_item =
tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
let output =
&parent_item.fn_decl().expect("coroutine lowered from gen fn should be in fn").output;
span = output.span();
" of gen function"
}
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Block)) => " of async gen block",
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Closure)) |
hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::AsyncGen)
=> {
" of async gen closure"
}
hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen,
hir::CoroutineSource::Fn)) => {
let parent_item =
tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
let output =
&parent_item.fn_decl().expect("coroutine lowered from async gen fn should be in fn").output;
span = output.span();
" of async gen function"
}
hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine(_))
=> {
" of coroutine"
}
hir::ClosureKind::Closure => " of closure",
};
(span, mir_description, hir_ty)
}
node =>
match node.fn_decl() {
Some(fn_decl) => {
let hir_ty =
match fn_decl.output {
hir::FnRetTy::DefaultReturn(_) => None,
hir::FnRetTy::Return(ty) => Some(ty),
};
(fn_decl.output.span(), "", hir_ty)
}
None => (self.body.span, "", None),
},
};
let highlight =
hir_ty.and_then(|hir_ty|
self.highlight_if_we_can_match_hir_ty(fr, return_ty,
hir_ty)).unwrap_or_else(||
{
let counter = *self.next_region_name.try_borrow().unwrap();
self.highlight_if_we_cannot_match_hir_ty(fr, return_ty,
return_span, counter)
});
Some(RegionName {
name: self.synthesize_region_name(),
source: RegionNameSource::AnonRegionFromOutput(highlight,
mir_description),
})
}
}
}#[instrument(level = "trace", skip(self))]
790 fn give_name_if_anonymous_region_appears_in_output(&self, fr: RegionVid) -> Option<RegionName> {
791 let tcx = self.infcx.tcx;
792
793 let mut return_ty = self.regioncx.universal_regions().unnormalized_output_ty;
794 debug!("give_name_if_anonymous_region_appears_in_output: return_ty = {:?}", return_ty);
795 if !tcx.any_free_region_meets(&return_ty, |r| r.as_var() == fr) {
796 return None;
797 }
798
799 if let ty::Coroutine(_, args) = return_ty.kind() {
800 return_ty = args.as_coroutine().return_ty();
803 }
804
805 let mir_hir_id = self.mir_hir_id();
806
807 let (return_span, mir_description, hir_ty) = match tcx.hir_node(mir_hir_id) {
808 hir::Node::Expr(&hir::Expr {
809 kind: hir::ExprKind::Closure(&hir::Closure { fn_decl, kind, fn_decl_span, .. }),
810 ..
811 }) => {
812 let (mut span, mut hir_ty) = match fn_decl.output {
813 hir::FnRetTy::DefaultReturn(_) => {
814 (tcx.sess.source_map().end_point(fn_decl_span), None)
815 }
816 hir::FnRetTy::Return(hir_ty) => (fn_decl.output.span(), Some(hir_ty)),
817 };
818 let mir_description = match kind {
819 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
820 hir::CoroutineDesugaring::Async,
821 hir::CoroutineSource::Block,
822 )) => " of async block",
823
824 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
825 hir::CoroutineDesugaring::Async,
826 hir::CoroutineSource::Closure,
827 ))
828 | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) => {
829 " of async closure"
830 }
831
832 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
833 hir::CoroutineDesugaring::Async,
834 hir::CoroutineSource::Fn,
835 )) => {
836 let parent_item =
837 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
838 let output = &parent_item
839 .fn_decl()
840 .expect("coroutine lowered from async fn should be in fn")
841 .output;
842 span = output.span();
843 if let hir::FnRetTy::Return(ret) = output {
844 hir_ty = Some(self.get_future_inner_return_ty(ret));
845 }
846 " of async function"
847 }
848
849 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
850 hir::CoroutineDesugaring::Gen,
851 hir::CoroutineSource::Block,
852 )) => " of gen block",
853
854 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
855 hir::CoroutineDesugaring::Gen,
856 hir::CoroutineSource::Closure,
857 ))
858 | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Gen) => {
859 " of gen closure"
860 }
861
862 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
863 hir::CoroutineDesugaring::Gen,
864 hir::CoroutineSource::Fn,
865 )) => {
866 let parent_item =
867 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
868 let output = &parent_item
869 .fn_decl()
870 .expect("coroutine lowered from gen fn should be in fn")
871 .output;
872 span = output.span();
873 " of gen function"
874 }
875
876 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
877 hir::CoroutineDesugaring::AsyncGen,
878 hir::CoroutineSource::Block,
879 )) => " of async gen block",
880
881 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
882 hir::CoroutineDesugaring::AsyncGen,
883 hir::CoroutineSource::Closure,
884 ))
885 | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::AsyncGen) => {
886 " of async gen closure"
887 }
888
889 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
890 hir::CoroutineDesugaring::AsyncGen,
891 hir::CoroutineSource::Fn,
892 )) => {
893 let parent_item =
894 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
895 let output = &parent_item
896 .fn_decl()
897 .expect("coroutine lowered from async gen fn should be in fn")
898 .output;
899 span = output.span();
900 " of async gen function"
901 }
902
903 hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine(_)) => {
904 " of coroutine"
905 }
906 hir::ClosureKind::Closure => " of closure",
907 };
908 (span, mir_description, hir_ty)
909 }
910 node => match node.fn_decl() {
911 Some(fn_decl) => {
912 let hir_ty = match fn_decl.output {
913 hir::FnRetTy::DefaultReturn(_) => None,
914 hir::FnRetTy::Return(ty) => Some(ty),
915 };
916 (fn_decl.output.span(), "", hir_ty)
917 }
918 None => (self.body.span, "", None),
919 },
920 };
921
922 let highlight = hir_ty
923 .and_then(|hir_ty| self.highlight_if_we_can_match_hir_ty(fr, return_ty, hir_ty))
924 .unwrap_or_else(|| {
925 let counter = *self.next_region_name.try_borrow().unwrap();
929 self.highlight_if_we_cannot_match_hir_ty(fr, return_ty, return_span, counter)
930 });
931
932 Some(RegionName {
933 name: self.synthesize_region_name(),
934 source: RegionNameSource::AnonRegionFromOutput(highlight, mir_description),
935 })
936 }
937
938 fn get_future_inner_return_ty(&self, hir_ty: &'tcx hir::Ty<'tcx>) -> &'tcx hir::Ty<'tcx> {
952 let hir::TyKind::OpaqueDef(opaque_ty) = hir_ty.kind else {
953 bug_impl(Some(hir_ty.span),
format_args!("lowered return type of async fn is not OpaqueDef: {0:?}",
hir_ty), Location::caller());span_bug!(
954 hir_ty.span,
955 "lowered return type of async fn is not OpaqueDef: {:?}",
956 hir_ty
957 );
958 };
959 if let hir::OpaqueTy { bounds: [hir::GenericBound::Trait(trait_ref)], .. } = opaque_ty
960 && let Some(segment) = trait_ref.trait_ref.path.segments.last()
961 && let Some(args) = segment.args
962 && let [constraint] = args.constraints
963 && constraint.ident.name == sym::Output
964 && let Some(ty) = constraint.ty()
965 {
966 ty
967 } else {
968 bug_impl(Some(hir_ty.span),
format_args!("bounds from lowered return type of async fn did not match expected format: {0:?}",
opaque_ty), Location::caller());span_bug!(
969 hir_ty.span,
970 "bounds from lowered return type of async fn did not match expected format: {opaque_ty:?}",
971 );
972 }
973 }
974
975 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::TRACE <=
::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("give_name_if_anonymous_region_appears_in_yield_ty",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::TRACE,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(975u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("fr")
}> =
::tracing::__macro_support::FieldName::new("fr");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::TRACE <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::TRACE <=
::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};
meta.fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&fr)
as &dyn ::tracing::field::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<RegionName> = loop {};
return __tracing_attr_fake_return;
}
{
let yield_ty = self.regioncx.universal_regions().yield_ty?;
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs:983",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(983u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {0:?}",
yield_ty) as &dyn ::tracing::field::Value))])
});
} else { ; }
};
let tcx = self.infcx.tcx;
if !tcx.any_free_region_meets(&yield_ty, |r| r.as_var() == fr) {
return None;
}
let mut highlight = RegionHighlightMode::default();
highlight.highlighting_region_vid(tcx, fr,
*self.next_region_name.try_borrow().unwrap());
let type_name =
self.infcx.err_ctxt().extract_inference_diagnostics_data(yield_ty.into(),
highlight).name;
let yield_span =
match tcx.hir_node(self.mir_hir_id()) {
hir::Node::Expr(&hir::Expr {
kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span,
.. }), .. }) =>
tcx.sess.source_map().end_point(fn_decl_span),
_ => self.body.span,
};
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs:1007",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/923c95cdf5ba65cea505aa2ea829f578e1506ed8/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(1007u32),
::tracing_core::__macro_support::Option::Some("rustc_borrowck::diagnostics::region_name"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
__CALLSITE.metadata().fields().value_set_all(&[(::tracing::__macro_support::Option::Some(&format_args!("give_name_if_anonymous_region_appears_in_yield_ty: type_name = {0:?}, yield_span = {1:?}",
yield_span, type_name) as &dyn ::tracing::field::Value))])
});
} else { ; }
};
Some(RegionName {
name: self.synthesize_region_name(),
source: RegionNameSource::AnonRegionFromYieldTy(yield_span,
Symbol::intern(&type_name)),
})
}
}
}#[instrument(level = "trace", skip(self))]
976 fn give_name_if_anonymous_region_appears_in_yield_ty(
977 &self,
978 fr: RegionVid,
979 ) -> Option<RegionName> {
980 let yield_ty = self.regioncx.universal_regions().yield_ty?;
983 debug!("give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {:?}", yield_ty);
984
985 let tcx = self.infcx.tcx;
986
987 if !tcx.any_free_region_meets(&yield_ty, |r| r.as_var() == fr) {
988 return None;
989 }
990
991 let mut highlight = RegionHighlightMode::default();
992 highlight.highlighting_region_vid(tcx, fr, *self.next_region_name.try_borrow().unwrap());
993 let type_name = self
994 .infcx
995 .err_ctxt()
996 .extract_inference_diagnostics_data(yield_ty.into(), highlight)
997 .name;
998
999 let yield_span = match tcx.hir_node(self.mir_hir_id()) {
1000 hir::Node::Expr(&hir::Expr {
1001 kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }),
1002 ..
1003 }) => tcx.sess.source_map().end_point(fn_decl_span),
1004 _ => self.body.span,
1005 };
1006
1007 debug!(
1008 "give_name_if_anonymous_region_appears_in_yield_ty: \
1009 type_name = {:?}, yield_span = {:?}",
1010 yield_span, type_name,
1011 );
1012
1013 Some(RegionName {
1014 name: self.synthesize_region_name(),
1015 source: RegionNameSource::AnonRegionFromYieldTy(yield_span, Symbol::intern(&type_name)),
1016 })
1017 }
1018
1019 fn give_name_if_anonymous_region_appears_in_impl_signature(
1020 &self,
1021 fr: RegionVid,
1022 ) -> Option<RegionName> {
1023 let ty::ReEarlyParam(region) = self.regioncx.to_error_region(fr)?.kind() else {
1024 return None;
1025 };
1026 if region.is_named() {
1027 return None;
1028 };
1029
1030 let tcx = self.infcx.tcx;
1031 let region_def = tcx.generics_of(self.mir_def_id()).region_param(region, tcx).def_id;
1032 let region_parent = tcx.parent(region_def);
1033 let DefKind::Impl { .. } = tcx.def_kind(region_parent) else {
1034 return None;
1035 };
1036
1037 let found = tcx.any_free_region_meets(
1038 &tcx.type_of(region_parent).instantiate_identity().skip_norm_wip(),
1039 |r| r.kind() == ty::ReEarlyParam(region),
1040 );
1041
1042 Some(RegionName {
1043 name: self.synthesize_region_name(),
1044 source: RegionNameSource::AnonRegionFromImplSignature(
1045 tcx.def_span(region_def),
1046 if found { "self type" } else { "header" },
1050 ),
1051 })
1052 }
1053
1054 fn give_name_if_anonymous_region_appears_in_arg_position_impl_trait(
1055 &self,
1056 fr: RegionVid,
1057 ) -> Option<RegionName> {
1058 let ty::ReEarlyParam(region) = self.regioncx.to_error_region(fr)?.kind() else {
1059 return None;
1060 };
1061 if region.is_named() {
1062 return None;
1063 };
1064
1065 let clauses: Vec<_> = self
1066 .infcx
1067 .tcx
1068 .clauses_of(self.body.source.def_id())
1069 .instantiate_identity(self.infcx.tcx)
1070 .clauses
1071 .into_iter()
1072 .map(Unnormalized::skip_norm_wip)
1073 .collect();
1074
1075 if let Some(upvar_index) = self
1076 .regioncx
1077 .universal_regions()
1078 .defining_ty
1079 .upvar_tys()
1080 .iter()
1081 .position(|ty| self.any_param_clause_mentions(&clauses, ty, region))
1082 {
1083 let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
1084 self.infcx.tcx,
1085 self.upvars,
1086 upvar_index,
1087 );
1088 let region_name = self.synthesize_region_name();
1089
1090 Some(RegionName {
1091 name: region_name,
1092 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name),
1093 })
1094 } else if let Some(arg_index) = self
1095 .regioncx
1096 .universal_regions()
1097 .unnormalized_input_tys
1098 .iter()
1099 .position(|ty| self.any_param_clause_mentions(&clauses, *ty, region))
1100 {
1101 let (arg_name, arg_span) = self.regioncx.get_argument_name_and_span_for_region(
1102 self.body,
1103 self.local_names(),
1104 arg_index,
1105 );
1106 let region_name = self.synthesize_region_name();
1107
1108 Some(RegionName {
1109 name: region_name,
1110 source: RegionNameSource::AnonRegionFromArgument(
1111 RegionNameHighlight::CannotMatchHirTy(arg_span, arg_name?),
1112 ),
1113 })
1114 } else {
1115 None
1116 }
1117 }
1118
1119 fn any_param_clause_mentions(
1120 &self,
1121 clauses: &[ty::Clause<'tcx>],
1122 ty: Ty<'tcx>,
1123 region: ty::EarlyParamRegion,
1124 ) -> bool {
1125 let tcx = self.infcx.tcx;
1126 ty.walk().any(|arg| {
1127 if let ty::GenericArgKind::Type(ty) = arg.kind()
1128 && let ty::Param(_) = ty.kind()
1129 {
1130 clauses.iter().any(|pred| {
1131 match pred.kind().skip_binder() {
1132 ty::ClauseKind::Trait(data) if data.self_ty() == ty => {}
1133 ty::ClauseKind::Projection(data)
1134 if data.projection_term.self_ty() == ty => {}
1135 _ => return false,
1136 }
1137 tcx.any_free_region_meets(pred, |r| r.kind() == ty::ReEarlyParam(region))
1138 })
1139 } else {
1140 false
1141 }
1142 })
1143 }
1144}