rustc_hir_analysis/
lib.rs1#![allow(internal_features)]
60#![allow(rustc::diagnostic_outside_of_impl)]
61#![allow(rustc::untranslatable_diagnostic)]
62#![cfg_attr(bootstrap, feature(let_chains))]
63#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
64#![doc(rust_logo)]
65#![feature(assert_matches)]
66#![feature(coroutines)]
67#![feature(debug_closure_helpers)]
68#![feature(if_let_guard)]
69#![feature(iter_from_coroutine)]
70#![feature(iter_intersperse)]
71#![feature(never_type)]
72#![feature(rustdoc_internals)]
73#![feature(slice_partition_dedup)]
74#![feature(try_blocks)]
75#![feature(unwrap_infallible)]
76pub mod check;
80
81pub mod autoderef;
82mod check_unused;
83mod coherence;
84mod collect;
85mod constrained_generic_params;
86mod delegation;
87mod errors;
88pub mod hir_ty_lowering;
89pub mod hir_wf_check;
90mod impl_wf_check;
91mod outlives;
92mod variance;
93
94pub use errors::NoVariantNamed;
95use rustc_abi::ExternAbi;
96use rustc_hir as hir;
97use rustc_hir::def::DefKind;
98use rustc_middle::middle;
99use rustc_middle::mir::interpret::GlobalId;
100use rustc_middle::query::Providers;
101use rustc_middle::ty::{self, Const, Ty, TyCtxt};
102use rustc_session::parse::feature_err;
103use rustc_span::symbol::sym;
104use rustc_span::{ErrorGuaranteed, Span};
105use rustc_trait_selection::traits;
106
107pub use crate::collect::suggest_impl_trait;
108use crate::hir_ty_lowering::{FeedConstTy, HirTyLowerer};
109
110rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
111
112fn require_c_abi_if_c_variadic(
113 tcx: TyCtxt<'_>,
114 decl: &hir::FnDecl<'_>,
115 abi: ExternAbi,
116 span: Span,
117) {
118 const CONVENTIONS_UNSTABLE: &str =
119 "`C`, `cdecl`, `system`, `aapcs`, `win64`, `sysv64` or `efiapi`";
120 const CONVENTIONS_STABLE: &str = "`C` or `cdecl`";
121 const UNSTABLE_EXPLAIN: &str =
122 "using calling conventions other than `C` or `cdecl` for varargs functions is unstable";
123
124 if !decl.c_variadic || matches!(abi, ExternAbi::C { .. } | ExternAbi::Cdecl { .. }) {
126 return;
127 }
128
129 let extended_abi_support = tcx.features().extended_varargs_abi_support();
131 let extern_system_varargs = tcx.features().extern_system_varargs();
132
133 if extern_system_varargs && let ExternAbi::System { .. } = abi {
135 return;
136 };
137 if extended_abi_support && abi.supports_varargs() {
138 return;
139 };
140
141 match abi {
144 ExternAbi::System { .. } => {
145 feature_err(&tcx.sess, sym::extern_system_varargs, span, UNSTABLE_EXPLAIN)
146 }
147 abi if abi.supports_varargs() => {
148 feature_err(&tcx.sess, sym::extended_varargs_abi_support, span, UNSTABLE_EXPLAIN)
149 }
150 _ => tcx.dcx().create_err(errors::VariadicFunctionCompatibleConvention {
151 span,
152 conventions: if tcx.sess.opts.unstable_features.is_nightly_build() {
153 CONVENTIONS_UNSTABLE
154 } else {
155 CONVENTIONS_STABLE
156 },
157 }),
158 }
159 .emit();
160}
161
162pub fn provide(providers: &mut Providers) {
163 collect::provide(providers);
164 coherence::provide(providers);
165 check::provide(providers);
166 check_unused::provide(providers);
167 variance::provide(providers);
168 outlives::provide(providers);
169 hir_wf_check::provide(providers);
170 *providers = Providers {
171 inherit_sig_for_delegation_item: delegation::inherit_sig_for_delegation_item,
172 enforce_impl_non_lifetime_params_are_constrained:
173 impl_wf_check::enforce_impl_non_lifetime_params_are_constrained,
174 ..*providers
175 };
176}
177
178pub fn check_crate(tcx: TyCtxt<'_>) {
179 let _prof_timer = tcx.sess.timer("type_check_crate");
180
181 tcx.sess.time("coherence_checking", || {
182 type R = Result<(), ErrorGuaranteed>;
185
186 tcx.par_hir_for_each_module(|module| {
187 let _: R = tcx.ensure_ok().check_mod_type_wf(module);
188 });
189
190 for &trait_def_id in tcx.all_local_trait_impls(()).keys() {
191 let _: R = tcx.ensure_ok().coherent_trait(trait_def_id);
192 }
193 let _: R = tcx.ensure_ok().crate_inherent_impls_validity_check(());
195 let _: R = tcx.ensure_ok().crate_inherent_impls_overlap_check(());
196 });
197
198 if tcx.features().rustc_attrs() {
199 tcx.sess.time("dumping_rustc_attr_data", || {
200 outlives::dump::inferred_outlives(tcx);
201 variance::dump::variances(tcx);
202 collect::dump::opaque_hidden_types(tcx);
203 collect::dump::predicates_and_item_bounds(tcx);
204 collect::dump::def_parents(tcx);
205 collect::dump::vtables(tcx);
206 });
207 }
208
209 tcx.par_hir_body_owners(|item_def_id| {
212 let def_kind = tcx.def_kind(item_def_id);
213 match def_kind {
214 DefKind::Static { .. } => {
215 tcx.ensure_ok().eval_static_initializer(item_def_id);
216 check::maybe_check_static_with_link_section(tcx, item_def_id);
217 }
218 DefKind::Const if tcx.generics_of(item_def_id).is_empty() => {
219 let instance = ty::Instance::new_raw(item_def_id.into(), ty::GenericArgs::empty());
220 let cid = GlobalId { instance, promoted: None };
221 let typing_env = ty::TypingEnv::fully_monomorphized();
222 tcx.ensure_ok().eval_to_const_value_raw(typing_env.as_query_input(cid));
223 }
224 _ => (),
225 }
226 });
227
228 tcx.par_hir_body_owners(|item_def_id| {
229 let def_kind = tcx.def_kind(item_def_id);
230 if !matches!(def_kind, DefKind::AnonConst) {
232 tcx.ensure_ok().typeck(item_def_id);
233 }
234 });
235
236 tcx.ensure_ok().check_unused_traits(());
237}
238
239pub fn lower_ty<'tcx>(tcx: TyCtxt<'tcx>, hir_ty: &hir::Ty<'tcx>) -> Ty<'tcx> {
250 let env_def_id = tcx.hir_get_parent_item(hir_ty.hir_id);
254 collect::ItemCtxt::new(tcx, env_def_id.def_id)
255 .lowerer()
256 .lower_ty_maybe_return_type_notation(hir_ty)
257}
258
259pub fn lower_const_arg_for_rustdoc<'tcx>(
262 tcx: TyCtxt<'tcx>,
263 hir_ct: &hir::ConstArg<'tcx>,
264 feed: FeedConstTy<'_, 'tcx>,
265) -> Const<'tcx> {
266 let env_def_id = tcx.hir_get_parent_item(hir_ct.hir_id);
267 collect::ItemCtxt::new(tcx, env_def_id.def_id).lowerer().lower_const_arg(hir_ct, feed)
268}