Skip to content

Commit 99a7779

Browse files
committed
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.
1 parent f6634cc commit 99a7779

File tree

3 files changed

+505
-7
lines changed

3 files changed

+505
-7
lines changed

compiler/rustc_symbol_mangling/src/legacy.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,14 +216,32 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
216216
Ok(self)
217217
}
218218

219-
fn print_type(self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
219+
fn print_type(mut self, ty: Ty<'tcx>) -> Result<Self::Type, Self::Error> {
220220
match *ty.kind() {
221221
// Print all nominal types as paths (unlike `pretty_print_type`).
222222
ty::FnDef(def_id, substs)
223223
| ty::Opaque(def_id, substs)
224224
| ty::Projection(ty::ProjectionTy { item_def_id: def_id, substs })
225225
| ty::Closure(def_id, substs)
226226
| ty::Generator(def_id, substs, _) => self.print_def_path(def_id, substs),
227+
228+
// The `pretty_print_type` formatting of array size depends on
229+
// -Zverbose flag, so we cannot reuse it here.
230+
ty::Array(ty, size) => {
231+
self.write_str("[")?;
232+
self = self.print_type(ty)?;
233+
self.write_str("; ")?;
234+
if let Some(size) = size.val().try_to_bits(self.tcx().data_layout.pointer_size) {
235+
write!(self, "{}", size)?
236+
} else if let ty::ConstKind::Param(param) = size.val() {
237+
self = param.print(self)?
238+
} else {
239+
self.write_str("_")?
240+
}
241+
self.write_str("]")?;
242+
Ok(self)
243+
}
244+
227245
_ => self.pretty_print_type(ty),
228246
}
229247
}
@@ -245,12 +263,22 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
245263

246264
fn print_const(self, ct: ty::Const<'tcx>) -> Result<Self::Const, Self::Error> {
247265
// only print integers
248-
if let ty::ConstKind::Value(ConstValue::Scalar(Scalar::Int { .. })) = ct.val() {
249-
if ct.ty().is_integral() {
250-
return self.pretty_print_const(ct, true);
266+
match (ct.val(), ct.ty().kind()) {
267+
(
268+
ty::ConstKind::Value(ConstValue::Scalar(Scalar::Int(scalar))),
269+
ty::Int(_) | ty::Uint(_),
270+
) => {
271+
// The `pretty_print_const` formatting depends on -Zverbose
272+
// flag, so we cannot reuse it here.
273+
let signed = matches!(ct.ty().kind(), ty::Int(_));
274+
write!(
275+
self,
276+
"{:#?}",
277+
ty::ConstInt::new(scalar, signed, ct.ty().is_ptr_sized_integral())
278+
)?;
251279
}
280+
_ => self.write_str("_")?,
252281
}
253-
self.write_str("_")?;
254282
Ok(self)
255283
}
256284

src/test/ui/symbol-names/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// build-fail
2-
// revisions: legacy
2+
// revisions: legacy verbose-legacy
33
// compile-flags: --crate-name=a -C symbol-mangling-version=legacy -Z unstable-options
4-
//
4+
//[verbose-legacy]compile-flags: -Zverbose
55
// normalize-stderr-test: "h[[:xdigit:]]{16}" -> "h[HASH]"
66

77
#![feature(never_type)]

0 commit comments

Comments
 (0)