1use std::cell::Cell;
18use std::ops::{Bound, ControlFlow};
19use std::{assert_matches, iter};
20
21use rustc_abi::{ExternAbi, Size};
22use rustc_ast::Recovered;
23use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
24use rustc_errors::{
25 Applicability, Diag, DiagCtxtHandle, Diagnostic, E0228, ErrorGuaranteed, Level, StashKey,
26};
27use rustc_hir::def::{DefKind, Res};
28use rustc_hir::def_id::{DefId, LocalDefId};
29use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
30use rustc_hir::{self as hir, GenericParamKind, HirId, Node, PreciseCapturingArgKind, find_attr};
31use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
32use rustc_infer::traits::{DynCompatibilityViolation, ObligationCause};
33use rustc_middle::hir::nested_filter;
34use rustc_middle::query::Providers;
35use rustc_middle::ty::util::{Discr, IntTypeExt};
36use rustc_middle::ty::{
37 self, AdtKind, Const, IsSuggestable, Ty, TyCtxt, TypeVisitableExt, TypingMode, fold_regions,
38};
39use rustc_middle::{bug, span_bug};
40use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
41use rustc_trait_selection::error_reporting::traits::suggestions::NextTypeParamName;
42use rustc_trait_selection::infer::InferCtxtExt;
43use rustc_trait_selection::traits::{
44 FulfillmentError, ObligationCtxt, hir_ty_lowering_dyn_compatibility_violations,
45};
46use tracing::{debug, instrument};
47
48use crate::errors;
49use crate::hir_ty_lowering::{HirTyLowerer, InherentAssocCandidate, RegionInferReason};
50
51pub(crate) mod dump;
52mod generics_of;
53mod item_bounds;
54mod predicates_of;
55mod resolve_bound_vars;
56mod type_of;
57
58pub(crate) fn provide(providers: &mut Providers) {
62 resolve_bound_vars::provide(providers);
63 *providers = Providers {
64 type_of: type_of::type_of,
65 type_of_opaque: type_of::type_of_opaque,
66 type_of_opaque_hir_typeck: type_of::type_of_opaque_hir_typeck,
67 type_alias_is_lazy: type_of::type_alias_is_lazy,
68 item_bounds: item_bounds::item_bounds,
69 explicit_item_bounds: item_bounds::explicit_item_bounds,
70 item_self_bounds: item_bounds::item_self_bounds,
71 explicit_item_self_bounds: item_bounds::explicit_item_self_bounds,
72 item_non_self_bounds: item_bounds::item_non_self_bounds,
73 impl_super_outlives: item_bounds::impl_super_outlives,
74 generics_of: generics_of::generics_of,
75 predicates_of: predicates_of::predicates_of,
76 explicit_predicates_of: predicates_of::explicit_predicates_of,
77 explicit_super_predicates_of: predicates_of::explicit_super_predicates_of,
78 explicit_implied_predicates_of: predicates_of::explicit_implied_predicates_of,
79 explicit_supertraits_containing_assoc_item:
80 predicates_of::explicit_supertraits_containing_assoc_item,
81 trait_explicit_predicates_and_bounds: predicates_of::trait_explicit_predicates_and_bounds,
82 const_conditions: predicates_of::const_conditions,
83 explicit_implied_const_bounds: predicates_of::explicit_implied_const_bounds,
84 type_param_predicates: predicates_of::type_param_predicates,
85 trait_def,
86 adt_def,
87 fn_sig,
88 impl_trait_header,
89 coroutine_kind,
90 coroutine_for_closure,
91 opaque_ty_origin,
92 rendered_precise_capturing_args,
93 const_param_default,
94 anon_const_kind,
95 const_of_item,
96 ..*providers
97 };
98}
99
100pub(crate) struct ItemCtxt<'tcx> {
130 tcx: TyCtxt<'tcx>,
131 item_def_id: LocalDefId,
132 tainted_by_errors: Cell<Option<ErrorGuaranteed>>,
133}
134
135#[derive(#[automatically_derived]
impl ::core::default::Default for HirPlaceholderCollector {
#[inline]
fn default() -> HirPlaceholderCollector {
HirPlaceholderCollector {
spans: ::core::default::Default::default(),
may_contain_const_infer: ::core::default::Default::default(),
}
}
}Default)]
138pub(crate) struct HirPlaceholderCollector {
139 pub spans: Vec<Span>,
140 pub may_contain_const_infer: bool,
143}
144
145impl<'v> Visitor<'v> for HirPlaceholderCollector {
146 fn visit_infer(&mut self, _inf_id: HirId, inf_span: Span, kind: InferKind<'v>) -> Self::Result {
147 self.spans.push(inf_span);
148
149 if let InferKind::Const(_) | InferKind::Ambig(_) = kind {
150 self.may_contain_const_infer = true;
151 }
152 }
153}
154
155fn placeholder_type_error_diag<'cx, 'tcx>(
156 cx: &'cx dyn HirTyLowerer<'tcx>,
157 generics: Option<&hir::Generics<'_>>,
158 placeholder_types: Vec<Span>,
159 additional_spans: Vec<Span>,
160 suggest: bool,
161 hir_ty: Option<&hir::Ty<'_>>,
162 kind: &'static str,
163) -> Diag<'cx> {
164 if placeholder_types.is_empty() {
165 return bad_placeholder(cx, additional_spans, kind);
166 }
167
168 let params = generics.map(|g| g.params).unwrap_or_default();
169 let type_name = params.next_type_param_name(None);
170 let mut sugg: Vec<_> =
171 placeholder_types.iter().map(|sp| (*sp, (*type_name).to_string())).collect();
172
173 if let Some(generics) = generics {
174 if let Some(span) = params.iter().find_map(|arg| match arg.name {
175 hir::ParamName::Plain(Ident { name: kw::Underscore, span }) => Some(span),
176 _ => None,
177 }) {
178 sugg.push((span, (*type_name).to_string()));
181 } else if let Some(span) = generics.span_for_param_suggestion() {
182 sugg.push((span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(", {0}", type_name))
})format!(", {type_name}")));
184 } else {
185 sugg.push((generics.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}>", type_name))
})format!("<{type_name}>")));
186 }
187 }
188
189 let mut err =
190 bad_placeholder(cx, placeholder_types.into_iter().chain(additional_spans).collect(), kind);
191
192 if suggest {
194 let mut is_fn = false;
195 let mut is_const_or_static = false;
196
197 if let Some(hir_ty) = hir_ty
198 && let hir::TyKind::FnPtr(_) = hir_ty.kind
199 {
200 is_fn = true;
201
202 is_const_or_static = #[allow(non_exhaustive_omitted_patterns)] match cx.tcx().parent_hir_node(hir_ty.hir_id)
{
Node::Item(&hir::Item {
kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..), .. }) |
Node::TraitItem(&hir::TraitItem { kind: hir::TraitItemKind::Const(..),
.. }) |
Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Const(..), ..
}) => true,
_ => false,
}matches!(
204 cx.tcx().parent_hir_node(hir_ty.hir_id),
205 Node::Item(&hir::Item {
206 kind: hir::ItemKind::Const(..) | hir::ItemKind::Static(..),
207 ..
208 }) | Node::TraitItem(&hir::TraitItem { kind: hir::TraitItemKind::Const(..), .. })
209 | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Const(..), .. })
210 );
211 }
212
213 if !(is_fn && is_const_or_static) {
216 err.multipart_suggestion(
217 "use type parameters instead",
218 sugg,
219 Applicability::HasPlaceholders,
220 );
221 }
222 }
223
224 err
225}
226
227fn bad_placeholder<'cx, 'tcx>(
231 cx: &'cx dyn HirTyLowerer<'tcx>,
232 mut spans: Vec<Span>,
233 kind: &'static str,
234) -> Diag<'cx> {
235 let kind = if kind.ends_with('s') { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}es", kind))
})format!("{kind}es") } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}s", kind))
})format!("{kind}s") };
236
237 spans.sort();
238 cx.dcx().create_err(errors::PlaceholderNotAllowedItemSignatures { spans, kind })
239}
240
241impl<'tcx> ItemCtxt<'tcx> {
242 pub(crate) fn new(tcx: TyCtxt<'tcx>, item_def_id: LocalDefId) -> ItemCtxt<'tcx> {
243 ItemCtxt { tcx, item_def_id, tainted_by_errors: Cell::new(None) }
244 }
245
246 pub(crate) fn lower_ty(&self, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
247 self.lowerer().lower_ty(hir_ty)
248 }
249
250 pub(crate) fn hir_id(&self) -> hir::HirId {
251 self.tcx.local_def_id_to_hir_id(self.item_def_id)
252 }
253
254 pub(crate) fn node(&self) -> hir::Node<'tcx> {
255 self.tcx.hir_node(self.hir_id())
256 }
257
258 fn check_tainted_by_errors(&self) -> Result<(), ErrorGuaranteed> {
259 match self.tainted_by_errors.get() {
260 Some(err) => Err(err),
261 None => Ok(()),
262 }
263 }
264
265 fn report_placeholder_type_error(
266 &self,
267 placeholder_types: Vec<Span>,
268 infer_replacements: Vec<(Span, String)>,
269 ) -> ErrorGuaranteed {
270 let node = self.tcx.hir_node_by_def_id(self.item_def_id);
271 let generics = node.generics();
272 let kind_id = match node {
273 Node::GenericParam(_) | Node::WherePredicate(_) | Node::Field(_) => {
274 self.tcx.local_parent(self.item_def_id)
275 }
276 _ => self.item_def_id,
277 };
278 let kind = self.tcx.def_descr(kind_id.into());
279 let mut diag = placeholder_type_error_diag(
280 self,
281 generics,
282 placeholder_types,
283 infer_replacements.iter().map(|&(span, _)| span).collect(),
284 false,
285 None,
286 kind,
287 );
288 if !infer_replacements.is_empty() {
289 diag.multipart_suggestion(
290 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("try replacing `_` with the type{0} in the corresponding trait method signature",
if infer_replacements.len() == 1 { "" } else { "s" }))
})format!(
291 "try replacing `_` with the type{} in the corresponding trait method \
292 signature",
293 rustc_errors::pluralize!(infer_replacements.len()),
294 ),
295 infer_replacements,
296 Applicability::MachineApplicable,
297 );
298 }
299
300 diag.emit()
301 }
302}
303
304impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
305 fn tcx(&self) -> TyCtxt<'tcx> {
306 self.tcx
307 }
308
309 fn dcx(&self) -> DiagCtxtHandle<'_> {
310 self.tcx.dcx().taintable_handle(&self.tainted_by_errors)
311 }
312
313 fn item_def_id(&self) -> LocalDefId {
314 self.item_def_id
315 }
316
317 fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
318 if let RegionInferReason::ObjectLifetimeDefault(sugg_sp) = reason {
319 let guar = self
322 .dcx()
323 .struct_span_err(
324 span,
325 "cannot deduce the lifetime bound for this trait object type from context",
326 )
327 .with_code(E0228)
328 .with_span_suggestion_verbose(
329 sugg_sp,
330 "please supply an explicit bound",
331 " + /* 'a */",
332 Applicability::HasPlaceholders,
333 )
334 .emit();
335 ty::Region::new_error(self.tcx(), guar)
336 } else {
337 ty::Region::new_error_with_message(self.tcx(), span, "unelided lifetime in signature")
339 }
340 }
341
342 fn ty_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Ty<'tcx> {
343 if !self.tcx.dcx().has_stashed_diagnostic(span, StashKey::ItemNoType) {
344 self.report_placeholder_type_error(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[span]))vec![span], ::alloc::vec::Vec::new()vec![]);
345 }
346 Ty::new_error_with_message(self.tcx(), span, "bad placeholder type")
347 }
348
349 fn ct_infer(&self, _: Option<&ty::GenericParamDef>, span: Span) -> Const<'tcx> {
350 self.report_placeholder_type_error(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[span]))vec![span], ::alloc::vec::Vec::new()vec![]);
351 ty::Const::new_error_with_message(self.tcx(), span, "bad placeholder constant")
352 }
353
354 fn register_trait_ascription_bounds(
355 &self,
356 _: Vec<(ty::Clause<'tcx>, Span)>,
357 _: HirId,
358 span: Span,
359 ) {
360 self.dcx().span_delayed_bug(span, "trait ascription type not allowed here");
361 }
362
363 fn probe_ty_param_bounds(
364 &self,
365 span: Span,
366 def_id: LocalDefId,
367 assoc_ident: Ident,
368 ) -> ty::EarlyBinder<'tcx, &'tcx [(ty::Clause<'tcx>, Span)]> {
369 self.tcx.at(span).type_param_predicates((self.item_def_id, def_id, assoc_ident))
370 }
371
372 x;#[instrument(level = "debug", skip(self, _span), ret)]
373 fn select_inherent_assoc_candidates(
374 &self,
375 _span: Span,
376 self_ty: Ty<'tcx>,
377 candidates: Vec<InherentAssocCandidate>,
378 ) -> (Vec<InherentAssocCandidate>, Vec<FulfillmentError<'tcx>>) {
379 assert!(!self_ty.has_infer());
380
381 let self_ty = self.tcx.expand_free_alias_tys(self_ty);
386 debug!("select_inherent_assoc_candidates: self_ty={:?}", self_ty);
387
388 let candidates = candidates
389 .into_iter()
390 .filter(|&InherentAssocCandidate { impl_, .. }| {
391 let impl_ty = self.tcx().type_of(impl_).instantiate_identity();
392
393 let impl_ty = self.tcx.expand_free_alias_tys(impl_ty);
395 debug!("select_inherent_assoc_candidates: impl_ty={:?}", impl_ty);
396
397 ty::DeepRejectCtxt::relate_rigid_infer(self.tcx).types_may_unify_with_depth(
414 self_ty,
415 impl_ty,
416 usize::MAX,
417 )
418 })
419 .collect();
420
421 (candidates, vec![])
422 }
423
424 fn lower_assoc_item_path(
425 &self,
426 span: Span,
427 item_def_id: DefId,
428 item_segment: &rustc_hir::PathSegment<'tcx>,
429 poly_trait_ref: ty::PolyTraitRef<'tcx>,
430 ) -> Result<(DefId, ty::GenericArgsRef<'tcx>), ErrorGuaranteed> {
431 if let Some(trait_ref) = poly_trait_ref.no_bound_vars() {
432 let item_args = self.lowerer().lower_generic_args_of_assoc_item(
433 span,
434 item_def_id,
435 item_segment,
436 trait_ref.args,
437 );
438 Ok((item_def_id, item_args))
439 } else {
440 let (mut mpart_sugg, mut inferred_sugg) = (None, None);
442 let mut bound = String::new();
443
444 match self.node() {
445 hir::Node::Field(_) | hir::Node::Ctor(_) | hir::Node::Variant(_) => {
446 let item = self
447 .tcx
448 .hir_expect_item(self.tcx.hir_get_parent_item(self.hir_id()).def_id);
449 match &item.kind {
450 hir::ItemKind::Enum(_, generics, _)
451 | hir::ItemKind::Struct(_, generics, _)
452 | hir::ItemKind::Union(_, generics, _) => {
453 let lt_name = get_new_lifetime_name(self.tcx, poly_trait_ref, generics);
454 let (lt_sp, sugg) = match generics.params {
455 [] => (generics.span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("<{0}>", lt_name))
})format!("<{lt_name}>")),
456 [bound, ..] => (bound.span.shrink_to_lo(), ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}, ", lt_name))
})format!("{lt_name}, ")),
457 };
458 mpart_sugg = Some(errors::AssociatedItemTraitUninferredGenericParamsMultipartSuggestion {
459 fspan: lt_sp,
460 first: sugg,
461 sspan: span.with_hi(item_segment.ident.span.lo()),
462 second: ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::",
self.tcx.instantiate_bound_regions_uncached(poly_trait_ref,
|_|
{
ty::Region::new_early_param(self.tcx,
ty::EarlyParamRegion {
index: 0,
name: Symbol::intern(<_name),
})
})))
})format!(
463 "{}::",
464 self.tcx.instantiate_bound_regions_uncached(
466 poly_trait_ref,
467 |_| {
468 ty::Region::new_early_param(self.tcx, ty::EarlyParamRegion {
469 index: 0,
470 name: Symbol::intern(<_name),
471 })
472 }
473 ),
474 ),
475 });
476 }
477 _ => {}
478 }
479 }
480 hir::Node::Item(hir::Item {
481 kind:
482 hir::ItemKind::Struct(..) | hir::ItemKind::Enum(..) | hir::ItemKind::Union(..),
483 ..
484 }) => {}
485 hir::Node::Item(_)
486 | hir::Node::ForeignItem(_)
487 | hir::Node::TraitItem(_)
488 | hir::Node::ImplItem(_) => {
489 inferred_sugg = Some(span.with_hi(item_segment.ident.span.lo()));
490 bound = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}::",
self.tcx.anonymize_bound_vars(poly_trait_ref).skip_binder()))
})format!(
491 "{}::",
492 self.tcx.anonymize_bound_vars(poly_trait_ref).skip_binder(),
494 );
495 }
496 _ => {}
497 }
498
499 Err(self.tcx().dcx().emit_err(errors::AssociatedItemTraitUninferredGenericParams {
500 span,
501 inferred_sugg,
502 bound,
503 mpart_sugg,
504 what: self.tcx.def_descr(item_def_id),
505 }))
506 }
507 }
508
509 fn probe_adt(&self, _span: Span, ty: Ty<'tcx>) -> Option<ty::AdtDef<'tcx>> {
510 ty.ty_adt_def()
512 }
513
514 fn record_ty(&self, _hir_id: hir::HirId, _ty: Ty<'tcx>, _span: Span) {
515 }
517
518 fn infcx(&self) -> Option<&InferCtxt<'tcx>> {
519 None
520 }
521
522 fn lower_fn_sig(
523 &self,
524 decl: &hir::FnDecl<'tcx>,
525 _generics: Option<&hir::Generics<'_>>,
526 hir_id: rustc_hir::HirId,
527 _hir_ty: Option<&hir::Ty<'_>>,
528 ) -> (Vec<Ty<'tcx>>, Ty<'tcx>) {
529 let tcx = self.tcx();
530
531 let mut infer_replacements = ::alloc::vec::Vec::new()vec![];
532
533 let input_tys = decl
534 .inputs
535 .iter()
536 .enumerate()
537 .map(|(i, a)| {
538 if let hir::TyKind::Infer(()) = a.kind
539 && let Some(suggested_ty) =
540 self.lowerer().suggest_trait_fn_ty_for_impl_fn_infer(hir_id, Some(i))
541 {
542 infer_replacements.push((a.span, suggested_ty.to_string()));
543 return Ty::new_error_with_message(tcx, a.span, suggested_ty.to_string());
544 }
545
546 self.lowerer().lower_ty(a)
547 })
548 .collect();
549
550 let output_ty = match decl.output {
551 hir::FnRetTy::Return(output) => {
552 if let hir::TyKind::Infer(()) = output.kind
553 && let Some(suggested_ty) =
554 self.lowerer().suggest_trait_fn_ty_for_impl_fn_infer(hir_id, None)
555 {
556 infer_replacements.push((output.span, suggested_ty.to_string()));
557 Ty::new_error_with_message(tcx, output.span, suggested_ty.to_string())
558 } else {
559 self.lower_ty(output)
560 }
561 }
562 hir::FnRetTy::DefaultReturn(..) => tcx.types.unit,
563 };
564
565 if !infer_replacements.is_empty() {
566 self.report_placeholder_type_error(::alloc::vec::Vec::new()vec![], infer_replacements);
567 }
568 (input_tys, output_ty)
569 }
570
571 fn dyn_compatibility_violations(&self, trait_def_id: DefId) -> Vec<DynCompatibilityViolation> {
572 hir_ty_lowering_dyn_compatibility_violations(self.tcx, trait_def_id)
573 }
574}
575
576fn get_new_lifetime_name<'tcx>(
578 tcx: TyCtxt<'tcx>,
579 poly_trait_ref: ty::PolyTraitRef<'tcx>,
580 generics: &hir::Generics<'tcx>,
581) -> String {
582 let existing_lifetimes = tcx
583 .collect_referenced_late_bound_regions(poly_trait_ref)
584 .into_iter()
585 .filter_map(|lt| lt.get_name(tcx).map(|name| name.as_str().to_string()))
586 .chain(generics.params.iter().filter_map(|param| {
587 if let hir::GenericParamKind::Lifetime { .. } = ¶m.kind {
588 Some(param.name.ident().as_str().to_string())
589 } else {
590 None
591 }
592 }))
593 .collect::<FxHashSet<String>>();
594
595 let a_to_z_repeat_n = |n| {
596 (b'a'..=b'z').map(move |c| {
597 let mut s = '\''.to_string();
598 s.extend(std::iter::repeat_n(char::from(c), n));
599 s
600 })
601 };
602
603 (1..).flat_map(a_to_z_repeat_n).find(|lt| !existing_lifetimes.contains(lt.as_str())).unwrap()
605}
606
607pub(super) fn lower_variant_ctor(tcx: TyCtxt<'_>, def_id: LocalDefId) {
608 tcx.ensure_ok().generics_of(def_id);
609 tcx.ensure_ok().type_of(def_id);
610 tcx.ensure_ok().predicates_of(def_id);
611}
612
613pub(super) fn lower_enum_variant_types(tcx: TyCtxt<'_>, def_id: LocalDefId) {
614 struct ReprCIssue {
615 msg: &'static str,
616 }
617
618 impl<'a> Diagnostic<'a, ()> for ReprCIssue {
619 fn into_diag(self, dcx: DiagCtxtHandle<'a>, level: Level) -> Diag<'a, ()> {
620 let Self { msg } = self;
621 Diag::new(dcx, level, msg)
622 .with_note("`repr(C)` enums with big discriminants are non-portable, and their size in Rust might not match their size in C")
623 .with_help("use `repr($int_ty)` instead to explicitly set the size of this enum")
624 }
625 }
626
627 let def = tcx.adt_def(def_id);
628 let repr_type = def.repr().discr_type();
629 let initial = repr_type.initial_discriminant(tcx);
630 let mut prev_discr = None::<Discr<'_>>;
631 if !(tcx.sess.target.c_int_width < 128) {
::core::panicking::panic("assertion failed: tcx.sess.target.c_int_width < 128")
};assert!(tcx.sess.target.c_int_width < 128);
633 let mut min_discr = i128::MAX;
634 let mut max_discr = i128::MIN;
635
636 for variant in def.variants() {
638 let wrapped_discr = prev_discr.map_or(initial, |d| d.wrap_incr(tcx));
639 let cur_discr = if let ty::VariantDiscr::Explicit(const_def_id) = variant.discr {
640 def.eval_explicit_discr(tcx, const_def_id).ok()
641 } else if let Some(discr) = repr_type.disr_incr(tcx, prev_discr) {
642 Some(discr)
643 } else {
644 let span = tcx.def_span(variant.def_id);
645 tcx.dcx().emit_err(errors::EnumDiscriminantOverflowed {
646 span,
647 discr: prev_discr.unwrap().to_string(),
648 item_name: tcx.item_ident(variant.def_id),
649 wrapped_discr: wrapped_discr.to_string(),
650 });
651 None
652 }
653 .unwrap_or(wrapped_discr);
654
655 if def.repr().c() {
656 let c_int = Size::from_bits(tcx.sess.target.c_int_width);
657 let c_uint_max = i128::try_from(c_int.unsigned_int_max()).unwrap();
658 let discr_size = cur_discr.ty.int_size_and_signed(tcx).0;
660 let discr_val = discr_size.sign_extend(cur_discr.val);
661 min_discr = min_discr.min(discr_val);
662 max_discr = max_discr.max(discr_val);
663
664 if !(min_discr >= c_int.signed_int_min() && max_discr <= c_int.signed_int_max())
666 && !(min_discr >= 0 && max_discr <= c_uint_max)
667 {
668 let span = tcx.def_span(variant.def_id);
669 let msg = if discr_val < c_int.signed_int_min() || discr_val > c_uint_max {
670 "`repr(C)` enum discriminant does not fit into C `int` nor into C `unsigned int`"
671 } else if discr_val < 0 {
672 "`repr(C)` enum discriminant does not fit into C `unsigned int`, and a previous discriminant does not fit into C `int`"
673 } else {
674 "`repr(C)` enum discriminant does not fit into C `int`, and a previous discriminant does not fit into C `unsigned int`"
675 };
676 tcx.emit_node_span_lint(
677 rustc_session::lint::builtin::REPR_C_ENUMS_LARGER_THAN_INT,
678 tcx.local_def_id_to_hir_id(def_id),
679 span,
680 ReprCIssue { msg },
681 );
682 }
683 }
684
685 prev_discr = Some(cur_discr);
686
687 for f in &variant.fields {
688 tcx.ensure_ok().generics_of(f.did);
689 tcx.ensure_ok().type_of(f.did);
690 tcx.ensure_ok().predicates_of(f.did);
691 }
692
693 if let Some(ctor_def_id) = variant.ctor_def_id() {
695 lower_variant_ctor(tcx, ctor_def_id.expect_local());
696 }
697 }
698}
699
700#[derive(#[automatically_derived]
impl ::core::clone::Clone for NestedSpan {
#[inline]
fn clone(&self) -> NestedSpan {
let _: ::core::clone::AssertParamIsClone<Span>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for NestedSpan { }Copy)]
701struct NestedSpan {
702 span: Span,
703 nested_field_span: Span,
704}
705
706impl NestedSpan {
707 fn to_field_already_declared_nested_help(&self) -> errors::FieldAlreadyDeclaredNestedHelp {
708 errors::FieldAlreadyDeclaredNestedHelp { span: self.span }
709 }
710}
711
712#[derive(#[automatically_derived]
impl ::core::clone::Clone for FieldDeclSpan {
#[inline]
fn clone(&self) -> FieldDeclSpan {
let _: ::core::clone::AssertParamIsClone<Span>;
let _: ::core::clone::AssertParamIsClone<NestedSpan>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for FieldDeclSpan { }Copy)]
713enum FieldDeclSpan {
714 NotNested(Span),
715 Nested(NestedSpan),
716}
717
718impl From<Span> for FieldDeclSpan {
719 fn from(span: Span) -> Self {
720 Self::NotNested(span)
721 }
722}
723
724impl From<NestedSpan> for FieldDeclSpan {
725 fn from(span: NestedSpan) -> Self {
726 Self::Nested(span)
727 }
728}
729
730struct FieldUniquenessCheckContext<'tcx> {
731 tcx: TyCtxt<'tcx>,
732 seen_fields: FxIndexMap<Ident, FieldDeclSpan>,
733}
734
735impl<'tcx> FieldUniquenessCheckContext<'tcx> {
736 fn new(tcx: TyCtxt<'tcx>) -> Self {
737 Self { tcx, seen_fields: FxIndexMap::default() }
738 }
739
740 fn check_field_decl(&mut self, field_name: Ident, field_decl: FieldDeclSpan) {
742 use FieldDeclSpan::*;
743 let field_name = field_name.normalize_to_macros_2_0();
744 match (field_decl, self.seen_fields.get(&field_name).copied()) {
745 (NotNested(span), Some(NotNested(prev_span))) => {
746 self.tcx.dcx().emit_err(errors::FieldAlreadyDeclared::NotNested {
747 field_name,
748 span,
749 prev_span,
750 });
751 }
752 (NotNested(span), Some(Nested(prev))) => {
753 self.tcx.dcx().emit_err(errors::FieldAlreadyDeclared::PreviousNested {
754 field_name,
755 span,
756 prev_span: prev.span,
757 prev_nested_field_span: prev.nested_field_span,
758 prev_help: prev.to_field_already_declared_nested_help(),
759 });
760 }
761 (
762 Nested(current @ NestedSpan { span, nested_field_span, .. }),
763 Some(NotNested(prev_span)),
764 ) => {
765 self.tcx.dcx().emit_err(errors::FieldAlreadyDeclared::CurrentNested {
766 field_name,
767 span,
768 nested_field_span,
769 help: current.to_field_already_declared_nested_help(),
770 prev_span,
771 });
772 }
773 (Nested(current @ NestedSpan { span, nested_field_span }), Some(Nested(prev))) => {
774 self.tcx.dcx().emit_err(errors::FieldAlreadyDeclared::BothNested {
775 field_name,
776 span,
777 nested_field_span,
778 help: current.to_field_already_declared_nested_help(),
779 prev_span: prev.span,
780 prev_nested_field_span: prev.nested_field_span,
781 prev_help: prev.to_field_already_declared_nested_help(),
782 });
783 }
784 (field_decl, None) => {
785 self.seen_fields.insert(field_name, field_decl);
786 }
787 }
788 }
789}
790
791fn lower_variant<'tcx>(
792 tcx: TyCtxt<'tcx>,
793 variant_did: Option<LocalDefId>,
794 ident: Ident,
795 discr: ty::VariantDiscr,
796 def: &hir::VariantData<'tcx>,
797 adt_kind: ty::AdtKind,
798 parent_did: LocalDefId,
799) -> ty::VariantDef {
800 let mut field_uniqueness_check_ctx = FieldUniquenessCheckContext::new(tcx);
801 let fields = def
802 .fields()
803 .iter()
804 .inspect(|field| {
805 field_uniqueness_check_ctx.check_field_decl(field.ident, field.span.into());
806 })
807 .map(|f| ty::FieldDef {
808 did: f.def_id.to_def_id(),
809 name: f.ident.name,
810 vis: tcx.visibility(f.def_id),
811 safety: f.safety,
812 value: f.default.map(|v| v.def_id.to_def_id()),
813 })
814 .collect();
815 let recovered = match def {
816 hir::VariantData::Struct { recovered: Recovered::Yes(guar), .. } => Some(*guar),
817 _ => None,
818 };
819 ty::VariantDef::new(
820 ident.name,
821 variant_did.map(LocalDefId::to_def_id),
822 def.ctor().map(|(kind, _, def_id)| (kind, def_id.to_def_id())),
823 discr,
824 fields,
825 parent_did.to_def_id(),
826 recovered,
827 adt_kind == AdtKind::Struct && {
#[allow(deprecated)]
{
{
'done:
{
for i in tcx.get_all_attrs(parent_did) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(NonExhaustive(..)) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}.is_some()find_attr!(tcx, parent_did, NonExhaustive(..))
828 || variant_did
829 .is_some_and(|variant_did| {
#[allow(deprecated)]
{
{
'done:
{
for i in tcx.get_all_attrs(variant_did) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(NonExhaustive(..)) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}.is_some()find_attr!(tcx, variant_did, NonExhaustive(..))),
830 )
831}
832
833fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
834 use rustc_hir::*;
835
836 let Node::Item(item) = tcx.hir_node_by_def_id(def_id) else {
837 ::rustc_middle::util::bug::bug_fmt(format_args!("expected ADT to be an item"));bug!("expected ADT to be an item");
838 };
839
840 let repr = tcx.repr_options_of_def(def_id);
841 let (kind, variants) = match &item.kind {
842 ItemKind::Enum(_, _, def) => {
843 let mut distance_from_explicit = 0;
844 let variants = def
845 .variants
846 .iter()
847 .map(|v| {
848 let discr = if let Some(e) = &v.disr_expr {
849 distance_from_explicit = 0;
850 ty::VariantDiscr::Explicit(e.def_id.to_def_id())
851 } else {
852 ty::VariantDiscr::Relative(distance_from_explicit)
853 };
854 distance_from_explicit += 1;
855
856 lower_variant(
857 tcx,
858 Some(v.def_id),
859 v.ident,
860 discr,
861 &v.data,
862 AdtKind::Enum,
863 def_id,
864 )
865 })
866 .collect();
867
868 (AdtKind::Enum, variants)
869 }
870 ItemKind::Struct(ident, _, def) | ItemKind::Union(ident, _, def) => {
871 let adt_kind = match item.kind {
872 ItemKind::Struct(..) => AdtKind::Struct,
873 _ => AdtKind::Union,
874 };
875 let variants = std::iter::once(lower_variant(
876 tcx,
877 None,
878 *ident,
879 ty::VariantDiscr::Relative(0),
880 def,
881 adt_kind,
882 def_id,
883 ))
884 .collect();
885
886 (adt_kind, variants)
887 }
888 _ => ::rustc_middle::util::bug::bug_fmt(format_args!("{0:?} is not an ADT",
item.owner_id.def_id))bug!("{:?} is not an ADT", item.owner_id.def_id),
889 };
890 tcx.mk_adt_def(def_id.to_def_id(), kind, variants, repr)
891}
892
893fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
894 let item = tcx.hir_expect_item(def_id);
895
896 let (constness, is_alias, is_auto, safety) = match item.kind {
897 hir::ItemKind::Trait(constness, is_auto, safety, ..) => {
898 (constness, false, is_auto == hir::IsAuto::Yes, safety)
899 }
900 hir::ItemKind::TraitAlias(constness, ..) => (constness, true, false, hir::Safety::Safe),
901 _ => ::rustc_middle::util::bug::span_bug_fmt(item.span,
format_args!("trait_def_of_item invoked on non-trait"))span_bug!(item.span, "trait_def_of_item invoked on non-trait"),
902 };
903
904 #[allow(deprecated)]
906 let attrs = tcx.get_all_attrs(def_id);
907
908 let paren_sugar = {
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcParenSugar(_)) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, RustcParenSugar(_));
909 if paren_sugar && !tcx.features().unboxed_closures() {
910 tcx.dcx().emit_err(errors::ParenSugarAttribute { span: item.span });
911 }
912
913 let is_marker = !is_alias && {
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(Marker(_)) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, Marker(_));
915
916 let rustc_coinductive = {
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcCoinductive(_)) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, RustcCoinductive(_));
917 let is_fundamental = {
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(Fundamental) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, Fundamental);
918
919 let [skip_array_during_method_dispatch, skip_boxed_slice_during_method_dispatch] = {
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcSkipDuringMethodDispatch {
array, boxed_slice, span: _ }) => {
break 'done Some([*array, *boxed_slice]);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(
920 attrs,
921 RustcSkipDuringMethodDispatch { array, boxed_slice, span: _ } => [*array, *boxed_slice]
922 )
923 .unwrap_or([false; 2]);
924
925 let specialization_kind = if {
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcUnsafeSpecializationMarker(_))
=> {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, RustcUnsafeSpecializationMarker(_)) {
926 ty::trait_def::TraitSpecializationKind::Marker
927 } else if {
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcSpecializationTrait(_)) =>
{
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, RustcSpecializationTrait(_)) {
928 ty::trait_def::TraitSpecializationKind::AlwaysApplicable
929 } else {
930 ty::trait_def::TraitSpecializationKind::None
931 };
932
933 let must_implement_one_of = {
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcMustImplementOneOf {
fn_names, .. }) => {
break 'done
Some(fn_names.iter().cloned().collect::<Box<[_]>>());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(
934 attrs,
935 RustcMustImplementOneOf { fn_names, .. } =>
936 fn_names
937 .iter()
938 .cloned()
939 .collect::<Box<[_]>>()
940 );
941
942 let deny_explicit_impl = {
{
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcDenyExplicitImpl(_)) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}.is_some()
}find_attr!(attrs, RustcDenyExplicitImpl(_));
943 let force_dyn_incompatible = {
'done:
{
for i in attrs {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcDynIncompatibleTrait(span))
=> {
break 'done Some(*span);
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}find_attr!(attrs, RustcDynIncompatibleTrait(span) => *span);
944
945 ty::TraitDef {
946 def_id: def_id.to_def_id(),
947 safety,
948 constness,
949 paren_sugar,
950 has_auto_impl: is_auto,
951 is_marker,
952 is_coinductive: rustc_coinductive || is_auto,
953 is_fundamental,
954 skip_array_during_method_dispatch,
955 skip_boxed_slice_during_method_dispatch,
956 specialization_kind,
957 must_implement_one_of,
958 force_dyn_incompatible,
959 deny_explicit_impl,
960 }
961}
962
963x;#[instrument(level = "debug", skip(tcx), ret)]
964fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFnSig<'_>> {
965 use rustc_hir::Node::*;
966 use rustc_hir::*;
967
968 let hir_id = tcx.local_def_id_to_hir_id(def_id);
969
970 let icx = ItemCtxt::new(tcx, def_id);
971
972 let output = match tcx.hir_node(hir_id) {
973 TraitItem(hir::TraitItem {
974 kind: TraitItemKind::Fn(sig, TraitFn::Provided(_)),
975 generics,
976 ..
977 })
978 | Item(hir::Item { kind: ItemKind::Fn { sig, generics, .. }, .. }) => {
979 lower_fn_sig_recovering_infer_ret_ty(&icx, sig, generics, def_id)
980 }
981
982 ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => {
983 if let Item(hir::Item { kind: ItemKind::Impl(i), .. }) = tcx.parent_hir_node(hir_id)
985 && i.of_trait.is_some()
986 {
987 icx.lowerer().lower_fn_ty(
988 hir_id,
989 sig.header.safety(),
990 sig.header.abi,
991 sig.decl,
992 Some(generics),
993 None,
994 )
995 } else {
996 lower_fn_sig_recovering_infer_ret_ty(&icx, sig, generics, def_id)
997 }
998 }
999
1000 TraitItem(hir::TraitItem {
1001 kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _),
1002 generics,
1003 ..
1004 }) => icx.lowerer().lower_fn_ty(
1005 hir_id,
1006 header.safety(),
1007 header.abi,
1008 decl,
1009 Some(generics),
1010 None,
1011 ),
1012
1013 ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(sig, _, _), .. }) => {
1014 let abi = tcx.hir_get_foreign_abi(hir_id);
1015 compute_sig_of_foreign_fn_decl(tcx, def_id, sig.decl, abi, sig.header.safety())
1016 }
1017
1018 Ctor(data) => {
1019 assert_matches!(data.ctor(), Some(_));
1020 let adt_def_id = tcx.hir_get_parent_item(hir_id).def_id.to_def_id();
1021 let ty = tcx.type_of(adt_def_id).instantiate_identity();
1022 let inputs = data.fields().iter().map(|f| tcx.type_of(f.def_id).instantiate_identity());
1023 let safety = match tcx.layout_scalar_valid_range(adt_def_id) {
1025 (Bound::Unbounded, Bound::Unbounded) => hir::Safety::Safe,
1026 _ => hir::Safety::Unsafe,
1027 };
1028 ty::Binder::dummy(tcx.mk_fn_sig(inputs, ty, false, safety, ExternAbi::Rust))
1029 }
1030
1031 Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
1032 bug!("to get the signature of a closure, use `args.as_closure().sig()` not `fn_sig()`",);
1043 }
1044
1045 x => {
1046 bug!("unexpected sort of node in fn_sig(): {:?}", x);
1047 }
1048 };
1049 ty::EarlyBinder::bind(output)
1050}
1051
1052fn lower_fn_sig_recovering_infer_ret_ty<'tcx>(
1053 icx: &ItemCtxt<'tcx>,
1054 sig: &'tcx hir::FnSig<'tcx>,
1055 generics: &'tcx hir::Generics<'tcx>,
1056 def_id: LocalDefId,
1057) -> ty::PolyFnSig<'tcx> {
1058 if let Some(infer_ret_ty) = sig.decl.output.is_suggestable_infer_ty() {
1059 return recover_infer_ret_ty(icx, infer_ret_ty, generics, def_id);
1060 }
1061
1062 icx.lowerer().lower_fn_ty(
1063 icx.tcx().local_def_id_to_hir_id(def_id),
1064 sig.header.safety(),
1065 sig.header.abi,
1066 sig.decl,
1067 Some(generics),
1068 None,
1069 )
1070}
1071
1072fn late_param_regions_to_bound<'tcx, T>(
1074 tcx: TyCtxt<'tcx>,
1075 scope: DefId,
1076 bound_vars: &'tcx ty::List<ty::BoundVariableKind<'tcx>>,
1077 value: T,
1078) -> ty::Binder<'tcx, T>
1079where
1080 T: ty::TypeFoldable<TyCtxt<'tcx>>,
1081{
1082 let value = fold_regions(tcx, value, |r, debruijn| match r.kind() {
1083 ty::ReLateParam(lp) => {
1084 match (&lp.scope, &scope) {
(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);
}
}
};assert_eq!(lp.scope, scope);
1086
1087 let br = match lp.kind {
1088 kind @ (ty::LateParamRegionKind::Anon(idx)
1090 | ty::LateParamRegionKind::NamedAnon(idx, _)) => {
1091 let idx = idx as usize;
1092 let var = ty::BoundVar::from_usize(idx);
1093
1094 let Some(ty::BoundVariableKind::Region(kind)) = bound_vars.get(idx).copied()
1095 else {
1096 ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected late-bound region {0:?} for bound vars {1:?}",
kind, bound_vars));bug!("unexpected late-bound region {kind:?} for bound vars {bound_vars:?}");
1097 };
1098
1099 ty::BoundRegion { var, kind }
1100 }
1101
1102 ty::LateParamRegionKind::Named(def_id) => bound_vars
1104 .iter()
1105 .enumerate()
1106 .find_map(|(idx, bv)| match bv {
1107 ty::BoundVariableKind::Region(kind @ ty::BoundRegionKind::Named(did))
1108 if did == def_id =>
1109 {
1110 Some(ty::BoundRegion { var: ty::BoundVar::from_usize(idx), kind })
1111 }
1112 _ => None,
1113 })
1114 .unwrap(),
1115
1116 ty::LateParamRegionKind::ClosureEnv => bound_vars
1117 .iter()
1118 .enumerate()
1119 .find_map(|(idx, bv)| match bv {
1120 ty::BoundVariableKind::Region(kind @ ty::BoundRegionKind::ClosureEnv) => {
1121 Some(ty::BoundRegion { var: ty::BoundVar::from_usize(idx), kind })
1122 }
1123 _ => None,
1124 })
1125 .unwrap(),
1126 };
1127
1128 ty::Region::new_bound(tcx, debruijn, br)
1129 }
1130 _ => r,
1131 });
1132
1133 ty::Binder::bind_with_vars(value, bound_vars)
1134}
1135
1136fn recover_infer_ret_ty<'tcx>(
1137 icx: &ItemCtxt<'tcx>,
1138 infer_ret_ty: &'tcx hir::Ty<'tcx>,
1139 generics: &'tcx hir::Generics<'tcx>,
1140 def_id: LocalDefId,
1141) -> ty::PolyFnSig<'tcx> {
1142 let tcx = icx.tcx;
1143 let hir_id = tcx.local_def_id_to_hir_id(def_id);
1144
1145 let fn_sig = tcx.typeck(def_id).liberated_fn_sigs()[hir_id];
1146
1147 let has_region_params = generics.params.iter().any(|param| match param.kind {
1152 GenericParamKind::Lifetime { .. } => true,
1153 _ => false,
1154 });
1155 let fn_sig = fold_regions(tcx, fn_sig, |r, _| match r.kind() {
1156 ty::ReErased => {
1157 if has_region_params {
1158 ty::Region::new_error_with_message(
1159 tcx,
1160 DUMMY_SP,
1161 "erased region is not allowed here in return type",
1162 )
1163 } else {
1164 tcx.lifetimes.re_static
1165 }
1166 }
1167 _ => r,
1168 });
1169
1170 let mut visitor = HirPlaceholderCollector::default();
1171 visitor.visit_ty_unambig(infer_ret_ty);
1172
1173 let mut diag = bad_placeholder(icx.lowerer(), visitor.spans, "return type");
1174 let ret_ty = fn_sig.output();
1175
1176 let mut recovered_ret_ty = None;
1180 if let Some(suggestable_ret_ty) = ret_ty.make_suggestable(tcx, false, None) {
1181 diag.span_suggestion_verbose(
1182 infer_ret_ty.span,
1183 "replace with the correct return type",
1184 suggestable_ret_ty,
1185 Applicability::MachineApplicable,
1186 );
1187 recovered_ret_ty = Some(suggestable_ret_ty);
1188 } else if let Some(sugg) = suggest_impl_trait(
1189 &tcx.infer_ctxt().build(TypingMode::non_body_analysis()),
1190 tcx.param_env(def_id),
1191 ret_ty,
1192 ) {
1193 diag.span_suggestion_verbose(
1194 infer_ret_ty.span,
1195 "replace with an appropriate return type",
1196 sugg,
1197 Applicability::MachineApplicable,
1198 );
1199 } else if ret_ty.is_closure() {
1200 diag.help("consider using an `Fn`, `FnMut`, or `FnOnce` trait bound");
1201 }
1202
1203 if ret_ty.is_closure() {
1205 diag.note(
1206 "for more information on `Fn` traits and closure types, see \
1207 https://doc.rust-lang.org/book/ch13-01-closures.html",
1208 );
1209 }
1210 let guar = diag.emit();
1211
1212 let bound_vars = tcx.late_bound_vars(hir_id);
1216 let scope = def_id.to_def_id();
1217
1218 let fn_sig = tcx.mk_fn_sig(
1219 fn_sig.inputs().iter().copied(),
1220 recovered_ret_ty.unwrap_or_else(|| Ty::new_error(tcx, guar)),
1221 fn_sig.c_variadic,
1222 fn_sig.safety,
1223 fn_sig.abi,
1224 );
1225
1226 late_param_regions_to_bound(tcx, scope, bound_vars, fn_sig)
1227}
1228
1229pub fn suggest_impl_trait<'tcx>(
1230 infcx: &InferCtxt<'tcx>,
1231 param_env: ty::ParamEnv<'tcx>,
1232 ret_ty: Ty<'tcx>,
1233) -> Option<String> {
1234 let format_as_assoc: fn(_, _, _, _, _) -> _ =
1235 |tcx: TyCtxt<'tcx>,
1236 _: ty::GenericArgsRef<'tcx>,
1237 trait_def_id: DefId,
1238 assoc_item_def_id: DefId,
1239 item_ty: Ty<'tcx>| {
1240 let trait_name = tcx.item_name(trait_def_id);
1241 let assoc_name = tcx.item_name(assoc_item_def_id);
1242 Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("impl {0}<{1} = {2}>", trait_name,
assoc_name, item_ty))
})format!("impl {trait_name}<{assoc_name} = {item_ty}>"))
1243 };
1244 let format_as_parenthesized: fn(_, _, _, _, _) -> _ =
1245 |tcx: TyCtxt<'tcx>,
1246 args: ty::GenericArgsRef<'tcx>,
1247 trait_def_id: DefId,
1248 _: DefId,
1249 item_ty: Ty<'tcx>| {
1250 let trait_name = tcx.item_name(trait_def_id);
1251 let args_tuple = args.type_at(1);
1252 let ty::Tuple(types) = *args_tuple.kind() else {
1253 return None;
1254 };
1255 let types = types.make_suggestable(tcx, false, None)?;
1256 let maybe_ret =
1257 if item_ty.is_unit() { String::new() } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" -> {0}", item_ty))
})format!(" -> {item_ty}") };
1258 Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("impl {1}({0}){2}",
types.iter().map(|ty|
ty.to_string()).collect::<Vec<_>>().join(", "), trait_name,
maybe_ret))
})format!(
1259 "impl {trait_name}({}){maybe_ret}",
1260 types.iter().map(|ty| ty.to_string()).collect::<Vec<_>>().join(", ")
1261 ))
1262 };
1263
1264 for (trait_def_id, assoc_item_def_id, formatter) in [
1265 (
1266 infcx.tcx.get_diagnostic_item(sym::Iterator),
1267 infcx.tcx.get_diagnostic_item(sym::IteratorItem),
1268 format_as_assoc,
1269 ),
1270 (
1271 infcx.tcx.lang_items().future_trait(),
1272 infcx.tcx.lang_items().future_output(),
1273 format_as_assoc,
1274 ),
1275 (
1276 infcx.tcx.lang_items().async_fn_trait(),
1277 infcx.tcx.lang_items().async_fn_once_output(),
1278 format_as_parenthesized,
1279 ),
1280 (
1281 infcx.tcx.lang_items().async_fn_mut_trait(),
1282 infcx.tcx.lang_items().async_fn_once_output(),
1283 format_as_parenthesized,
1284 ),
1285 (
1286 infcx.tcx.lang_items().async_fn_once_trait(),
1287 infcx.tcx.lang_items().async_fn_once_output(),
1288 format_as_parenthesized,
1289 ),
1290 (
1291 infcx.tcx.lang_items().fn_trait(),
1292 infcx.tcx.lang_items().fn_once_output(),
1293 format_as_parenthesized,
1294 ),
1295 (
1296 infcx.tcx.lang_items().fn_mut_trait(),
1297 infcx.tcx.lang_items().fn_once_output(),
1298 format_as_parenthesized,
1299 ),
1300 (
1301 infcx.tcx.lang_items().fn_once_trait(),
1302 infcx.tcx.lang_items().fn_once_output(),
1303 format_as_parenthesized,
1304 ),
1305 ] {
1306 let Some(trait_def_id) = trait_def_id else {
1307 continue;
1308 };
1309 let Some(assoc_item_def_id) = assoc_item_def_id else {
1310 continue;
1311 };
1312 if infcx.tcx.def_kind(assoc_item_def_id) != DefKind::AssocTy {
1313 continue;
1314 }
1315 let sugg = infcx.probe(|_| {
1316 let args = ty::GenericArgs::for_item(infcx.tcx, trait_def_id, |param, _| {
1317 if param.index == 0 { ret_ty.into() } else { infcx.var_for_def(DUMMY_SP, param) }
1318 });
1319 if !infcx
1320 .type_implements_trait(trait_def_id, args, param_env)
1321 .must_apply_modulo_regions()
1322 {
1323 return None;
1324 }
1325 let ocx = ObligationCtxt::new(&infcx);
1326 let item_ty = ocx.normalize(
1327 &ObligationCause::dummy(),
1328 param_env,
1329 Ty::new_projection_from_args(infcx.tcx, assoc_item_def_id, args),
1330 );
1331 if ocx.try_evaluate_obligations().is_empty()
1333 && let item_ty = infcx.resolve_vars_if_possible(item_ty)
1334 && let Some(item_ty) = item_ty.make_suggestable(infcx.tcx, false, None)
1335 && let Some(sugg) = formatter(
1336 infcx.tcx,
1337 infcx.resolve_vars_if_possible(args),
1338 trait_def_id,
1339 assoc_item_def_id,
1340 item_ty,
1341 )
1342 {
1343 return Some(sugg);
1344 }
1345
1346 None
1347 });
1348
1349 if sugg.is_some() {
1350 return sugg;
1351 }
1352 }
1353 None
1354}
1355
1356fn impl_trait_header(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplTraitHeader<'_> {
1357 let icx = ItemCtxt::new(tcx, def_id);
1358 let item = tcx.hir_expect_item(def_id);
1359 let impl_ = item.expect_impl();
1360 let of_trait = impl_
1361 .of_trait
1362 .unwrap_or_else(|| {
::core::panicking::panic_fmt(format_args!("expected impl trait, found inherent impl on {0:?}",
def_id));
}panic!("expected impl trait, found inherent impl on {def_id:?}"));
1363 let selfty = tcx.type_of(def_id).instantiate_identity();
1364 let is_rustc_reservation = {
#[allow(deprecated)]
{
{
'done:
{
for i in tcx.get_all_attrs(def_id) {
#[allow(unused_imports)]
use rustc_hir::attrs::AttributeKind::*;
let i: &rustc_hir::Attribute = i;
match i {
rustc_hir::Attribute::Parsed(RustcReservationImpl(..)) => {
break 'done Some(());
}
rustc_hir::Attribute::Unparsed(..) =>
{}
#[deny(unreachable_patterns)]
_ => {}
}
}
None
}
}
}
}.is_some()find_attr!(tcx, def_id, RustcReservationImpl(..));
1365
1366 check_impl_constness(tcx, impl_.constness, &of_trait.trait_ref);
1367
1368 let trait_ref = icx.lowerer().lower_impl_trait_ref(&of_trait.trait_ref, selfty);
1369
1370 ty::ImplTraitHeader {
1371 trait_ref: ty::EarlyBinder::bind(trait_ref),
1372 safety: of_trait.safety,
1373 polarity: polarity_of_impl(tcx, of_trait, is_rustc_reservation),
1374 constness: impl_.constness,
1375 }
1376}
1377
1378fn check_impl_constness(
1379 tcx: TyCtxt<'_>,
1380 constness: hir::Constness,
1381 hir_trait_ref: &hir::TraitRef<'_>,
1382) {
1383 if let hir::Constness::NotConst = constness {
1384 return;
1385 }
1386
1387 let Some(trait_def_id) = hir_trait_ref.trait_def_id() else { return };
1388 if tcx.is_const_trait(trait_def_id) {
1389 return;
1390 }
1391
1392 let trait_name = tcx.item_name(trait_def_id).to_string();
1393 let (suggestion, suggestion_pre) = match (trait_def_id.as_local(), tcx.sess.is_nightly_build())
1394 {
1395 (Some(trait_def_id), true) => {
1396 let span = tcx.hir_expect_item(trait_def_id).vis_span;
1397 let span = tcx.sess.source_map().span_extend_while_whitespace(span);
1398
1399 (
1400 Some(span.shrink_to_hi()),
1401 if tcx.features().const_trait_impl() {
1402 ""
1403 } else {
1404 "enable `#![feature(const_trait_impl)]` in your crate and "
1405 },
1406 )
1407 }
1408 (None, _) | (_, false) => (None, ""),
1409 };
1410 tcx.dcx().emit_err(errors::ConstImplForNonConstTrait {
1411 trait_ref_span: hir_trait_ref.path.span,
1412 trait_name,
1413 suggestion,
1414 suggestion_pre,
1415 marking: (),
1416 adding: (),
1417 });
1418}
1419
1420fn polarity_of_impl(
1421 tcx: TyCtxt<'_>,
1422 of_trait: &hir::TraitImplHeader<'_>,
1423 is_rustc_reservation: bool,
1424) -> ty::ImplPolarity {
1425 match of_trait.polarity {
1426 hir::ImplPolarity::Negative(span) => {
1427 if is_rustc_reservation {
1428 let span = span.to(of_trait.trait_ref.path.span);
1429 tcx.dcx().span_err(span, "reservation impls can't be negative");
1430 }
1431 ty::ImplPolarity::Negative
1432 }
1433 hir::ImplPolarity::Positive => {
1434 if is_rustc_reservation {
1435 ty::ImplPolarity::Reservation
1436 } else {
1437 ty::ImplPolarity::Positive
1438 }
1439 }
1440 }
1441}
1442
1443fn early_bound_lifetimes_from_generics<'a, 'tcx>(
1449 tcx: TyCtxt<'tcx>,
1450 generics: &'a hir::Generics<'a>,
1451) -> impl Iterator<Item = &'a hir::GenericParam<'a>> {
1452 generics.params.iter().filter(move |param| match param.kind {
1453 GenericParamKind::Lifetime { .. } => !tcx.is_late_bound(param.hir_id),
1454 _ => false,
1455 })
1456}
1457
1458fn compute_sig_of_foreign_fn_decl<'tcx>(
1459 tcx: TyCtxt<'tcx>,
1460 def_id: LocalDefId,
1461 decl: &'tcx hir::FnDecl<'tcx>,
1462 abi: ExternAbi,
1463 safety: hir::Safety,
1464) -> ty::PolyFnSig<'tcx> {
1465 let hir_id = tcx.local_def_id_to_hir_id(def_id);
1466 let fty =
1467 ItemCtxt::new(tcx, def_id).lowerer().lower_fn_ty(hir_id, safety, abi, decl, None, None);
1468
1469 if !tcx.features().simd_ffi() {
1472 let check = |hir_ty: &hir::Ty<'_>, ty: Ty<'_>| {
1473 if ty.is_simd() {
1474 let snip = tcx
1475 .sess
1476 .source_map()
1477 .span_to_snippet(hir_ty.span)
1478 .map_or_else(|_| String::new(), |s| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" `{0}`", s))
})format!(" `{s}`"));
1479 tcx.dcx().emit_err(errors::SIMDFFIHighlyExperimental { span: hir_ty.span, snip });
1480 }
1481 };
1482 for (input, ty) in iter::zip(decl.inputs, fty.inputs().skip_binder()) {
1483 check(input, *ty)
1484 }
1485 if let hir::FnRetTy::Return(ty) = decl.output {
1486 check(ty, fty.output().skip_binder())
1487 }
1488 }
1489
1490 fty
1491}
1492
1493fn coroutine_kind(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<hir::CoroutineKind> {
1494 match tcx.hir_node_by_def_id(def_id) {
1495 Node::Expr(&hir::Expr {
1496 kind:
1497 hir::ExprKind::Closure(&rustc_hir::Closure {
1498 kind: hir::ClosureKind::Coroutine(kind),
1499 ..
1500 }),
1501 ..
1502 }) => Some(kind),
1503 _ => None,
1504 }
1505}
1506
1507fn coroutine_for_closure(tcx: TyCtxt<'_>, def_id: LocalDefId) -> DefId {
1508 let &rustc_hir::Closure { kind: hir::ClosureKind::CoroutineClosure(_), body, .. } =
1509 tcx.hir_node_by_def_id(def_id).expect_closure()
1510 else {
1511 ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!()
1512 };
1513
1514 let &hir::Expr {
1515 kind:
1516 hir::ExprKind::Closure(&rustc_hir::Closure {
1517 def_id,
1518 kind: hir::ClosureKind::Coroutine(_),
1519 ..
1520 }),
1521 ..
1522 } = tcx.hir_body(body).value
1523 else {
1524 ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!()
1525 };
1526
1527 def_id.to_def_id()
1528}
1529
1530fn opaque_ty_origin<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> hir::OpaqueTyOrigin<DefId> {
1531 match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
1532 hir::OpaqueTyOrigin::FnReturn { parent, in_trait_or_impl } => {
1533 hir::OpaqueTyOrigin::FnReturn { parent: parent.to_def_id(), in_trait_or_impl }
1534 }
1535 hir::OpaqueTyOrigin::AsyncFn { parent, in_trait_or_impl } => {
1536 hir::OpaqueTyOrigin::AsyncFn { parent: parent.to_def_id(), in_trait_or_impl }
1537 }
1538 hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty } => {
1539 hir::OpaqueTyOrigin::TyAlias { parent: parent.to_def_id(), in_assoc_ty }
1540 }
1541 }
1542}
1543
1544fn rendered_precise_capturing_args<'tcx>(
1545 tcx: TyCtxt<'tcx>,
1546 def_id: LocalDefId,
1547) -> Option<&'tcx [PreciseCapturingArgKind<Symbol, Symbol>]> {
1548 if let Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) =
1549 tcx.opt_rpitit_info(def_id.to_def_id())
1550 {
1551 return tcx.rendered_precise_capturing_args(opaque_def_id);
1552 }
1553
1554 tcx.hir_node_by_def_id(def_id).expect_opaque_ty().bounds.iter().find_map(|bound| match bound {
1555 hir::GenericBound::Use(args, ..) => {
1556 Some(&*tcx.arena.alloc_from_iter(args.iter().map(|arg| match arg {
1557 PreciseCapturingArgKind::Lifetime(_) => {
1558 PreciseCapturingArgKind::Lifetime(arg.name())
1559 }
1560 PreciseCapturingArgKind::Param(_) => PreciseCapturingArgKind::Param(arg.name()),
1561 })))
1562 }
1563 _ => None,
1564 })
1565}
1566
1567fn const_param_default<'tcx>(
1568 tcx: TyCtxt<'tcx>,
1569 local_def_id: LocalDefId,
1570) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
1571 let hir::Node::GenericParam(hir::GenericParam {
1572 kind: hir::GenericParamKind::Const { default: Some(default_ct), .. },
1573 ..
1574 }) = tcx.hir_node_by_def_id(local_def_id)
1575 else {
1576 ::rustc_middle::util::bug::span_bug_fmt(tcx.def_span(local_def_id),
format_args!("`const_param_default` expected a generic parameter with a constant"))span_bug!(
1577 tcx.def_span(local_def_id),
1578 "`const_param_default` expected a generic parameter with a constant"
1579 )
1580 };
1581
1582 let icx = ItemCtxt::new(tcx, local_def_id);
1583
1584 let def_id = local_def_id.to_def_id();
1585 let identity_args = ty::GenericArgs::identity_for_item(tcx, tcx.parent(def_id));
1586
1587 let ct = icx
1588 .lowerer()
1589 .lower_const_arg(default_ct, tcx.type_of(def_id).instantiate(tcx, identity_args));
1590 ty::EarlyBinder::bind(ct)
1591}
1592
1593fn anon_const_kind<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> ty::AnonConstKind {
1594 let hir_id = tcx.local_def_id_to_hir_id(def);
1595 let const_arg_id = tcx.parent_hir_id(hir_id);
1596 match tcx.hir_node(const_arg_id) {
1597 hir::Node::ConstArg(_) => {
1598 let parent_hir_node = tcx.hir_node(tcx.parent_hir_id(const_arg_id));
1599 if tcx.features().generic_const_exprs() {
1600 ty::AnonConstKind::GCE
1601 } else if tcx.features().opaque_generic_const_args() {
1602 if !is_anon_const_rhs_of_const_item(tcx, def) {
1606 return ty::AnonConstKind::MCG;
1607 }
1608
1609 let body = tcx.hir_body_owned_by(def);
1610 let mut visitor = OGCAParamVisitor(tcx);
1611 match visitor.visit_body(body) {
1612 ControlFlow::Break(UsesParam) => ty::AnonConstKind::OGCA,
1613 ControlFlow::Continue(()) => ty::AnonConstKind::MCG,
1614 }
1615 } else if tcx.features().min_generic_const_args() {
1616 ty::AnonConstKind::MCG
1617 } else if let hir::Node::Expr(hir::Expr {
1618 kind: hir::ExprKind::Repeat(_, repeat_count),
1619 ..
1620 }) = parent_hir_node
1621 && repeat_count.hir_id == const_arg_id
1622 {
1623 ty::AnonConstKind::RepeatExprCount
1624 } else {
1625 ty::AnonConstKind::MCG
1626 }
1627 }
1628 _ => ty::AnonConstKind::NonTypeSystem,
1629 }
1630}
1631
1632fn is_anon_const_rhs_of_const_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
1633 let hir_id = tcx.local_def_id_to_hir_id(def_id);
1634 let Some((_, grandparent_node)) = tcx.hir_parent_iter(hir_id).nth(1) else { return false };
1635 let (Node::Item(hir::Item { kind: hir::ItemKind::Const(_, _, _, ct_rhs), .. })
1636 | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(_, ct_rhs), .. })
1637 | Node::TraitItem(hir::TraitItem {
1638 kind: hir::TraitItemKind::Const(_, Some(ct_rhs), _),
1639 ..
1640 })) = grandparent_node
1641 else {
1642 return false;
1643 };
1644 let hir::ConstItemRhs::TypeConst(hir::ConstArg {
1645 kind: hir::ConstArgKind::Anon(rhs_anon), ..
1646 }) = ct_rhs
1647 else {
1648 return false;
1649 };
1650 def_id == rhs_anon.def_id
1651}
1652
1653struct OGCAParamVisitor<'tcx>(TyCtxt<'tcx>);
1654
1655struct UsesParam;
1656
1657impl<'tcx> Visitor<'tcx> for OGCAParamVisitor<'tcx> {
1658 type NestedFilter = nested_filter::OnlyBodies;
1659 type Result = ControlFlow<UsesParam>;
1660
1661 fn maybe_tcx(&mut self) -> TyCtxt<'tcx> {
1662 self.0
1663 }
1664
1665 fn visit_path(&mut self, path: &hir::Path<'tcx>, _id: HirId) -> ControlFlow<UsesParam> {
1666 if let Res::Def(DefKind::TyParam | DefKind::ConstParam | DefKind::LifetimeParam, _) =
1667 path.res
1668 {
1669 return ControlFlow::Break(UsesParam);
1670 }
1671
1672 intravisit::walk_path(self, path)
1673 }
1674}
1675
1676x;#[instrument(level = "debug", skip(tcx), ret)]
1677fn const_of_item<'tcx>(
1678 tcx: TyCtxt<'tcx>,
1679 def_id: LocalDefId,
1680) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
1681 let ct_rhs = match tcx.hir_node_by_def_id(def_id) {
1682 hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(.., ct), .. }) => *ct,
1683 hir::Node::TraitItem(hir::TraitItem {
1684 kind: hir::TraitItemKind::Const(_, ct, _), ..
1685 }) => ct.expect("no default value for trait assoc const"),
1686 hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(.., ct), .. }) => *ct,
1687 _ => {
1688 span_bug!(tcx.def_span(def_id), "`const_of_item` expected a const or assoc const item")
1689 }
1690 };
1691 let ct_arg = match ct_rhs {
1692 hir::ConstItemRhs::TypeConst(ct_arg) => ct_arg,
1693 hir::ConstItemRhs::Body(_) => {
1694 let e = tcx.dcx().span_delayed_bug(
1695 tcx.def_span(def_id),
1696 "cannot call const_of_item on a non-type_const",
1697 );
1698 return ty::EarlyBinder::bind(Const::new_error(tcx, e));
1699 }
1700 };
1701 let icx = ItemCtxt::new(tcx, def_id);
1702 let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
1703 let ct = icx
1704 .lowerer()
1705 .lower_const_arg(ct_arg, tcx.type_of(def_id.to_def_id()).instantiate(tcx, identity_args));
1706 if let Err(e) = icx.check_tainted_by_errors()
1707 && !ct.references_error()
1708 {
1709 ty::EarlyBinder::bind(Const::new_error(tcx, e))
1710 } else {
1711 ty::EarlyBinder::bind(ct)
1712 }
1713}