1 #![feature(if_let_guard)]
2 #![feature(let_chains)]
3 #![feature(try_blocks)]
4 #![feature(never_type)]
5 #![feature(min_specialization)]
6 #![feature(control_flow_enum)]
7 #![feature(drain_filter)]
8 #![allow(rustc::potential_query_instability)]
9 #![recursion_limit = "256"]
15 extern crate rustc_middle;
31 pub mod expr_use_visitor;
35 mod generator_interior;
38 mod mem_categorization;
47 pub use diverges::Diverges;
48 pub use expectation::Expectation;
50 pub use inherited::{Inherited, InheritedBuilder};
52 use crate::check::check_fn;
53 use crate::coercion::DynamicCoerceMany;
54 use crate::gather_locals::GatherLocalsVisitor;
55 use rustc_data_structures::unord::UnordSet;
56 use rustc_errors::{struct_span_err, DiagnosticId, ErrorGuaranteed, MultiSpan};
58 use rustc_hir::def::{DefKind, Res};
59 use rustc_hir::intravisit::Visitor;
60 use rustc_hir::{HirIdMap, Node};
61 use rustc_hir_analysis::astconv::AstConv;
62 use rustc_hir_analysis::check::check_abi;
63 use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
64 use rustc_middle::traits;
65 use rustc_middle::ty::query::Providers;
66 use rustc_middle::ty::{self, Ty, TyCtxt};
67 use rustc_session::config;
68 use rustc_session::Session;
69 use rustc_span::def_id::{DefId, LocalDefId};
73 macro_rules! type_error_struct {
74 ($session:expr, $span:expr, $typ:expr, $code:ident, $($message:tt)*) => ({
75 let mut err = rustc_errors::struct_span_err!($session, $span, $code, $($message)*);
77 if $typ.references_error() {
78 err.downgrade_to_delayed_bug();
85 /// The type of a local binding, including the revealed type for anon types.
86 #[derive(Copy, Clone, Debug)]
87 pub struct LocalTy<'tcx> {
89 revealed_ty: Ty<'tcx>,
92 /// If this `DefId` is a "primary tables entry", returns
93 /// `Some((body_id, body_ty, fn_sig))`. Otherwise, returns `None`.
95 /// If this function returns `Some`, then `typeck_results(def_id)` will
96 /// succeed; if it returns `None`, then `typeck_results(def_id)` may or
97 /// may not succeed. In some cases where this function returns `None`
98 /// (notably closures), `typeck_results(def_id)` would wind up
99 /// redirecting to the owning function.
103 ) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnSig<'_>>)> {
104 match tcx.hir().get(id) {
105 Node::Item(item) => match item.kind {
106 hir::ItemKind::Const(ty, body) | hir::ItemKind::Static(ty, _, body) => {
107 Some((body, Some(ty), None))
109 hir::ItemKind::Fn(ref sig, .., body) => Some((body, None, Some(sig))),
112 Node::TraitItem(item) => match item.kind {
113 hir::TraitItemKind::Const(ty, Some(body)) => Some((body, Some(ty), None)),
114 hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Provided(body)) => {
115 Some((body, None, Some(sig)))
119 Node::ImplItem(item) => match item.kind {
120 hir::ImplItemKind::Const(ty, body) => Some((body, Some(ty), None)),
121 hir::ImplItemKind::Fn(ref sig, body) => Some((body, None, Some(sig))),
124 Node::AnonConst(constant) => Some((constant.body, None, None)),
129 fn has_typeck_results(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
130 // Closures' typeck results come from their outermost function,
131 // as they are part of the same "inference environment".
132 let typeck_root_def_id = tcx.typeck_root_def_id(def_id);
133 if typeck_root_def_id != def_id {
134 return tcx.has_typeck_results(typeck_root_def_id);
137 if let Some(def_id) = def_id.as_local() {
138 let id = tcx.hir().local_def_id_to_hir_id(def_id);
139 primary_body_of(tcx, id).is_some()
145 fn used_trait_imports(tcx: TyCtxt<'_>, def_id: LocalDefId) -> &UnordSet<LocalDefId> {
146 &*tcx.typeck(def_id).used_trait_imports
149 fn typeck_item_bodies(tcx: TyCtxt<'_>, (): ()) {
150 tcx.hir().par_body_owners(|body_owner_def_id| tcx.ensure().typeck(body_owner_def_id));
153 fn typeck_const_arg<'tcx>(
155 (did, param_did): (LocalDefId, DefId),
156 ) -> &ty::TypeckResults<'tcx> {
157 let fallback = move || tcx.type_of(param_did);
158 typeck_with_fallback(tcx, did, fallback)
161 fn typeck<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &ty::TypeckResults<'tcx> {
162 if let Some(param_did) = tcx.opt_const_param_of(def_id) {
163 tcx.typeck_const_arg((def_id, param_did))
165 let fallback = move || tcx.type_of(def_id.to_def_id());
166 typeck_with_fallback(tcx, def_id, fallback)
170 /// Used only to get `TypeckResults` for type inference during error recovery.
171 /// Currently only used for type inference of `static`s and `const`s to avoid type cycle errors.
172 fn diagnostic_only_typeck<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> &ty::TypeckResults<'tcx> {
173 let fallback = move || {
174 let span = tcx.hir().span(tcx.hir().local_def_id_to_hir_id(def_id));
175 tcx.ty_error_with_message(span, "diagnostic only typeck table used")
177 typeck_with_fallback(tcx, def_id, fallback)
180 #[instrument(level = "debug", skip(tcx, fallback), ret)]
181 fn typeck_with_fallback<'tcx>(
184 fallback: impl Fn() -> Ty<'tcx> + 'tcx,
185 ) -> &'tcx ty::TypeckResults<'tcx> {
186 // Closures' typeck results come from their outermost function,
187 // as they are part of the same "inference environment".
188 let typeck_root_def_id = tcx.typeck_root_def_id(def_id.to_def_id()).expect_local();
189 if typeck_root_def_id != def_id {
190 return tcx.typeck(typeck_root_def_id);
193 let id = tcx.hir().local_def_id_to_hir_id(def_id);
194 let span = tcx.hir().span(id);
196 // Figure out what primary body this item has.
197 let (body_id, body_ty, fn_sig) = primary_body_of(tcx, id).unwrap_or_else(|| {
198 span_bug!(span, "can't type-check body of {:?}", def_id);
200 let body = tcx.hir().body(body_id);
202 let typeck_results = Inherited::build(tcx, def_id).enter(|inh| {
203 let param_env = tcx.param_env(def_id);
204 let mut fcx = FnCtxt::new(&inh, param_env, body.value.hir_id);
206 if let Some(hir::FnSig { header, decl, .. }) = fn_sig {
207 let fn_sig = if rustc_hir_analysis::collect::get_infer_ret_ty(&decl.output).is_some() {
208 <dyn AstConv<'_>>::ty_of_fn(&fcx, id, header.unsafety, header.abi, decl, None, None)
213 check_abi(tcx, id, span, fn_sig.abi());
215 // Compute the function signature from point of view of inside the fn.
216 let fn_sig = tcx.liberate_late_bound_regions(def_id.to_def_id(), fn_sig);
217 let fn_sig = fcx.normalize(body.value.span, fn_sig);
219 check_fn(&mut fcx, fn_sig, decl, def_id, body, None);
221 let expected_type = body_ty
222 .and_then(|ty| match ty.kind {
223 hir::TyKind::Infer => Some(<dyn AstConv<'_>>::ast_ty_to_ty(&fcx, ty)),
226 .unwrap_or_else(|| match tcx.hir().get(id) {
227 Node::AnonConst(_) => match tcx.hir().get(tcx.hir().get_parent_node(id)) {
228 Node::Expr(&hir::Expr {
229 kind: hir::ExprKind::ConstBlock(ref anon_const),
231 }) if anon_const.hir_id == id => fcx.next_ty_var(TypeVariableOrigin {
232 kind: TypeVariableOriginKind::TypeInference,
236 kind: hir::TyKind::Typeof(ref anon_const), ..
237 }) if anon_const.hir_id == id => fcx.next_ty_var(TypeVariableOrigin {
238 kind: TypeVariableOriginKind::TypeInference,
241 Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), .. })
242 | Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), .. }) => {
246 .filter_map(|(op, _op_sp)| match op {
247 hir::InlineAsmOperand::Const { anon_const }
248 if anon_const.hir_id == id =>
250 // Inline assembly constants must be integers.
251 Some(fcx.next_int_var())
253 hir::InlineAsmOperand::SymFn { anon_const }
254 if anon_const.hir_id == id =>
256 Some(fcx.next_ty_var(TypeVariableOrigin {
257 kind: TypeVariableOriginKind::MiscVariable,
264 operand_ty.unwrap_or_else(fallback)
271 let expected_type = fcx.normalize(body.value.span, expected_type);
272 fcx.require_type_is_sized(expected_type, body.value.span, traits::ConstSized);
274 // Gather locals in statics (because of block expressions).
275 GatherLocalsVisitor::new(&fcx).visit_body(body);
277 fcx.check_expr_coercable_to_type(&body.value, expected_type, None);
279 fcx.write_ty(id, expected_type);
282 fcx.type_inference_fallback();
284 // Even though coercion casts provide type hints, we check casts after fallback for
285 // backwards compatibility. This makes fallback a stronger type hint than a cast coercion.
287 fcx.select_obligations_where_possible(|_| {});
289 // Closure and generator analysis may run after fallback
290 // because they don't constrain other type variables.
291 // Closure analysis only runs on closures. Therefore they only need to fulfill non-const predicates (as of now)
292 let prev_constness = fcx.param_env.constness();
293 fcx.param_env = fcx.param_env.without_const();
294 fcx.closure_analyze(body);
295 fcx.param_env = fcx.param_env.with_constness(prev_constness);
296 assert!(fcx.deferred_call_resolutions.borrow().is_empty());
297 // Before the generator analysis, temporary scopes shall be marked to provide more
298 // precise information on types to be captured.
299 fcx.resolve_rvalue_scopes(def_id.to_def_id());
300 fcx.resolve_generator_interiors(def_id.to_def_id());
302 for (ty, span, code) in fcx.deferred_sized_obligations.borrow_mut().drain(..) {
303 let ty = fcx.normalize_ty(span, ty);
304 fcx.require_type_is_sized(ty, span, code);
307 fcx.select_all_obligations_or_error();
309 if let None = fcx.infcx.tainted_by_errors() {
310 fcx.check_transmutes();
315 fcx.infcx.skip_region_resolution();
317 fcx.resolve_type_vars_in_body(body)
320 // Consistency check our TypeckResults instance can hold all ItemLocalIds
321 // it will need to hold.
322 assert_eq!(typeck_results.hir_owner, id.owner);
327 /// When `check_fn` is invoked on a generator (i.e., a body that
328 /// includes yield), it returns back some information about the yield
330 struct GeneratorTypes<'tcx> {
331 /// Type of generator argument / values returned by `yield`.
334 /// Type of value that is yielded.
337 /// Types that are captured (see `GeneratorInterior` for more).
340 /// Indicates if the generator is movable or static (immovable).
341 movability: hir::Movability,
344 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
351 fn maybe_mut_place(m: hir::Mutability) -> Self {
353 hir::Mutability::Mut => Needs::MutPlace,
354 hir::Mutability::Not => Needs::None,
359 #[derive(Debug, Copy, Clone)]
365 pub struct BreakableCtxt<'tcx> {
368 // this is `null` for loops where break with a value is illegal,
369 // such as `while`, `for`, and `while let`
370 coerce: Option<DynamicCoerceMany<'tcx>>,
373 pub struct EnclosingBreakables<'tcx> {
374 stack: Vec<BreakableCtxt<'tcx>>,
375 by_id: HirIdMap<usize>,
378 impl<'tcx> EnclosingBreakables<'tcx> {
379 fn find_breakable(&mut self, target_id: hir::HirId) -> &mut BreakableCtxt<'tcx> {
380 self.opt_find_breakable(target_id).unwrap_or_else(|| {
381 bug!("could not find enclosing breakable with id {}", target_id);
385 fn opt_find_breakable(&mut self, target_id: hir::HirId) -> Option<&mut BreakableCtxt<'tcx>> {
386 match self.by_id.get(&target_id) {
387 Some(ix) => Some(&mut self.stack[*ix]),
393 fn report_unexpected_variant_res(
396 qpath: &hir::QPath<'_>,
400 ) -> ErrorGuaranteed {
401 let res_descr = match res {
402 Res::Def(DefKind::Variant, _) => "struct variant",
405 let path_str = rustc_hir_pretty::qpath_to_string(qpath);
406 let mut err = tcx.sess.struct_span_err_with_code(
408 format!("expected {expected}, found {res_descr} `{path_str}`"),
409 DiagnosticId::Error(err_code.into()),
412 Res::Def(DefKind::Fn | DefKind::AssocFn, _) if err_code == "E0164" => {
413 let patterns_url = "https://doc.rust-lang.org/book/ch18-00-patterns.html";
414 err.span_label(span, "`fn` calls are not allowed in patterns");
415 err.help(format!("for more information, visit {patterns_url}"))
417 _ => err.span_label(span, format!("not a {expected}")),
422 /// Controls whether the arguments are tupled. This is used for the call
425 /// Tupling means that all call-side arguments are packed into a tuple and
426 /// passed as a single parameter. For example, if tupling is enabled, this
429 /// fn f(x: (isize, isize)) {}
431 /// Can be called as:
432 /// ```ignore UNSOLVED (can this be done in user code?)
433 /// # fn f(x: (isize, isize)) {}
438 /// # fn f(x: (isize, isize)) {}
441 #[derive(Copy, Clone, Eq, PartialEq)]
442 enum TupleArgumentsFlag {
447 fn fatally_break_rust(sess: &Session) {
448 let handler = sess.diagnostic();
449 handler.span_bug_no_panic(
451 "It looks like you're trying to break rust; would you like some ICE?",
453 handler.note_without_error("the compiler expectedly panicked. this is a feature.");
454 handler.note_without_error(
455 "we would appreciate a joke overview: \
456 https://github.com/rust-lang/rust/issues/43162#issuecomment-320764675",
458 handler.note_without_error(&format!(
459 "rustc {} running on {}",
460 option_env!("CFG_VERSION").unwrap_or("unknown_version"),
461 config::host_triple(),
465 fn has_expected_num_generic_args(
467 trait_did: Option<DefId>,
470 trait_did.map_or(true, |trait_did| {
471 let generics = tcx.generics_of(trait_did);
472 generics.count() == expected + if generics.has_self { 1 } else { 0 }
476 pub fn provide(providers: &mut Providers) {
477 method::provide(providers);
478 *providers = Providers {
482 diagnostic_only_typeck,