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, GenericArgs, 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]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"RegionName name source", &self.name, &&self.source)
}
}Debug, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for RegionName {
}
#[automatically_derived]impl ::core::clone::Clone for RegionName {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
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]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::NamedEarlyParamRegion(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"NamedEarlyParamRegion", &__self_0),
Self::NamedLateParamRegion(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"NamedLateParamRegion", &__self_0),
Self::Static => ::core::fmt::Formatter::write_str(f, "Static"),
Self::SynthesizedFreeEnvRegion(__self_0, __self_1) =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"SynthesizedFreeEnvRegion", __self_0, &__self_1),
Self::AnonRegionFromArgument(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"AnonRegionFromArgument", &__self_0),
Self::AnonRegionFromUpvar(__self_0, __self_1) =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"AnonRegionFromUpvar", __self_0, &__self_1),
Self::AnonRegionFromOutput(__self_0, __self_1) =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"AnonRegionFromOutput", __self_0, &__self_1),
Self::AnonRegionFromYieldTy(__self_0, __self_1) =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"AnonRegionFromYieldTy", __self_0, &__self_1),
Self::AnonRegionFromAsyncFn(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"AnonRegionFromAsyncFn", &__self_0),
Self::AnonRegionFromImplSignature(__self_0, __self_1) =>
::core::fmt::Formatter::debug_struct_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]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
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]
#[rustc_trivial_field_reads]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::MatchedHirTy(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"MatchedHirTy", &__self_0),
Self::MatchedAdtAndSegment(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"MatchedAdtAndSegment", &__self_0),
Self::CannotMatchHirTy(__self_0, __self_1, __self_2) =>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"CannotMatchHirTy", __self_0, __self_1, &__self_2),
Self::Occluded(__self_0, __self_1, __self_2) =>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"Occluded", __self_0, __self_1, &__self_2),
}
}
}Debug, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for RegionNameHighlight {
}
#[automatically_derived]impl ::core::clone::Clone for RegionNameHighlight {
#[inline]
#[rustc_trivial_field_reads]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<Span>;
let _: ::core::clone::AssertParamIsClone<Symbol>;
let _: ::core::clone::AssertParamIsClone<Option<Span>>;
let _: ::core::clone::AssertParamIsClone<Option<Span>>;
*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, Option<Span>),
65 Occluded(Span, Symbol, Option<Span>),
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(&self, diag: &mut Diag<'_>) {
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 lt_span,
124 )) => {
125 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("has type `{0}`", type_name))
})format!("has type `{type_name}`"));
126 if let Some(span) = lt_span {
127 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` introduced here",
self))
})format!("lifetime `{self}` introduced here"));
128 }
129 }
130 RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::MatchedHirTy(span))
131 | RegionNameSource::AnonRegionFromOutput(RegionNameHighlight::MatchedHirTy(span), _)
132 | RegionNameSource::AnonRegionFromAsyncFn(span) => {
133 diag.span_label(
134 *span,
135 ::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}`"),
136 );
137 }
138 RegionNameSource::AnonRegionFromArgument(
139 RegionNameHighlight::MatchedAdtAndSegment(span),
140 )
141 | RegionNameSource::AnonRegionFromOutput(
142 RegionNameHighlight::MatchedAdtAndSegment(span),
143 _,
144 ) => {
145 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}`"));
146 }
147 RegionNameSource::AnonRegionFromArgument(RegionNameHighlight::Occluded(
148 span,
149 type_name,
150 lt_span,
151 )) => {
152 diag.span_label(
153 *span,
154 ::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}`"),
155 );
156 if let Some(span) = lt_span {
157 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` introduced here",
self))
})format!("lifetime `{self}` introduced here"));
158 }
159 }
160 RegionNameSource::AnonRegionFromOutput(
161 RegionNameHighlight::Occluded(span, type_name, lt_span),
162 mir_description,
163 ) => {
164 diag.span_label(
165 *span,
166 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("return type{0} `{1}` contains a lifetime `{2}`",
mir_description, type_name, self))
})format!(
167 "return type{mir_description} `{type_name}` contains a lifetime `{self}`"
168 ),
169 );
170 if let Some(span) = lt_span {
171 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` introduced here",
self))
})format!("lifetime `{self}` introduced here"));
172 }
173 }
174 RegionNameSource::AnonRegionFromUpvar(span, upvar_name) => {
175 diag.span_label(
176 *span,
177 ::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}`"),
178 );
179 }
180 RegionNameSource::AnonRegionFromOutput(
181 RegionNameHighlight::CannotMatchHirTy(span, type_name, lt_span),
182 mir_description,
183 ) => {
184 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}`"));
185 if let Some(span) = lt_span {
186 diag.span_label(*span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("lifetime `{0}` introduced here",
self))
})format!("lifetime `{self}` introduced here"));
187 }
188 }
189 RegionNameSource::AnonRegionFromYieldTy(span, type_name) => {
190 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}"));
191 }
192 RegionNameSource::AnonRegionFromImplSignature(span, location) => {
193 diag.span_label(
194 *span,
195 ::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}"),
196 );
197 }
198 RegionNameSource::Static => {}
199 }
200 }
201}
202
203impl Display for RegionName {
204 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
205 f.write_fmt(format_args!("{0}", self.name))write!(f, "{}", self.name)
206 }
207}
208
209impl rustc_errors::IntoDiagArg for RegionName {
210 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
211 self.to_string().into_diag_arg(path)
212 }
213}
214
215impl<'tcx> MirBorrowckCtxt<'_, '_, 'tcx> {
216 pub(crate) fn mir_def_id(&self) -> hir::def_id::LocalDefId {
217 self.body.source.def_id().expect_local()
218 }
219
220 pub(crate) fn mir_hir_id(&self) -> hir::HirId {
221 self.infcx.tcx.local_def_id_to_hir_id(self.mir_def_id())
222 }
223
224 pub(crate) fn synthesize_region_name(&self) -> Symbol {
229 let c = self.next_region_name.replace_with(|counter| *counter + 1);
230 Symbol::intern(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\'{0:?}", c))
})format!("'{c:?}"))
231 }
232
233 pub(crate) fn give_region_a_name(&self, fr: RegionVid) -> Option<RegionName> {
261 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs:261",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(261u32),
::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!(
262 "give_region_a_name(fr={:?}, counter={:?})",
263 fr,
264 self.next_region_name.try_borrow().unwrap()
265 );
266
267 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));
268
269 match self.region_names.borrow_mut().entry(fr) {
270 IndexEntry::Occupied(precomputed_name) => Some(*precomputed_name.get()),
271 IndexEntry::Vacant(slot) => {
272 let new_name = self
273 .give_name_from_error_region(fr)
274 .or_else(|| self.give_name_if_anonymous_region_appears_in_arguments(fr))
275 .or_else(|| self.give_name_if_anonymous_region_appears_in_upvars(fr))
276 .or_else(|| self.give_name_if_anonymous_region_appears_in_output(fr))
277 .or_else(|| self.give_name_if_anonymous_region_appears_in_yield_ty(fr))
278 .or_else(|| self.give_name_if_anonymous_region_appears_in_impl_signature(fr))
279 .or_else(|| {
280 self.give_name_if_anonymous_region_appears_in_arg_position_impl_trait(fr)
281 });
282
283 if let Some(new_name) = new_name {
284 slot.insert(new_name);
285 }
286 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs:286",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(286u32),
::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);
287
288 new_name
289 }
290 }
291 }
292
293 {}
#[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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(297u32),
::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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs:303",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(303u32),
::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())
}
_ => {
::rustc_span::macros::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 {
::rustc_span::macros::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(_, _) =>
::rustc_span::macros::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))]
298 fn give_name_from_error_region(&self, fr: RegionVid) -> Option<RegionName> {
299 let error_region = self.regioncx.to_error_region(fr)?;
300
301 let tcx = self.infcx.tcx;
302
303 debug!("give_region_a_name: error_region = {:?}", error_region);
304 match error_region.kind() {
305 ty::ReEarlyParam(ebr) => ebr.is_named().then(|| {
306 let def_id = tcx.generics_of(self.mir_def_id()).region_param(ebr, tcx).def_id;
307 let span = tcx.hir_span_if_local(def_id).unwrap_or(DUMMY_SP);
308 RegionName { name: ebr.name, source: RegionNameSource::NamedEarlyParamRegion(span) }
309 }),
310
311 ty::ReStatic => {
312 Some(RegionName { name: kw::StaticLifetime, source: RegionNameSource::Static })
313 }
314
315 ty::ReLateParam(late_param) => match late_param.kind {
316 ty::LateParamRegionKind::Named(region_def_id) => {
317 let span = tcx.hir_span_if_local(region_def_id).unwrap_or(DUMMY_SP);
319
320 if let Some(name) = late_param.kind.get_name(tcx) {
321 Some(RegionName {
323 name,
324 source: RegionNameSource::NamedLateParamRegion(span),
325 })
326 } else if tcx.asyncness(self.mir_hir_id().owner).is_async() {
327 let name = self.synthesize_region_name();
333 Some(RegionName {
334 name,
335 source: RegionNameSource::AnonRegionFromAsyncFn(span),
336 })
337 } else {
338 None
339 }
340 }
341
342 ty::LateParamRegionKind::ClosureEnv => {
343 let def_ty = self.regioncx.universal_regions().defining_ty;
344
345 let (is_lending_coroutine_closure, closure_kind) = match def_ty {
346 DefiningTy::Closure(_, args) => (false, args.as_closure().kind()),
347 DefiningTy::CoroutineClosure(_, args) => {
348 let args = args.as_coroutine_closure();
349 (
350 !args.tupled_upvars_ty().is_ty_var() && args.has_self_borrows(),
351 args.kind(),
352 )
353 }
354 _ => {
355 bug!("BrEnv outside of closure.");
357 }
358 };
359 let hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }) =
360 tcx.hir_expect_expr(self.mir_hir_id()).kind
361 else {
362 bug!("Closure is not defined by a closure expr");
363 };
364 let region_name = self.synthesize_region_name();
365 let closure_trait = match (is_lending_coroutine_closure, closure_kind) {
366 (false, kind) => kind.as_str(),
367 (true, ty::ClosureKind::Fn) => "AsyncFn",
368 (true, ty::ClosureKind::FnMut) => "AsyncFnMut",
369 (true, ty::ClosureKind::FnOnce) => "AsyncFnOnce",
370 };
371
372 Some(RegionName {
373 name: region_name,
374 source: RegionNameSource::SynthesizedFreeEnvRegion(
375 fn_decl_span,
376 closure_trait,
377 ),
378 })
379 }
380
381 ty::LateParamRegionKind::Anon(_) => None,
382 ty::LateParamRegionKind::NamedAnon(_, _) => bug!("only used for pretty printing"),
383 },
384
385 ty::ReBound(..)
386 | ty::ReVar(..)
387 | ty::RePlaceholder(..)
388 | ty::ReErased
389 | ty::ReError(_) => None,
390 }
391 }
392
393 {}
#[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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(403u32),
::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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs:446",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(446u32),
::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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs:463",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(463u32),
::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))]
404 fn give_name_if_we_can_match_upvar_args(
405 &self,
406 fr: RegionVid,
407 upvar_index: usize,
408 ) -> Option<RegionName> {
409 let tcx = self.infcx.tcx;
410 let defining_ty = self.regioncx.universal_regions().defining_ty;
411
412 let closure_def_id = match defining_ty {
413 DefiningTy::Closure(def_id, _)
414 | DefiningTy::Coroutine(def_id, _)
415 | DefiningTy::CoroutineClosure(def_id, _) => def_id,
416 _ => return None,
417 };
418
419 let parent_def_id = tcx.parent(closure_def_id);
420
421 if !matches!(tcx.def_kind(parent_def_id), DefKind::Fn | DefKind::AssocFn) {
423 return None;
424 }
425
426 let captured_place = self.upvars.get(upvar_index)?;
431 let upvar_hir_id = captured_place.get_root_variable();
432 let parent_local_def_id = parent_def_id.as_local()?;
433 let parent_body = tcx.hir_body_owned_by(parent_local_def_id);
434 let param_index =
435 parent_body.params.iter().position(|param| param.pat.hir_id == upvar_hir_id)?;
436
437 let parent_fn_sig = tcx.fn_sig(parent_def_id).instantiate_identity().skip_norm_wip();
440 let liberated_sig = tcx.liberate_late_bound_regions(parent_def_id, parent_fn_sig);
441 let parent_param_ty = *liberated_sig.inputs().get(param_index)?;
442
443 let upvar_nll_ty = *defining_ty.upvar_tys().get(upvar_index)?;
445
446 debug!(
447 "give_name_if_we_can_match_upvar_args: parent_param_ty={:?}, upvar_nll_ty={:?}",
448 parent_param_ty, upvar_nll_ty
449 );
450
451 let mut parent_regions = vec![];
457 tcx.for_each_free_region(&parent_param_ty, |r| parent_regions.push(r));
458
459 let mut nll_regions = vec![];
460 tcx.for_each_free_region(&upvar_nll_ty, |r| nll_regions.push(r));
461
462 if parent_regions.len() != nll_regions.len() {
463 debug!(
464 "give_name_if_we_can_match_upvar_args: region count mismatch ({} vs {})",
465 parent_regions.len(),
466 nll_regions.len()
467 );
468 return None;
469 }
470
471 for (parent_r, nll_r) in iter::zip(&parent_regions, &nll_regions) {
472 if nll_r.as_var() == fr {
473 match parent_r.kind() {
474 ty::ReLateParam(late_param) => {
475 if let Some(name) = late_param.kind.get_name(tcx) {
476 let span = late_param
477 .kind
478 .get_id()
479 .and_then(|id| tcx.hir_span_if_local(id))
480 .unwrap_or(DUMMY_SP);
481 return Some(RegionName {
482 name,
483 source: RegionNameSource::NamedLateParamRegion(span),
484 });
485 }
486 }
487 ty::ReEarlyParam(ebr) if ebr.is_named() => {
488 let def_id = tcx.generics_of(parent_def_id).region_param(ebr, tcx).def_id;
489 let span = tcx.hir_span_if_local(def_id).unwrap_or(DUMMY_SP);
490 return Some(RegionName {
491 name: ebr.name,
492 source: RegionNameSource::NamedEarlyParamRegion(span),
493 });
494 }
495 _ => {}
496 }
497 }
498 }
499
500 None
501 }
502
503 {}
#[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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(511u32),
::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))]
512 fn give_name_if_anonymous_region_appears_in_arguments(
513 &self,
514 fr: RegionVid,
515 ) -> Option<RegionName> {
516 let implicit_inputs = self.regioncx.universal_regions().defining_ty.implicit_inputs();
517 let user_arg_index = self.regioncx.get_user_arg_index_for_region(self.infcx.tcx, fr)?;
518
519 let arg_ty = self.regioncx.universal_regions().unnormalized_input_tys
520 [implicit_inputs + user_arg_index];
521 let (_, span) = self.regioncx.get_argument_name_and_span_for_region(
522 self.body,
523 self.local_names(),
524 user_arg_index,
525 );
526
527 let highlight = self
528 .get_argument_hir_ty_for_highlighting(user_arg_index)
529 .and_then(|arg_hir_ty| self.highlight_if_we_can_match_hir_ty(fr, arg_ty, arg_hir_ty))
530 .unwrap_or_else(|| {
531 let counter = *self.next_region_name.try_borrow().unwrap();
535 self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, span, counter)
536 });
537
538 Some(RegionName {
539 name: self.synthesize_region_name(),
540 source: RegionNameSource::AnonRegionFromArgument(highlight),
541 })
542 }
543
544 fn get_argument_hir_ty_for_highlighting(
545 &self,
546 user_arg_index: usize,
547 ) -> Option<&hir::Ty<'tcx>> {
548 let fn_decl = self.infcx.tcx.hir_fn_decl_by_hir_id(self.mir_hir_id())?;
549 let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(user_arg_index)?;
551 match argument_hir_ty.kind {
552 hir::TyKind::Infer(()) => None,
558
559 _ => Some(argument_hir_ty),
560 }
561 }
562
563 fn highlight_if_we_cannot_match_hir_ty(
575 &self,
576 needle_fr: RegionVid,
577 ty: Ty<'tcx>,
578 span: Span,
579 counter: usize,
580 ) -> RegionNameHighlight {
581 let mut highlight = RegionHighlightMode::default();
582 highlight.highlighting_region_vid(self.infcx.tcx, needle_fr, counter);
583 let type_name =
584 self.infcx.err_ctxt().extract_inference_diagnostics_data(ty.into(), highlight).name;
585
586 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs:586",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(586u32),
::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!(
587 "highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
588 type_name, needle_fr
589 );
590 let lt_span = if let Some(needle) = self.regioncx.to_error_region(needle_fr)
591 && let Some(def_id) = needle.opt_param_def_id(self.infcx.tcx, self.mir_def_id().into())
592 {
593 Some(self.infcx.tcx.def_span(def_id))
594 } else {
595 None
596 };
597 if type_name.contains(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\'{0}", counter))
})format!("'{counter}")) {
598 RegionNameHighlight::CannotMatchHirTy(span, Symbol::intern(&type_name), lt_span)
600 } else {
601 RegionNameHighlight::Occluded(span, Symbol::intern(&type_name), lt_span)
602 }
603 }
604
605 fn highlight_if_we_can_match_hir_ty(
627 &self,
628 needle_fr: RegionVid,
629 ty: Ty<'tcx>,
630 hir_ty: &hir::Ty<'_>,
631 ) -> Option<RegionNameHighlight> {
632 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)];
633
634 while let Some((ty, hir_ty)) = search_stack.pop() {
635 match (ty.kind(), &hir_ty.kind) {
636 (
643 ty::Ref(region, referent_ty, _),
644 hir::TyKind::Ref(_lifetime, referent_hir_ty, _),
645 ) => {
646 if region.as_var() == needle_fr {
647 let source_map = self.infcx.tcx.sess.source_map();
649 let ampersand_span = source_map.start_point(hir_ty.span);
650
651 return Some(RegionNameHighlight::MatchedHirTy(ampersand_span));
652 }
653
654 search_stack.push((*referent_ty, referent_hir_ty));
656 }
657
658 (ty::Adt(_adt_def, args), hir::TyKind::Path(hir::QPath::Resolved(None, path))) => {
660 match path.res {
661 Res::Def(DefKind::TyAlias, _) => (),
666 _ => {
667 if let Some(last_segment) = path.segments.last()
668 && let Some(highlight) = self.match_adt_and_segment(
669 *args,
670 needle_fr,
671 last_segment,
672 search_stack,
673 )
674 {
675 return Some(highlight);
676 }
677 }
678 }
679 }
680
681 (&ty::Tuple(elem_tys), hir::TyKind::Tup(elem_hir_tys)) => {
685 search_stack.extend(iter::zip(elem_tys, *elem_hir_tys));
686 }
687
688 (ty::Slice(elem_ty), hir::TyKind::Slice(elem_hir_ty))
689 | (ty::Array(elem_ty, _), hir::TyKind::Array(elem_hir_ty, _)) => {
690 search_stack.push((*elem_ty, elem_hir_ty));
691 }
692
693 (ty::RawPtr(inner_ty, _), hir::TyKind::Ptr(hir_ty, _)) => {
694 search_stack.push((*inner_ty, hir_ty));
695 }
696
697 _ => {
698 }
700 }
701 }
702
703 None
704 }
705
706 fn match_adt_and_segment<'hir>(
712 &self,
713 args: GenericArgs<'tcx>,
714 needle_fr: RegionVid,
715 last_segment: &'hir hir::PathSegment<'hir>,
716 search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
717 ) -> Option<RegionNameHighlight> {
718 let explicit_args = last_segment.args.as_ref()?;
720 let lifetime =
721 self.try_match_adt_and_generic_args(args, needle_fr, explicit_args, search_stack)?;
722 if lifetime.is_anonymous() {
723 None
724 } else {
725 Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime.ident.span))
726 }
727 }
728
729 fn try_match_adt_and_generic_args<'hir>(
735 &self,
736 args: GenericArgs<'tcx>,
737 needle_fr: RegionVid,
738 hir_args: &'hir hir::GenericArgs<'hir>,
739 search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
740 ) -> Option<&'hir hir::Lifetime> {
741 for (arg, hir_arg) in iter::zip(args, hir_args.args) {
742 match (arg.kind(), hir_arg) {
743 (GenericArgKind::Lifetime(r), hir::GenericArg::Lifetime(lt)) => {
744 if r.as_var() == needle_fr {
745 return Some(lt);
746 }
747 }
748
749 (GenericArgKind::Type(ty), hir::GenericArg::Type(hir_ty)) => {
750 search_stack.push((ty, hir_ty.as_unambig_ty()));
751 }
752
753 (GenericArgKind::Const(_ct), hir::GenericArg::Const(_hir_ct)) => {
754 }
757
758 (
759 GenericArgKind::Lifetime(_)
760 | GenericArgKind::Type(_)
761 | GenericArgKind::Const(_),
762 _,
763 ) => {
764 self.dcx().span_delayed_bug(
765 hir_arg.span(),
766 ::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:?}"),
767 );
768 }
769 }
770 }
771
772 None
773 }
774
775 {}
#[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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(783u32),
::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))]
784 fn give_name_if_anonymous_region_appears_in_upvars(&self, fr: RegionVid) -> Option<RegionName> {
785 let upvar_index = self.regioncx.get_upvar_index_for_region(self.infcx.tcx, fr)?;
786
787 if let Some(region_name) = self.give_name_if_we_can_match_upvar_args(fr, upvar_index) {
790 return Some(region_name);
791 }
792
793 let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
794 self.infcx.tcx,
795 self.upvars,
796 upvar_index,
797 );
798 let region_name = self.synthesize_region_name();
799
800 Some(RegionName {
801 name: region_name,
802 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name),
803 })
804 }
805
806 {}
#[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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(810u32),
::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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs:815",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(815u32),
::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))]
811 fn give_name_if_anonymous_region_appears_in_output(&self, fr: RegionVid) -> Option<RegionName> {
812 let tcx = self.infcx.tcx;
813
814 let mut return_ty = self.regioncx.universal_regions().unnormalized_output_ty;
815 debug!("give_name_if_anonymous_region_appears_in_output: return_ty = {:?}", return_ty);
816 if !tcx.any_free_region_meets(&return_ty, |r| r.as_var() == fr) {
817 return None;
818 }
819
820 if let ty::Coroutine(_, args) = return_ty.kind() {
821 return_ty = args.as_coroutine().return_ty();
824 }
825
826 let mir_hir_id = self.mir_hir_id();
827
828 let (return_span, mir_description, hir_ty) = match tcx.hir_node(mir_hir_id) {
829 hir::Node::Expr(&hir::Expr {
830 kind: hir::ExprKind::Closure(&hir::Closure { fn_decl, kind, fn_decl_span, .. }),
831 ..
832 }) => {
833 let (mut span, mut hir_ty) = match fn_decl.output {
834 hir::FnRetTy::DefaultReturn(_) => {
835 (tcx.sess.source_map().end_point(fn_decl_span), None)
836 }
837 hir::FnRetTy::Return(hir_ty) => (fn_decl.output.span(), Some(hir_ty)),
838 };
839 let mir_description = match kind {
840 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
841 hir::CoroutineDesugaring::Async,
842 hir::CoroutineSource::Block,
843 )) => " of async block",
844
845 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
846 hir::CoroutineDesugaring::Async,
847 hir::CoroutineSource::Closure,
848 ))
849 | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) => {
850 " of async closure"
851 }
852
853 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
854 hir::CoroutineDesugaring::Async,
855 hir::CoroutineSource::Fn,
856 )) => {
857 let parent_item =
858 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
859 let output = &parent_item
860 .fn_decl()
861 .expect("coroutine lowered from async fn should be in fn")
862 .output;
863 span = output.span();
864 if let hir::FnRetTy::Return(ret) = output {
865 hir_ty = Some(self.get_future_inner_return_ty(ret));
866 }
867 " of async function"
868 }
869
870 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
871 hir::CoroutineDesugaring::Gen,
872 hir::CoroutineSource::Block,
873 )) => " of gen block",
874
875 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
876 hir::CoroutineDesugaring::Gen,
877 hir::CoroutineSource::Closure,
878 ))
879 | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Gen) => {
880 " of gen closure"
881 }
882
883 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
884 hir::CoroutineDesugaring::Gen,
885 hir::CoroutineSource::Fn,
886 )) => {
887 let parent_item =
888 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
889 let output = &parent_item
890 .fn_decl()
891 .expect("coroutine lowered from gen fn should be in fn")
892 .output;
893 span = output.span();
894 " of gen function"
895 }
896
897 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
898 hir::CoroutineDesugaring::AsyncGen,
899 hir::CoroutineSource::Block,
900 )) => " of async gen block",
901
902 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
903 hir::CoroutineDesugaring::AsyncGen,
904 hir::CoroutineSource::Closure,
905 ))
906 | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::AsyncGen) => {
907 " of async gen closure"
908 }
909
910 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
911 hir::CoroutineDesugaring::AsyncGen,
912 hir::CoroutineSource::Fn,
913 )) => {
914 let parent_item =
915 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
916 let output = &parent_item
917 .fn_decl()
918 .expect("coroutine lowered from async gen fn should be in fn")
919 .output;
920 span = output.span();
921 " of async gen function"
922 }
923
924 hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine(_)) => {
925 " of coroutine"
926 }
927 hir::ClosureKind::Closure => " of closure",
928 };
929 (span, mir_description, hir_ty)
930 }
931 node => match node.fn_decl() {
932 Some(fn_decl) => {
933 let hir_ty = match fn_decl.output {
934 hir::FnRetTy::DefaultReturn(_) => None,
935 hir::FnRetTy::Return(ty) => Some(ty),
936 };
937 (fn_decl.output.span(), "", hir_ty)
938 }
939 None => (self.body.span, "", None),
940 },
941 };
942
943 let highlight = hir_ty
944 .and_then(|hir_ty| self.highlight_if_we_can_match_hir_ty(fr, return_ty, hir_ty))
945 .unwrap_or_else(|| {
946 let counter = *self.next_region_name.try_borrow().unwrap();
950 self.highlight_if_we_cannot_match_hir_ty(fr, return_ty, return_span, counter)
951 });
952
953 Some(RegionName {
954 name: self.synthesize_region_name(),
955 source: RegionNameSource::AnonRegionFromOutput(highlight, mir_description),
956 })
957 }
958
959 fn get_future_inner_return_ty(&self, hir_ty: &'tcx hir::Ty<'tcx>) -> &'tcx hir::Ty<'tcx> {
973 let hir::TyKind::OpaqueDef(opaque_ty) = hir_ty.kind else {
974 ::rustc_span::macros::bug_impl(Some(hir_ty.span),
format_args!("lowered return type of async fn is not OpaqueDef: {0:?}",
hir_ty), Location::caller());span_bug!(
975 hir_ty.span,
976 "lowered return type of async fn is not OpaqueDef: {:?}",
977 hir_ty
978 );
979 };
980 if let hir::OpaqueTy { bounds: [hir::GenericBound::Trait(trait_ref)], .. } = opaque_ty
981 && let Some(segment) = trait_ref.trait_ref.path.segments.last()
982 && let Some(args) = segment.args
983 && let [constraint] = args.constraints
984 && constraint.ident.name == sym::Output
985 && let Some(ty) = constraint.ty()
986 {
987 ty
988 } else {
989 ::rustc_span::macros::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!(
990 hir_ty.span,
991 "bounds from lowered return type of async fn did not match expected format: {opaque_ty:?}",
992 );
993 }
994 }
995
996 {}
#[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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(996u32),
::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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs:1004",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(1004u32),
::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/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs:1028",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/75a3786afbc93bbddf2c0259033b63957b45ace1/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(1028u32),
::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))]
997 fn give_name_if_anonymous_region_appears_in_yield_ty(
998 &self,
999 fr: RegionVid,
1000 ) -> Option<RegionName> {
1001 let yield_ty = self.regioncx.universal_regions().yield_ty?;
1004 debug!("give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {:?}", yield_ty);
1005
1006 let tcx = self.infcx.tcx;
1007
1008 if !tcx.any_free_region_meets(&yield_ty, |r| r.as_var() == fr) {
1009 return None;
1010 }
1011
1012 let mut highlight = RegionHighlightMode::default();
1013 highlight.highlighting_region_vid(tcx, fr, *self.next_region_name.try_borrow().unwrap());
1014 let type_name = self
1015 .infcx
1016 .err_ctxt()
1017 .extract_inference_diagnostics_data(yield_ty.into(), highlight)
1018 .name;
1019
1020 let yield_span = match tcx.hir_node(self.mir_hir_id()) {
1021 hir::Node::Expr(&hir::Expr {
1022 kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }),
1023 ..
1024 }) => tcx.sess.source_map().end_point(fn_decl_span),
1025 _ => self.body.span,
1026 };
1027
1028 debug!(
1029 "give_name_if_anonymous_region_appears_in_yield_ty: \
1030 type_name = {:?}, yield_span = {:?}",
1031 yield_span, type_name,
1032 );
1033
1034 Some(RegionName {
1035 name: self.synthesize_region_name(),
1036 source: RegionNameSource::AnonRegionFromYieldTy(yield_span, Symbol::intern(&type_name)),
1037 })
1038 }
1039
1040 fn give_name_if_anonymous_region_appears_in_impl_signature(
1041 &self,
1042 fr: RegionVid,
1043 ) -> Option<RegionName> {
1044 let ty::ReEarlyParam(region) = self.regioncx.to_error_region(fr)?.kind() else {
1045 return None;
1046 };
1047 if region.is_named() {
1048 return None;
1049 };
1050
1051 let tcx = self.infcx.tcx;
1052 let region_def = tcx.generics_of(self.mir_def_id()).region_param(region, tcx).def_id;
1053 let region_parent = tcx.parent(region_def);
1054 let DefKind::Impl { .. } = tcx.def_kind(region_parent) else {
1055 return None;
1056 };
1057
1058 let found = tcx.any_free_region_meets(
1059 &tcx.type_of(region_parent).instantiate_identity().skip_norm_wip(),
1060 |r| r.kind() == ty::ReEarlyParam(region),
1061 );
1062
1063 Some(RegionName {
1064 name: self.synthesize_region_name(),
1065 source: RegionNameSource::AnonRegionFromImplSignature(
1066 tcx.def_span(region_def),
1067 if found { "self type" } else { "header" },
1071 ),
1072 })
1073 }
1074
1075 fn give_name_if_anonymous_region_appears_in_arg_position_impl_trait(
1076 &self,
1077 fr: RegionVid,
1078 ) -> Option<RegionName> {
1079 let ty::ReEarlyParam(region) = self.regioncx.to_error_region(fr)?.kind() else {
1080 return None;
1081 };
1082 if region.is_named() {
1083 return None;
1084 };
1085
1086 let clauses: Vec<_> = self
1087 .infcx
1088 .tcx
1089 .clauses_of(self.body.source.def_id())
1090 .instantiate_identity(self.infcx.tcx)
1091 .clauses
1092 .into_iter()
1093 .map(Unnormalized::skip_norm_wip)
1094 .collect();
1095
1096 if let Some(upvar_index) = self
1097 .regioncx
1098 .universal_regions()
1099 .defining_ty
1100 .upvar_tys()
1101 .iter()
1102 .position(|ty| self.any_param_clause_mentions(&clauses, ty, region))
1103 {
1104 let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
1105 self.infcx.tcx,
1106 self.upvars,
1107 upvar_index,
1108 );
1109 let region_name = self.synthesize_region_name();
1110
1111 Some(RegionName {
1112 name: region_name,
1113 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name),
1114 })
1115 } else if let Some(arg_index) = self
1116 .regioncx
1117 .universal_regions()
1118 .unnormalized_input_tys
1119 .iter()
1120 .position(|ty| self.any_param_clause_mentions(&clauses, *ty, region))
1121 {
1122 let (arg_name, arg_span) = self.regioncx.get_argument_name_and_span_for_region(
1123 self.body,
1124 self.local_names(),
1125 arg_index,
1126 );
1127 let region_name = self.synthesize_region_name();
1128
1129 Some(RegionName {
1130 name: region_name,
1131 source: RegionNameSource::AnonRegionFromArgument(
1132 RegionNameHighlight::CannotMatchHirTy(arg_span, arg_name?, None),
1133 ),
1134 })
1135 } else {
1136 None
1137 }
1138 }
1139
1140 fn any_param_clause_mentions(
1141 &self,
1142 clauses: &[ty::Clause<'tcx>],
1143 ty: Ty<'tcx>,
1144 region: ty::EarlyParamRegion,
1145 ) -> bool {
1146 let tcx = self.infcx.tcx;
1147 ty.walk().any(|arg| {
1148 if let ty::GenericArgKind::Type(ty) = arg.kind()
1149 && let ty::Param(_) = ty.kind()
1150 {
1151 clauses.iter().any(|pred| {
1152 match pred.kind().skip_binder() {
1153 ty::ClauseKind::Trait(data) if data.self_ty() == ty => {}
1154 ty::ClauseKind::Projection(data)
1155 if data.projection_term.self_ty() == ty => {}
1156 _ => return false,
1157 }
1158 tcx.any_free_region_meets(pred, |r| r.kind() == ty::ReEarlyParam(region))
1159 })
1160 } else {
1161 false
1162 }
1163 })
1164 }
1165}