Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix inconsistent symbol mangling of integers constants with -Zverbose
The `PrettyPrinter` changes formatting of array size and integer
constants based on `-Zverbose`, so its implementation cannot be used in
legacy symbol mangling.
  • Loading branch information
tmiasko committed Feb 28, 2022
commit 99a77798d3b78f3ce4e92ac6768110682ec8b664
38 changes: 33 additions & 5 deletions compiler/rustc_symbol_mangling/src/legacy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,14 +216,32 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
Ok(self)
}

fn print_type(self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
match *ty.kind() {
// Print all nominal types as paths (unlike `pretty_print_type`).
ty::FnDef(def_id, substs)
| ty::Opaque(def_id, substs)
| ty::Projection(ty::ProjectionTy { item_def_id: def_id, substs })
| ty::Closure(def_id, substs)
| ty::Generator(def_id, substs, _) => self.print_def_path(def_id, substs),

// The `pretty_print_type` formatting of array size depends on
// -Zverbose flag, so we cannot reuse it here.
ty::Array(ty, size) => {
self.write_str("[")?;
self = self.print_type(ty)?;
self.write_str("; ")?;
if let Some(size) = size.val().try_to_bits(self.tcx().data_layout.pointer_size) {
write!(self, "{}", size)?
} else if let ty::ConstKind::Param(param) = size.val() {
self = param.print(self)?
} else {
self.write_str("_")?
}
self.write_str("]")?;
Ok(self)
}

_ => self.pretty_print_type(ty),
}
}
Expand All @@ -245,12 +263,22 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {

fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
// only print integers
if let ty::ConstKind::Value(ConstValue::Scalar(Scalar::Int { .. })) = ct.val() {
if ct.ty().is_integral() {
return self.pretty_print_const(ct, true);
match (ct.val(), ct.ty().kind()) {
(
ty::ConstKind::Value(ConstValue::Scalar(Scalar::Int(scalar))),
ty::Int(_) | ty::Uint(_),
) => {
// The `pretty_print_const` formatting depends on -Zverbose
// flag, so we cannot reuse it here.
let signed = matches!(ct.ty().kind(), ty::Int(_));
write!(
self,
"{:#?}",
ty::ConstInt::new(scalar, signed, ct.ty().is_ptr_sized_integral())
)?;
}
_ => self.write_str("_")?,
}
self.write_str("_")?;
Ok(self)
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/symbol-names/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// build-fail
// revisions: legacy
// revisions: legacy verbose-legacy
// compile-flags: --crate-name=a -C symbol-mangling-version=legacy -Z unstable-options
//
//[verbose-legacy]compile-flags: -Zverbose
// normalize-stderr-test: "h[[:xdigit:]]{16}" -> "h[HASH]"

#![feature(never_type)]
Expand Down
Loading