1 use crate::check::intrinsicck::InlineAsmCtxt;
3 use super::compare_method::check_type_bounds;
4 use super::compare_method::{compare_impl_method, compare_ty_impl};
6 use rustc_attr as attr;
7 use rustc_errors::{Applicability, ErrorGuaranteed, MultiSpan};
9 use rustc_hir::def::{DefKind, Res};
10 use rustc_hir::def_id::{DefId, LocalDefId};
11 use rustc_hir::intravisit::Visitor;
12 use rustc_hir::{ItemKind, Node, PathSegment};
13 use rustc_infer::infer::outlives::env::OutlivesEnvironment;
14 use rustc_infer::infer::{DefiningAnchor, RegionVariableOrigin, TyCtxtInferExt};
15 use rustc_infer::traits::Obligation;
16 use rustc_lint::builtin::REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS;
17 use rustc_middle::hir::nested_filter;
18 use rustc_middle::middle::stability::EvalResult;
19 use rustc_middle::ty::layout::{LayoutError, MAX_SIMD_LANES};
20 use rustc_middle::ty::subst::GenericArgKind;
21 use rustc_middle::ty::util::{Discr, IntTypeExt};
22 use rustc_middle::ty::{
23 self, ParamEnv, ToPredicate, Ty, TyCtxt, TypeSuperVisitable, TypeVisitable,
25 use rustc_session::lint::builtin::{UNINHABITED_STATIC, UNSUPPORTED_CALLING_CONVENTIONS};
26 use rustc_span::symbol::sym;
27 use rustc_span::{self, Span};
28 use rustc_target::spec::abi::Abi;
29 use rustc_trait_selection::traits::error_reporting::TypeErrCtxtExt as _;
30 use rustc_trait_selection::traits::{self, ObligationCtxt};
32 use std::ops::ControlFlow;
34 pub fn check_abi(tcx: TyCtxt<'_>, hir_id: hir::HirId, span: Span, abi: Abi) {
35 match tcx.sess.target.is_abi_supported(abi) {
42 "`{abi}` is not a supported ABI for the current target",
47 tcx.struct_span_lint_hir(
48 UNSUPPORTED_CALLING_CONVENTIONS,
51 "use of calling convention not supported on this target",
57 // This ABI is only allowed on function pointers
58 if abi == Abi::CCmseNonSecureCall {
63 "the `\"C-cmse-nonsecure-call\"` ABI is only allowed on function pointers"
69 fn check_struct(tcx: TyCtxt<'_>, def_id: LocalDefId) {
70 let def = tcx.adt_def(def_id);
71 let span = tcx.def_span(def_id);
72 def.destructor(tcx); // force the destructor to be evaluated
74 if def.repr().simd() {
75 check_simd(tcx, span, def_id);
78 check_transparent(tcx, span, def);
79 check_packed(tcx, span, def);
82 fn check_union(tcx: TyCtxt<'_>, def_id: LocalDefId) {
83 let def = tcx.adt_def(def_id);
84 let span = tcx.def_span(def_id);
85 def.destructor(tcx); // force the destructor to be evaluated
86 check_transparent(tcx, span, def);
87 check_union_fields(tcx, span, def_id);
88 check_packed(tcx, span, def);
91 /// Check that the fields of the `union` do not need dropping.
92 fn check_union_fields(tcx: TyCtxt<'_>, span: Span, item_def_id: LocalDefId) -> bool {
93 let item_type = tcx.type_of(item_def_id);
94 if let ty::Adt(def, substs) = item_type.kind() {
95 assert!(def.is_union());
97 fn allowed_union_field<'tcx>(
100 param_env: ty::ParamEnv<'tcx>,
103 // We don't just accept all !needs_drop fields, due to semver concerns.
105 ty::Ref(..) => true, // references never drop (even mutable refs, which are non-Copy and hence fail the later check)
107 // allow tuples of allowed types
108 tys.iter().all(|ty| allowed_union_field(ty, tcx, param_env, span))
110 ty::Array(elem, _len) => {
111 // Like `Copy`, we do *not* special-case length 0.
112 allowed_union_field(*elem, tcx, param_env, span)
115 // Fallback case: allow `ManuallyDrop` and things that are `Copy`.
116 ty.ty_adt_def().is_some_and(|adt_def| adt_def.is_manually_drop())
117 || ty.is_copy_modulo_regions(tcx.at(span), param_env)
122 let param_env = tcx.param_env(item_def_id);
123 for field in &def.non_enum_variant().fields {
124 let field_ty = field.ty(tcx, substs);
126 if !allowed_union_field(field_ty, tcx, param_env, span) {
127 let (field_span, ty_span) = match tcx.hir().get_if_local(field.did) {
128 // We are currently checking the type this field came from, so it must be local.
129 Some(Node::Field(field)) => (field.span, field.ty.span),
130 _ => unreachable!("mir field has to correspond to hir field"),
136 "unions cannot contain fields that may need dropping"
139 "a type is guaranteed not to need dropping \
140 when it implements `Copy`, or when it is the special `ManuallyDrop<_>` type",
142 .multipart_suggestion_verbose(
143 "when the type does not implement `Copy`, \
144 wrap it inside a `ManuallyDrop<_>` and ensure it is manually dropped",
146 (ty_span.shrink_to_lo(), "std::mem::ManuallyDrop<".into()),
147 (ty_span.shrink_to_hi(), ">".into()),
149 Applicability::MaybeIncorrect,
153 } else if field_ty.needs_drop(tcx, param_env) {
154 // This should never happen. But we can get here e.g. in case of name resolution errors.
155 tcx.sess.delay_span_bug(span, "we should never accept maybe-dropping union fields");
159 span_bug!(span, "unions must be ty::Adt, but got {:?}", item_type.kind());
164 /// Check that a `static` is inhabited.
165 fn check_static_inhabited<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
166 // Make sure statics are inhabited.
167 // Other parts of the compiler assume that there are no uninhabited places. In principle it
168 // would be enough to check this for `extern` statics, as statics with an initializer will
169 // have UB during initialization if they are uninhabited, but there also seems to be no good
170 // reason to allow any statics to be uninhabited.
171 let ty = tcx.type_of(def_id);
172 let span = tcx.def_span(def_id);
173 let layout = match tcx.layout_of(ParamEnv::reveal_all().and(ty)) {
175 // Foreign statics that overflow their allowed size should emit an error
176 Err(LayoutError::SizeOverflow(_))
178 let node = tcx.hir().get_by_def_id(def_id);
181 hir::Node::ForeignItem(hir::ForeignItem {
182 kind: hir::ForeignItemKind::Static(..),
189 .struct_span_err(span, "extern static is too large for the current architecture")
193 // Generic statics are rejected, but we still reach this case.
195 tcx.sess.delay_span_bug(span, &e.to_string());
199 if layout.abi.is_uninhabited() {
200 tcx.struct_span_lint_hir(
202 tcx.hir().local_def_id_to_hir_id(def_id),
204 "static of uninhabited type",
207 .note("uninhabited statics cannot be initialized, and any access would be an immediate error")
213 /// Checks that an opaque type does not contain cycles and does not use `Self` or `T::Foo`
214 /// projections that would result in "inheriting lifetimes".
215 fn check_opaque<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
216 let item = tcx.hir().item(id);
217 let hir::ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) = item.kind else {
218 tcx.sess.delay_span_bug(tcx.hir().span(id.hir_id()), "expected opaque item");
222 // HACK(jynelson): trying to infer the type of `impl trait` breaks documenting
223 // `async-std` (and `pub async fn` in general).
224 // Since rustdoc doesn't care about the concrete type behind `impl Trait`, just don't look at it!
225 // See https://github.com/rust-lang/rust/issues/75100
226 if tcx.sess.opts.actually_rustdoc {
230 let substs = InternalSubsts::identity_for_item(tcx, item.def_id.to_def_id());
231 let span = tcx.def_span(item.def_id.def_id);
233 check_opaque_for_inheriting_lifetimes(tcx, item.def_id.def_id, span);
234 if tcx.type_of(item.def_id.def_id).references_error() {
237 if check_opaque_for_cycles(tcx, item.def_id.def_id, substs, span, &origin).is_err() {
240 check_opaque_meets_bounds(tcx, item.def_id.def_id, substs, span, &origin);
242 /// Checks that an opaque type does not use `Self` or `T::Foo` projections that would result
243 /// in "inheriting lifetimes".
244 #[instrument(level = "debug", skip(tcx, span))]
245 pub(super) fn check_opaque_for_inheriting_lifetimes<'tcx>(
250 let item = tcx.hir().expect_item(def_id);
251 debug!(?item, ?span);
253 struct FoundParentLifetime;
254 struct FindParentLifetimeVisitor<'tcx>(&'tcx ty::Generics);
255 impl<'tcx> ty::visit::TypeVisitor<'tcx> for FindParentLifetimeVisitor<'tcx> {
256 type BreakTy = FoundParentLifetime;
258 fn visit_region(&mut self, r: ty::Region<'tcx>) -> ControlFlow<Self::BreakTy> {
259 debug!("FindParentLifetimeVisitor: r={:?}", r);
260 if let ty::ReEarlyBound(ty::EarlyBoundRegion { index, .. }) = *r {
261 if index < self.0.parent_count as u32 {
262 return ControlFlow::Break(FoundParentLifetime);
264 return ControlFlow::CONTINUE;
268 r.super_visit_with(self)
271 fn visit_const(&mut self, c: ty::Const<'tcx>) -> ControlFlow<Self::BreakTy> {
272 if let ty::ConstKind::Unevaluated(..) = c.kind() {
273 // FIXME(#72219) We currently don't detect lifetimes within substs
274 // which would violate this check. Even though the particular substitution is not used
275 // within the const, this should still be fixed.
276 return ControlFlow::CONTINUE;
278 c.super_visit_with(self)
282 struct ProhibitOpaqueVisitor<'tcx> {
284 opaque_identity_ty: Ty<'tcx>,
285 generics: &'tcx ty::Generics,
286 selftys: Vec<(Span, Option<String>)>,
289 impl<'tcx> ty::visit::TypeVisitor<'tcx> for ProhibitOpaqueVisitor<'tcx> {
290 type BreakTy = Ty<'tcx>;
292 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
293 debug!("check_opaque_for_inheriting_lifetimes: (visit_ty) t={:?}", t);
294 if t == self.opaque_identity_ty {
295 ControlFlow::CONTINUE
297 t.super_visit_with(&mut FindParentLifetimeVisitor(self.generics))
298 .map_break(|FoundParentLifetime| t)
303 impl<'tcx> Visitor<'tcx> for ProhibitOpaqueVisitor<'tcx> {
304 type NestedFilter = nested_filter::OnlyBodies;
306 fn nested_visit_map(&mut self) -> Self::Map {
310 fn visit_ty(&mut self, arg: &'tcx hir::Ty<'tcx>) {
312 hir::TyKind::Path(hir::QPath::Resolved(None, path)) => match &path.segments {
313 [PathSegment { res: Res::SelfTyParam { .. }, .. }] => {
314 let impl_ty_name = None;
315 self.selftys.push((path.span, impl_ty_name));
317 [PathSegment { res: Res::SelfTyAlias { alias_to: def_id, .. }, .. }] => {
318 let impl_ty_name = Some(self.tcx.def_path_str(*def_id));
319 self.selftys.push((path.span, impl_ty_name));
325 hir::intravisit::walk_ty(self, arg);
329 if let ItemKind::OpaqueTy(hir::OpaqueTy {
330 origin: hir::OpaqueTyOrigin::AsyncFn(..) | hir::OpaqueTyOrigin::FnReturn(..),
334 let mut visitor = ProhibitOpaqueVisitor {
335 opaque_identity_ty: tcx.mk_opaque(
337 InternalSubsts::identity_for_item(tcx, def_id.to_def_id()),
339 generics: tcx.generics_of(def_id),
343 let prohibit_opaque = tcx
344 .explicit_item_bounds(def_id)
346 .try_for_each(|(predicate, _)| predicate.visit_with(&mut visitor));
348 "check_opaque_for_inheriting_lifetimes: prohibit_opaque={:?}, visitor.opaque_identity_ty={:?}, visitor.generics={:?}",
349 prohibit_opaque, visitor.opaque_identity_ty, visitor.generics
352 if let Some(ty) = prohibit_opaque.break_value() {
353 visitor.visit_item(&item);
354 let is_async = match item.kind {
355 ItemKind::OpaqueTy(hir::OpaqueTy { origin, .. }) => {
356 matches!(origin, hir::OpaqueTyOrigin::AsyncFn(..))
361 let mut err = struct_span_err!(
365 "`{}` return type cannot contain a projection or `Self` that references lifetimes from \
367 if is_async { "async fn" } else { "impl Trait" },
370 for (span, name) in visitor.selftys {
373 "consider spelling out the type instead",
374 name.unwrap_or_else(|| format!("{:?}", ty)),
375 Applicability::MaybeIncorrect,
383 /// Checks that an opaque type does not contain cycles.
384 pub(super) fn check_opaque_for_cycles<'tcx>(
387 substs: SubstsRef<'tcx>,
389 origin: &hir::OpaqueTyOrigin,
390 ) -> Result<(), ErrorGuaranteed> {
391 if tcx.try_expand_impl_trait_type(def_id.to_def_id(), substs).is_err() {
392 let reported = match origin {
393 hir::OpaqueTyOrigin::AsyncFn(..) => async_opaque_type_cycle_error(tcx, span),
394 _ => opaque_type_cycle_error(tcx, def_id, span),
402 /// Check that the concrete type behind `impl Trait` actually implements `Trait`.
404 /// This is mostly checked at the places that specify the opaque type, but we
405 /// check those cases in the `param_env` of that function, which may have
406 /// bounds not on this opaque type:
408 /// ```ignore (illustrative)
409 /// type X<T> = impl Clone;
410 /// fn f<T: Clone>(t: T) -> X<T> {
415 /// Without this check the above code is incorrectly accepted: we would ICE if
416 /// some tried, for example, to clone an `Option<X<&mut ()>>`.
417 #[instrument(level = "debug", skip(tcx))]
418 fn check_opaque_meets_bounds<'tcx>(
421 substs: SubstsRef<'tcx>,
423 origin: &hir::OpaqueTyOrigin,
425 let hir_id = tcx.hir().local_def_id_to_hir_id(def_id);
426 let defining_use_anchor = match *origin {
427 hir::OpaqueTyOrigin::FnReturn(did) | hir::OpaqueTyOrigin::AsyncFn(did) => did,
428 hir::OpaqueTyOrigin::TyAlias => def_id,
430 let param_env = tcx.param_env(defining_use_anchor);
434 .with_opaque_type_inference(DefiningAnchor::Bind(defining_use_anchor))
436 let ocx = ObligationCtxt::new(&infcx);
437 let opaque_ty = tcx.mk_opaque(def_id.to_def_id(), substs);
439 // `ReErased` regions appear in the "parent_substs" of closures/generators.
440 // We're ignoring them here and replacing them with fresh region variables.
441 // See tests in ui/type-alias-impl-trait/closure_{parent_substs,wf_outlives}.rs.
443 // FIXME: Consider wrapping the hidden type in an existential `Binder` and instantiating it
444 // here rather than using ReErased.
445 let hidden_ty = tcx.bound_type_of(def_id.to_def_id()).subst(tcx, substs);
446 let hidden_ty = tcx.fold_regions(hidden_ty, |re, _dbi| match re.kind() {
447 ty::ReErased => infcx.next_region_var(RegionVariableOrigin::MiscVariable(span)),
451 let misc_cause = traits::ObligationCause::misc(span, hir_id);
453 match infcx.at(&misc_cause, param_env).eq(opaque_ty, hidden_ty) {
454 Ok(infer_ok) => ocx.register_infer_ok_obligations(infer_ok),
456 tcx.sess.delay_span_bug(
458 &format!("could not unify `{hidden_ty}` with revealed type:\n{ty_err}"),
463 // Additionally require the hidden type to be well-formed with only the generics of the opaque type.
464 // Defining use functions may have more bounds than the opaque type, which is ok, as long as the
465 // hidden type is well formed even without those bounds.
467 ty::Binder::dummy(ty::PredicateKind::WellFormed(hidden_ty.into())).to_predicate(tcx);
468 ocx.register_obligation(Obligation::new(misc_cause, param_env, predicate));
470 // Check that all obligations are satisfied by the implementation's
472 let errors = ocx.select_all_or_error();
473 if !errors.is_empty() {
474 infcx.err_ctxt().report_fulfillment_errors(&errors, None, false);
477 // Checked when type checking the function containing them.
478 hir::OpaqueTyOrigin::FnReturn(..) | hir::OpaqueTyOrigin::AsyncFn(..) => {}
479 // Can have different predicates to their defining use
480 hir::OpaqueTyOrigin::TyAlias => {
481 let outlives_environment = OutlivesEnvironment::new(param_env);
482 infcx.check_region_obligations_and_report_errors(
484 &outlives_environment,
488 // Clean up after ourselves
489 let _ = infcx.inner.borrow_mut().opaque_type_storage.take_opaque_types();
492 fn check_item_type<'tcx>(tcx: TyCtxt<'tcx>, id: hir::ItemId) {
494 "check_item_type(it.def_id={:?}, it.name={})",
496 tcx.def_path_str(id.def_id.to_def_id())
498 let _indenter = indenter();
499 match tcx.def_kind(id.def_id) {
500 DefKind::Static(..) => {
501 tcx.ensure().typeck(id.def_id.def_id);
502 maybe_check_static_with_link_section(tcx, id.def_id.def_id);
503 check_static_inhabited(tcx, id.def_id.def_id);
506 tcx.ensure().typeck(id.def_id.def_id);
509 let item = tcx.hir().item(id);
510 let hir::ItemKind::Enum(ref enum_definition, _) = item.kind else {
513 check_enum(tcx, &enum_definition.variants, item.def_id.def_id);
515 DefKind::Fn => {} // entirely within check_item_body
517 let it = tcx.hir().item(id);
518 let hir::ItemKind::Impl(ref impl_) = it.kind else {
521 debug!("ItemKind::Impl {} with id {:?}", it.ident, it.def_id);
522 if let Some(impl_trait_ref) = tcx.impl_trait_ref(it.def_id) {
523 check_impl_items_against_trait(
530 check_on_unimplemented(tcx, it);
534 let it = tcx.hir().item(id);
535 let hir::ItemKind::Trait(_, _, _, _, ref items) = it.kind else {
538 check_on_unimplemented(tcx, it);
540 for item in items.iter() {
541 let item = tcx.hir().trait_item(item.id);
543 hir::TraitItemKind::Fn(ref sig, _) => {
544 let abi = sig.header.abi;
545 fn_maybe_err(tcx, item.ident.span, abi);
547 hir::TraitItemKind::Type(.., Some(default)) => {
548 let assoc_item = tcx.associated_item(item.def_id);
550 InternalSubsts::identity_for_item(tcx, it.def_id.to_def_id());
551 let _: Result<_, rustc_errors::ErrorGuaranteed> = check_type_bounds(
556 ty::TraitRef { def_id: it.def_id.to_def_id(), substs: trait_substs },
564 check_struct(tcx, id.def_id.def_id);
567 check_union(tcx, id.def_id.def_id);
569 DefKind::OpaqueTy => {
570 check_opaque(tcx, id);
572 DefKind::ImplTraitPlaceholder => {
573 let parent = tcx.impl_trait_in_trait_parent(id.def_id.to_def_id());
574 // Only check the validity of this opaque type if the function has a default body
575 if let hir::Node::TraitItem(hir::TraitItem {
576 kind: hir::TraitItemKind::Fn(_, hir::TraitFn::Provided(_)),
578 }) = tcx.hir().get_by_def_id(parent.expect_local())
580 check_opaque(tcx, id);
583 DefKind::TyAlias => {
584 let pty_ty = tcx.type_of(id.def_id);
585 let generics = tcx.generics_of(id.def_id);
586 check_type_params_are_used(tcx, &generics, pty_ty);
588 DefKind::ForeignMod => {
589 let it = tcx.hir().item(id);
590 let hir::ItemKind::ForeignMod { abi, items } = it.kind else {
593 check_abi(tcx, it.hir_id(), it.span, abi);
595 if abi == Abi::RustIntrinsic {
597 let item = tcx.hir().foreign_item(item.id);
598 intrinsic::check_intrinsic_type(tcx, item);
600 } else if abi == Abi::PlatformIntrinsic {
602 let item = tcx.hir().foreign_item(item.id);
603 intrinsic::check_platform_intrinsic_type(tcx, item);
607 let def_id = item.id.def_id.def_id;
608 let generics = tcx.generics_of(def_id);
609 let own_counts = generics.own_counts();
610 if generics.params.len() - own_counts.lifetimes != 0 {
611 let (kinds, kinds_pl, egs) = match (own_counts.types, own_counts.consts) {
612 (_, 0) => ("type", "types", Some("u32")),
613 // We don't specify an example value, because we can't generate
614 // a valid value for any type.
615 (0, _) => ("const", "consts", None),
616 _ => ("type or const", "types or consts", None),
622 "foreign items may not have {kinds} parameters",
624 .span_label(item.span, &format!("can't have {kinds} parameters"))
626 // FIXME: once we start storing spans for type arguments, turn this
627 // into a suggestion.
629 "replace the {} parameters with concrete {}{}",
632 egs.map(|egs| format!(" like `{}`", egs)).unwrap_or_default(),
638 let item = tcx.hir().foreign_item(item.id);
640 hir::ForeignItemKind::Fn(ref fn_decl, _, _) => {
641 require_c_abi_if_c_variadic(tcx, fn_decl, abi, item.span);
643 hir::ForeignItemKind::Static(..) => {
644 check_static_inhabited(tcx, def_id);
651 DefKind::GlobalAsm => {
652 let it = tcx.hir().item(id);
653 let hir::ItemKind::GlobalAsm(asm) = it.kind else { span_bug!(it.span, "DefKind::GlobalAsm but got {:#?}", it) };
654 InlineAsmCtxt::new_global_asm(tcx).check_asm(asm, id.hir_id());
660 pub(super) fn check_on_unimplemented(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {
661 // an error would be reported if this fails.
662 let _ = traits::OnUnimplementedDirective::of_item(tcx, item.def_id.to_def_id());
665 pub(super) fn check_specialization_validity<'tcx>(
667 trait_def: &ty::TraitDef,
668 trait_item: &ty::AssocItem,
670 impl_item: &hir::ImplItemRef,
672 let Ok(ancestors) = trait_def.ancestors(tcx, impl_id) else { return };
673 let mut ancestor_impls = ancestors.skip(1).filter_map(|parent| {
674 if parent.is_from_trait() {
677 Some((parent, parent.item(tcx, trait_item.def_id)))
681 let opt_result = ancestor_impls.find_map(|(parent_impl, parent_item)| {
683 // Parent impl exists, and contains the parent item we're trying to specialize, but
684 // doesn't mark it `default`.
685 Some(parent_item) if traits::impl_item_is_final(tcx, &parent_item) => {
686 Some(Err(parent_impl.def_id()))
689 // Parent impl contains item and makes it specializable.
690 Some(_) => Some(Ok(())),
692 // Parent impl doesn't mention the item. This means it's inherited from the
693 // grandparent. In that case, if parent is a `default impl`, inherited items use the
694 // "defaultness" from the grandparent, else they are final.
696 if tcx.impl_defaultness(parent_impl.def_id()).is_default() {
699 Some(Err(parent_impl.def_id()))
705 // If `opt_result` is `None`, we have only encountered `default impl`s that don't contain the
706 // item. This is allowed, the item isn't actually getting specialized here.
707 let result = opt_result.unwrap_or(Ok(()));
709 if let Err(parent_impl) = result {
710 report_forbidden_specialization(tcx, impl_item, parent_impl);
714 fn check_impl_items_against_trait<'tcx>(
716 full_impl_span: Span,
718 impl_trait_ref: ty::TraitRef<'tcx>,
719 impl_item_refs: &[hir::ImplItemRef],
721 // If the trait reference itself is erroneous (so the compilation is going
722 // to fail), skip checking the items here -- the `impl_item` table in `tcx`
723 // isn't populated for such impls.
724 if impl_trait_ref.references_error() {
728 // Negative impls are not expected to have any items
729 match tcx.impl_polarity(impl_id) {
730 ty::ImplPolarity::Reservation | ty::ImplPolarity::Positive => {}
731 ty::ImplPolarity::Negative => {
732 if let [first_item_ref, ..] = impl_item_refs {
733 let first_item_span = tcx.hir().impl_item(first_item_ref.id).span;
738 "negative impls cannot have any items"
746 let trait_def = tcx.trait_def(impl_trait_ref.def_id);
748 for impl_item in impl_item_refs {
749 let ty_impl_item = tcx.associated_item(impl_item.id.def_id);
750 let ty_trait_item = if let Some(trait_item_id) = ty_impl_item.trait_item_def_id {
751 tcx.associated_item(trait_item_id)
753 // Checked in `associated_item`.
754 tcx.sess.delay_span_bug(impl_item.span, "missing associated item in trait");
757 let impl_item_full = tcx.hir().impl_item(impl_item.id);
758 match impl_item_full.kind {
759 hir::ImplItemKind::Const(..) => {
760 let _ = tcx.compare_assoc_const_impl_item_with_trait_item((
761 impl_item.id.def_id.def_id,
762 ty_impl_item.trait_item_def_id.unwrap(),
765 hir::ImplItemKind::Fn(..) => {
766 let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
775 hir::ImplItemKind::Type(impl_ty) => {
776 let opt_trait_span = tcx.hir().span_if_local(ty_trait_item.def_id);
788 check_specialization_validity(
797 if let Ok(ancestors) = trait_def.ancestors(tcx, impl_id.to_def_id()) {
798 // Check for missing items from trait
799 let mut missing_items = Vec::new();
801 let mut must_implement_one_of: Option<&[Ident]> =
802 trait_def.must_implement_one_of.as_deref();
804 for &trait_item_id in tcx.associated_item_def_ids(impl_trait_ref.def_id) {
805 let is_implemented = ancestors
806 .leaf_def(tcx, trait_item_id)
807 .map_or(false, |node_item| node_item.item.defaultness(tcx).has_value());
809 if !is_implemented && tcx.impl_defaultness(impl_id).is_final() {
810 missing_items.push(tcx.associated_item(trait_item_id));
813 // true if this item is specifically implemented in this impl
814 let is_implemented_here = ancestors
815 .leaf_def(tcx, trait_item_id)
816 .map_or(false, |node_item| !node_item.defining_node.is_from_trait());
818 if !is_implemented_here {
819 match tcx.eval_default_body_stability(trait_item_id, full_impl_span) {
820 EvalResult::Deny { feature, reason, issue, .. } => default_body_is_unstable(
829 // Unmarked default bodies are considered stable (at least for now).
830 EvalResult::Allow | EvalResult::Unmarked => {}
834 if let Some(required_items) = &must_implement_one_of {
835 if is_implemented_here {
836 let trait_item = tcx.associated_item(trait_item_id);
837 if required_items.contains(&trait_item.ident(tcx)) {
838 must_implement_one_of = None;
844 if !missing_items.is_empty() {
845 missing_items_err(tcx, tcx.def_span(impl_id), &missing_items, full_impl_span);
848 if let Some(missing_items) = must_implement_one_of {
850 .get_attr(impl_trait_ref.def_id, sym::rustc_must_implement_one_of)
851 .map(|attr| attr.span);
853 missing_items_must_implement_one_of_err(
855 tcx.def_span(impl_id),
863 pub fn check_simd(tcx: TyCtxt<'_>, sp: Span, def_id: LocalDefId) {
864 let t = tcx.type_of(def_id);
865 if let ty::Adt(def, substs) = t.kind()
868 let fields = &def.non_enum_variant().fields;
869 if fields.is_empty() {
870 struct_span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty").emit();
873 let e = fields[0].ty(tcx, substs);
874 if !fields.iter().all(|f| f.ty(tcx, substs) == e) {
875 struct_span_err!(tcx.sess, sp, E0076, "SIMD vector should be homogeneous")
876 .span_label(sp, "SIMD elements must have the same type")
881 let len = if let ty::Array(_ty, c) = e.kind() {
882 c.try_eval_usize(tcx, tcx.param_env(def.did()))
884 Some(fields.len() as u64)
886 if let Some(len) = len {
888 struct_span_err!(tcx.sess, sp, E0075, "SIMD vector cannot be empty").emit();
890 } else if len > MAX_SIMD_LANES {
895 "SIMD vector cannot have more than {MAX_SIMD_LANES} elements",
902 // Check that we use types valid for use in the lanes of a SIMD "vector register"
903 // These are scalar types which directly match a "machine" type
904 // Yes: Integers, floats, "thin" pointers
905 // No: char, "fat" pointers, compound types
907 ty::Param(_) => (), // pass struct<T>(T, T, T, T) through, let monomorphization catch errors
908 ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_) => (), // struct(u8, u8, u8, u8) is ok
909 ty::Array(t, _) if matches!(t.kind(), ty::Param(_)) => (), // pass struct<T>([T; N]) through, let monomorphization catch errors
913 ty::Int(_) | ty::Uint(_) | ty::Float(_) | ty::RawPtr(_)
915 { /* struct([f32; 4]) is ok */ }
921 "SIMD vector element type should be a \
922 primitive scalar (integer/float/pointer) type"
931 pub(super) fn check_packed(tcx: TyCtxt<'_>, sp: Span, def: ty::AdtDef<'_>) {
932 let repr = def.repr();
934 for attr in tcx.get_attrs(def.did(), sym::repr) {
935 for r in attr::parse_repr_attr(&tcx.sess, attr) {
936 if let attr::ReprPacked(pack) = r
937 && let Some(repr_pack) = repr.pack
938 && pack as u64 != repr_pack.bytes()
944 "type has conflicting packed representation hints"
950 if repr.align.is_some() {
955 "type has conflicting packed and align representation hints"
959 if let Some(def_spans) = check_packed_inner(tcx, def.did(), &mut vec![]) {
960 let mut err = struct_span_err!(
964 "packed type cannot transitively contain a `#[repr(align)]` type"
968 tcx.def_span(def_spans[0].0),
970 "`{}` has a `#[repr(align)]` attribute",
971 tcx.item_name(def_spans[0].0)
975 if def_spans.len() > 2 {
976 let mut first = true;
977 for (adt_def, span) in def_spans.iter().skip(1).rev() {
978 let ident = tcx.item_name(*adt_def);
983 "`{}` contains a field of type `{}`",
984 tcx.type_of(def.did()),
988 format!("...which contains a field of type `{ident}`")
1001 pub(super) fn check_packed_inner(
1004 stack: &mut Vec<DefId>,
1005 ) -> Option<Vec<(DefId, Span)>> {
1006 if let ty::Adt(def, substs) = tcx.type_of(def_id).kind() {
1007 if def.is_struct() || def.is_union() {
1008 if def.repr().align.is_some() {
1009 return Some(vec![(def.did(), DUMMY_SP)]);
1013 for field in &def.non_enum_variant().fields {
1014 if let ty::Adt(def, _) = field.ty(tcx, substs).kind()
1015 && !stack.contains(&def.did())
1016 && let Some(mut defs) = check_packed_inner(tcx, def.did(), stack)
1018 defs.push((def.did(), field.ident(tcx).span));
1029 pub(super) fn check_transparent<'tcx>(tcx: TyCtxt<'tcx>, sp: Span, adt: ty::AdtDef<'tcx>) {
1030 if !adt.repr().transparent() {
1034 if adt.is_union() && !tcx.features().transparent_unions {
1036 &tcx.sess.parse_sess,
1037 sym::transparent_unions,
1039 "transparent unions are unstable",
1044 if adt.variants().len() != 1 {
1045 bad_variant_count(tcx, adt, sp, adt.did());
1046 if adt.variants().is_empty() {
1047 // Don't bother checking the fields. No variants (and thus no fields) exist.
1052 // For each field, figure out if it's known to be a ZST and align(1), with "known"
1053 // respecting #[non_exhaustive] attributes.
1054 let field_infos = adt.all_fields().map(|field| {
1055 let ty = field.ty(tcx, InternalSubsts::identity_for_item(tcx, field.did));
1056 let param_env = tcx.param_env(field.did);
1057 let layout = tcx.layout_of(param_env.and(ty));
1058 // We are currently checking the type this field came from, so it must be local
1059 let span = tcx.hir().span_if_local(field.did).unwrap();
1060 let zst = layout.map_or(false, |layout| layout.is_zst());
1061 let align1 = layout.map_or(false, |layout| layout.align.abi.bytes() == 1);
1063 return (span, zst, align1, None);
1066 fn check_non_exhaustive<'tcx>(
1069 ) -> ControlFlow<(&'static str, DefId, SubstsRef<'tcx>, bool)> {
1071 ty::Tuple(list) => list.iter().try_for_each(|t| check_non_exhaustive(tcx, t)),
1072 ty::Array(ty, _) => check_non_exhaustive(tcx, *ty),
1073 ty::Adt(def, subst) => {
1074 if !def.did().is_local() {
1075 let non_exhaustive = def.is_variant_list_non_exhaustive()
1079 .any(ty::VariantDef::is_field_list_non_exhaustive);
1080 let has_priv = def.all_fields().any(|f| !f.vis.is_public());
1081 if non_exhaustive || has_priv {
1082 return ControlFlow::Break((
1091 .map(|field| field.ty(tcx, subst))
1092 .try_for_each(|t| check_non_exhaustive(tcx, t))
1094 _ => ControlFlow::Continue(()),
1098 (span, zst, align1, check_non_exhaustive(tcx, ty).break_value())
1101 let non_zst_fields = field_infos
1103 .filter_map(|(span, zst, _align1, _non_exhaustive)| if !zst { Some(span) } else { None });
1104 let non_zst_count = non_zst_fields.clone().count();
1105 if non_zst_count >= 2 {
1106 bad_non_zero_sized_fields(tcx, adt, non_zst_count, non_zst_fields, sp);
1108 let incompatible_zst_fields =
1109 field_infos.clone().filter(|(_, _, _, opt)| opt.is_some()).count();
1110 let incompat = incompatible_zst_fields + non_zst_count >= 2 && non_zst_count < 2;
1111 for (span, zst, align1, non_exhaustive) in field_infos {
1117 "zero-sized field in transparent {} has alignment larger than 1",
1120 .span_label(span, "has alignment larger than 1")
1123 if incompat && let Some((descr, def_id, substs, non_exhaustive)) = non_exhaustive {
1124 tcx.struct_span_lint_hir(
1125 REPR_TRANSPARENT_EXTERNAL_PRIVATE_FIELDS,
1126 tcx.hir().local_def_id_to_hir_id(adt.did().expect_local()),
1128 "zero-sized fields in `repr(transparent)` cannot contain external non-exhaustive types",
1130 let note = if non_exhaustive {
1131 "is marked with `#[non_exhaustive]`"
1133 "contains private fields"
1135 let field_ty = tcx.def_path_str_with_substs(def_id, substs);
1137 .note(format!("this {descr} contains `{field_ty}`, which {note}, \
1138 and makes it not a breaking change to become non-zero-sized in the future."))
1145 #[allow(trivial_numeric_casts)]
1146 fn check_enum<'tcx>(tcx: TyCtxt<'tcx>, vs: &'tcx [hir::Variant<'tcx>], def_id: LocalDefId) {
1147 let def = tcx.adt_def(def_id);
1148 let sp = tcx.def_span(def_id);
1149 def.destructor(tcx); // force the destructor to be evaluated
1152 if let Some(attr) = tcx.get_attrs(def_id.to_def_id(), sym::repr).next() {
1157 "unsupported representation for zero-variant enum"
1159 .span_label(sp, "zero-variant enum")
1164 let repr_type_ty = def.repr().discr_type().to_ty(tcx);
1165 if repr_type_ty == tcx.types.i128 || repr_type_ty == tcx.types.u128 {
1166 if !tcx.features().repr128 {
1168 &tcx.sess.parse_sess,
1171 "repr with 128-bit type is unstable",
1178 if let Some(ref e) = v.disr_expr {
1179 tcx.ensure().typeck(tcx.hir().local_def_id(e.hir_id));
1183 if tcx.adt_def(def_id).repr().int.is_none() && tcx.features().arbitrary_enum_discriminant {
1184 let is_unit = |var: &hir::Variant<'_>| matches!(var.data, hir::VariantData::Unit(..));
1186 let has_disr = |var: &hir::Variant<'_>| var.disr_expr.is_some();
1187 let has_non_units = vs.iter().any(|var| !is_unit(var));
1188 let disr_units = vs.iter().any(|var| is_unit(&var) && has_disr(&var));
1189 let disr_non_unit = vs.iter().any(|var| !is_unit(&var) && has_disr(&var));
1191 if disr_non_unit || (disr_units && has_non_units) {
1193 struct_span_err!(tcx.sess, sp, E0732, "`#[repr(inttype)]` must be specified");
1198 detect_discriminant_duplicate(tcx, def.discriminants(tcx).collect(), vs, sp);
1200 check_transparent(tcx, sp, def);
1203 /// Part of enum check. Given the discriminants of an enum, errors if two or more discriminants are equal
1204 fn detect_discriminant_duplicate<'tcx>(
1206 mut discrs: Vec<(VariantIdx, Discr<'tcx>)>,
1207 vs: &'tcx [hir::Variant<'tcx>],
1210 // Helper closure to reduce duplicate code. This gets called everytime we detect a duplicate.
1211 // Here `idx` refers to the order of which the discriminant appears, and its index in `vs`
1212 let report = |dis: Discr<'tcx>, idx: usize, err: &mut Diagnostic| {
1213 let var = &vs[idx]; // HIR for the duplicate discriminant
1214 let (span, display_discr) = match var.disr_expr {
1216 // In the case the discriminant is both a duplicate and overflowed, let the user know
1217 if let hir::ExprKind::Lit(lit) = &tcx.hir().body(expr.body).value.kind
1218 && let rustc_ast::LitKind::Int(lit_value, _int_kind) = &lit.node
1219 && *lit_value != dis.val
1221 (tcx.hir().span(expr.hir_id), format!("`{dis}` (overflowed from `{lit_value}`)"))
1222 // Otherwise, format the value as-is
1224 (tcx.hir().span(expr.hir_id), format!("`{dis}`"))
1228 // At this point we know this discriminant is a duplicate, and was not explicitly
1229 // assigned by the user. Here we iterate backwards to fetch the HIR for the last
1230 // explicitly assigned discriminant, and letting the user know that this was the
1231 // increment startpoint, and how many steps from there leading to the duplicate
1232 if let Some((n, hir::Variant { span, ident, .. })) =
1233 vs[..idx].iter().rev().enumerate().find(|v| v.1.disr_expr.is_some())
1235 let ve_ident = var.ident;
1237 let sp = if n > 1 { "variants" } else { "variant" };
1241 format!("discriminant for `{ve_ident}` incremented from this startpoint (`{ident}` + {n} {sp} later => `{ve_ident}` = {dis})"),
1245 (vs[idx].span, format!("`{dis}`"))
1249 err.span_label(span, format!("{display_discr} assigned here"));
1252 // Here we loop through the discriminants, comparing each discriminant to another.
1253 // When a duplicate is detected, we instantiate an error and point to both
1254 // initial and duplicate value. The duplicate discriminant is then discarded by swapping
1255 // it with the last element and decrementing the `vec.len` (which is why we have to evaluate
1256 // `discrs.len()` anew every iteration, and why this could be tricky to do in a functional
1257 // style as we are mutating `discrs` on the fly).
1259 while i < discrs.len() {
1260 let hir_var_i_idx = discrs[i].0.index();
1261 let mut error: Option<DiagnosticBuilder<'_, _>> = None;
1264 while o < discrs.len() {
1265 let hir_var_o_idx = discrs[o].0.index();
1267 if discrs[i].1.val == discrs[o].1.val {
1268 let err = error.get_or_insert_with(|| {
1269 let mut ret = struct_span_err!(
1273 "discriminant value `{}` assigned more than once",
1277 report(discrs[i].1, hir_var_i_idx, &mut ret);
1282 report(discrs[o].1, hir_var_o_idx, err);
1284 // Safe to unwrap here, as we wouldn't reach this point if `discrs` was empty
1285 discrs[o] = *discrs.last().unwrap();
1292 if let Some(mut e) = error {
1300 pub(super) fn check_type_params_are_used<'tcx>(
1302 generics: &ty::Generics,
1305 debug!("check_type_params_are_used(generics={:?}, ty={:?})", generics, ty);
1307 assert_eq!(generics.parent, None);
1309 if generics.own_counts().types == 0 {
1313 let mut params_used = BitSet::new_empty(generics.params.len());
1315 if ty.references_error() {
1316 // If there is already another error, do not emit
1317 // an error for not using a type parameter.
1318 assert!(tcx.sess.has_errors().is_some());
1322 for leaf in ty.walk() {
1323 if let GenericArgKind::Type(leaf_ty) = leaf.unpack()
1324 && let ty::Param(param) = leaf_ty.kind()
1326 debug!("found use of ty param {:?}", param);
1327 params_used.insert(param.index);
1331 for param in &generics.params {
1332 if !params_used.contains(param.index)
1333 && let ty::GenericParamDefKind::Type { .. } = param.kind
1335 let span = tcx.def_span(param.def_id);
1340 "type parameter `{}` is unused",
1343 .span_label(span, "unused type parameter")
1349 pub(super) fn check_mod_item_types(tcx: TyCtxt<'_>, module_def_id: LocalDefId) {
1350 let module = tcx.hir_module_items(module_def_id);
1351 for id in module.items() {
1352 check_item_type(tcx, id);
1356 fn async_opaque_type_cycle_error(tcx: TyCtxt<'_>, span: Span) -> ErrorGuaranteed {
1357 struct_span_err!(tcx.sess, span, E0733, "recursion in an `async fn` requires boxing")
1358 .span_label(span, "recursive `async fn`")
1359 .note("a recursive `async fn` must be rewritten to return a boxed `dyn Future`")
1361 "consider using the `async_recursion` crate: https://crates.io/crates/async_recursion",
1366 /// Emit an error for recursive opaque types.
1368 /// If this is a return `impl Trait`, find the item's return expressions and point at them. For
1369 /// direct recursion this is enough, but for indirect recursion also point at the last intermediary
1372 /// If all the return expressions evaluate to `!`, then we explain that the error will go away
1373 /// after changing it. This can happen when a user uses `panic!()` or similar as a placeholder.
1374 fn opaque_type_cycle_error(tcx: TyCtxt<'_>, def_id: LocalDefId, span: Span) -> ErrorGuaranteed {
1375 let mut err = struct_span_err!(tcx.sess, span, E0720, "cannot resolve opaque type");
1377 let mut label = false;
1378 if let Some((def_id, visitor)) = get_owner_return_paths(tcx, def_id) {
1379 let typeck_results = tcx.typeck(def_id);
1383 .filter_map(|expr| typeck_results.node_type_opt(expr.hir_id))
1384 .all(|ty| matches!(ty.kind(), ty::Never))
1389 .filter(|expr| typeck_results.node_type_opt(expr.hir_id).is_some())
1390 .map(|expr| expr.span)
1391 .collect::<Vec<Span>>();
1392 let span_len = spans.len();
1394 err.span_label(spans[0], "this returned value is of `!` type");
1396 let mut multispan: MultiSpan = spans.clone().into();
1398 multispan.push_span_label(span, "this returned value is of `!` type");
1400 err.span_note(multispan, "these returned values have a concrete \"never\" type");
1402 err.help("this error will resolve once the item's body returns a concrete type");
1404 let mut seen = FxHashSet::default();
1406 err.span_label(span, "recursive opaque type");
1408 for (sp, ty) in visitor
1411 .filter_map(|e| typeck_results.node_type_opt(e.hir_id).map(|t| (e.span, t)))
1412 .filter(|(_, ty)| !matches!(ty.kind(), ty::Never))
1414 struct OpaqueTypeCollector(Vec<DefId>);
1415 impl<'tcx> ty::visit::TypeVisitor<'tcx> for OpaqueTypeCollector {
1416 fn visit_ty(&mut self, t: Ty<'tcx>) -> ControlFlow<Self::BreakTy> {
1418 ty::Opaque(def, _) => {
1420 ControlFlow::CONTINUE
1422 _ => t.super_visit_with(self),
1426 let mut visitor = OpaqueTypeCollector(vec![]);
1427 ty.visit_with(&mut visitor);
1428 for def_id in visitor.0 {
1429 let ty_span = tcx.def_span(def_id);
1430 if !seen.contains(&ty_span) {
1431 err.span_label(ty_span, &format!("returning this opaque type `{ty}`"));
1432 seen.insert(ty_span);
1434 err.span_label(sp, &format!("returning here with type `{ty}`"));
1440 err.span_label(span, "cannot resolve opaque type");