1
Fork 0

handle unsized consts with type str in v0 symbol mangling

This commit is contained in:
Jaic1 2024-09-14 16:48:23 +08:00
parent 506f22b466
commit c2ccd89ce4
2 changed files with 51 additions and 32 deletions

View file

@ -593,27 +593,18 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
let _ = write!(self.out, "{bits:x}_"); let _ = write!(self.out, "{bits:x}_");
} }
// FIXME(valtrees): Remove the special case for `str` // Handle `str` as partial support for unsized constants
// here and fully support unsized constants. ty::Str => {
ty::Ref(_, inner_ty, mutbl) => { let tcx = self.tcx();
self.push(match mutbl { // HACK(jaic1): hide the `str` type behind a reference
hir::Mutability::Not => "R", // for the following transformation from valtree to raw bytes
hir::Mutability::Mut => "Q", let ref_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, ct_ty);
let slice = valtree.try_to_raw_bytes(tcx, ref_ty).unwrap_or_else(|| {
bug!("expected to get raw bytes from valtree {:?} for type {:}", valtree, ct_ty)
}); });
let s = std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");
match inner_ty.kind() { // "e" for str as a basic type
ty::Str if mutbl.is_not() => {
let slice =
valtree.try_to_raw_bytes(self.tcx(), ct_ty).unwrap_or_else(|| {
bug!(
"expected to get raw bytes from valtree {:?} for type {:}",
valtree,
ct_ty
)
});
let s =
std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");
self.push("e"); self.push("e");
// FIXME(eddyb) use a specialized hex-encoding loop. // FIXME(eddyb) use a specialized hex-encoding loop.
@ -623,16 +614,20 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
self.push("_"); self.push("_");
} }
_ => {
let pointee_ty = ct_ty // FIXME(valtrees): Remove the special case for `str`
.builtin_deref(true) // here and fully support unsized constants.
.expect("tried to dereference on non-ptr type"); ty::Ref(_, _, mutbl) => {
let dereferenced_const = self.push(match mutbl {
ty::Const::new_value(self.tcx, valtree, pointee_ty); hir::Mutability::Not => "R",
hir::Mutability::Mut => "Q",
});
let pointee_ty =
ct_ty.builtin_deref(true).expect("tried to dereference on non-ptr type");
let dereferenced_const = ty::Const::new_value(self.tcx, valtree, pointee_ty);
dereferenced_const.print(self)?; dereferenced_const.print(self)?;
} }
}
}
ty::Array(..) | ty::Tuple(..) | ty::Adt(..) | ty::Slice(_) => { ty::Array(..) | ty::Tuple(..) | ty::Adt(..) | ty::Slice(_) => {
let contents = self.tcx.destructure_const(ct); let contents = self.tcx.destructure_const(ct);

View file

@ -0,0 +1,24 @@
//@ check-pass
//@ compile-flags: -Csymbol-mangling-version=v0
#![allow(incomplete_features)]
#![feature(unsized_const_params)]
// Regression test for #116303
#[derive(PartialEq, Eq)]
struct MyStr(str);
impl std::marker::UnsizedConstParamTy for MyStr {}
fn function_with_my_str<const S: &'static MyStr>() -> &'static MyStr {
S
}
impl MyStr {
const fn new(s: &'static str) -> &'static MyStr {
unsafe { std::mem::transmute(s) }
}
}
pub fn main() {
let f = function_with_my_str::<{ MyStr::new("hello") }>();
}