rustc_hir_analysis/
lib.rs1#![cfg_attr(bootstrap, feature(unwrap_infallible))]
60#![feature(default_field_values)]
61#![feature(gen_blocks)]
62#![feature(iter_intersperse)]
63#![feature(slice_partition_dedup)]
64#![feature(try_blocks)]
65pub mod check;
69
70pub mod autoderef;
71mod check_unused;
72mod coherence;
73mod collect;
74mod constrained_generic_params;
75pub mod delegation;
76pub mod diagnostics;
77pub mod hir_ty_lowering;
78pub mod hir_wf_check;
79mod impl_wf_check;
80mod outlives;
81mod variance;
82
83use rustc_abi::{CVariadicStatus, ExternAbi};
84use rustc_hir as hir;
85use rustc_hir::def::DefKind;
86use rustc_middle::mir::interpret::GlobalId;
87use rustc_middle::query::Providers;
88use rustc_middle::ty::{Const, Ty, TyCtxt};
89use rustc_middle::{middle, ty};
90use rustc_session::diagnostics::feature_err;
91use rustc_span::{ErrorGuaranteed, Span};
92use rustc_trait_selection::traits;
93
94pub use crate::collect::suggest_impl_trait;
95use crate::hir_ty_lowering::HirTyLowerer;
96
97fn check_c_variadic_abi(tcx: TyCtxt<'_>, decl: &hir::FnDecl<'_>, abi: ExternAbi, span: Span) {
98 if !decl.c_variadic() {
99 return;
101 }
102
103 match abi.supports_c_variadic() {
104 CVariadicStatus::Stable => {}
105 CVariadicStatus::NotSupported => {
106 tcx.dcx().emit_err(diagnostics::VariadicFunctionCompatibleConvention {
107 span,
108 convention: &::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}", abi))
})format!("{abi}"),
109 });
110 }
111 CVariadicStatus::Unstable { feature } => {
112 if !tcx.features().enabled(feature) {
113 feature_err(
114 &tcx.sess,
115 feature,
116 span,
117 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("C-variadic functions with the {0} calling convention are unstable",
abi))
})format!("C-variadic functions with the {abi} calling convention are unstable"),
118 )
119 .emit();
120 }
121 }
122 }
123}
124
125pub fn provide(providers: &mut Providers) {
127 collect::provide(providers);
128 coherence::provide(providers);
129 check::provide(providers);
130 *providers = Providers {
131 check_unused_traits: check_unused::check_unused_traits,
132 diagnostic_hir_wf_check: hir_wf_check::diagnostic_hir_wf_check,
133 inferred_outlives_crate: outlives::inferred_outlives_crate,
134 inferred_outlives_of: outlives::inferred_outlives_of,
135 inherit_sig_for_delegation_item: delegation::inherit_sig_for_delegation_item,
136 delegation_user_specified_args: delegation::delegation_user_specified_args,
137 enforce_impl_non_lifetime_params_are_constrained:
138 impl_wf_check::enforce_impl_non_lifetime_params_are_constrained,
139 crate_variances: variance::crate_variances,
140 variances_of: variance::variances_of,
141 ..*providers
142 };
143}
144
145pub fn check_crate(tcx: TyCtxt<'_>) {
146 let _prof_timer = tcx.sess.timer("type_check_crate");
147
148 tcx.sess.time("coherence_checking", || {
149 type R = Result<(), ErrorGuaranteed>;
152
153 let _: R = tcx.ensure_result().check_type_wf(());
154
155 for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
156 let _: R = tcx.ensure_result().coherent_trait(trait_def_id);
157 }
158 let _: R = tcx.ensure_result().crate_inherent_impls_validity_check(());
160 let _: R = tcx.ensure_result().crate_inherent_impls_overlap_check(());
161 });
162
163 tcx.par_hir_body_owners(|item_def_id| {
164 let def_kind = tcx.def_kind(item_def_id);
165 match def_kind {
168 DefKind::Static { .. } => {
169 tcx.ensure_ok().eval_static_initializer(item_def_id);
170 check::maybe_check_static_with_link_section(tcx, item_def_id);
171 }
172 DefKind::Const
173 if !tcx.generics_of(item_def_id).own_requires_monomorphization()
174 && tcx.const_of_item(item_def_id).is_none() =>
175 {
176 let instance = ty::Instance::new_raw(item_def_id.into(), ty::GenericArgs::empty());
179 let cid = GlobalId { instance, promoted: None };
180 let typing_env = ty::TypingEnv::fully_monomorphized();
181 tcx.ensure_ok().eval_to_const_value_raw(typing_env.as_query_input(cid));
182 }
183 _ => (),
184 }
185 if !(def_kind == DefKind::AnonConst
189 && tcx.anon_const_kind(item_def_id) != ty::AnonConstKind::NonTypeSystemInline
190 || tcx.is_typeck_child(item_def_id.to_def_id()))
191 {
192 tcx.ensure_ok().typeck(item_def_id);
193 }
194 });
195
196 tcx.par_hir_body_owners(|item_def_id| {
204 if tcx.needs_coroutine_by_move_body_def_id(item_def_id.to_def_id()) {
207 tcx.ensure_done().coroutine_by_move_body_def_id(item_def_id);
208 }
209 });
210
211 if tcx.features().rustc_attrs() {
212 tcx.sess.time("dumping_rustc_attr_data", || {
213 collect::dump::clauses_and_item_bounds(tcx);
215 collect::dump::def_parents(tcx);
216 collect::dump::generics(tcx);
217 collect::dump::object_lifetime_defaults(tcx);
218 collect::dump::opaque_hidden_types(tcx);
219 collect::dump::vtables(tcx);
220 outlives::dump::inferred_outlives(tcx);
221 variance::dump::variances(tcx);
222 });
224 }
225
226 tcx.ensure_ok().check_unused_traits(());
227}
228
229pub fn lower_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'_>) -> Ty<'tcx> {
240 let env_def_id = tcx.hir_get_parent_item(hir_ty.hir_id);
244 collect::ItemCtxt::new(tcx, env_def_id.def_id)
245 .lowerer()
246 .lower_ty_maybe_return_type_notation(hir_ty)
247}
248
249pub fn lower_const_arg_for_rustdoc<'tcx>(
252 tcx: TyCtxt<'tcx>,
253 hir_ct: &hir::ConstArg<'_>,
254 ty: Ty<'tcx>,
255) -> Const<'tcx> {
256 let env_def_id = tcx.hir_get_parent_item(hir_ct.hir_id);
257 collect::ItemCtxt::new(tcx, env_def_id.def_id).lowerer().lower_const_arg(hir_ct, ty)
258}