rustc_hir_analysis/
lib.rs1#![allow(internal_features)]
60#![allow(rustc::diagnostic_outside_of_impl)]
61#![allow(rustc::untranslatable_diagnostic)]
62#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
63#![doc(rust_logo)]
64#![feature(assert_matches)]
65#![feature(debug_closure_helpers)]
66#![feature(gen_blocks)]
67#![feature(if_let_guard)]
68#![feature(iter_intersperse)]
69#![feature(never_type)]
70#![feature(rustdoc_internals)]
71#![feature(slice_partition_dedup)]
72#![feature(try_blocks)]
73#![feature(unwrap_infallible)]
74pub mod check;
78
79pub mod autoderef;
80mod check_unused;
81mod coherence;
82mod collect;
83mod constrained_generic_params;
84mod delegation;
85mod errors;
86pub mod hir_ty_lowering;
87pub mod hir_wf_check;
88mod impl_wf_check;
89mod outlives;
90mod variance;
91
92pub use errors::NoVariantNamed;
93use rustc_abi::ExternAbi;
94use rustc_hir::def::DefKind;
95use rustc_hir::lints::DelayedLint;
96use rustc_hir::{self as hir};
97use rustc_middle::middle;
98use rustc_middle::mir::interpret::GlobalId;
99use rustc_middle::query::Providers;
100use rustc_middle::ty::{self, Const, Ty, TyCtxt};
101use rustc_session::parse::feature_err;
102use rustc_span::symbol::sym;
103use rustc_span::{ErrorGuaranteed, Span};
104use rustc_trait_selection::traits;
105
106pub use crate::collect::suggest_impl_trait;
107use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer};
108
109rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
110
111fn require_c_abi_if_c_variadic(
112 tcx: TyCtxt<'_>,
113 decl: &hir::FnDecl<'_>,
114 abi: ExternAbi,
115 span: Span,
116) {
117 if !decl.c_variadic || matches!(abi, ExternAbi::C { .. } | ExternAbi::Cdecl { .. }) {
119 return;
120 }
121
122 let extended_abi_support = tcx.features().extended_varargs_abi_support();
124 let extern_system_varargs = tcx.features().extern_system_varargs();
125
126 if extern_system_varargs && let ExternAbi::System { .. } = abi {
128 return;
129 };
130 if extended_abi_support && abi.supports_varargs() {
131 return;
132 };
133
134 let unstable_explain =
137 format!("C-variadic functions with the {abi} calling convention are unstable");
138 match abi {
139 ExternAbi::System { .. } => {
140 feature_err(&tcx.sess, sym::extern_system_varargs, span, unstable_explain)
141 }
142 abi if abi.supports_varargs() => {
143 feature_err(&tcx.sess, sym::extended_varargs_abi_support, span, unstable_explain)
144 }
145 _ => tcx.dcx().create_err(errors::VariadicFunctionCompatibleConvention {
146 span,
147 convention: &format!("{abi}"),
148 }),
149 }
150 .emit();
151}
152
153pub fn provide(providers: &mut Providers) {
154 collect::provide(providers);
155 coherence::provide(providers);
156 check::provide(providers);
157 check_unused::provide(providers);
158 variance::provide(providers);
159 outlives::provide(providers);
160 hir_wf_check::provide(providers);
161 *providers = Providers {
162 inherit_sig_for_delegation_item: delegation::inherit_sig_for_delegation_item,
163 enforce_impl_non_lifetime_params_are_constrained:
164 impl_wf_check::enforce_impl_non_lifetime_params_are_constrained,
165 ..*providers
166 };
167}
168
169fn emit_delayed_lint(lint: &DelayedLint, tcx: TyCtxt<'_>) {
170 match lint {
171 DelayedLint::AttributeParsing(attribute_lint) => {
172 rustc_attr_parsing::emit_attribute_lint(attribute_lint, tcx)
173 }
174 }
175}
176
177pub fn check_crate(tcx: TyCtxt<'_>) {
178 let _prof_timer = tcx.sess.timer("type_check_crate");
179
180 tcx.sess.time("coherence_checking", || {
181 type R = Result<(), ErrorGuaranteed>;
184
185 let _: R = tcx.ensure_ok().check_type_wf(());
186
187 for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
188 let _: R = tcx.ensure_ok().coherent_trait(trait_def_id);
189 }
190 let _: R = tcx.ensure_ok().crate_inherent_impls_validity_check(());
192 let _: R = tcx.ensure_ok().crate_inherent_impls_overlap_check(());
193 });
194
195 tcx.sess.time("emit_ast_lowering_delayed_lints", || {
196 #[cfg(debug_assertions)]
207 {
208 for owner_id in tcx.hir_crate_items(()).owners() {
210 if let Some(delayed_lints) = tcx.opt_ast_lowering_delayed_lints(owner_id) {
212 if !delayed_lints.lints.is_empty() {
213 assert!(
215 tcx.hir_crate_items(()).delayed_lint_items().any(|i| i == owner_id)
216 );
217 }
218 }
219 }
220 }
221
222 for owner_id in tcx.hir_crate_items(()).delayed_lint_items() {
223 if let Some(delayed_lints) = tcx.opt_ast_lowering_delayed_lints(owner_id) {
224 for lint in &delayed_lints.lints {
225 emit_delayed_lint(lint, tcx);
226 }
227 }
228 }
229 });
230
231 tcx.par_hir_body_owners(|item_def_id| {
232 let def_kind = tcx.def_kind(item_def_id);
233 match def_kind {
236 DefKind::Static { .. } => {
237 tcx.ensure_ok().eval_static_initializer(item_def_id);
238 check::maybe_check_static_with_link_section(tcx, item_def_id);
239 }
240 DefKind::Const if !tcx.generics_of(item_def_id).own_requires_monomorphization() => {
241 let instance = ty::Instance::new_raw(item_def_id.into(), ty::GenericArgs::empty());
244 let cid = GlobalId { instance, promoted: None };
245 let typing_env = ty::TypingEnv::fully_monomorphized();
246 tcx.ensure_ok().eval_to_const_value_raw(typing_env.as_query_input(cid));
247 }
248 _ => (),
249 }
250 if !matches!(def_kind, DefKind::AnonConst) {
252 tcx.ensure_ok().typeck(item_def_id);
253 }
254 if tcx.needs_coroutine_by_move_body_def_id(item_def_id.to_def_id()) {
257 tcx.ensure_done().coroutine_by_move_body_def_id(item_def_id);
258 }
259 });
260
261 if tcx.features().rustc_attrs() {
262 tcx.sess.time("dumping_rustc_attr_data", || {
263 outlives::dump::inferred_outlives(tcx);
264 variance::dump::variances(tcx);
265 collect::dump::opaque_hidden_types(tcx);
266 collect::dump::predicates_and_item_bounds(tcx);
267 collect::dump::def_parents(tcx);
268 collect::dump::vtables(tcx);
269 });
270 }
271
272 tcx.ensure_ok().check_unused_traits(());
273}
274
275pub fn lower_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
286 let env_def_id = tcx.hir_get_parent_item(hir_ty.hir_id);
290 collect::ItemCtxt::new(tcx, env_def_id.def_id)
291 .lowerer()
292 .lower_ty_maybe_return_type_notation(hir_ty)
293}
294
295pub fn lower_const_arg_for_rustdoc<'tcx>(
298 tcx: TyCtxt<'tcx>,
299 hir_ct: &hir::ConstArg<'tcx>,
300 feed: FeedConstTy<'_, 'tcx>,
301) -> Const<'tcx> {
302 let env_def_id = tcx.hir_get_parent_item(hir_ct.hir_id);
303 collect::ItemCtxt::new(tcx, env_def_id.def_id).lowerer().lower_const_arg(hir_ct, feed)
304}