Skip to content

Commit 6b52603

Browse files
committed
Support signed integers and char in v0 mangling
1 parent 1d27267 commit 6b52603

File tree

2 files changed

+107
-6
lines changed

2 files changed

+107
-6
lines changed

compiler/rustc_symbol_mangling/src/v0.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustc_data_structures::fx::{FxHashMap, FxHashSet};
44
use rustc_hir as hir;
55
use rustc_hir::def_id::{CrateNum, DefId};
66
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
7+
use rustc_middle::mir::interpret::sign_extend;
78
use rustc_middle::ty::print::{Print, Printer};
89
use rustc_middle::ty::subst::{GenericArg, GenericArgKind, Subst};
910
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable};
@@ -527,17 +528,30 @@ impl Printer<'tcx> for SymbolMangler<'tcx> {
527528
}
528529
let start = self.out.len();
529530

530-
match ct.ty.kind() {
531-
ty::Uint(_) => {}
532-
ty::Bool => {}
531+
let mut neg = false;
532+
let val = match ct.ty.kind() {
533+
ty::Uint(_) | ty::Bool | ty::Char => {
534+
ct.try_eval_bits(self.tcx, ty::ParamEnv::reveal_all(), ct.ty)
535+
}
536+
ty::Int(_) => {
537+
let param_env = ty::ParamEnv::reveal_all();
538+
ct.try_eval_bits(self.tcx, param_env, ct.ty).and_then(|b| {
539+
let sz = self.tcx.layout_of(param_env.and(ct.ty)).ok()?.size;
540+
let val = sign_extend(b, sz) as i128;
541+
if val < 0 {
542+
neg = true;
543+
}
544+
Some(val.wrapping_abs() as u128)
545+
})
546+
}
533547
_ => {
534548
bug!("symbol_names: unsupported constant of type `{}` ({:?})", ct.ty, ct);
535549
}
536-
}
550+
};
537551
self = ct.ty.print(self)?;
538552

539-
if let Some(bits) = ct.try_eval_bits(self.tcx, ty::ParamEnv::reveal_all(), ct.ty) {
540-
let _ = write!(self.out, "{:x}_", bits);
553+
if let Some(bits) = val {
554+
let _ = write!(self.out, "{}{:x}_", if neg { "n" } else { "" }, bits);
541555
} else {
542556
// NOTE(eddyb) despite having the path, we need to
543557
// encode a placeholder, as the path could refer
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// check-pass
2+
// revisions: legacy v0
3+
//[legacy]compile-flags: -Z symbol-mangling-version=legacy --crate-type=lib
4+
//[v0]compile-flags: -Z symbol-mangling-version=v0 --crate-type=lib
5+
6+
#![feature(min_const_generics)]
7+
8+
// `char`
9+
pub struct Char<const F: char>;
10+
11+
impl Char<'A'> {
12+
pub fn foo() {}
13+
}
14+
15+
impl<const F: char> Char<F> {
16+
pub fn bar() {}
17+
}
18+
19+
// `i8`
20+
pub struct I8<const F: i8>;
21+
22+
impl I8<{std::i8::MIN}> {
23+
pub fn foo() {}
24+
}
25+
26+
impl I8<{std::i8::MAX}> {
27+
pub fn foo() {}
28+
}
29+
30+
impl<const F: i8> I8<F> {
31+
pub fn bar() {}
32+
}
33+
34+
// `i16`
35+
pub struct I16<const F: i16>;
36+
37+
impl I16<{std::i16::MIN}> {
38+
pub fn foo() {}
39+
}
40+
41+
impl<const F: i16> I16<F> {
42+
pub fn bar() {}
43+
}
44+
45+
// `i32`
46+
pub struct I32<const F: i32>;
47+
48+
impl I32<{std::i32::MIN}> {
49+
pub fn foo() {}
50+
}
51+
52+
impl<const F: i32> I32<F> {
53+
pub fn bar() {}
54+
}
55+
56+
// `i64`
57+
pub struct I64<const F: i64>;
58+
59+
impl I64<{std::i64::MIN}> {
60+
pub fn foo() {}
61+
}
62+
63+
impl<const F: i64> I64<F> {
64+
pub fn bar() {}
65+
}
66+
67+
// `i128`
68+
pub struct I128<const F: i128>;
69+
70+
impl I128<{std::i128::MIN}> {
71+
pub fn foo() {}
72+
}
73+
74+
impl<const F: i128> I128<F> {
75+
pub fn bar() {}
76+
}
77+
78+
// `isize`
79+
pub struct ISize<const F: isize>;
80+
81+
impl ISize<3> {
82+
pub fn foo() {}
83+
}
84+
85+
impl<const F: isize> ISize<F> {
86+
pub fn bar() {}
87+
}

0 commit comments

Comments
 (0)