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