1use std::cell::Cell;
18use std::iter;
19use std::ops::{Bound, ControlFlow};
20
21use rustc_abi::{ExternAbi, Size};
22use rustc_ast::Recovered;
23use rustc_data_structures::assert_matches;
24use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
25use rustc_errors::{Applicability, Diag, DiagCtxtHandle, E0228, ErrorGuaranteed, StashKey};
26use rustc_hir::def::{DefKind, Res};
27use rustc_hir::def_id::{DefId, LocalDefId};
28use rustc_hir::intravisit::{self, InferKind, Visitor, VisitorExt};
29use rustc_hir::{self as hir, GenericParamKind, HirId, Node, PreciseCapturingArgKind, find_attr};
30use rustc_infer::infer::{InferCtxt, TyCtxtInferExt};
31use rustc_infer::traits::{DynCompatibilityViolation, ObligationCause};
32use rustc_middle::hir::nested_filter;
33use rustc_middle::query::Providers;
34use rustc_middle::ty::util::{Discr, IntTypeExt};
35use rustc_middle::ty::{
36 self, AdtKind, Const, IsSuggestable, Ty, TyCtxt, TypeVisitableExt, TypingMode, fold_regions,
37};
38use rustc_middle::{bug, span_bug};
39use rustc_span::{DUMMY_SP, Ident, Span, Symbol, kw, sym};
40use rustc_trait_selection::error_reporting::traits::suggestions::NextTypeParamName;
41use rustc_trait_selection::infer::InferCtxtExt;
42use rustc_trait_selection::traits::{
43 FulfillmentError, ObligationCtxt, hir_ty_lowering_dyn_compatibility_violations,
44};
45use tracing::{debug, instrument};
46
47use crate::errors;
48use crate::hir_ty_lowering::{HirTyLowerer, InherentAssocCandidate, RegionInferReason};
49
50pub(crate) mod dump;
51mod generics_of;
52mod item_bounds;
53mod predicates_of;
54mod resolve_bound_vars;
55mod type_of;
56
57pub(crate) fn provide(providers: &mut Providers) {
61 resolve_bound_vars::provide(providers);
62 *providers = Providers {
63 type_of: type_of::type_of,
64 type_of_opaque: type_of::type_of_opaque,
65 type_of_opaque_hir_typeck: type_of::type_of_opaque_hir_typeck,
66 type_alias_is_lazy: type_of::type_alias_is_lazy,
67 item_bounds: item_bounds::item_bounds,
68 explicit_item_bounds: item_bounds::explicit_item_bounds,
69 item_self_bounds: item_bounds::item_self_bounds,
70 explicit_item_self_bounds: item_bounds::explicit_item_self_bounds,
71 item_non_self_bounds: item_bounds::item_non_self_bounds,
72 impl_super_outlives: item_bounds::impl_super_outlives,
73 generics_of: generics_of::generics_of,
74 predicates_of: predicates_of::predicates_of,
75 explicit_predicates_of: predicates_of::explicit_predicates_of,
76 explicit_super_predicates_of: predicates_of::explicit_super_predicates_of,
77 explicit_implied_predicates_of: predicates_of::explicit_implied_predicates_of,
78 explicit_supertraits_containing_assoc_item:
79 predicates_of::explicit_supertraits_containing_assoc_item,
80 trait_explicit_predicates_and_bounds: predicates_of::trait_explicit_predicates_and_bounds,
81 const_conditions: predicates_of::const_conditions,
82 explicit_implied_const_bounds: predicates_of::explicit_implied_const_bounds,
83 type_param_predicates: predicates_of::type_param_predicates,
84 trait_def,
85 adt_def,
86 fn_sig,
87 impl_trait_header,
88 coroutine_kind,
89 coroutine_for_closure,
90 opaque_ty_origin,
91 rendered_precise_capturing_args,
92 const_param_default,
93 anon_const_kind,
94 const_of_item,
95 is_rhs_type_const,
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 let def = tcx.adt_def(def_id);
615 let repr_type = def.repr().discr_type();
616 let initial = repr_type.initial_discriminant(tcx);
617 let mut prev_discr = None::<Discr<'_>>;
618 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);
620 let mut min_discr = i128::MAX;
621 let mut max_discr = i128::MIN;
622
623 for variant in def.variants() {
625 let wrapped_discr = prev_discr.map_or(initial, |d| d.wrap_incr(tcx));
626 let cur_discr = if let ty::VariantDiscr::Explicit(const_def_id) = variant.discr {
627 def.eval_explicit_discr(tcx, const_def_id).ok()
628 } else if let Some(discr) = repr_type.disr_incr(tcx, prev_discr) {
629 Some(discr)
630 } else {
631 let span = tcx.def_span(variant.def_id);
632 tcx.dcx().emit_err(errors::EnumDiscriminantOverflowed {
633 span,
634 discr: prev_discr.unwrap().to_string(),
635 item_name: tcx.item_ident(variant.def_id),
636 wrapped_discr: wrapped_discr.to_string(),
637 });
638 None
639 }
640 .unwrap_or(wrapped_discr);
641
642 if def.repr().c() {
643 let c_int = Size::from_bits(tcx.sess.target.c_int_width);
644 let c_uint_max = i128::try_from(c_int.unsigned_int_max()).unwrap();
645 let discr_size = cur_discr.ty.int_size_and_signed(tcx).0;
647 let discr_val = discr_size.sign_extend(cur_discr.val);
648 min_discr = min_discr.min(discr_val);
649 max_discr = max_discr.max(discr_val);
650
651 if !(min_discr >= c_int.signed_int_min() && max_discr <= c_int.signed_int_max())
653 && !(min_discr >= 0 && max_discr <= c_uint_max)
654 {
655 let span = tcx.def_span(variant.def_id);
656 let msg = if discr_val < c_int.signed_int_min() || discr_val > c_uint_max {
657 "`repr(C)` enum discriminant does not fit into C `int` nor into C `unsigned int`"
658 } else if discr_val < 0 {
659 "`repr(C)` enum discriminant does not fit into C `unsigned int`, and a previous discriminant does not fit into C `int`"
660 } else {
661 "`repr(C)` enum discriminant does not fit into C `int`, and a previous discriminant does not fit into C `unsigned int`"
662 };
663 tcx.node_span_lint(
664 rustc_session::lint::builtin::REPR_C_ENUMS_LARGER_THAN_INT,
665 tcx.local_def_id_to_hir_id(def_id),
666 span,
667 |d| {
668 d.primary_message(msg)
669 .note("`repr(C)` enums with big discriminants are non-portable, and their size in Rust might not match their size in C")
670 .help("use `repr($int_ty)` instead to explicitly set the size of this enum");
671 }
672 );
673 }
674 }
675
676 prev_discr = Some(cur_discr);
677
678 for f in &variant.fields {
679 tcx.ensure_ok().generics_of(f.did);
680 tcx.ensure_ok().type_of(f.did);
681 tcx.ensure_ok().predicates_of(f.did);
682 }
683
684 if let Some(ctor_def_id) = variant.ctor_def_id() {
686 lower_variant_ctor(tcx, ctor_def_id.expect_local());
687 }
688 }
689}
690
691#[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)]
692struct NestedSpan {
693 span: Span,
694 nested_field_span: Span,
695}
696
697impl NestedSpan {
698 fn to_field_already_declared_nested_help(&self) -> errors::FieldAlreadyDeclaredNestedHelp {
699 errors::FieldAlreadyDeclaredNestedHelp { span: self.span }
700 }
701}
702
703#[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)]
704enum FieldDeclSpan {
705 NotNested(Span),
706 Nested(NestedSpan),
707}
708
709impl From<Span> for FieldDeclSpan {
710 fn from(span: Span) -> Self {
711 Self::NotNested(span)
712 }
713}
714
715impl From<NestedSpan> for FieldDeclSpan {
716 fn from(span: NestedSpan) -> Self {
717 Self::Nested(span)
718 }
719}
720
721struct FieldUniquenessCheckContext<'tcx> {
722 tcx: TyCtxt<'tcx>,
723 seen_fields: FxIndexMap<Ident, FieldDeclSpan>,
724}
725
726impl<'tcx> FieldUniquenessCheckContext<'tcx> {
727 fn new(tcx: TyCtxt<'tcx>) -> Self {
728 Self { tcx, seen_fields: FxIndexMap::default() }
729 }
730
731 fn check_field_decl(&mut self, field_name: Ident, field_decl: FieldDeclSpan) {
733 use FieldDeclSpan::*;
734 let field_name = field_name.normalize_to_macros_2_0();
735 match (field_decl, self.seen_fields.get(&field_name).copied()) {
736 (NotNested(span), Some(NotNested(prev_span))) => {
737 self.tcx.dcx().emit_err(errors::FieldAlreadyDeclared::NotNested {
738 field_name,
739 span,
740 prev_span,
741 });
742 }
743 (NotNested(span), Some(Nested(prev))) => {
744 self.tcx.dcx().emit_err(errors::FieldAlreadyDeclared::PreviousNested {
745 field_name,
746 span,
747 prev_span: prev.span,
748 prev_nested_field_span: prev.nested_field_span,
749 prev_help: prev.to_field_already_declared_nested_help(),
750 });
751 }
752 (
753 Nested(current @ NestedSpan { span, nested_field_span, .. }),
754 Some(NotNested(prev_span)),
755 ) => {
756 self.tcx.dcx().emit_err(errors::FieldAlreadyDeclared::CurrentNested {
757 field_name,
758 span,
759 nested_field_span,
760 help: current.to_field_already_declared_nested_help(),
761 prev_span,
762 });
763 }
764 (Nested(current @ NestedSpan { span, nested_field_span }), Some(Nested(prev))) => {
765 self.tcx.dcx().emit_err(errors::FieldAlreadyDeclared::BothNested {
766 field_name,
767 span,
768 nested_field_span,
769 help: current.to_field_already_declared_nested_help(),
770 prev_span: prev.span,
771 prev_nested_field_span: prev.nested_field_span,
772 prev_help: prev.to_field_already_declared_nested_help(),
773 });
774 }
775 (field_decl, None) => {
776 self.seen_fields.insert(field_name, field_decl);
777 }
778 }
779 }
780}
781
782fn lower_variant<'tcx>(
783 tcx: TyCtxt<'tcx>,
784 variant_did: Option<LocalDefId>,
785 ident: Ident,
786 discr: ty::VariantDiscr,
787 def: &hir::VariantData<'tcx>,
788 adt_kind: ty::AdtKind,
789 parent_did: LocalDefId,
790) -> ty::VariantDef {
791 let mut field_uniqueness_check_ctx = FieldUniquenessCheckContext::new(tcx);
792 let fields = def
793 .fields()
794 .iter()
795 .inspect(|field| {
796 field_uniqueness_check_ctx.check_field_decl(field.ident, field.span.into());
797 })
798 .map(|f| ty::FieldDef {
799 did: f.def_id.to_def_id(),
800 name: f.ident.name,
801 vis: tcx.visibility(f.def_id),
802 safety: f.safety,
803 value: f.default.map(|v| v.def_id.to_def_id()),
804 })
805 .collect();
806 let recovered = match def {
807 hir::VariantData::Struct { recovered: Recovered::Yes(guar), .. } => Some(*guar),
808 _ => None,
809 };
810 ty::VariantDef::new(
811 ident.name,
812 variant_did.map(LocalDefId::to_def_id),
813 def.ctor().map(|(kind, _, def_id)| (kind, def_id.to_def_id())),
814 discr,
815 fields,
816 parent_did.to_def_id(),
817 recovered,
818 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(..))
819 || variant_did
820 .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(..))),
821 )
822}
823
824fn adt_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::AdtDef<'_> {
825 use rustc_hir::*;
826
827 let Node::Item(item) = tcx.hir_node_by_def_id(def_id) else {
828 ::rustc_middle::util::bug::bug_fmt(format_args!("expected ADT to be an item"));bug!("expected ADT to be an item");
829 };
830
831 let repr = tcx.repr_options_of_def(def_id);
832 let (kind, variants) = match &item.kind {
833 ItemKind::Enum(_, _, def) => {
834 let mut distance_from_explicit = 0;
835 let variants = def
836 .variants
837 .iter()
838 .map(|v| {
839 let discr = if let Some(e) = &v.disr_expr {
840 distance_from_explicit = 0;
841 ty::VariantDiscr::Explicit(e.def_id.to_def_id())
842 } else {
843 ty::VariantDiscr::Relative(distance_from_explicit)
844 };
845 distance_from_explicit += 1;
846
847 lower_variant(
848 tcx,
849 Some(v.def_id),
850 v.ident,
851 discr,
852 &v.data,
853 AdtKind::Enum,
854 def_id,
855 )
856 })
857 .collect();
858
859 (AdtKind::Enum, variants)
860 }
861 ItemKind::Struct(ident, _, def) | ItemKind::Union(ident, _, def) => {
862 let adt_kind = match item.kind {
863 ItemKind::Struct(..) => AdtKind::Struct,
864 _ => AdtKind::Union,
865 };
866 let variants = std::iter::once(lower_variant(
867 tcx,
868 None,
869 *ident,
870 ty::VariantDiscr::Relative(0),
871 def,
872 adt_kind,
873 def_id,
874 ))
875 .collect();
876
877 (adt_kind, variants)
878 }
879 _ => ::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),
880 };
881 tcx.mk_adt_def(def_id.to_def_id(), kind, variants, repr)
882}
883
884fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
885 let item = tcx.hir_expect_item(def_id);
886
887 let (constness, is_alias, is_auto, safety) = match item.kind {
888 hir::ItemKind::Trait(constness, is_auto, safety, ..) => {
889 (constness, false, is_auto == hir::IsAuto::Yes, safety)
890 }
891 hir::ItemKind::TraitAlias(constness, ..) => (constness, true, false, hir::Safety::Safe),
892 _ => ::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"),
893 };
894
895 #[allow(deprecated)]
897 let attrs = tcx.get_all_attrs(def_id);
898
899 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(_));
900 if paren_sugar && !tcx.features().unboxed_closures() {
901 tcx.dcx().emit_err(errors::ParenSugarAttribute { span: item.span });
902 }
903
904 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(_));
906
907 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(_));
908 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);
909
910 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!(
911 attrs,
912 RustcSkipDuringMethodDispatch { array, boxed_slice, span: _ } => [*array, *boxed_slice]
913 )
914 .unwrap_or([false; 2]);
915
916 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(_)) {
917 ty::trait_def::TraitSpecializationKind::Marker
918 } 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(_)) {
919 ty::trait_def::TraitSpecializationKind::AlwaysApplicable
920 } else {
921 ty::trait_def::TraitSpecializationKind::None
922 };
923
924 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!(
925 attrs,
926 RustcMustImplementOneOf { fn_names, .. } =>
927 fn_names
928 .iter()
929 .cloned()
930 .collect::<Box<[_]>>()
931 );
932
933 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(_));
934 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);
935
936 ty::TraitDef {
937 def_id: def_id.to_def_id(),
938 safety,
939 constness,
940 paren_sugar,
941 has_auto_impl: is_auto,
942 is_marker,
943 is_coinductive: rustc_coinductive || is_auto,
944 is_fundamental,
945 skip_array_during_method_dispatch,
946 skip_boxed_slice_during_method_dispatch,
947 specialization_kind,
948 must_implement_one_of,
949 force_dyn_incompatible,
950 deny_explicit_impl,
951 }
952}
953
954x;#[instrument(level = "debug", skip(tcx), ret)]
955fn fn_sig(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_, ty::PolyFnSig<'_>> {
956 use rustc_hir::Node::*;
957 use rustc_hir::*;
958
959 let hir_id = tcx.local_def_id_to_hir_id(def_id);
960
961 let icx = ItemCtxt::new(tcx, def_id);
962
963 let output = match tcx.hir_node(hir_id) {
964 TraitItem(hir::TraitItem {
965 kind: TraitItemKind::Fn(sig, TraitFn::Provided(_)),
966 generics,
967 ..
968 })
969 | Item(hir::Item { kind: ItemKind::Fn { sig, generics, .. }, .. }) => {
970 lower_fn_sig_recovering_infer_ret_ty(&icx, sig, generics, def_id)
971 }
972
973 ImplItem(hir::ImplItem { kind: ImplItemKind::Fn(sig, _), generics, .. }) => {
974 if let Item(hir::Item { kind: ItemKind::Impl(i), .. }) = tcx.parent_hir_node(hir_id)
976 && i.of_trait.is_some()
977 {
978 icx.lowerer().lower_fn_ty(
979 hir_id,
980 sig.header.safety(),
981 sig.header.abi,
982 sig.decl,
983 Some(generics),
984 None,
985 )
986 } else {
987 lower_fn_sig_recovering_infer_ret_ty(&icx, sig, generics, def_id)
988 }
989 }
990
991 TraitItem(hir::TraitItem {
992 kind: TraitItemKind::Fn(FnSig { header, decl, span: _ }, _),
993 generics,
994 ..
995 }) => icx.lowerer().lower_fn_ty(
996 hir_id,
997 header.safety(),
998 header.abi,
999 decl,
1000 Some(generics),
1001 None,
1002 ),
1003
1004 ForeignItem(&hir::ForeignItem { kind: ForeignItemKind::Fn(sig, _, _), .. }) => {
1005 let abi = tcx.hir_get_foreign_abi(hir_id);
1006 compute_sig_of_foreign_fn_decl(tcx, def_id, sig.decl, abi, sig.header.safety())
1007 }
1008
1009 Ctor(data) => {
1010 assert_matches!(data.ctor(), Some(_));
1011 let adt_def_id = tcx.hir_get_parent_item(hir_id).def_id.to_def_id();
1012 let ty = tcx.type_of(adt_def_id).instantiate_identity();
1013 let inputs = data.fields().iter().map(|f| tcx.type_of(f.def_id).instantiate_identity());
1014 let safety = match tcx.layout_scalar_valid_range(adt_def_id) {
1016 (Bound::Unbounded, Bound::Unbounded) => hir::Safety::Safe,
1017 _ => hir::Safety::Unsafe,
1018 };
1019 ty::Binder::dummy(tcx.mk_fn_sig(inputs, ty, false, safety, ExternAbi::Rust))
1020 }
1021
1022 Expr(&hir::Expr { kind: hir::ExprKind::Closure { .. }, .. }) => {
1023 bug!("to get the signature of a closure, use `args.as_closure().sig()` not `fn_sig()`",);
1034 }
1035
1036 x => {
1037 bug!("unexpected sort of node in fn_sig(): {:?}", x);
1038 }
1039 };
1040 ty::EarlyBinder::bind(output)
1041}
1042
1043fn lower_fn_sig_recovering_infer_ret_ty<'tcx>(
1044 icx: &ItemCtxt<'tcx>,
1045 sig: &'tcx hir::FnSig<'tcx>,
1046 generics: &'tcx hir::Generics<'tcx>,
1047 def_id: LocalDefId,
1048) -> ty::PolyFnSig<'tcx> {
1049 if let Some(infer_ret_ty) = sig.decl.output.is_suggestable_infer_ty() {
1050 return recover_infer_ret_ty(icx, infer_ret_ty, generics, def_id);
1051 }
1052
1053 icx.lowerer().lower_fn_ty(
1054 icx.tcx().local_def_id_to_hir_id(def_id),
1055 sig.header.safety(),
1056 sig.header.abi,
1057 sig.decl,
1058 Some(generics),
1059 None,
1060 )
1061}
1062
1063fn late_param_regions_to_bound<'tcx, T>(
1065 tcx: TyCtxt<'tcx>,
1066 scope: DefId,
1067 bound_vars: &'tcx ty::List<ty::BoundVariableKind<'tcx>>,
1068 value: T,
1069) -> ty::Binder<'tcx, T>
1070where
1071 T: ty::TypeFoldable<TyCtxt<'tcx>>,
1072{
1073 let value = fold_regions(tcx, value, |r, debruijn| match r.kind() {
1074 ty::ReLateParam(lp) => {
1075 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);
1077
1078 let br = match lp.kind {
1079 kind @ (ty::LateParamRegionKind::Anon(idx)
1081 | ty::LateParamRegionKind::NamedAnon(idx, _)) => {
1082 let idx = idx as usize;
1083 let var = ty::BoundVar::from_usize(idx);
1084
1085 let Some(ty::BoundVariableKind::Region(kind)) = bound_vars.get(idx).copied()
1086 else {
1087 ::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:?}");
1088 };
1089
1090 ty::BoundRegion { var, kind }
1091 }
1092
1093 ty::LateParamRegionKind::Named(def_id) => bound_vars
1095 .iter()
1096 .enumerate()
1097 .find_map(|(idx, bv)| match bv {
1098 ty::BoundVariableKind::Region(kind @ ty::BoundRegionKind::Named(did))
1099 if did == def_id =>
1100 {
1101 Some(ty::BoundRegion { var: ty::BoundVar::from_usize(idx), kind })
1102 }
1103 _ => None,
1104 })
1105 .unwrap(),
1106
1107 ty::LateParamRegionKind::ClosureEnv => bound_vars
1108 .iter()
1109 .enumerate()
1110 .find_map(|(idx, bv)| match bv {
1111 ty::BoundVariableKind::Region(kind @ ty::BoundRegionKind::ClosureEnv) => {
1112 Some(ty::BoundRegion { var: ty::BoundVar::from_usize(idx), kind })
1113 }
1114 _ => None,
1115 })
1116 .unwrap(),
1117 };
1118
1119 ty::Region::new_bound(tcx, debruijn, br)
1120 }
1121 _ => r,
1122 });
1123
1124 ty::Binder::bind_with_vars(value, bound_vars)
1125}
1126
1127fn recover_infer_ret_ty<'tcx>(
1128 icx: &ItemCtxt<'tcx>,
1129 infer_ret_ty: &'tcx hir::Ty<'tcx>,
1130 generics: &'tcx hir::Generics<'tcx>,
1131 def_id: LocalDefId,
1132) -> ty::PolyFnSig<'tcx> {
1133 let tcx = icx.tcx;
1134 let hir_id = tcx.local_def_id_to_hir_id(def_id);
1135
1136 let fn_sig = tcx.typeck(def_id).liberated_fn_sigs()[hir_id];
1137
1138 let has_region_params = generics.params.iter().any(|param| match param.kind {
1143 GenericParamKind::Lifetime { .. } => true,
1144 _ => false,
1145 });
1146 let fn_sig = fold_regions(tcx, fn_sig, |r, _| match r.kind() {
1147 ty::ReErased => {
1148 if has_region_params {
1149 ty::Region::new_error_with_message(
1150 tcx,
1151 DUMMY_SP,
1152 "erased region is not allowed here in return type",
1153 )
1154 } else {
1155 tcx.lifetimes.re_static
1156 }
1157 }
1158 _ => r,
1159 });
1160
1161 let mut visitor = HirPlaceholderCollector::default();
1162 visitor.visit_ty_unambig(infer_ret_ty);
1163
1164 let mut diag = bad_placeholder(icx.lowerer(), visitor.spans, "return type");
1165 let ret_ty = fn_sig.output();
1166
1167 let mut recovered_ret_ty = None;
1171 if let Some(suggestable_ret_ty) = ret_ty.make_suggestable(tcx, false, None) {
1172 diag.span_suggestion_verbose(
1173 infer_ret_ty.span,
1174 "replace with the correct return type",
1175 suggestable_ret_ty,
1176 Applicability::MachineApplicable,
1177 );
1178 recovered_ret_ty = Some(suggestable_ret_ty);
1179 } else if let Some(sugg) = suggest_impl_trait(
1180 &tcx.infer_ctxt().build(TypingMode::non_body_analysis()),
1181 tcx.param_env(def_id),
1182 ret_ty,
1183 ) {
1184 diag.span_suggestion_verbose(
1185 infer_ret_ty.span,
1186 "replace with an appropriate return type",
1187 sugg,
1188 Applicability::MachineApplicable,
1189 );
1190 } else if ret_ty.is_closure() {
1191 diag.help("consider using an `Fn`, `FnMut`, or `FnOnce` trait bound");
1192 }
1193
1194 if ret_ty.is_closure() {
1196 diag.note(
1197 "for more information on `Fn` traits and closure types, see \
1198 https://doc.rust-lang.org/book/ch13-01-closures.html",
1199 );
1200 }
1201 let guar = diag.emit();
1202
1203 let bound_vars = tcx.late_bound_vars(hir_id);
1207 let scope = def_id.to_def_id();
1208
1209 let fn_sig = tcx.mk_fn_sig(
1210 fn_sig.inputs().iter().copied(),
1211 recovered_ret_ty.unwrap_or_else(|| Ty::new_error(tcx, guar)),
1212 fn_sig.c_variadic,
1213 fn_sig.safety,
1214 fn_sig.abi,
1215 );
1216
1217 late_param_regions_to_bound(tcx, scope, bound_vars, fn_sig)
1218}
1219
1220pub fn suggest_impl_trait<'tcx>(
1221 infcx: &InferCtxt<'tcx>,
1222 param_env: ty::ParamEnv<'tcx>,
1223 ret_ty: Ty<'tcx>,
1224) -> Option<String> {
1225 let format_as_assoc: fn(_, _, _, _, _) -> _ =
1226 |tcx: TyCtxt<'tcx>,
1227 _: ty::GenericArgsRef<'tcx>,
1228 trait_def_id: DefId,
1229 assoc_item_def_id: DefId,
1230 item_ty: Ty<'tcx>| {
1231 let trait_name = tcx.item_name(trait_def_id);
1232 let assoc_name = tcx.item_name(assoc_item_def_id);
1233 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}>"))
1234 };
1235 let format_as_parenthesized: fn(_, _, _, _, _) -> _ =
1236 |tcx: TyCtxt<'tcx>,
1237 args: ty::GenericArgsRef<'tcx>,
1238 trait_def_id: DefId,
1239 _: DefId,
1240 item_ty: Ty<'tcx>| {
1241 let trait_name = tcx.item_name(trait_def_id);
1242 let args_tuple = args.type_at(1);
1243 let ty::Tuple(types) = *args_tuple.kind() else {
1244 return None;
1245 };
1246 let types = types.make_suggestable(tcx, false, None)?;
1247 let maybe_ret =
1248 if item_ty.is_unit() { String::new() } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" -> {0}", item_ty))
})format!(" -> {item_ty}") };
1249 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!(
1250 "impl {trait_name}({}){maybe_ret}",
1251 types.iter().map(|ty| ty.to_string()).collect::<Vec<_>>().join(", ")
1252 ))
1253 };
1254
1255 for (trait_def_id, assoc_item_def_id, formatter) in [
1256 (
1257 infcx.tcx.get_diagnostic_item(sym::Iterator),
1258 infcx.tcx.get_diagnostic_item(sym::IteratorItem),
1259 format_as_assoc,
1260 ),
1261 (
1262 infcx.tcx.lang_items().future_trait(),
1263 infcx.tcx.lang_items().future_output(),
1264 format_as_assoc,
1265 ),
1266 (
1267 infcx.tcx.lang_items().async_fn_trait(),
1268 infcx.tcx.lang_items().async_fn_once_output(),
1269 format_as_parenthesized,
1270 ),
1271 (
1272 infcx.tcx.lang_items().async_fn_mut_trait(),
1273 infcx.tcx.lang_items().async_fn_once_output(),
1274 format_as_parenthesized,
1275 ),
1276 (
1277 infcx.tcx.lang_items().async_fn_once_trait(),
1278 infcx.tcx.lang_items().async_fn_once_output(),
1279 format_as_parenthesized,
1280 ),
1281 (
1282 infcx.tcx.lang_items().fn_trait(),
1283 infcx.tcx.lang_items().fn_once_output(),
1284 format_as_parenthesized,
1285 ),
1286 (
1287 infcx.tcx.lang_items().fn_mut_trait(),
1288 infcx.tcx.lang_items().fn_once_output(),
1289 format_as_parenthesized,
1290 ),
1291 (
1292 infcx.tcx.lang_items().fn_once_trait(),
1293 infcx.tcx.lang_items().fn_once_output(),
1294 format_as_parenthesized,
1295 ),
1296 ] {
1297 let Some(trait_def_id) = trait_def_id else {
1298 continue;
1299 };
1300 let Some(assoc_item_def_id) = assoc_item_def_id else {
1301 continue;
1302 };
1303 if infcx.tcx.def_kind(assoc_item_def_id) != DefKind::AssocTy {
1304 continue;
1305 }
1306 let sugg = infcx.probe(|_| {
1307 let args = ty::GenericArgs::for_item(infcx.tcx, trait_def_id, |param, _| {
1308 if param.index == 0 { ret_ty.into() } else { infcx.var_for_def(DUMMY_SP, param) }
1309 });
1310 if !infcx
1311 .type_implements_trait(trait_def_id, args, param_env)
1312 .must_apply_modulo_regions()
1313 {
1314 return None;
1315 }
1316 let ocx = ObligationCtxt::new(&infcx);
1317 let item_ty = ocx.normalize(
1318 &ObligationCause::dummy(),
1319 param_env,
1320 Ty::new_projection_from_args(infcx.tcx, assoc_item_def_id, args),
1321 );
1322 if ocx.try_evaluate_obligations().is_empty()
1324 && let item_ty = infcx.resolve_vars_if_possible(item_ty)
1325 && let Some(item_ty) = item_ty.make_suggestable(infcx.tcx, false, None)
1326 && let Some(sugg) = formatter(
1327 infcx.tcx,
1328 infcx.resolve_vars_if_possible(args),
1329 trait_def_id,
1330 assoc_item_def_id,
1331 item_ty,
1332 )
1333 {
1334 return Some(sugg);
1335 }
1336
1337 None
1338 });
1339
1340 if sugg.is_some() {
1341 return sugg;
1342 }
1343 }
1344 None
1345}
1346
1347fn impl_trait_header(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::ImplTraitHeader<'_> {
1348 let icx = ItemCtxt::new(tcx, def_id);
1349 let item = tcx.hir_expect_item(def_id);
1350 let impl_ = item.expect_impl();
1351 let of_trait = impl_
1352 .of_trait
1353 .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:?}"));
1354 let selfty = tcx.type_of(def_id).instantiate_identity();
1355 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(..));
1356
1357 check_impl_constness(tcx, impl_.constness, &of_trait.trait_ref);
1358
1359 let trait_ref = icx.lowerer().lower_impl_trait_ref(&of_trait.trait_ref, selfty);
1360
1361 ty::ImplTraitHeader {
1362 trait_ref: ty::EarlyBinder::bind(trait_ref),
1363 safety: of_trait.safety,
1364 polarity: polarity_of_impl(tcx, of_trait, is_rustc_reservation),
1365 constness: impl_.constness,
1366 }
1367}
1368
1369fn check_impl_constness(
1370 tcx: TyCtxt<'_>,
1371 constness: hir::Constness,
1372 hir_trait_ref: &hir::TraitRef<'_>,
1373) {
1374 if let hir::Constness::NotConst = constness {
1375 return;
1376 }
1377
1378 let Some(trait_def_id) = hir_trait_ref.trait_def_id() else { return };
1379 if tcx.is_const_trait(trait_def_id) {
1380 return;
1381 }
1382
1383 let trait_name = tcx.item_name(trait_def_id).to_string();
1384 let (suggestion, suggestion_pre) = match (trait_def_id.as_local(), tcx.sess.is_nightly_build())
1385 {
1386 (Some(trait_def_id), true) => {
1387 let span = tcx.hir_expect_item(trait_def_id).vis_span;
1388 let span = tcx.sess.source_map().span_extend_while_whitespace(span);
1389
1390 (
1391 Some(span.shrink_to_hi()),
1392 if tcx.features().const_trait_impl() {
1393 ""
1394 } else {
1395 "enable `#![feature(const_trait_impl)]` in your crate and "
1396 },
1397 )
1398 }
1399 (None, _) | (_, false) => (None, ""),
1400 };
1401 tcx.dcx().emit_err(errors::ConstImplForNonConstTrait {
1402 trait_ref_span: hir_trait_ref.path.span,
1403 trait_name,
1404 suggestion,
1405 suggestion_pre,
1406 marking: (),
1407 adding: (),
1408 });
1409}
1410
1411fn polarity_of_impl(
1412 tcx: TyCtxt<'_>,
1413 of_trait: &hir::TraitImplHeader<'_>,
1414 is_rustc_reservation: bool,
1415) -> ty::ImplPolarity {
1416 match of_trait.polarity {
1417 hir::ImplPolarity::Negative(span) => {
1418 if is_rustc_reservation {
1419 let span = span.to(of_trait.trait_ref.path.span);
1420 tcx.dcx().span_err(span, "reservation impls can't be negative");
1421 }
1422 ty::ImplPolarity::Negative
1423 }
1424 hir::ImplPolarity::Positive => {
1425 if is_rustc_reservation {
1426 ty::ImplPolarity::Reservation
1427 } else {
1428 ty::ImplPolarity::Positive
1429 }
1430 }
1431 }
1432}
1433
1434fn early_bound_lifetimes_from_generics<'a, 'tcx>(
1440 tcx: TyCtxt<'tcx>,
1441 generics: &'a hir::Generics<'a>,
1442) -> impl Iterator<Item = &'a hir::GenericParam<'a>> {
1443 generics.params.iter().filter(move |param| match param.kind {
1444 GenericParamKind::Lifetime { .. } => !tcx.is_late_bound(param.hir_id),
1445 _ => false,
1446 })
1447}
1448
1449fn compute_sig_of_foreign_fn_decl<'tcx>(
1450 tcx: TyCtxt<'tcx>,
1451 def_id: LocalDefId,
1452 decl: &'tcx hir::FnDecl<'tcx>,
1453 abi: ExternAbi,
1454 safety: hir::Safety,
1455) -> ty::PolyFnSig<'tcx> {
1456 let hir_id = tcx.local_def_id_to_hir_id(def_id);
1457 let fty =
1458 ItemCtxt::new(tcx, def_id).lowerer().lower_fn_ty(hir_id, safety, abi, decl, None, None);
1459
1460 if !tcx.features().simd_ffi() {
1463 let check = |hir_ty: &hir::Ty<'_>, ty: Ty<'_>| {
1464 if ty.is_simd() {
1465 let snip = tcx
1466 .sess
1467 .source_map()
1468 .span_to_snippet(hir_ty.span)
1469 .map_or_else(|_| String::new(), |s| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" `{0}`", s))
})format!(" `{s}`"));
1470 tcx.dcx().emit_err(errors::SIMDFFIHighlyExperimental { span: hir_ty.span, snip });
1471 }
1472 };
1473 for (input, ty) in iter::zip(decl.inputs, fty.inputs().skip_binder()) {
1474 check(input, *ty)
1475 }
1476 if let hir::FnRetTy::Return(ty) = decl.output {
1477 check(ty, fty.output().skip_binder())
1478 }
1479 }
1480
1481 fty
1482}
1483
1484fn coroutine_kind(tcx: TyCtxt<'_>, def_id: LocalDefId) -> Option<hir::CoroutineKind> {
1485 match tcx.hir_node_by_def_id(def_id) {
1486 Node::Expr(&hir::Expr {
1487 kind:
1488 hir::ExprKind::Closure(&rustc_hir::Closure {
1489 kind: hir::ClosureKind::Coroutine(kind),
1490 ..
1491 }),
1492 ..
1493 }) => Some(kind),
1494 _ => None,
1495 }
1496}
1497
1498fn coroutine_for_closure(tcx: TyCtxt<'_>, def_id: LocalDefId) -> DefId {
1499 let &rustc_hir::Closure { kind: hir::ClosureKind::CoroutineClosure(_), body, .. } =
1500 tcx.hir_node_by_def_id(def_id).expect_closure()
1501 else {
1502 ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!()
1503 };
1504
1505 let &hir::Expr {
1506 kind:
1507 hir::ExprKind::Closure(&rustc_hir::Closure {
1508 def_id,
1509 kind: hir::ClosureKind::Coroutine(_),
1510 ..
1511 }),
1512 ..
1513 } = tcx.hir_body(body).value
1514 else {
1515 ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!()
1516 };
1517
1518 def_id.to_def_id()
1519}
1520
1521fn opaque_ty_origin<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> hir::OpaqueTyOrigin<DefId> {
1522 match tcx.hir_node_by_def_id(def_id).expect_opaque_ty().origin {
1523 hir::OpaqueTyOrigin::FnReturn { parent, in_trait_or_impl } => {
1524 hir::OpaqueTyOrigin::FnReturn { parent: parent.to_def_id(), in_trait_or_impl }
1525 }
1526 hir::OpaqueTyOrigin::AsyncFn { parent, in_trait_or_impl } => {
1527 hir::OpaqueTyOrigin::AsyncFn { parent: parent.to_def_id(), in_trait_or_impl }
1528 }
1529 hir::OpaqueTyOrigin::TyAlias { parent, in_assoc_ty } => {
1530 hir::OpaqueTyOrigin::TyAlias { parent: parent.to_def_id(), in_assoc_ty }
1531 }
1532 }
1533}
1534
1535fn rendered_precise_capturing_args<'tcx>(
1536 tcx: TyCtxt<'tcx>,
1537 def_id: LocalDefId,
1538) -> Option<&'tcx [PreciseCapturingArgKind<Symbol, Symbol>]> {
1539 if let Some(ty::ImplTraitInTraitData::Trait { opaque_def_id, .. }) =
1540 tcx.opt_rpitit_info(def_id.to_def_id())
1541 {
1542 return tcx.rendered_precise_capturing_args(opaque_def_id);
1543 }
1544
1545 tcx.hir_node_by_def_id(def_id).expect_opaque_ty().bounds.iter().find_map(|bound| match bound {
1546 hir::GenericBound::Use(args, ..) => {
1547 Some(&*tcx.arena.alloc_from_iter(args.iter().map(|arg| match arg {
1548 PreciseCapturingArgKind::Lifetime(_) => {
1549 PreciseCapturingArgKind::Lifetime(arg.name())
1550 }
1551 PreciseCapturingArgKind::Param(_) => PreciseCapturingArgKind::Param(arg.name()),
1552 })))
1553 }
1554 _ => None,
1555 })
1556}
1557
1558fn const_param_default<'tcx>(
1559 tcx: TyCtxt<'tcx>,
1560 local_def_id: LocalDefId,
1561) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
1562 let hir::Node::GenericParam(hir::GenericParam {
1563 kind: hir::GenericParamKind::Const { default: Some(default_ct), .. },
1564 ..
1565 }) = tcx.hir_node_by_def_id(local_def_id)
1566 else {
1567 ::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!(
1568 tcx.def_span(local_def_id),
1569 "`const_param_default` expected a generic parameter with a constant"
1570 )
1571 };
1572
1573 let icx = ItemCtxt::new(tcx, local_def_id);
1574
1575 let def_id = local_def_id.to_def_id();
1576 let identity_args = ty::GenericArgs::identity_for_item(tcx, tcx.parent(def_id));
1577
1578 let ct = icx
1579 .lowerer()
1580 .lower_const_arg(default_ct, tcx.type_of(def_id).instantiate(tcx, identity_args));
1581 ty::EarlyBinder::bind(ct)
1582}
1583
1584fn anon_const_kind<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> ty::AnonConstKind {
1585 let hir_id = tcx.local_def_id_to_hir_id(def);
1586 let const_arg_id = tcx.parent_hir_id(hir_id);
1587 match tcx.hir_node(const_arg_id) {
1588 hir::Node::ConstArg(_) => {
1589 let parent_hir_node = tcx.hir_node(tcx.parent_hir_id(const_arg_id));
1590 if tcx.features().generic_const_exprs() {
1591 ty::AnonConstKind::GCE
1592 } else if tcx.features().opaque_generic_const_args() {
1593 if !is_anon_const_rhs_of_const_item(tcx, def) {
1597 return ty::AnonConstKind::MCG;
1598 }
1599
1600 let body = tcx.hir_body_owned_by(def);
1601 let mut visitor = OGCAParamVisitor(tcx);
1602 match visitor.visit_body(body) {
1603 ControlFlow::Break(UsesParam) => ty::AnonConstKind::OGCA,
1604 ControlFlow::Continue(()) => ty::AnonConstKind::MCG,
1605 }
1606 } else if tcx.features().min_generic_const_args() {
1607 ty::AnonConstKind::MCG
1608 } else if let hir::Node::Expr(hir::Expr {
1609 kind: hir::ExprKind::Repeat(_, repeat_count),
1610 ..
1611 }) = parent_hir_node
1612 && repeat_count.hir_id == const_arg_id
1613 {
1614 ty::AnonConstKind::RepeatExprCount
1615 } else {
1616 ty::AnonConstKind::MCG
1617 }
1618 }
1619 _ => ty::AnonConstKind::NonTypeSystem,
1620 }
1621}
1622
1623fn is_anon_const_rhs_of_const_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> bool {
1624 let hir_id = tcx.local_def_id_to_hir_id(def_id);
1625 let Some((_, grandparent_node)) = tcx.hir_parent_iter(hir_id).nth(1) else { return false };
1626 let (Node::Item(hir::Item { kind: hir::ItemKind::Const(_, _, _, ct_rhs), .. })
1627 | Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(_, ct_rhs), .. })
1628 | Node::TraitItem(hir::TraitItem {
1629 kind: hir::TraitItemKind::Const(_, Some(ct_rhs), _),
1630 ..
1631 })) = grandparent_node
1632 else {
1633 return false;
1634 };
1635 let hir::ConstItemRhs::TypeConst(hir::ConstArg {
1636 kind: hir::ConstArgKind::Anon(rhs_anon), ..
1637 }) = ct_rhs
1638 else {
1639 return false;
1640 };
1641 def_id == rhs_anon.def_id
1642}
1643
1644struct OGCAParamVisitor<'tcx>(TyCtxt<'tcx>);
1645
1646struct UsesParam;
1647
1648impl<'tcx> Visitor<'tcx> for OGCAParamVisitor<'tcx> {
1649 type NestedFilter = nested_filter::OnlyBodies;
1650 type Result = ControlFlow<UsesParam>;
1651
1652 fn maybe_tcx(&mut self) -> TyCtxt<'tcx> {
1653 self.0
1654 }
1655
1656 fn visit_path(&mut self, path: &hir::Path<'tcx>, _id: HirId) -> ControlFlow<UsesParam> {
1657 if let Res::Def(DefKind::TyParam | DefKind::ConstParam | DefKind::LifetimeParam, _) =
1658 path.res
1659 {
1660 return ControlFlow::Break(UsesParam);
1661 }
1662
1663 intravisit::walk_path(self, path)
1664 }
1665}
1666
1667x;#[instrument(level = "debug", skip(tcx), ret)]
1668fn const_of_item<'tcx>(
1669 tcx: TyCtxt<'tcx>,
1670 def_id: LocalDefId,
1671) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
1672 let ct_rhs = match tcx.hir_node_by_def_id(def_id) {
1673 hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(.., ct), .. }) => *ct,
1674 hir::Node::TraitItem(hir::TraitItem {
1675 kind: hir::TraitItemKind::Const(_, ct, _), ..
1676 }) => ct.expect("no default value for trait assoc const"),
1677 hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Const(.., ct), .. }) => *ct,
1678 _ => {
1679 span_bug!(tcx.def_span(def_id), "`const_of_item` expected a const or assoc const item")
1680 }
1681 };
1682 let ct_arg = match ct_rhs {
1683 hir::ConstItemRhs::TypeConst(ct_arg) => ct_arg,
1684 hir::ConstItemRhs::Body(_) => {
1685 let e = tcx.dcx().span_delayed_bug(
1686 tcx.def_span(def_id),
1687 "cannot call const_of_item on a non-type_const",
1688 );
1689 return ty::EarlyBinder::bind(Const::new_error(tcx, e));
1690 }
1691 };
1692 let icx = ItemCtxt::new(tcx, def_id);
1693 let identity_args = ty::GenericArgs::identity_for_item(tcx, def_id);
1694 let ct = icx
1695 .lowerer()
1696 .lower_const_arg(ct_arg, tcx.type_of(def_id.to_def_id()).instantiate(tcx, identity_args));
1697 if let Err(e) = icx.check_tainted_by_errors()
1698 && !ct.references_error()
1699 {
1700 ty::EarlyBinder::bind(Const::new_error(tcx, e))
1701 } else {
1702 ty::EarlyBinder::bind(ct)
1703 }
1704}
1705
1706fn is_rhs_type_const<'tcx>(tcx: TyCtxt<'tcx>, def: LocalDefId) -> bool {
1708 match tcx.hir_node_by_def_id(def) {
1709 hir::Node::Item(hir::Item {
1710 kind: hir::ItemKind::Const(_, _, _, hir::ConstItemRhs::TypeConst(_)),
1711 ..
1712 })
1713 | hir::Node::ImplItem(hir::ImplItem {
1714 kind: hir::ImplItemKind::Const(_, hir::ConstItemRhs::TypeConst(_)),
1715 ..
1716 })
1717 | hir::Node::TraitItem(hir::TraitItem {
1718 kind: hir::TraitItemKind::Const(_, _, hir::IsTypeConst::Yes),
1719 ..
1720 }) => return true,
1721 _ => return false,
1722 }
1723}