Skip to content

Commit 16d0ee7

Browse files
authored
Unrolled build for rust-lang#130344
Rollup merge of rust-lang#130344 - Jaic1:fix-116306, r=BoxyUwU Handle unsized consts with type `str` in v0 symbol mangling This PR fixes rust-lang#116303 by handling consts with type `str` in v0 symbol mangling as partial support for unsized consts. This PR is related to `#![feature(adt_const_params)]` (rust-lang#95174) and `#![feature(unsized_const_params)]` (rust-lang#128028). r? ``@BoxyUwU``
2 parents 66b0b29 + c2ccd89 commit 16d0ee7

File tree

2 files changed

+51
-32
lines changed

2 files changed

+51
-32
lines changed

compiler/rustc_symbol_mangling/src/v0.rs

+27-32
Original file line numberDiff line numberDiff line change
@@ -607,45 +607,40 @@ impl<'tcx> Printer<'tcx> for SymbolMangler<'tcx> {
607607
let _ = write!(self.out, "{bits:x}_");
608608
}
609609

610+
// Handle `str` as partial support for unsized constants
611+
ty::Str => {
612+
let tcx = self.tcx();
613+
// HACK(jaic1): hide the `str` type behind a reference
614+
// for the following transformation from valtree to raw bytes
615+
let ref_ty = Ty::new_imm_ref(tcx, tcx.lifetimes.re_static, ct_ty);
616+
let slice = valtree.try_to_raw_bytes(tcx, ref_ty).unwrap_or_else(|| {
617+
bug!("expected to get raw bytes from valtree {:?} for type {:}", valtree, ct_ty)
618+
});
619+
let s = std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");
620+
621+
// "e" for str as a basic type
622+
self.push("e");
623+
624+
// FIXME(eddyb) use a specialized hex-encoding loop.
625+
for byte in s.bytes() {
626+
let _ = write!(self.out, "{byte:02x}");
627+
}
628+
629+
self.push("_");
630+
}
631+
610632
// FIXME(valtrees): Remove the special case for `str`
611633
// here and fully support unsized constants.
612-
ty::Ref(_, inner_ty, mutbl) => {
634+
ty::Ref(_, _, mutbl) => {
613635
self.push(match mutbl {
614636
hir::Mutability::Not => "R",
615637
hir::Mutability::Mut => "Q",
616638
});
617639

618-
match inner_ty.kind() {
619-
ty::Str if mutbl.is_not() => {
620-
let slice =
621-
valtree.try_to_raw_bytes(self.tcx(), ct_ty).unwrap_or_else(|| {
622-
bug!(
623-
"expected to get raw bytes from valtree {:?} for type {:}",
624-
valtree,
625-
ct_ty
626-
)
627-
});
628-
let s =
629-
std::str::from_utf8(slice).expect("non utf8 str from MIR interpreter");
630-
631-
self.push("e");
632-
633-
// FIXME(eddyb) use a specialized hex-encoding loop.
634-
for byte in s.bytes() {
635-
let _ = write!(self.out, "{byte:02x}");
636-
}
637-
638-
self.push("_");
639-
}
640-
_ => {
641-
let pointee_ty = ct_ty
642-
.builtin_deref(true)
643-
.expect("tried to dereference on non-ptr type");
644-
let dereferenced_const =
645-
ty::Const::new_value(self.tcx, valtree, pointee_ty);
646-
dereferenced_const.print(self)?;
647-
}
648-
}
640+
let pointee_ty =
641+
ct_ty.builtin_deref(true).expect("tried to dereference on non-ptr type");
642+
let dereferenced_const = ty::Const::new_value(self.tcx, valtree, pointee_ty);
643+
dereferenced_const.print(self)?;
649644
}
650645

651646
ty::Array(..) | ty::Tuple(..) | ty::Adt(..) | ty::Slice(_) => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//@ check-pass
2+
//@ compile-flags: -Csymbol-mangling-version=v0
3+
#![allow(incomplete_features)]
4+
#![feature(unsized_const_params)]
5+
6+
// Regression test for #116303
7+
8+
#[derive(PartialEq, Eq)]
9+
struct MyStr(str);
10+
impl std::marker::UnsizedConstParamTy for MyStr {}
11+
12+
fn function_with_my_str<const S: &'static MyStr>() -> &'static MyStr {
13+
S
14+
}
15+
16+
impl MyStr {
17+
const fn new(s: &'static str) -> &'static MyStr {
18+
unsafe { std::mem::transmute(s) }
19+
}
20+
}
21+
22+
pub fn main() {
23+
let f = function_with_my_str::<{ MyStr::new("hello") }>();
24+
}

0 commit comments

Comments
 (0)