Skip to content

Commit e550d70

Browse files
authored
Merge pull request #1552 from N1ark/consts-rework-str
Move handling of `--unsized-strings` to `rustc_const_uneval`
2 parents 39bface + 4e17396 commit e550d70

11 files changed

Lines changed: 158 additions & 42 deletions

File tree

‎charon/rustc_const_uneval/src/lib.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ pub struct Config {
2929
/// Whether to refer to anonymous allocations (e.g. the bytes of `b"foo"`) as globals, or to
3030
/// inline their contents at each use.
3131
pub anon_allocs_as_globals: bool,
32+
/// Whether to view string data as `[u8; N]`, and make it a global.
33+
pub unsized_strings: bool,
3234
}
3335

3436
/// Evaluates and reads constants in a given typing environment.

‎charon/rustc_const_uneval/src/pointer.rs‎

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ impl<'tcx> ConstReader<'tcx> {
3636
} else {
3737
None
3838
};
39-
// A slice or `dyn Trait` value is viewed at the sized type it was unsized from. Other
40-
// unsized values (e.g. a `CStr`) have no such type: we read them at their unsized type.
41-
let view_ty = match ty.kind() {
42-
_ if place.layout.is_sized() => Some(ty),
43-
ty::Slice(_) | ty::Dynamic(..) => unsize.map(|unsize| unsize.from),
44-
_ => None,
39+
// The sized type at which we view the pointee, if any.
40+
let view_ty = match unsize {
41+
None => place.layout.is_sized().then_some(ty),
42+
Some(unsize) => self.is_viewed_unsized_from(ty).then_some(unsize.from),
4543
};
4644

4745
let alloc_target = self.alloc_target(alloc_id);
@@ -123,6 +121,16 @@ impl<'tcx> ConstReader<'tcx> {
123121
})
124122
}
125123

124+
/// Whether we view a value of the unsized type `ty` at the sized type it was unsized from: a
125+
/// slice or `dyn Trait` value is, and so is a `str` with `unsized_strings`.
126+
pub(crate) fn is_viewed_unsized_from(&self, ty: Ty<'tcx>) -> bool {
127+
match ty.kind() {
128+
ty::Slice(_) | ty::Dynamic(..) => true,
129+
ty::Str => self.config.unsized_strings,
130+
_ => false,
131+
}
132+
}
133+
126134
/// Express the place of type `ty` located `offset` bytes into `global` (whose allocation
127135
/// `place` points into) as projections from the global. `in_bounds` tells whether that place
128136
/// is inside the global (incl. one past its end). Returns the type of the global, the

‎charon/rustc_const_uneval/src/valtree.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ impl<'tcx> ConstReader<'tcx> {
3131
let len = fields.len() as u64;
3232
// The array the pointee was unsized from.
3333
let array_ty = Ty::new_array(tcx, inner_ty.sequence_element_type(tcx), len);
34-
// We read slices at that array type, but `str`s as themselves.
35-
let pointee_ty = match inner_ty.kind() {
36-
ty::Slice(_) => array_ty,
37-
_ => *inner_ty,
34+
let pointee_ty = if self.is_viewed_unsized_from(*inner_ty) {
35+
array_ty
36+
} else {
37+
*inner_ty
3838
};
3939
ConstKind::Ptr {
4040
target: PtrTarget::Inline(Box::new(read_at(pointee_ty, valtree)?)),

‎charon/src/bin/charon-driver/hax/constant_utils/uneval.rs‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,7 @@ fn const_reader<'tcx, S: UnderOwnerState<'tcx>>(s: &S) -> ConstReader<'tcx> {
121121
ConstReader {
122122
tcx: s.base().tcx,
123123
typing_env: s.typing_env(),
124-
config: rustc_const_uneval::Config {
125-
anon_allocs_as_globals: s.base().options.anon_allocs_as_globals,
126-
},
124+
config: s.base().options.consts,
127125
}
128126
}
129127

‎charon/src/bin/charon-driver/hax/mod.rs‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ pub mod options {
2626
/// blocks or advanced constant expressions as in `[T; N+1]`), or refer to them as
2727
/// `GlobalName`s.
2828
pub inline_anon_consts: bool,
29-
/// Whether to turn anonymous const-eval allocations (e.g. the bytes of `b"foo"`) into
30-
/// `RawMemory` globals, or to inline their contents at each use.
31-
pub anon_allocs_as_globals: bool,
29+
/// How to read evaluated constants.
30+
pub consts: rustc_const_uneval::Config,
3231
}
3332
}

‎charon/src/bin/charon-driver/translate/translate_constants.rs‎

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ impl<'tcx, 'ctx> ItemTransCtx<'tcx, 'ctx> {
1414
) -> Result<ConstantExprKind, Error> {
1515
Ok(match v {
1616
hax::ConstantLiteral::ByteStr(bs) => ConstantExprKind::ByteStr(bs.clone()),
17-
// The data backing a string, when we represent strings as unsized [u8]s.
18-
hax::ConstantLiteral::Str(str) if self.t_ctx.options.unsized_strings => {
19-
ConstantExprKind::RawMemory(str.bytes().map(Byte::Value).collect())
20-
}
2117
// A `str` value not behind a reference, e.g. the tail of a `str`-tailed DST
2218
hax::ConstantLiteral::Str(str) => {
2319
let ty_is_sized = self.translate_sized_proof(span, self.tcx.types.u8)?;
@@ -175,8 +171,7 @@ impl<'tcx, 'ctx> ItemTransCtx<'tcx, 'ctx> {
175171
..
176172
} if ty.is_ref()
177173
&& let hax::ConstantExprKind::Literal(hax::ConstantLiteral::Str(s)) =
178-
arg.contents.as_ref()
179-
&& !self.t_ctx.options.unsized_strings =>
174+
arg.contents.as_ref() =>
180175
{
181176
ConstantExprKind::Str(s.clone())
182177
}
@@ -191,20 +186,7 @@ impl<'tcx, 'ctx> ItemTransCtx<'tcx, 'ctx> {
191186
ty: self.translate_ty(span, base_ty)?,
192187
},
193188
hax::ConstantPtrBase::Value(arg) => {
194-
let mut val = self.translate_constant_expr(span, arg)?;
195-
// With `--unsized-strings`, a string literal is the `[u8; N]` behind the
196-
// `&str`.
197-
if ty.is_ref()
198-
&& let hax::ConstantExprKind::Literal(hax::ConstantLiteral::Str(s)) =
199-
arg.contents.as_ref()
200-
{
201-
let len = ConstantExpr::mk_usize(s.len() as u128);
202-
let ty_is_sized =
203-
self.translate_sized_proof(span, self.tcx.types.u8)?;
204-
let array_ty = Ty::mk_array(Ty::mk_u8(), len, ty_is_sized);
205-
val.with_contents_mut(|_, ty| *ty = array_ty);
206-
}
207-
GlobalPlace::new_value(val)
189+
GlobalPlace::new_value(self.translate_constant_expr(span, arg)?)
208190
}
209191
};
210192
for (proj, proj_ty) in projections {

‎charon/src/bin/charon-driver/translate/translate_crate.rs‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,10 +1130,13 @@ pub fn translate<'tcx>(
11301130
tcx,
11311131
hax::options::Options {
11321132
inline_anon_consts: !translate_options.raw_consts,
1133-
anon_allocs_as_globals: matches!(
1134-
translate_options.consts,
1135-
ConstHandling::Values | ConstHandling::Bytes
1136-
),
1133+
consts: rustc_const_uneval::Config {
1134+
anon_allocs_as_globals: matches!(
1135+
translate_options.consts,
1136+
ConstHandling::Values | ConstHandling::Bytes
1137+
),
1138+
unsized_strings: translate_options.unsized_strings,
1139+
},
11371140
},
11381141
hax::options::BoundsOptions {
11391142
add_destruct_bounds: translate_options.add_destruct_bounds,
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@ revisions=values,unsized_strings
2+
//@[values] charon-args=--consts=values
3+
//@[unsized_strings] charon-args=--consts=values --unsized-strings
4+
pub static STR: &str = "hello";
5+
pub static STR_SUFFIX: &str = "hello".split_at(1).1;
6+
pub static CSTR: &core::ffi::CStr = c"hi";
7+
pub struct W<T: ?Sized>(pub u8, pub T);
8+
pub static WS: W<[u8; 2]> = W(0, [1, 2]);
9+
pub static W_SLICE: &W<[u8]> = &WS;
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Final LLBC before serialization:
2+
3+
// Full name: core::ffi::c_str::CStr
4+
#[lang_item(CStr)]
5+
#[diagnostic_item("cstr_type")]
6+
pub opaque type CStr
7+
8+
// Full name: core::marker::MetaSized
9+
#[fundamental]
10+
#[lang_item(MetaSized)]
11+
pub trait MetaSized<Self>
12+
13+
// Full name: core::marker::Sized
14+
#[fundamental]
15+
#[lang_item(Sized)]
16+
pub trait Sized<Self>
17+
{
18+
proof ImpliedClause0: (Self: MetaSized)
19+
non-dyn-compatible
20+
}
21+
22+
// Full name: core::mem::maybe_uninit::MaybeUninit
23+
#[lang_item(MaybeUninit)]
24+
pub opaque type MaybeUninit<T>
25+
where
26+
TraitClause0: (T: Sized),
27+
28+
struct () {}
29+
30+
struct str {
31+
_0: [u8],
32+
}
33+
34+
// Full name: test_crate::{promoted_const}
35+
static {promoted_const}: MaybeUninit<[u8; 5usize]>[{built_in impl Sized for [u8; 5usize]}] = RawMemory(0x68, 0x65, 0x6c, 0x6c, 0x6f)
36+
37+
// Full name: test_crate::STR
38+
pub static STR: &'static str = unsize_cast<&'_ str, 5usize>(cast<*const [u8; 5usize]>(&raw const {promoted_const}))
39+
40+
// Full name: test_crate::STR_SUFFIX
41+
pub static STR_SUFFIX: &'static str = &{promoted_const}.offset!(1isize) with_metadata(4usize)
42+
43+
// Full name: test_crate::CSTR
44+
pub static CSTR: &'static CStr = &CStr { 0: [104i8, 105i8, 0i8] } with_metadata(3usize)
45+
46+
// Full name: test_crate::W
47+
pub struct W<T>
48+
where
49+
TraitClause0: (T: MetaSized),
50+
{
51+
_0: u8,
52+
_1: T,
53+
}
54+
55+
// Full name: test_crate::WS
56+
pub static WS: W<[u8; 2usize]>[{built_in impl MetaSized for [u8; 2usize]}] = W { _0: 0u8, _1: [1u8, 2u8] }
57+
58+
// Full name: test_crate::W_SLICE
59+
pub static W_SLICE: &'static W<[u8]>[{built_in impl MetaSized for [u8]}] = &W { _0: 0u8, _1: [1u8, 2u8] } with_metadata(2usize)
60+
61+
62+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Final LLBC before serialization:
2+
3+
// Full name: core::ffi::c_str::CStr
4+
#[lang_item(CStr)]
5+
#[diagnostic_item("cstr_type")]
6+
pub opaque type CStr
7+
8+
// Full name: core::marker::MetaSized
9+
#[fundamental]
10+
#[lang_item(MetaSized)]
11+
pub trait MetaSized<Self>
12+
13+
// Full name: core::marker::Sized
14+
#[fundamental]
15+
#[lang_item(Sized)]
16+
pub trait Sized<Self>
17+
{
18+
proof ImpliedClause0: (Self: MetaSized)
19+
non-dyn-compatible
20+
}
21+
22+
struct () {}
23+
24+
struct str {
25+
_0: [u8],
26+
}
27+
28+
// Full name: test_crate::STR
29+
pub static STR: &'static str = "hello"
30+
31+
// Full name: test_crate::STR_SUFFIX
32+
pub static STR_SUFFIX: &'static str = "ello"
33+
34+
// Full name: test_crate::CSTR
35+
pub static CSTR: &'static CStr = &CStr { 0: [104i8, 105i8, 0i8] } with_metadata(3usize)
36+
37+
// Full name: test_crate::W
38+
pub struct W<T>
39+
where
40+
TraitClause0: (T: MetaSized),
41+
{
42+
_0: u8,
43+
_1: T,
44+
}
45+
46+
// Full name: test_crate::WS
47+
pub static WS: W<[u8; 2usize]>[{built_in impl MetaSized for [u8; 2usize]}] = W { _0: 0u8, _1: [1u8, 2u8] }
48+
49+
// Full name: test_crate::W_SLICE
50+
pub static W_SLICE: &'static W<[u8]>[{built_in impl MetaSized for [u8]}] = &W { _0: 0u8, _1: [1u8, 2u8] } with_metadata(2usize)
51+
52+
53+

0 commit comments

Comments
 (0)