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