1use rustc_hir::def::DefKind;
2use rustc_infer::traits::ObligationCause;
3use rustc_middle::bug;
4use rustc_middle::ty::{
5 self, DefiningScopeKind, DefinitionSiteHiddenType, OpaqueTypeKey, ProvisionalHiddenType,
6 TypeVisitableExt, Unnormalized,
7};
8use rustc_trait_selection::error_reporting::infer::need_type_info::TypeAnnotationNeeded;
9use rustc_trait_selection::opaque_types::{
10 NonDefiningUseReason, opaque_type_has_defining_use_args, report_item_does_not_constrain_error,
11};
12use rustc_trait_selection::solve;
13use tracing::{debug, instrument};
14
15use crate::FnCtxt;
16
17impl<'tcx> FnCtxt<'_, 'tcx> {
18 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("try_handle_opaque_type_uses_next",
"rustc_hir_typeck::opaque_types", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/opaque_types.rs"),
::tracing_core::__macro_support::Option::Some(28u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::opaque_types"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set_all(&[]) })
} 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: () = loop {};
return __tracing_attr_fake_return;
}
{
let opaque_types: Vec<_> = self.infcx.clone_opaque_types();
self.compute_definition_site_hidden_types(opaque_types, false);
}
}
}#[instrument(level = "debug", skip(self))]
29 pub(super) fn try_handle_opaque_type_uses_next(&mut self) {
30 let opaque_types: Vec<_> = self.infcx.clone_opaque_types();
33
34 self.compute_definition_site_hidden_types(opaque_types, false);
35 }
36
37 {}
#[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("handle_opaque_type_uses_next",
"rustc_hir_typeck::opaque_types", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/opaque_types.rs"),
::tracing_core::__macro_support::Option::Some(45u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::opaque_types"),
::tracing_core::field::FieldSet::new(&[],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{ meta.fields().value_set_all(&[]) })
} 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: () = loop {};
return __tracing_attr_fake_return;
}
{
let opaque_types: Vec<_> = self.infcx.clone_opaque_types();
let num_entries =
self.inner.borrow_mut().opaque_types().num_entries();
let prev =
self.checked_opaque_types_storage_entries.replace(Some(num_entries));
if true {
{
match (&prev, &None) {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
}
};
};
self.compute_definition_site_hidden_types(opaque_types, true);
}
}
}#[instrument(level = "debug", skip(self))]
46 pub(super) fn handle_opaque_type_uses_next(&mut self) {
47 let opaque_types: Vec<_> = self.infcx.clone_opaque_types();
50 let num_entries = self.inner.borrow_mut().opaque_types().num_entries();
51 let prev = self.checked_opaque_types_storage_entries.replace(Some(num_entries));
52 debug_assert_eq!(prev, None);
53
54 self.compute_definition_site_hidden_types(opaque_types, true);
55 }
56}
57
58#[derive(#[automatically_derived]
impl<'tcx> ::core::marker::Copy for UsageKind<'tcx> { }Copy, #[automatically_derived]
#[doc(hidden)]
unsafe impl<'tcx> ::core::clone::TrivialClone for UsageKind<'tcx> { }
#[automatically_derived]
impl<'tcx> ::core::clone::Clone for UsageKind<'tcx> {
#[inline]
fn clone(&self) -> UsageKind<'tcx> {
let _: ::core::clone::AssertParamIsClone<OpaqueTypeKey<'tcx>>;
let _: ::core::clone::AssertParamIsClone<ProvisionalHiddenType<'tcx>>;
let _: ::core::clone::AssertParamIsClone<ProvisionalHiddenType<'tcx>>;
let _:
::core::clone::AssertParamIsClone<DefinitionSiteHiddenType<'tcx>>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::fmt::Debug for UsageKind<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
UsageKind::None => ::core::fmt::Formatter::write_str(f, "None"),
UsageKind::NonDefiningUse(__self_0, __self_1) =>
::core::fmt::Formatter::debug_tuple_field2_finish(f,
"NonDefiningUse", __self_0, &__self_1),
UsageKind::UnconstrainedHiddenType(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"UnconstrainedHiddenType", &__self_0),
UsageKind::HasDefiningUse(__self_0) =>
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"HasDefiningUse", &__self_0),
}
}
}Debug)]
59enum UsageKind<'tcx> {
60 None,
61 NonDefiningUse(OpaqueTypeKey<'tcx>, ProvisionalHiddenType<'tcx>),
62 UnconstrainedHiddenType(ProvisionalHiddenType<'tcx>),
63 HasDefiningUse(DefinitionSiteHiddenType<'tcx>),
64}
65
66impl<'tcx> UsageKind<'tcx> {
67 fn merge(&mut self, other: UsageKind<'tcx>) {
68 match (&*self, &other) {
69 (UsageKind::HasDefiningUse(_), _) | (_, UsageKind::None) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
70 (UsageKind::None, _) => *self = other,
71 (
74 UsageKind::NonDefiningUse(..) | UsageKind::UnconstrainedHiddenType(..),
75 UsageKind::NonDefiningUse(..),
76 ) => {}
77 (
82 UsageKind::NonDefiningUse(..) | UsageKind::UnconstrainedHiddenType(..),
83 UsageKind::UnconstrainedHiddenType(..) | UsageKind::HasDefiningUse(_),
84 ) => *self = other,
85 }
86 }
87}
88
89impl<'tcx> FnCtxt<'_, 'tcx> {
90 fn compute_definition_site_hidden_types(
91 &mut self,
92 mut opaque_types: Vec<(OpaqueTypeKey<'tcx>, ProvisionalHiddenType<'tcx>)>,
93 error_on_missing_defining_use: bool,
94 ) {
95 for entry in opaque_types.iter_mut() {
96 *entry = self.resolve_vars_if_possible(*entry);
97 }
98 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/opaque_types.rs:98",
"rustc_hir_typeck::opaque_types", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/opaque_types.rs"),
::tracing_core::__macro_support::Option::Some(98u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::opaque_types"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("opaque_types")
}> =
::tracing::__macro_support::FieldName::new("opaque_types");
NAME.as_str()
}], ::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(&::tracing::field::debug(&opaque_types)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};debug!(?opaque_types);
99
100 let tcx = self.tcx;
101 let defining_opaque_types_and_generators = match self.typing_mode() {
102 ty::TypingMode::Typeck { defining_opaque_types_and_generators } => {
103 defining_opaque_types_and_generators
104 }
105 ty::TypingMode::Coherence
106 | ty::TypingMode::Reflection
107 | ty::TypingMode::PostTypeckUntilBorrowck { .. }
108 | ty::TypingMode::PostBorrowck { .. }
109 | ty::TypingMode::PostAnalysis
110 | ty::TypingMode::Codegen => {
111 ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!()
112 }
113 };
114
115 for def_id in defining_opaque_types_and_generators {
116 match tcx.def_kind(def_id) {
117 DefKind::OpaqueTy => {}
118 DefKind::Closure => continue,
119 _ => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("not opaque or generator: {0:?}", def_id)));
}unreachable!("not opaque or generator: {def_id:?}"),
120 }
121
122 let mut usage_kind = UsageKind::None;
126 for &(opaque_type_key, hidden_type) in &opaque_types {
127 if opaque_type_key.def_id != def_id {
128 continue;
129 }
130
131 usage_kind.merge(self.consider_opaque_type_use(opaque_type_key, hidden_type));
132
133 if let UsageKind::HasDefiningUse(..) = usage_kind {
134 break;
135 }
136 }
137
138 if let UsageKind::HasDefiningUse(ty) = usage_kind {
139 for &(opaque_type_key, hidden_type) in &opaque_types {
140 if opaque_type_key.def_id != def_id {
141 continue;
142 }
143
144 let expected = ty.ty.instantiate(tcx, opaque_type_key.args).skip_norm_wip();
145 self.demand_eqtype(hidden_type.span, expected, hidden_type.ty);
146 }
147
148 let _ = self.typeck_results.borrow_mut().hidden_types.insert(def_id, ty);
153 }
154
155 if !error_on_missing_defining_use {
158 continue;
159 }
160
161 let guar = match usage_kind {
162 UsageKind::HasDefiningUse(_) => continue,
163 UsageKind::None => {
164 if let Some(guar) = self.tainted_by_errors() {
165 guar
166 } else {
167 report_item_does_not_constrain_error(
168 self.tcx,
169 self.body_def_id,
170 def_id,
171 None,
172 )
173 }
174 }
175 UsageKind::NonDefiningUse(opaque_type_key, hidden_type) => {
176 report_item_does_not_constrain_error(
177 self.tcx,
178 self.body_def_id,
179 def_id,
180 Some((opaque_type_key, hidden_type.span)),
181 )
182 }
183 UsageKind::UnconstrainedHiddenType(hidden_type) => {
184 if let Some(guar) = self.tainted_by_errors() {
185 guar
186 } else {
187 let infer_var = hidden_type
188 .ty
189 .walk()
190 .filter_map(ty::GenericArg::as_term)
191 .find(|term| term.is_infer())
192 .unwrap_or_else(|| hidden_type.ty.into());
193 self.err_ctxt()
194 .emit_inference_failure_err(
195 self.body_def_id,
196 hidden_type.span,
197 infer_var,
198 TypeAnnotationNeeded::E0282,
199 false,
200 )
201 .emit()
202 }
203 }
204 };
205
206 self.typeck_results
207 .borrow_mut()
208 .hidden_types
209 .insert(def_id, DefinitionSiteHiddenType::new_error(tcx, guar));
210 self.set_tainted_by_errors(guar);
211 }
212 }
213
214 {}
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::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("consider_opaque_type_use",
"rustc_hir_typeck::opaque_types", ::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/opaque_types.rs"),
::tracing_core::__macro_support::Option::Some(214u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::opaque_types"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("opaque_type_key")
}> =
::tracing::__macro_support::FieldName::new("opaque_type_key");
NAME.as_str()
},
{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("hidden_type")
}> =
::tracing::__macro_support::FieldName::new("hidden_type");
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::INFO <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::INFO <=
::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(&opaque_type_key)
as &dyn ::tracing::field::Value)),
(::tracing::__macro_support::Option::Some(&::tracing::field::debug(&hidden_type)
as &dyn ::tracing::field::Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[allow(clippy :: redundant_closure_call)]
let x =
(move ||
{
#[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: UsageKind<'tcx> = loop {};
return __tracing_attr_fake_return;
}
{
if let Err(err) =
opaque_type_has_defining_use_args(&self, opaque_type_key,
hidden_type.span, DefiningScopeKind::HirTypeck) {
match err {
NonDefiningUseReason::Tainted(guar) => {
return UsageKind::HasDefiningUse(DefinitionSiteHiddenType::new_error(self.tcx,
guar));
}
_ =>
return UsageKind::NonDefiningUse(opaque_type_key,
hidden_type),
};
}
if hidden_type.ty.has_non_region_infer() {
return UsageKind::UnconstrainedHiddenType(hidden_type);
}
let cause =
ObligationCause::misc(hidden_type.span, self.body_def_id);
let at = self.at(&cause, self.param_env);
let hidden_type =
match solve::deeply_normalize(at,
Unnormalized::new_wip(hidden_type)) {
Ok(hidden_type) => hidden_type,
Err(errors) => {
let guar =
self.err_ctxt().report_fulfillment_errors(errors);
ProvisionalHiddenType::new_error(self.tcx, guar)
}
};
let hidden_type =
hidden_type.remap_generic_params_to_declaration_params(opaque_type_key,
self.tcx, DefiningScopeKind::HirTypeck);
UsageKind::HasDefiningUse(hidden_type)
}
})();
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_typeck/src/opaque_types.rs:214",
"rustc_hir_typeck::opaque_types", ::tracing::Level::INFO,
::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_typeck/src/opaque_types.rs"),
::tracing_core::__macro_support::Option::Some(214u32),
::tracing_core::__macro_support::Option::Some("rustc_hir_typeck::opaque_types"),
::tracing_core::field::FieldSet::new(&[{
const NAME:
::tracing::__macro_support::FieldName<{
::tracing::__macro_support::FieldName::len("return")
}> =
::tracing::__macro_support::FieldName::new("return");
NAME.as_str()
}], ::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::INFO <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::INFO <=
::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(&::tracing::field::debug(&x)
as &dyn ::tracing::field::Value))])
});
} else { ; }
};
x;#[tracing::instrument(skip(self), ret)]
215 fn consider_opaque_type_use(
216 &self,
217 opaque_type_key: OpaqueTypeKey<'tcx>,
218 hidden_type: ProvisionalHiddenType<'tcx>,
219 ) -> UsageKind<'tcx> {
220 if let Err(err) = opaque_type_has_defining_use_args(
221 &self,
222 opaque_type_key,
223 hidden_type.span,
224 DefiningScopeKind::HirTypeck,
225 ) {
226 match err {
227 NonDefiningUseReason::Tainted(guar) => {
228 return UsageKind::HasDefiningUse(DefinitionSiteHiddenType::new_error(
229 self.tcx, guar,
230 ));
231 }
232 _ => return UsageKind::NonDefiningUse(opaque_type_key, hidden_type),
233 };
234 }
235
236 if hidden_type.ty.has_non_region_infer() {
241 return UsageKind::UnconstrainedHiddenType(hidden_type);
242 }
243
244 let cause = ObligationCause::misc(hidden_type.span, self.body_def_id);
245 let at = self.at(&cause, self.param_env);
246 let hidden_type = match solve::deeply_normalize(at, Unnormalized::new_wip(hidden_type)) {
247 Ok(hidden_type) => hidden_type,
248 Err(errors) => {
249 let guar = self.err_ctxt().report_fulfillment_errors(errors);
250 ProvisionalHiddenType::new_error(self.tcx, guar)
251 }
252 };
253 let hidden_type = hidden_type.remap_generic_params_to_declaration_params(
254 opaque_type_key,
255 self.tcx,
256 DefiningScopeKind::HirTypeck,
257 );
258 UsageKind::HasDefiningUse(hidden_type)
259 }
260
261 pub(super) fn detect_opaque_types_added_during_writeback(&self) {
274 let num_entries = self.checked_opaque_types_storage_entries.take().unwrap();
275 for (key, hidden_type) in
276 self.inner.borrow_mut().opaque_types().opaque_types_added_since(num_entries)
277 {
278 let opaque_type_string = self.tcx.def_path_str(key.def_id);
279 let msg = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("unexpected cyclic definition of `{0}`",
opaque_type_string))
})format!("unexpected cyclic definition of `{opaque_type_string}`");
280 self.dcx().span_delayed_bug(hidden_type.span, msg);
281 }
282 let _ = self.take_opaque_types();
283 }
284}