|
| 1 | +use hir::def::{CtorKind, CtorOf, DefKind}; |
1 | 2 | use rustc_hir as hir;
|
2 | 3 | use rustc_hir::def_id::DefId;
|
3 | 4 | use rustc_hir::lang_items::LangItem;
|
@@ -834,27 +835,40 @@ fn build_call_shim<'tcx>(
|
834 | 835 | }
|
835 | 836 |
|
836 | 837 | pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
|
837 |
| - debug_assert!(tcx.is_constructor(ctor_id)); |
838 |
| - |
839 | 838 | let param_env = tcx.param_env_reveal_all_normalized(ctor_id);
|
| 839 | + let span = tcx.def_span(ctor_id); |
840 | 840 |
|
841 |
| - // Normalize the sig. |
842 |
| - let sig = tcx |
843 |
| - .fn_sig(ctor_id) |
844 |
| - .subst_identity() |
845 |
| - .no_bound_vars() |
846 |
| - .expect("LBR in ADT constructor signature"); |
847 |
| - let sig = tcx.normalize_erasing_regions(param_env, sig); |
| 841 | + let (local_decls, inputs, adt_ty) = match tcx.def_kind(ctor_id) { |
| 842 | + DefKind::Ctor(_, CtorKind::Fn) => { |
| 843 | + // Normalize the sig. |
| 844 | + let sig = tcx |
| 845 | + .fn_sig(ctor_id) |
| 846 | + .subst_identity() |
| 847 | + .no_bound_vars() |
| 848 | + .expect("LBR in ADT constructor signature"); |
| 849 | + let sig = tcx.normalize_erasing_regions(param_env, sig); |
| 850 | + debug!("build_ctor: ctor_id={:?} sig={:?}", ctor_id, sig); |
| 851 | + |
| 852 | + (local_decls_for_sig(&sig, span), sig.inputs().len(), sig.output()) |
| 853 | + } |
| 854 | + DefKind::Ctor(of, CtorKind::Const) => { |
| 855 | + let mut ctor_id = ctor_id; |
| 856 | + if let CtorOf::Variant = of { |
| 857 | + ctor_id = tcx.parent(ctor_id); |
| 858 | + } |
| 859 | + let adt = tcx.parent(ctor_id); |
| 860 | + let adt_ty = |
| 861 | + tcx.normalize_erasing_regions(param_env, tcx.type_of(adt).subst_identity()); |
848 | 862 |
|
849 |
| - let ty::Adt(adt_def, substs) = sig.output().kind() else { |
850 |
| - bug!("unexpected type for ADT ctor {:?}", sig.output()); |
| 863 | + let local_decls = std::iter::once(LocalDecl::new(adt_ty, span)).collect(); |
| 864 | + (local_decls, 0, adt_ty) |
| 865 | + } |
| 866 | + def_kind => bug!("ctor_id {:?} was expected to be a `DefKind::Ctor`", def_kind), |
851 | 867 | };
|
852 | 868 |
|
853 |
| - debug!("build_ctor: ctor_id={:?} sig={:?}", ctor_id, sig); |
854 |
| - |
855 |
| - let span = tcx.def_span(ctor_id); |
856 |
| - |
857 |
| - let local_decls = local_decls_for_sig(&sig, span); |
| 869 | + let ty::Adt(adt_def, substs) = adt_ty.kind() else { |
| 870 | + bug!("unexpected type for ADT ctor {:?}", adt_ty); |
| 871 | + }; |
858 | 872 |
|
859 | 873 | let source_info = SourceInfo::outermost(span);
|
860 | 874 |
|
@@ -891,13 +905,7 @@ pub fn build_adt_ctor(tcx: TyCtxt<'_>, ctor_id: DefId) -> Body<'_> {
|
891 | 905 | };
|
892 | 906 |
|
893 | 907 | let source = MirSource::item(ctor_id);
|
894 |
| - let body = new_body( |
895 |
| - source, |
896 |
| - IndexVec::from_elem_n(start_block, 1), |
897 |
| - local_decls, |
898 |
| - sig.inputs().len(), |
899 |
| - span, |
900 |
| - ); |
| 908 | + let body = new_body(source, IndexVec::from_elem_n(start_block, 1), local_decls, inputs, span); |
901 | 909 |
|
902 | 910 | crate::pass_manager::dump_mir_for_phase_change(tcx, &body);
|
903 | 911 |
|
|
0 commit comments