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