1 use rustc_data_structures::fx::FxHashSet;
2 use rustc_errors::{struct_span_err, Applicability, ErrorReported, StashKey};
4 use rustc_hir::def::{DefKind, Res};
5 use rustc_hir::def_id::DefId;
6 use rustc_hir::intravisit;
7 use rustc_hir::intravisit::Visitor;
9 use rustc_middle::hir::map::Map;
10 use rustc_middle::ty::subst::{GenericArgKind, InternalSubsts};
11 use rustc_middle::ty::util::IntTypeExt;
12 use rustc_middle::ty::{self, DefIdTree, Ty, TyCtxt, TypeFoldable};
13 use rustc_session::parse::feature_err;
14 use rustc_span::symbol::{sym, Ident};
15 use rustc_span::{Span, DUMMY_SP};
16 use rustc_trait_selection::traits;
19 use super::{bad_placeholder_type, is_suggestable_infer_ty};
21 pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
24 let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
26 let icx = ItemCtxt::new(tcx, def_id);
28 match tcx.hir().get(hir_id) {
29 Node::TraitItem(item) => match item.kind {
30 TraitItemKind::Fn(..) => {
31 let substs = InternalSubsts::identity_for_item(tcx, def_id);
32 tcx.mk_fn_def(def_id, substs)
34 TraitItemKind::Const(ref ty, body_id) => body_id
36 if is_suggestable_infer_ty(ty) {
37 Some(infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident))
42 .unwrap_or_else(|| icx.to_ty(ty)),
43 TraitItemKind::Type(_, Some(ref ty)) => icx.to_ty(ty),
44 TraitItemKind::Type(_, None) => {
45 span_bug!(item.span, "associated type missing default");
49 Node::ImplItem(item) => match item.kind {
50 ImplItemKind::Fn(..) => {
51 let substs = InternalSubsts::identity_for_item(tcx, def_id);
52 tcx.mk_fn_def(def_id, substs)
54 ImplItemKind::Const(ref ty, body_id) => {
55 if is_suggestable_infer_ty(ty) {
56 infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident)
61 ImplItemKind::OpaqueTy(_) => {
62 if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() {
63 report_assoc_ty_on_inherent_impl(tcx, item.span);
66 find_opaque_ty_constraints(tcx, def_id)
68 ImplItemKind::TyAlias(ref ty) => {
69 if tcx.impl_trait_ref(tcx.hir().get_parent_did(hir_id).to_def_id()).is_none() {
70 report_assoc_ty_on_inherent_impl(tcx, item.span);
79 ItemKind::Static(ref ty, .., body_id) | ItemKind::Const(ref ty, body_id) => {
80 if is_suggestable_infer_ty(ty) {
81 infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident)
86 ItemKind::TyAlias(ref self_ty, _) | ItemKind::Impl { ref self_ty, .. } => {
90 let substs = InternalSubsts::identity_for_item(tcx, def_id);
91 tcx.mk_fn_def(def_id, substs)
93 ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => {
94 let def = tcx.adt_def(def_id);
95 let substs = InternalSubsts::identity_for_item(tcx, def_id);
96 tcx.mk_adt(def, substs)
98 ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: None, .. }) => {
99 find_opaque_ty_constraints(tcx, def_id)
101 // Opaque types desugared from `impl Trait`.
102 ItemKind::OpaqueTy(OpaqueTy { impl_trait_fn: Some(owner), origin, .. }) => {
103 let concrete_types = match origin {
104 OpaqueTyOrigin::FnReturn | OpaqueTyOrigin::AsyncFn => {
105 &tcx.mir_borrowck(owner).concrete_opaque_types
107 OpaqueTyOrigin::Misc => {
108 // We shouldn't leak borrowck results through impl trait in bindings.
109 // For example, we shouldn't be able to tell if `x` in
110 // `let x: impl Sized + 'a = &()` has type `&'static ()` or `&'a ()`.
111 &tcx.typeck_tables_of(owner).concrete_opaque_types
113 OpaqueTyOrigin::TypeAlias => {
114 span_bug!(item.span, "Type alias impl trait shouldn't have an owner")
117 let concrete_ty = concrete_types
119 .map(|opaque| opaque.concrete_type)
121 tcx.sess.delay_span_bug(
124 "owner {:?} has no opaque type for {:?} in its tables",
128 if let Some(ErrorReported) =
129 tcx.typeck_tables_of(owner).tainted_by_errors
132 // owner fn prevented us from populating
133 // the `concrete_opaque_types` table.
136 // We failed to resolve the opaque type or it
137 // resolves to itself. Return the non-revealed
138 // type, which should result in E0720.
141 InternalSubsts::identity_for_item(tcx, def_id),
145 debug!("concrete_ty = {:?}", concrete_ty);
146 if concrete_ty.has_erased_regions() {
147 // FIXME(impl_trait_in_bindings) Handle this case.
150 "lifetimes in impl Trait types in bindings are not currently supported",
156 | ItemKind::TraitAlias(..)
158 | ItemKind::ForeignMod(..)
159 | ItemKind::GlobalAsm(..)
160 | ItemKind::ExternCrate(..)
161 | ItemKind::Use(..) => {
164 "compute_type_of_item: unexpected item type: {:?}",
171 Node::ForeignItem(foreign_item) => match foreign_item.kind {
172 ForeignItemKind::Fn(..) => {
173 let substs = InternalSubsts::identity_for_item(tcx, def_id);
174 tcx.mk_fn_def(def_id, substs)
176 ForeignItemKind::Static(ref t, _) => icx.to_ty(t),
177 ForeignItemKind::Type => tcx.mk_foreign(def_id),
180 Node::Ctor(&ref def) | Node::Variant(Variant { data: ref def, .. }) => match *def {
181 VariantData::Unit(..) | VariantData::Struct(..) => {
182 tcx.type_of(tcx.hir().get_parent_did(hir_id).to_def_id())
184 VariantData::Tuple(..) => {
185 let substs = InternalSubsts::identity_for_item(tcx, def_id);
186 tcx.mk_fn_def(def_id, substs)
190 Node::Field(field) => icx.to_ty(&field.ty),
192 Node::Expr(&Expr { kind: ExprKind::Closure(.., gen), .. }) => {
193 let substs = InternalSubsts::identity_for_item(tcx, def_id);
194 if let Some(movability) = gen {
195 tcx.mk_generator(def_id, substs, movability)
197 tcx.mk_closure(def_id, substs)
201 Node::AnonConst(_) => {
202 let parent_node = tcx.hir().get(tcx.hir().get_parent_node(hir_id));
204 Node::Ty(&Ty { kind: TyKind::Array(_, ref constant), .. })
205 | Node::Ty(&Ty { kind: TyKind::Typeof(ref constant), .. })
206 | Node::Expr(&Expr { kind: ExprKind::Repeat(_, ref constant), .. })
207 if constant.hir_id == hir_id =>
212 Node::Variant(Variant { disr_expr: Some(ref e), .. }) if e.hir_id == hir_id => tcx
213 .adt_def(tcx.hir().get_parent_did(hir_id).to_def_id())
218 Node::Ty(&Ty { kind: TyKind::Path(_), .. })
219 | Node::Expr(&Expr { kind: ExprKind::Struct(..), .. })
220 | Node::Expr(&Expr { kind: ExprKind::Path(_), .. })
221 | Node::TraitRef(..) => {
222 let path = match parent_node {
223 Node::Ty(&Ty { kind: TyKind::Path(QPath::Resolved(_, path)), .. })
226 ExprKind::Path(QPath::Resolved(_, path))
227 | ExprKind::Struct(&QPath::Resolved(_, path), ..),
230 | Node::TraitRef(&TraitRef { path, .. }) => &*path,
232 tcx.sess.delay_span_bug(
234 &format!("unexpected const parent path {:?}", parent_node),
236 return tcx.types.err;
240 // We've encountered an `AnonConst` in some path, so we need to
241 // figure out which generic parameter it corresponds to and return
242 // the relevant type.
244 let (arg_index, segment) = path
247 .filter_map(|seg| seg.args.as_ref().map(|args| (args.args, seg)))
248 .find_map(|(args, seg)| {
250 .filter(|arg| arg.is_const())
252 .filter(|(_, arg)| arg.id() == hir_id)
253 .map(|(index, _)| (index, seg))
257 bug!("no arg matching AnonConst in path");
260 // Try to use the segment resolution if it is valid, otherwise we
261 // default to the path resolution.
262 let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
263 let generics = match res {
264 Res::Def(DefKind::Ctor(..), def_id) => {
265 tcx.generics_of(tcx.parent(def_id).unwrap())
267 Res::Def(_, def_id) => tcx.generics_of(def_id),
269 tcx.sess.delay_span_bug(
272 "unexpected anon const res {:?} in path: {:?}",
276 return tcx.types.err;
284 if let ty::GenericParamDefKind::Const = param.kind {
291 .map(|param| tcx.type_of(param.def_id));
293 if let Some(ty) = ty {
296 // This is no generic parameter associated with the arg. This is
297 // probably from an extra arg where one is not needed.
298 tcx.sess.delay_span_bug(
301 "missing generic parameter for `AnonConst`, parent: {:?}, res: {:?}",
310 tcx.sess.delay_span_bug(
312 &format!("unexpected const parent in type_of_def_id(): {:?}", x),
319 Node::GenericParam(param) => match ¶m.kind {
320 GenericParamKind::Type { default: Some(ref ty), .. } => icx.to_ty(ty),
321 GenericParamKind::Const { ty: ref hir_ty, .. } => {
322 let ty = icx.to_ty(hir_ty);
323 if !tcx.features().const_compare_raw_pointers {
324 let err = match ty.peel_refs().kind {
325 ty::FnPtr(_) => Some("function pointers"),
326 ty::RawPtr(_) => Some("raw pointers"),
329 if let Some(unsupported_type) = err {
331 &tcx.sess.parse_sess,
332 sym::const_compare_raw_pointers,
335 "using {} as const generic parameters is unstable",
342 if traits::search_for_structural_match_violation(param.hir_id, param.span, tcx, ty)
345 // We use the same error code in both branches, because this is really the same
346 // issue: we just special-case the message for type parameters to make it
348 if let ty::Param(_) = ty.peel_refs().kind {
349 // Const parameters may not have type parameters as their types,
350 // because we cannot be sure that the type parameter derives `PartialEq`
351 // and `Eq` (just implementing them is not enough for `structural_match`).
356 "`{}` is not guaranteed to `#[derive(PartialEq, Eq)]`, so may not be \
357 used as the type of a const parameter",
362 format!("`{}` may not derive both `PartialEq` and `Eq`", ty),
365 "it is not currently possible to use a type parameter as the type of a \
374 "`{}` must be annotated with `#[derive(PartialEq, Eq)]` to be used as \
375 the type of a const parameter",
380 format!("`{}` doesn't derive both `PartialEq` and `Eq`", ty),
387 x => bug!("unexpected non-type Node::GenericParam: {:?}", x),
391 bug!("unexpected sort of node in type_of_def_id(): {:?}", x);
396 fn find_opaque_ty_constraints(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
397 use rustc_hir::{Expr, ImplItem, Item, TraitItem};
399 debug!("find_opaque_ty_constraints({:?})", def_id);
401 struct ConstraintLocator<'tcx> {
404 // (first found type span, actual type)
405 found: Option<(Span, Ty<'tcx>)>,
408 impl ConstraintLocator<'_> {
409 fn check(&mut self, def_id: DefId) {
410 // Don't try to check items that cannot possibly constrain the type.
411 if !self.tcx.has_typeck_tables(def_id) {
413 "find_opaque_ty_constraints: no constraint for `{:?}` at `{:?}`: no tables",
418 // Calling `mir_borrowck` can lead to cycle errors through
419 // const-checking, avoid calling it if we don't have to.
420 if !self.tcx.typeck_tables_of(def_id).concrete_opaque_types.contains_key(&self.def_id) {
422 "find_opaque_ty_constraints: no constraint for `{:?}` at `{:?}`",
427 // Use borrowck to get the type with unerased regions.
428 let ty = self.tcx.mir_borrowck(def_id).concrete_opaque_types.get(&self.def_id);
429 if let Some(ty::ResolvedOpaqueTy { concrete_type, substs }) = ty {
431 "find_opaque_ty_constraints: found constraint for `{:?}` at `{:?}`: {:?}",
432 self.def_id, def_id, ty,
435 // FIXME(oli-obk): trace the actual span from inference to improve errors.
436 let span = self.tcx.def_span(def_id);
438 // HACK(eddyb) this check shouldn't be needed, as `wfcheck`
439 // performs the same checks, in theory, but I've kept it here
440 // using `delay_span_bug`, just in case `wfcheck` slips up.
441 let opaque_generics = self.tcx.generics_of(self.def_id);
442 let mut used_params: FxHashSet<_> = FxHashSet::default();
443 for (i, arg) in substs.iter().enumerate() {
444 let arg_is_param = match arg.unpack() {
445 GenericArgKind::Type(ty) => matches!(ty.kind, ty::Param(_)),
446 GenericArgKind::Lifetime(lt) => {
447 matches!(lt, ty::ReEarlyBound(_) | ty::ReFree(_))
449 GenericArgKind::Const(ct) => matches!(ct.val, ty::ConstKind::Param(_)),
453 if !used_params.insert(arg) {
454 // There was already an entry for `arg`, meaning a generic parameter
456 self.tcx.sess.delay_span_bug(
459 "defining opaque type use restricts opaque \
460 type by using the generic parameter `{}` twice",
466 let param = opaque_generics.param_at(i, self.tcx);
467 self.tcx.sess.delay_span_bug(
470 "defining opaque type use does not fully define opaque type: \
471 generic parameter `{}` is specified as concrete {} `{}`",
480 if let Some((prev_span, prev_ty)) = self.found {
481 if *concrete_type != prev_ty {
482 debug!("find_opaque_ty_constraints: span={:?}", span);
483 // Found different concrete types for the opaque type.
484 let mut err = self.tcx.sess.struct_span_err(
486 "concrete type differs from previous defining opaque type use",
490 format!("expected `{}`, got `{}`", prev_ty, concrete_type),
492 err.span_note(prev_span, "previous use here");
496 self.found = Some((span, concrete_type));
500 "find_opaque_ty_constraints: no constraint for `{:?}` at `{:?}`",
507 impl<'tcx> intravisit::Visitor<'tcx> for ConstraintLocator<'tcx> {
508 type Map = Map<'tcx>;
510 fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
511 intravisit::NestedVisitorMap::All(self.tcx.hir())
513 fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
514 if let hir::ExprKind::Closure(..) = ex.kind {
515 let def_id = self.tcx.hir().local_def_id(ex.hir_id);
518 intravisit::walk_expr(self, ex);
520 fn visit_item(&mut self, it: &'tcx Item<'tcx>) {
521 debug!("find_existential_constraints: visiting {:?}", it);
522 let def_id = self.tcx.hir().local_def_id(it.hir_id);
523 // The opaque type itself or its children are not within its reveal scope.
524 if def_id != self.def_id {
526 intravisit::walk_item(self, it);
529 fn visit_impl_item(&mut self, it: &'tcx ImplItem<'tcx>) {
530 debug!("find_existential_constraints: visiting {:?}", it);
531 let def_id = self.tcx.hir().local_def_id(it.hir_id);
532 // The opaque type itself or its children are not within its reveal scope.
533 if def_id != self.def_id {
535 intravisit::walk_impl_item(self, it);
538 fn visit_trait_item(&mut self, it: &'tcx TraitItem<'tcx>) {
539 debug!("find_existential_constraints: visiting {:?}", it);
540 let def_id = self.tcx.hir().local_def_id(it.hir_id);
542 intravisit::walk_trait_item(self, it);
546 let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
547 let scope = tcx.hir().get_defining_scope(hir_id);
548 let mut locator = ConstraintLocator { def_id, tcx, found: None };
550 debug!("find_opaque_ty_constraints: scope={:?}", scope);
552 if scope == hir::CRATE_HIR_ID {
553 intravisit::walk_crate(&mut locator, tcx.hir().krate());
555 debug!("find_opaque_ty_constraints: scope={:?}", tcx.hir().get(scope));
556 match tcx.hir().get(scope) {
557 // We explicitly call `visit_*` methods, instead of using `intravisit::walk_*` methods
558 // This allows our visitor to process the defining item itself, causing
559 // it to pick up any 'sibling' defining uses.
561 // For example, this code:
564 // type Blah = impl Debug;
565 // let my_closure = || -> Blah { true };
569 // requires us to explicitly process `foo()` in order
570 // to notice the defining usage of `Blah`.
571 Node::Item(ref it) => locator.visit_item(it),
572 Node::ImplItem(ref it) => locator.visit_impl_item(it),
573 Node::TraitItem(ref it) => locator.visit_trait_item(it),
574 other => bug!("{:?} is not a valid scope for an opaque type item", other),
578 match locator.found {
581 let span = tcx.def_span(def_id);
582 tcx.sess.span_err(span, "could not find defining uses");
588 fn infer_placeholder_type(
591 body_id: hir::BodyId,
595 let ty = tcx.diagnostic_only_typeck_tables_of(def_id).node_type(body_id.hir_id);
597 // If this came from a free `const` or `static mut?` item,
598 // then the user may have written e.g. `const A = 42;`.
599 // In this case, the parser has stashed a diagnostic for
600 // us to improve in typeck so we do that now.
601 match tcx.sess.diagnostic().steal_diagnostic(span, StashKey::ItemNoType) {
603 // The parser provided a sub-optimal `HasPlaceholders` suggestion for the type.
604 // We are typeck and have the real type, so remove that and suggest the actual type.
605 err.suggestions.clear();
608 "provide a type for the item",
609 format!("{}: {}", item_ident, ty),
610 Applicability::MachineApplicable,
615 let mut diag = bad_placeholder_type(tcx, vec![span]);
616 if ty != tcx.types.err {
617 diag.span_suggestion(
619 "replace `_` with the correct type",
621 Applicability::MaybeIncorrect,
628 // Typeck doesn't expect erased regions to be returned from `type_of`.
629 tcx.fold_regions(&ty, &mut false, |r, _| match r {
630 ty::ReErased => tcx.lifetimes.re_static,
635 fn report_assoc_ty_on_inherent_impl(tcx: TyCtxt<'_>, span: Span) {
640 "associated types are not yet supported in inherent impls (see #8995)"