|
1 | 1 | use rustc_errors::{Applicability, StashKey};
|
2 | 2 | use rustc_hir as hir;
|
3 |
| -use rustc_hir::def_id::LocalDefId; |
| 3 | +use rustc_hir::def_id::{DefId, LocalDefId}; |
4 | 4 | use rustc_hir::HirId;
|
| 5 | +use rustc_middle::query::plumbing::CyclePlaceholder; |
5 | 6 | use rustc_middle::ty::print::with_forced_trimmed_paths;
|
6 | 7 | use rustc_middle::ty::util::IntTypeExt;
|
7 | 8 | use rustc_middle::ty::{self, ImplTraitInTraitData, IsSuggestable, Ty, TyCtxt, TypeVisitableExt};
|
@@ -388,86 +389,62 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
|
388 | 389 | }
|
389 | 390 | },
|
390 | 391 |
|
391 |
| - Node::Item(item) => { |
392 |
| - match item.kind { |
393 |
| - ItemKind::Static(ty, .., body_id) => { |
394 |
| - if is_suggestable_infer_ty(ty) { |
395 |
| - infer_placeholder_type( |
396 |
| - tcx, |
397 |
| - def_id, |
398 |
| - body_id, |
399 |
| - ty.span, |
400 |
| - item.ident, |
401 |
| - "static variable", |
402 |
| - ) |
403 |
| - } else { |
404 |
| - icx.to_ty(ty) |
405 |
| - } |
406 |
| - } |
407 |
| - ItemKind::Const(ty, _, body_id) => { |
408 |
| - if is_suggestable_infer_ty(ty) { |
409 |
| - infer_placeholder_type( |
410 |
| - tcx, def_id, body_id, ty.span, item.ident, "constant", |
411 |
| - ) |
412 |
| - } else { |
413 |
| - icx.to_ty(ty) |
414 |
| - } |
415 |
| - } |
416 |
| - ItemKind::TyAlias(self_ty, _) => icx.to_ty(self_ty), |
417 |
| - ItemKind::Impl(hir::Impl { self_ty, .. }) => match self_ty.find_self_aliases() { |
418 |
| - spans if spans.len() > 0 => { |
419 |
| - let guar = tcx.sess.emit_err(crate::errors::SelfInImplSelf { |
420 |
| - span: spans.into(), |
421 |
| - note: (), |
422 |
| - }); |
423 |
| - Ty::new_error(tcx, guar) |
424 |
| - } |
425 |
| - _ => icx.to_ty(*self_ty), |
426 |
| - }, |
427 |
| - ItemKind::Fn(..) => { |
428 |
| - let args = ty::GenericArgs::identity_for_item(tcx, def_id); |
429 |
| - Ty::new_fn_def(tcx, def_id.to_def_id(), args) |
430 |
| - } |
431 |
| - ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => { |
432 |
| - let def = tcx.adt_def(def_id); |
433 |
| - let args = ty::GenericArgs::identity_for_item(tcx, def_id); |
434 |
| - Ty::new_adt(tcx, def, args) |
| 392 | + Node::Item(item) => match item.kind { |
| 393 | + ItemKind::Static(ty, .., body_id) => { |
| 394 | + if is_suggestable_infer_ty(ty) { |
| 395 | + infer_placeholder_type( |
| 396 | + tcx, |
| 397 | + def_id, |
| 398 | + body_id, |
| 399 | + ty.span, |
| 400 | + item.ident, |
| 401 | + "static variable", |
| 402 | + ) |
| 403 | + } else { |
| 404 | + icx.to_ty(ty) |
435 | 405 | }
|
436 |
| - ItemKind::OpaqueTy(OpaqueTy { |
437 |
| - origin: hir::OpaqueTyOrigin::TyAlias { .. }, |
438 |
| - .. |
439 |
| - }) => opaque::find_opaque_ty_constraints_for_tait(tcx, def_id), |
440 |
| - // Opaque types desugared from `impl Trait`. |
441 |
| - ItemKind::OpaqueTy(&OpaqueTy { |
442 |
| - origin: |
443 |
| - hir::OpaqueTyOrigin::FnReturn(owner) | hir::OpaqueTyOrigin::AsyncFn(owner), |
444 |
| - in_trait, |
445 |
| - .. |
446 |
| - }) => { |
447 |
| - if in_trait && !tcx.defaultness(owner).has_value() { |
448 |
| - span_bug!( |
449 |
| - tcx.def_span(def_id), |
450 |
| - "tried to get type of this RPITIT with no definition" |
451 |
| - ); |
452 |
| - } |
453 |
| - opaque::find_opaque_ty_constraints_for_rpit(tcx, def_id, owner) |
| 406 | + } |
| 407 | + ItemKind::Const(ty, _, body_id) => { |
| 408 | + if is_suggestable_infer_ty(ty) { |
| 409 | + infer_placeholder_type(tcx, def_id, body_id, ty.span, item.ident, "constant") |
| 410 | + } else { |
| 411 | + icx.to_ty(ty) |
454 | 412 | }
|
455 |
| - ItemKind::Trait(..) |
456 |
| - | ItemKind::TraitAlias(..) |
457 |
| - | ItemKind::Macro(..) |
458 |
| - | ItemKind::Mod(..) |
459 |
| - | ItemKind::ForeignMod { .. } |
460 |
| - | ItemKind::GlobalAsm(..) |
461 |
| - | ItemKind::ExternCrate(..) |
462 |
| - | ItemKind::Use(..) => { |
463 |
| - span_bug!( |
464 |
| - item.span, |
465 |
| - "compute_type_of_item: unexpected item type: {:?}", |
466 |
| - item.kind |
467 |
| - ); |
| 413 | + } |
| 414 | + ItemKind::TyAlias(self_ty, _) => icx.to_ty(self_ty), |
| 415 | + ItemKind::Impl(hir::Impl { self_ty, .. }) => match self_ty.find_self_aliases() { |
| 416 | + spans if spans.len() > 0 => { |
| 417 | + let guar = tcx |
| 418 | + .sess |
| 419 | + .emit_err(crate::errors::SelfInImplSelf { span: spans.into(), note: () }); |
| 420 | + Ty::new_error(tcx, guar) |
468 | 421 | }
|
| 422 | + _ => icx.to_ty(*self_ty), |
| 423 | + }, |
| 424 | + ItemKind::Fn(..) => { |
| 425 | + let args = ty::GenericArgs::identity_for_item(tcx, def_id); |
| 426 | + Ty::new_fn_def(tcx, def_id.to_def_id(), args) |
469 | 427 | }
|
470 |
| - } |
| 428 | + ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Union(..) => { |
| 429 | + let def = tcx.adt_def(def_id); |
| 430 | + let args = ty::GenericArgs::identity_for_item(tcx, def_id); |
| 431 | + Ty::new_adt(tcx, def, args) |
| 432 | + } |
| 433 | + ItemKind::OpaqueTy(..) => tcx.type_of_opaque(def_id).map_or_else( |
| 434 | + |CyclePlaceholder(guar)| Ty::new_error(tcx, guar), |
| 435 | + |ty| ty.instantiate_identity(), |
| 436 | + ), |
| 437 | + ItemKind::Trait(..) |
| 438 | + | ItemKind::TraitAlias(..) |
| 439 | + | ItemKind::Macro(..) |
| 440 | + | ItemKind::Mod(..) |
| 441 | + | ItemKind::ForeignMod { .. } |
| 442 | + | ItemKind::GlobalAsm(..) |
| 443 | + | ItemKind::ExternCrate(..) |
| 444 | + | ItemKind::Use(..) => { |
| 445 | + span_bug!(item.span, "compute_type_of_item: unexpected item type: {:?}", item.kind); |
| 446 | + } |
| 447 | + }, |
471 | 448 |
|
472 | 449 | Node::ForeignItem(foreign_item) => match foreign_item.kind {
|
473 | 450 | ForeignItemKind::Fn(..) => {
|
@@ -514,6 +491,51 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
|
514 | 491 | ty::EarlyBinder::bind(output)
|
515 | 492 | }
|
516 | 493 |
|
| 494 | +pub(super) fn type_of_opaque( |
| 495 | + tcx: TyCtxt<'_>, |
| 496 | + def_id: DefId, |
| 497 | +) -> Result<ty::EarlyBinder<Ty<'_>>, CyclePlaceholder> { |
| 498 | + if let Some(def_id) = def_id.as_local() { |
| 499 | + use rustc_hir::*; |
| 500 | + |
| 501 | + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); |
| 502 | + Ok(ty::EarlyBinder::bind(match tcx.hir().get(hir_id) { |
| 503 | + Node::Item(item) => match item.kind { |
| 504 | + ItemKind::OpaqueTy(OpaqueTy { |
| 505 | + origin: hir::OpaqueTyOrigin::TyAlias { .. }, |
| 506 | + .. |
| 507 | + }) => opaque::find_opaque_ty_constraints_for_tait(tcx, def_id), |
| 508 | + // Opaque types desugared from `impl Trait`. |
| 509 | + ItemKind::OpaqueTy(&OpaqueTy { |
| 510 | + origin: |
| 511 | + hir::OpaqueTyOrigin::FnReturn(owner) | hir::OpaqueTyOrigin::AsyncFn(owner), |
| 512 | + in_trait, |
| 513 | + .. |
| 514 | + }) => { |
| 515 | + if in_trait && !tcx.defaultness(owner).has_value() { |
| 516 | + span_bug!( |
| 517 | + tcx.def_span(def_id), |
| 518 | + "tried to get type of this RPITIT with no definition" |
| 519 | + ); |
| 520 | + } |
| 521 | + opaque::find_opaque_ty_constraints_for_rpit(tcx, def_id, owner) |
| 522 | + } |
| 523 | + _ => { |
| 524 | + span_bug!(item.span, "type_of_opaque: unexpected item type: {:?}", item.kind); |
| 525 | + } |
| 526 | + }, |
| 527 | + |
| 528 | + x => { |
| 529 | + bug!("unexpected sort of node in type_of_opaque(): {:?}", x); |
| 530 | + } |
| 531 | + })) |
| 532 | + } else { |
| 533 | + // Foreign opaque type will go through the foreign provider |
| 534 | + // and load the type from metadata. |
| 535 | + Ok(tcx.type_of(def_id)) |
| 536 | + } |
| 537 | +} |
| 538 | + |
517 | 539 | fn infer_placeholder_type<'a>(
|
518 | 540 | tcx: TyCtxt<'a>,
|
519 | 541 | def_id: LocalDefId,
|
|
0 commit comments