]> git.proxmox.com Git - rustc.git/blobdiff - compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs
New upstream version 1.60.0+dfsg1
[rustc.git] / compiler / rustc_codegen_ssa / src / debuginfo / type_names.rs
index 93bb1aee25f7d25d9aca664a58275d30fa9c3f17..b63851c195de6df091bed7ed0d4627ee3bb736ac 100644 (file)
@@ -13,9 +13,9 @@
 
 use rustc_data_structures::fx::FxHashSet;
 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
-use rustc_hir as hir;
 use rustc_hir::def_id::DefId;
 use rustc_hir::definitions::{DefPathData, DefPathDataName, DisambiguatedDefPathData};
+use rustc_hir::{AsyncGeneratorKind, GeneratorKind, Mutability};
 use rustc_middle::ty::layout::IntegerExt;
 use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
 use rustc_middle::ty::{self, AdtDef, ExistentialProjection, Ty, TyCtxt};
@@ -102,14 +102,14 @@ fn push_debuginfo_type_name<'tcx>(
         ty::RawPtr(ty::TypeAndMut { ty: inner_type, mutbl }) => {
             if cpp_like_debuginfo {
                 match mutbl {
-                    hir::Mutability::Not => output.push_str("ptr_const$<"),
-                    hir::Mutability::Mut => output.push_str("ptr_mut$<"),
+                    Mutability::Not => output.push_str("ptr_const$<"),
+                    Mutability::Mut => output.push_str("ptr_mut$<"),
                 }
             } else {
                 output.push('*');
                 match mutbl {
-                    hir::Mutability::Not => output.push_str("const "),
-                    hir::Mutability::Mut => output.push_str("mut "),
+                    Mutability::Not => output.push_str("const "),
+                    Mutability::Mut => output.push_str("mut "),
                 }
             }
 
@@ -131,8 +131,8 @@ fn push_debuginfo_type_name<'tcx>(
                 output.push_str(mutbl.prefix_str());
             } else if !is_slice_or_str {
                 match mutbl {
-                    hir::Mutability::Not => output.push_str("ref$<"),
-                    hir::Mutability::Mut => output.push_str("ref_mut$<"),
+                    Mutability::Not => output.push_str("ref$<"),
+                    Mutability::Mut => output.push_str("ref_mut$<"),
                 }
             }
 
@@ -146,7 +146,7 @@ fn push_debuginfo_type_name<'tcx>(
             if cpp_like_debuginfo {
                 output.push_str("array$<");
                 push_debuginfo_type_name(tcx, inner_type, true, output, visited);
-                match len.val {
+                match len.val() {
                     ty::ConstKind::Param(param) => write!(output, ",{}>", param.name).unwrap(),
                     _ => write!(output, ",{}>", len.eval_usize(tcx, ty::ParamEnv::reveal_all()))
                         .unwrap(),
@@ -154,7 +154,7 @@ fn push_debuginfo_type_name<'tcx>(
             } else {
                 output.push('[');
                 push_debuginfo_type_name(tcx, inner_type, true, output, visited);
-                match len.val {
+                match len.val() {
                     ty::ConstKind::Param(param) => write!(output, "; {}]", param.name).unwrap(),
                     _ => write!(output, "; {}]", len.eval_usize(tcx, ty::ParamEnv::reveal_all()))
                         .unwrap(),
@@ -203,8 +203,9 @@ fn push_debuginfo_type_name<'tcx>(
                 let projection_bounds: SmallVec<[_; 4]> = trait_data
                     .projection_bounds()
                     .map(|bound| {
-                        let ExistentialProjection { item_def_id, ty, .. } = bound.skip_binder();
-                        (item_def_id, ty)
+                        let ExistentialProjection { item_def_id, term, .. } = bound.skip_binder();
+                        // FIXME(associated_const_equality): allow for consts here
+                        (item_def_id, term.ty().unwrap())
                     })
                     .collect();
 
@@ -342,16 +343,41 @@ fn push_debuginfo_type_name<'tcx>(
             // We only care about avoiding recursing
             // directly back to the type we're currently
             // processing
-            visited.remove(t);
+            visited.remove(&t);
         }
-        ty::Closure(def_id, ..) | ty::Generator(def_id, ..) => {
-            let key = tcx.def_key(def_id);
+        ty::Closure(def_id, substs) | ty::Generator(def_id, substs, ..) => {
+            // Name will be "{closure_env#0}<T1, T2, ...>", "{generator_env#0}<T1, T2, ...>", or
+            // "{async_fn_env#0}<T1, T2, ...>", etc.
+            let def_key = tcx.def_key(def_id);
+
             if qualified {
-                let parent_def_id = DefId { index: key.parent.unwrap(), ..def_id };
+                let parent_def_id = DefId { index: def_key.parent.unwrap(), ..def_id };
                 push_item_name(tcx, parent_def_id, true, output);
                 output.push_str("::");
             }
-            push_unqualified_item_name(tcx, def_id, key.disambiguated_data, output);
+
+            let mut label = String::with_capacity(20);
+            write!(&mut label, "{}_env", generator_kind_label(tcx.generator_kind(def_id))).unwrap();
+
+            push_disambiguated_special_name(
+                &label,
+                def_key.disambiguated_data.disambiguator,
+                cpp_like_debuginfo,
+                output,
+            );
+
+            // We also need to add the generic arguments of the async fn/generator or
+            // the enclosing function (for closures or async blocks), so that we end
+            // up with a unique name for every instantiation.
+
+            // Find the generics of the enclosing function, as defined in the source code.
+            let enclosing_fn_def_id = tcx.typeck_root_def_id(def_id);
+            let generics = tcx.generics_of(enclosing_fn_def_id);
+
+            // Truncate the substs to the length of the above generics. This will cut off
+            // anything closure- or generator-specific.
+            let substs = substs.truncate_to(tcx, generics);
+            push_generic_params_internal(tcx, substs, output, visited);
         }
         // Type parameters from polymorphized functions.
         ty::Param(_) => {
@@ -409,14 +435,14 @@ fn push_debuginfo_type_name<'tcx>(
             let max = dataful_discriminant_range.end;
             let max = tag.value.size(&tcx).truncate(max);
 
-            let dataful_variant_name = def.variants[*dataful_variant].ident.as_str();
+            let dataful_variant_name = def.variants[*dataful_variant].name.as_str();
 
             output.push_str(&format!(", {}, {}, {}", min, max, dataful_variant_name));
         } else if let Variants::Single { index: variant_idx } = &layout.variants {
             // Uninhabited enums can't be constructed and should never need to be visualized so
             // skip this step for them.
             if def.variants.len() != 0 {
-                let variant = def.variants[*variant_idx].ident.as_str();
+                let variant = def.variants[*variant_idx].name.as_str();
 
                 output.push_str(&format!(", {}", variant));
             }
@@ -443,7 +469,14 @@ fn push_debuginfo_type_name<'tcx>(
     }
 }
 
-/// Computes a name for the global variable storing a vtable.
+pub enum VTableNameKind {
+    // Is the name for the const/static holding the vtable?
+    GlobalVariable,
+    // Is the name for the type of the vtable?
+    Type,
+}
+
+/// Computes a name for the global variable storing a vtable (or the type of that global variable).
 ///
 /// The name is of the form:
 ///
@@ -452,10 +485,15 @@ fn push_debuginfo_type_name<'tcx>(
 /// or, when generating C++-like names:
 ///
 /// `impl$<path::to::SomeType, path::to::SomeTrait>::vtable$`
+///
+/// If `kind` is `VTableNameKind::Type` then the last component is `{vtable_ty}` instead of just
+/// `{vtable}`, so that the type and the corresponding global variable get assigned different
+/// names.
 pub fn compute_debuginfo_vtable_name<'tcx>(
     tcx: TyCtxt<'tcx>,
     t: Ty<'tcx>,
     trait_ref: Option<ty::PolyExistentialTraitRef<'tcx>>,
+    kind: VTableNameKind,
 ) -> String {
     let cpp_like_debuginfo = cpp_like_debuginfo(tcx);
 
@@ -488,7 +526,12 @@ pub fn compute_debuginfo_vtable_name<'tcx>(
 
     push_close_angle_bracket(cpp_like_debuginfo, &mut vtable_name);
 
-    let suffix = if cpp_like_debuginfo { "::vtable$" } else { "::{vtable}" };
+    let suffix = match (cpp_like_debuginfo, kind) {
+        (true, VTableNameKind::GlobalVariable) => "::vtable$",
+        (false, VTableNameKind::GlobalVariable) => "::{vtable}",
+        (true, VTableNameKind::Type) => "::vtable_type$",
+        (false, VTableNameKind::Type) => "::{vtable_type}",
+    };
 
     vtable_name.reserve_exact(suffix.len());
     vtable_name.push_str(suffix);
@@ -508,6 +551,29 @@ pub fn push_item_name(tcx: TyCtxt<'_>, def_id: DefId, qualified: bool, output: &
     push_unqualified_item_name(tcx, def_id, def_key.disambiguated_data, output);
 }
 
+fn generator_kind_label(generator_kind: Option<GeneratorKind>) -> &'static str {
+    match generator_kind {
+        Some(GeneratorKind::Async(AsyncGeneratorKind::Block)) => "async_block",
+        Some(GeneratorKind::Async(AsyncGeneratorKind::Closure)) => "async_closure",
+        Some(GeneratorKind::Async(AsyncGeneratorKind::Fn)) => "async_fn",
+        Some(GeneratorKind::Gen) => "generator",
+        None => "closure",
+    }
+}
+
+fn push_disambiguated_special_name(
+    label: &str,
+    disambiguator: u32,
+    cpp_like_debuginfo: bool,
+    output: &mut String,
+) {
+    if cpp_like_debuginfo {
+        write!(output, "{}${}", label, disambiguator).unwrap();
+    } else {
+        write!(output, "{{{}#{}}}", label, disambiguator).unwrap();
+    }
+}
+
 fn push_unqualified_item_name(
     tcx: TyCtxt<'_>,
     def_id: DefId,
@@ -518,36 +584,32 @@ fn push_unqualified_item_name(
         DefPathData::CrateRoot => {
             output.push_str(tcx.crate_name(def_id.krate).as_str());
         }
-        DefPathData::ClosureExpr if tcx.generator_kind(def_id).is_some() => {
-            // Generators look like closures, but we want to treat them differently
-            // in the debug info.
-            if cpp_like_debuginfo(tcx) {
-                write!(output, "generator${}", disambiguated_data.disambiguator).unwrap();
-            } else {
-                write!(output, "{{generator#{}}}", disambiguated_data.disambiguator).unwrap();
-            }
+        DefPathData::ClosureExpr => {
+            let label = generator_kind_label(tcx.generator_kind(def_id));
+
+            push_disambiguated_special_name(
+                label,
+                disambiguated_data.disambiguator,
+                cpp_like_debuginfo(tcx),
+                output,
+            );
         }
         _ => match disambiguated_data.data.name() {
             DefPathDataName::Named(name) => {
                 output.push_str(name.as_str());
             }
             DefPathDataName::Anon { namespace } => {
-                if cpp_like_debuginfo(tcx) {
-                    write!(output, "{}${}", namespace, disambiguated_data.disambiguator).unwrap();
-                } else {
-                    write!(output, "{{{}#{}}}", namespace, disambiguated_data.disambiguator)
-                        .unwrap();
-                }
+                push_disambiguated_special_name(
+                    namespace.as_str(),
+                    disambiguated_data.disambiguator,
+                    cpp_like_debuginfo(tcx),
+                    output,
+                );
             }
         },
     };
 }
 
-// Pushes the generic parameters in the given `InternalSubsts` to the output string.
-// This ignores region parameters, since they can't reliably be
-// reconstructed for items from non-local crates. For local crates, this
-// would be possible but with inlining and LTO we have to use the least
-// common denominator - otherwise we would run into conflicts.
 fn push_generic_params_internal<'tcx>(
     tcx: TyCtxt<'tcx>,
     substs: SubstsRef<'tcx>,
@@ -583,19 +645,19 @@ fn push_generic_params_internal<'tcx>(
     true
 }
 
-fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: &'tcx ty::Const<'tcx>, output: &mut String) {
-    match ct.val {
+fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: ty::Const<'tcx>, output: &mut String) {
+    match ct.val() {
         ty::ConstKind::Param(param) => {
             write!(output, "{}", param.name)
         }
-        _ => match ct.ty.kind() {
+        _ => match ct.ty().kind() {
             ty::Int(ity) => {
-                let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty);
+                let bits = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty());
                 let val = Integer::from_int_ty(&tcx, *ity).size().sign_extend(bits) as i128;
                 write!(output, "{}", val)
             }
             ty::Uint(_) => {
-                let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty);
+                let val = ct.eval_bits(tcx, ty::ParamEnv::reveal_all(), ct.ty());
                 write!(output, "{}", val)
             }
             ty::Bool => {
@@ -610,7 +672,7 @@ fn push_const_param<'tcx>(tcx: TyCtxt<'tcx>, ct: &'tcx ty::Const<'tcx>, output:
                 let mut hasher = StableHasher::new();
                 hcx.while_hashing_spans(false, |hcx| {
                     hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
-                        ct.val.hash_stable(hcx, &mut hasher);
+                        ct.val().hash_stable(hcx, &mut hasher);
                     });
                 });
                 // Let's only emit 64 bits of the hash value. That should be plenty for