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