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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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) => {
488 if ebr.is_named() {
489 let def_id =
490 tcx.generics_of(parent_def_id).region_param(ebr, tcx).def_id;
491 let span = tcx.hir_span_if_local(def_id).unwrap_or(DUMMY_SP);
492 return Some(RegionName {
493 name: ebr.name,
494 source: RegionNameSource::NamedEarlyParamRegion(span),
495 });
496 }
497 }
498 _ => {}
499 }
500 }
501 }
502
503 None
504 }
505
506 {}
#[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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(514u32),
::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))]
515 fn give_name_if_anonymous_region_appears_in_arguments(
516 &self,
517 fr: RegionVid,
518 ) -> Option<RegionName> {
519 let implicit_inputs = self.regioncx.universal_regions().defining_ty.implicit_inputs();
520 let user_arg_index = self.regioncx.get_user_arg_index_for_region(self.infcx.tcx, fr)?;
521
522 let arg_ty = self.regioncx.universal_regions().unnormalized_input_tys
523 [implicit_inputs + user_arg_index];
524 let (_, span) = self.regioncx.get_argument_name_and_span_for_region(
525 self.body,
526 self.local_names(),
527 user_arg_index,
528 );
529
530 let highlight = self
531 .get_argument_hir_ty_for_highlighting(user_arg_index)
532 .and_then(|arg_hir_ty| self.highlight_if_we_can_match_hir_ty(fr, arg_ty, arg_hir_ty))
533 .unwrap_or_else(|| {
534 let counter = *self.next_region_name.try_borrow().unwrap();
538 self.highlight_if_we_cannot_match_hir_ty(fr, arg_ty, span, counter)
539 });
540
541 Some(RegionName {
542 name: self.synthesize_region_name(),
543 source: RegionNameSource::AnonRegionFromArgument(highlight),
544 })
545 }
546
547 fn get_argument_hir_ty_for_highlighting(
548 &self,
549 user_arg_index: usize,
550 ) -> Option<&hir::Ty<'tcx>> {
551 let fn_decl = self.infcx.tcx.hir_fn_decl_by_hir_id(self.mir_hir_id())?;
552 let argument_hir_ty: &hir::Ty<'_> = fn_decl.inputs.get(user_arg_index)?;
554 match argument_hir_ty.kind {
555 hir::TyKind::Infer(()) => None,
561
562 _ => Some(argument_hir_ty),
563 }
564 }
565
566 fn highlight_if_we_cannot_match_hir_ty(
578 &self,
579 needle_fr: RegionVid,
580 ty: Ty<'tcx>,
581 span: Span,
582 counter: usize,
583 ) -> RegionNameHighlight {
584 let mut highlight = RegionHighlightMode::default();
585 highlight.highlighting_region_vid(self.infcx.tcx, needle_fr, counter);
586 let type_name =
587 self.infcx.err_ctxt().extract_inference_diagnostics_data(ty.into(), highlight).name;
588
589 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event /rustc-dev/9c5648833e5ceb667a708d53c00b83ecc9e610d5/compiler/rustc_borrowck/src/diagnostics/region_name.rs:589",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/9c5648833e5ceb667a708d53c00b83ecc9e610d5/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(589u32),
::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!(
590 "highlight_if_we_cannot_match_hir_ty: type_name={:?} needle_fr={:?}",
591 type_name, needle_fr
592 );
593 let lt_span = if let Some(needle) = self.regioncx.to_error_region(needle_fr)
594 && let Some(def_id) = needle.opt_param_def_id(self.infcx.tcx, self.mir_def_id().into())
595 {
596 Some(self.infcx.tcx.def_span(def_id))
597 } else {
598 None
599 };
600 if type_name.contains(&::alloc::__export::must_use({
::alloc::fmt::format(format_args!("\'{0}", counter))
})format!("'{counter}")) {
601 RegionNameHighlight::CannotMatchHirTy(span, Symbol::intern(&type_name), lt_span)
603 } else {
604 RegionNameHighlight::Occluded(span, Symbol::intern(&type_name), lt_span)
605 }
606 }
607
608 fn highlight_if_we_can_match_hir_ty(
630 &self,
631 needle_fr: RegionVid,
632 ty: Ty<'tcx>,
633 hir_ty: &hir::Ty<'_>,
634 ) -> Option<RegionNameHighlight> {
635 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)];
636
637 while let Some((ty, hir_ty)) = search_stack.pop() {
638 match (ty.kind(), &hir_ty.kind) {
639 (
646 ty::Ref(region, referent_ty, _),
647 hir::TyKind::Ref(_lifetime, referent_hir_ty, _),
648 ) => {
649 if region.as_var() == needle_fr {
650 let source_map = self.infcx.tcx.sess.source_map();
652 let ampersand_span = source_map.start_point(hir_ty.span);
653
654 return Some(RegionNameHighlight::MatchedHirTy(ampersand_span));
655 }
656
657 search_stack.push((*referent_ty, referent_hir_ty));
659 }
660
661 (ty::Adt(_adt_def, args), hir::TyKind::Path(hir::QPath::Resolved(None, path))) => {
663 match path.res {
664 Res::Def(DefKind::TyAlias, _) => (),
669 _ => {
670 if let Some(last_segment) = path.segments.last()
671 && let Some(highlight) = self.match_adt_and_segment(
672 *args,
673 needle_fr,
674 last_segment,
675 search_stack,
676 )
677 {
678 return Some(highlight);
679 }
680 }
681 }
682 }
683
684 (&ty::Tuple(elem_tys), hir::TyKind::Tup(elem_hir_tys)) => {
688 search_stack.extend(iter::zip(elem_tys, *elem_hir_tys));
689 }
690
691 (ty::Slice(elem_ty), hir::TyKind::Slice(elem_hir_ty))
692 | (ty::Array(elem_ty, _), hir::TyKind::Array(elem_hir_ty, _)) => {
693 search_stack.push((*elem_ty, elem_hir_ty));
694 }
695
696 (ty::RawPtr(inner_ty, _), hir::TyKind::Ptr(hir_ty, _)) => {
697 search_stack.push((*inner_ty, hir_ty));
698 }
699
700 _ => {
701 }
703 }
704 }
705
706 None
707 }
708
709 fn match_adt_and_segment<'hir>(
715 &self,
716 args: GenericArgs<'tcx>,
717 needle_fr: RegionVid,
718 last_segment: &'hir hir::PathSegment<'hir>,
719 search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
720 ) -> Option<RegionNameHighlight> {
721 let explicit_args = last_segment.args.as_ref()?;
723 let lifetime =
724 self.try_match_adt_and_generic_args(args, needle_fr, explicit_args, search_stack)?;
725 if lifetime.is_anonymous() {
726 None
727 } else {
728 Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime.ident.span))
729 }
730 }
731
732 fn try_match_adt_and_generic_args<'hir>(
738 &self,
739 args: GenericArgs<'tcx>,
740 needle_fr: RegionVid,
741 hir_args: &'hir hir::GenericArgs<'hir>,
742 search_stack: &mut Vec<(Ty<'tcx>, &'hir hir::Ty<'hir>)>,
743 ) -> Option<&'hir hir::Lifetime> {
744 for (arg, hir_arg) in iter::zip(args, hir_args.args) {
745 match (arg.kind(), hir_arg) {
746 (GenericArgKind::Lifetime(r), hir::GenericArg::Lifetime(lt)) => {
747 if r.as_var() == needle_fr {
748 return Some(lt);
749 }
750 }
751
752 (GenericArgKind::Type(ty), hir::GenericArg::Type(hir_ty)) => {
753 search_stack.push((ty, hir_ty.as_unambig_ty()));
754 }
755
756 (GenericArgKind::Const(_ct), hir::GenericArg::Const(_hir_ct)) => {
757 }
760
761 (
762 GenericArgKind::Lifetime(_)
763 | GenericArgKind::Type(_)
764 | GenericArgKind::Const(_),
765 _,
766 ) => {
767 self.dcx().span_delayed_bug(
768 hir_arg.span(),
769 ::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:?}"),
770 );
771 }
772 }
773 }
774
775 None
776 }
777
778 {}
#[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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(786u32),
::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))]
787 fn give_name_if_anonymous_region_appears_in_upvars(&self, fr: RegionVid) -> Option<RegionName> {
788 let upvar_index = self.regioncx.get_upvar_index_for_region(self.infcx.tcx, fr)?;
789
790 if let Some(region_name) = self.give_name_if_we_can_match_upvar_args(fr, upvar_index) {
793 return Some(region_name);
794 }
795
796 let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
797 self.infcx.tcx,
798 self.upvars,
799 upvar_index,
800 );
801 let region_name = self.synthesize_region_name();
802
803 Some(RegionName {
804 name: region_name,
805 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name),
806 })
807 }
808
809 {}
#[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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(813u32),
::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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/compiler/rustc_borrowck/src/diagnostics/region_name.rs:818",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/9c5648833e5ceb667a708d53c00b83ecc9e610d5/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(818u32),
::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))]
814 fn give_name_if_anonymous_region_appears_in_output(&self, fr: RegionVid) -> Option<RegionName> {
815 let tcx = self.infcx.tcx;
816
817 let mut return_ty = self.regioncx.universal_regions().unnormalized_output_ty;
818 debug!("give_name_if_anonymous_region_appears_in_output: return_ty = {:?}", return_ty);
819 if !tcx.any_free_region_meets(&return_ty, |r| r.as_var() == fr) {
820 return None;
821 }
822
823 if let ty::Coroutine(_, args) = return_ty.kind() {
824 return_ty = args.as_coroutine().return_ty();
827 }
828
829 let mir_hir_id = self.mir_hir_id();
830
831 let (return_span, mir_description, hir_ty) = match tcx.hir_node(mir_hir_id) {
832 hir::Node::Expr(&hir::Expr {
833 kind: hir::ExprKind::Closure(&hir::Closure { fn_decl, kind, fn_decl_span, .. }),
834 ..
835 }) => {
836 let (mut span, mut hir_ty) = match fn_decl.output {
837 hir::FnRetTy::DefaultReturn(_) => {
838 (tcx.sess.source_map().end_point(fn_decl_span), None)
839 }
840 hir::FnRetTy::Return(hir_ty) => (fn_decl.output.span(), Some(hir_ty)),
841 };
842 let mir_description = match kind {
843 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
844 hir::CoroutineDesugaring::Async,
845 hir::CoroutineSource::Block,
846 )) => " of async block",
847
848 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
849 hir::CoroutineDesugaring::Async,
850 hir::CoroutineSource::Closure,
851 ))
852 | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) => {
853 " of async closure"
854 }
855
856 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
857 hir::CoroutineDesugaring::Async,
858 hir::CoroutineSource::Fn,
859 )) => {
860 let parent_item =
861 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
862 let output = &parent_item
863 .fn_decl()
864 .expect("coroutine lowered from async fn should be in fn")
865 .output;
866 span = output.span();
867 if let hir::FnRetTy::Return(ret) = output {
868 hir_ty = Some(self.get_future_inner_return_ty(ret));
869 }
870 " of async function"
871 }
872
873 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
874 hir::CoroutineDesugaring::Gen,
875 hir::CoroutineSource::Block,
876 )) => " of gen block",
877
878 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
879 hir::CoroutineDesugaring::Gen,
880 hir::CoroutineSource::Closure,
881 ))
882 | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Gen) => {
883 " of gen closure"
884 }
885
886 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
887 hir::CoroutineDesugaring::Gen,
888 hir::CoroutineSource::Fn,
889 )) => {
890 let parent_item =
891 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
892 let output = &parent_item
893 .fn_decl()
894 .expect("coroutine lowered from gen fn should be in fn")
895 .output;
896 span = output.span();
897 " of gen function"
898 }
899
900 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
901 hir::CoroutineDesugaring::AsyncGen,
902 hir::CoroutineSource::Block,
903 )) => " of async gen block",
904
905 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
906 hir::CoroutineDesugaring::AsyncGen,
907 hir::CoroutineSource::Closure,
908 ))
909 | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::AsyncGen) => {
910 " of async gen closure"
911 }
912
913 hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
914 hir::CoroutineDesugaring::AsyncGen,
915 hir::CoroutineSource::Fn,
916 )) => {
917 let parent_item =
918 tcx.hir_node_by_def_id(tcx.hir_get_parent_item(mir_hir_id).def_id);
919 let output = &parent_item
920 .fn_decl()
921 .expect("coroutine lowered from async gen fn should be in fn")
922 .output;
923 span = output.span();
924 " of async gen function"
925 }
926
927 hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine(_)) => {
928 " of coroutine"
929 }
930 hir::ClosureKind::Closure => " of closure",
931 };
932 (span, mir_description, hir_ty)
933 }
934 node => match node.fn_decl() {
935 Some(fn_decl) => {
936 let hir_ty = match fn_decl.output {
937 hir::FnRetTy::DefaultReturn(_) => None,
938 hir::FnRetTy::Return(ty) => Some(ty),
939 };
940 (fn_decl.output.span(), "", hir_ty)
941 }
942 None => (self.body.span, "", None),
943 },
944 };
945
946 let highlight = hir_ty
947 .and_then(|hir_ty| self.highlight_if_we_can_match_hir_ty(fr, return_ty, hir_ty))
948 .unwrap_or_else(|| {
949 let counter = *self.next_region_name.try_borrow().unwrap();
953 self.highlight_if_we_cannot_match_hir_ty(fr, return_ty, return_span, counter)
954 });
955
956 Some(RegionName {
957 name: self.synthesize_region_name(),
958 source: RegionNameSource::AnonRegionFromOutput(highlight, mir_description),
959 })
960 }
961
962 fn get_future_inner_return_ty(&self, hir_ty: &'tcx hir::Ty<'tcx>) -> &'tcx hir::Ty<'tcx> {
976 let hir::TyKind::OpaqueDef(opaque_ty) = hir_ty.kind else {
977 ::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!(
978 hir_ty.span,
979 "lowered return type of async fn is not OpaqueDef: {:?}",
980 hir_ty
981 );
982 };
983 if let hir::OpaqueTy { bounds: [hir::GenericBound::Trait(trait_ref)], .. } = opaque_ty
984 && let Some(segment) = trait_ref.trait_ref.path.segments.last()
985 && let Some(args) = segment.args
986 && let [constraint] = args.constraints
987 && constraint.ident.name == sym::Output
988 && let Some(ty) = constraint.ty()
989 {
990 ty
991 } else {
992 ::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!(
993 hir_ty.span,
994 "bounds from lowered return type of async fn did not match expected format: {opaque_ty:?}",
995 );
996 }
997 }
998
999 {}
#[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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(999u32),
::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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/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: 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/9c5648833e5ceb667a708d53c00b83ecc9e610d5/compiler/rustc_borrowck/src/diagnostics/region_name.rs:1031",
"rustc_borrowck::diagnostics::region_name",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("/rustc-dev/9c5648833e5ceb667a708d53c00b83ecc9e610d5/compiler/rustc_borrowck/src/diagnostics/region_name.rs"),
::tracing_core::__macro_support::Option::Some(1031u32),
::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))]
1000 fn give_name_if_anonymous_region_appears_in_yield_ty(
1001 &self,
1002 fr: RegionVid,
1003 ) -> Option<RegionName> {
1004 let yield_ty = self.regioncx.universal_regions().yield_ty?;
1007 debug!("give_name_if_anonymous_region_appears_in_yield_ty: yield_ty = {:?}", yield_ty);
1008
1009 let tcx = self.infcx.tcx;
1010
1011 if !tcx.any_free_region_meets(&yield_ty, |r| r.as_var() == fr) {
1012 return None;
1013 }
1014
1015 let mut highlight = RegionHighlightMode::default();
1016 highlight.highlighting_region_vid(tcx, fr, *self.next_region_name.try_borrow().unwrap());
1017 let type_name = self
1018 .infcx
1019 .err_ctxt()
1020 .extract_inference_diagnostics_data(yield_ty.into(), highlight)
1021 .name;
1022
1023 let yield_span = match tcx.hir_node(self.mir_hir_id()) {
1024 hir::Node::Expr(&hir::Expr {
1025 kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }),
1026 ..
1027 }) => tcx.sess.source_map().end_point(fn_decl_span),
1028 _ => self.body.span,
1029 };
1030
1031 debug!(
1032 "give_name_if_anonymous_region_appears_in_yield_ty: \
1033 type_name = {:?}, yield_span = {:?}",
1034 yield_span, type_name,
1035 );
1036
1037 Some(RegionName {
1038 name: self.synthesize_region_name(),
1039 source: RegionNameSource::AnonRegionFromYieldTy(yield_span, Symbol::intern(&type_name)),
1040 })
1041 }
1042
1043 fn give_name_if_anonymous_region_appears_in_impl_signature(
1044 &self,
1045 fr: RegionVid,
1046 ) -> Option<RegionName> {
1047 let ty::ReEarlyParam(region) = self.regioncx.to_error_region(fr)?.kind() else {
1048 return None;
1049 };
1050 if region.is_named() {
1051 return None;
1052 };
1053
1054 let tcx = self.infcx.tcx;
1055 let region_def = tcx.generics_of(self.mir_def_id()).region_param(region, tcx).def_id;
1056 let region_parent = tcx.parent(region_def);
1057 let DefKind::Impl { .. } = tcx.def_kind(region_parent) else {
1058 return None;
1059 };
1060
1061 let found = tcx.any_free_region_meets(
1062 &tcx.type_of(region_parent).instantiate_identity().skip_norm_wip(),
1063 |r| r.kind() == ty::ReEarlyParam(region),
1064 );
1065
1066 Some(RegionName {
1067 name: self.synthesize_region_name(),
1068 source: RegionNameSource::AnonRegionFromImplSignature(
1069 tcx.def_span(region_def),
1070 if found { "self type" } else { "header" },
1074 ),
1075 })
1076 }
1077
1078 fn give_name_if_anonymous_region_appears_in_arg_position_impl_trait(
1079 &self,
1080 fr: RegionVid,
1081 ) -> Option<RegionName> {
1082 let ty::ReEarlyParam(region) = self.regioncx.to_error_region(fr)?.kind() else {
1083 return None;
1084 };
1085 if region.is_named() {
1086 return None;
1087 };
1088
1089 let clauses: Vec<_> = self
1090 .infcx
1091 .tcx
1092 .clauses_of(self.body.source.def_id())
1093 .instantiate_identity(self.infcx.tcx)
1094 .clauses
1095 .into_iter()
1096 .map(Unnormalized::skip_norm_wip)
1097 .collect();
1098
1099 if let Some(upvar_index) = self
1100 .regioncx
1101 .universal_regions()
1102 .defining_ty
1103 .upvar_tys()
1104 .iter()
1105 .position(|ty| self.any_param_clause_mentions(&clauses, ty, region))
1106 {
1107 let (upvar_name, upvar_span) = self.regioncx.get_upvar_name_and_span_for_region(
1108 self.infcx.tcx,
1109 self.upvars,
1110 upvar_index,
1111 );
1112 let region_name = self.synthesize_region_name();
1113
1114 Some(RegionName {
1115 name: region_name,
1116 source: RegionNameSource::AnonRegionFromUpvar(upvar_span, upvar_name),
1117 })
1118 } else if let Some(arg_index) = self
1119 .regioncx
1120 .universal_regions()
1121 .unnormalized_input_tys
1122 .iter()
1123 .position(|ty| self.any_param_clause_mentions(&clauses, *ty, region))
1124 {
1125 let (arg_name, arg_span) = self.regioncx.get_argument_name_and_span_for_region(
1126 self.body,
1127 self.local_names(),
1128 arg_index,
1129 );
1130 let region_name = self.synthesize_region_name();
1131
1132 Some(RegionName {
1133 name: region_name,
1134 source: RegionNameSource::AnonRegionFromArgument(
1135 RegionNameHighlight::CannotMatchHirTy(arg_span, arg_name?, None),
1136 ),
1137 })
1138 } else {
1139 None
1140 }
1141 }
1142
1143 fn any_param_clause_mentions(
1144 &self,
1145 clauses: &[ty::Clause<'tcx>],
1146 ty: Ty<'tcx>,
1147 region: ty::EarlyParamRegion,
1148 ) -> bool {
1149 let tcx = self.infcx.tcx;
1150 ty.walk().any(|arg| {
1151 if let ty::GenericArgKind::Type(ty) = arg.kind()
1152 && let ty::Param(_) = ty.kind()
1153 {
1154 clauses.iter().any(|pred| {
1155 match pred.kind().skip_binder() {
1156 ty::ClauseKind::Trait(data) if data.self_ty() == ty => {}
1157 ty::ClauseKind::Projection(data)
1158 if data.projection_term.self_ty() == ty => {}
1159 _ => return false,
1160 }
1161 tcx.any_free_region_meets(pred, |r| r.kind() == ty::ReEarlyParam(region))
1162 })
1163 } else {
1164 false
1165 }
1166 })
1167 }
1168}