Skip to content

Commit f0a1c8e

Browse files
committed
Fix conversion to StaticDef and add test
1 parent c2bdfb3 commit f0a1c8e

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

compiler/stable_mir/src/compiler_interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ pub trait Context {
156156
fn krate(&self, def_id: DefId) -> Crate;
157157
fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol;
158158

159-
/// Return the number of bytes for a pointer size.
159+
/// Return information about the target machine.
160160
fn target_info(&self) -> MachineInfo;
161161
}
162162

compiler/stable_mir/src/mir/mono.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ impl TryFrom<CrateItem> for StaticDef {
219219
type Error = crate::Error;
220220

221221
fn try_from(value: CrateItem) -> Result<Self, Self::Error> {
222-
if matches!(value.kind(), ItemKind::Static | ItemKind::Const) {
222+
if matches!(value.kind(), ItemKind::Static) {
223223
Ok(StaticDef(value.0))
224224
} else {
225225
Err(Error::new(format!("Expected a static item, but found: {value:?}")))

tests/ui-fulldeps/stable-mir/check_allocation.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ fn test_stable_mir(_tcx: TyCtxt<'_>) -> ControlFlow<()> {
4040
let items = stable_mir::all_local_items();
4141
check_foo(*get_item(&items, (ItemKind::Static, "FOO")).unwrap());
4242
check_bar(*get_item(&items, (ItemKind::Static, "BAR")).unwrap());
43+
check_len(*get_item(&items, (ItemKind::Static, "LEN")).unwrap());
4344
ControlFlow::Continue(())
4445
}
4546

@@ -76,6 +77,19 @@ fn check_bar(item: CrateItem) {
7677
assert_eq!(allocation.bytes[0].unwrap(), Char::CapitalB.to_u8());
7778
assert_eq!(allocation.bytes[1].unwrap(), Char::SmallA.to_u8());
7879
assert_eq!(allocation.bytes[2].unwrap(), Char::SmallR.to_u8());
80+
assert_eq!(std::str::from_utf8(&allocation.raw_bytes().unwrap()), Ok("Bar"));
81+
}
82+
83+
/// Check the allocation data for `LEN`.
84+
///
85+
/// ```no_run
86+
/// static LEN: usize = 2;
87+
/// ```
88+
fn check_len(item: CrateItem) {
89+
let def = StaticDef::try_from(item).unwrap();
90+
let alloc = def.eval_initializer().unwrap();
91+
assert!(alloc.provenance.ptrs.is_empty());
92+
assert_eq!(alloc.read_uint(), Ok(2));
7993
}
8094

8195
// Use internal API to find a function in a crate.
@@ -109,11 +123,13 @@ fn generate_input(path: &str) -> std::io::Result<()> {
109123
write!(
110124
file,
111125
r#"
126+
static LEN: usize = 2;
112127
static FOO: [&str; 2] = ["hi", "there"];
113128
static BAR: &str = "Bar";
114129
115130
pub fn main() {{
116131
println!("{{FOO:?}}! {{BAR}}");
132+
assert_eq!(FOO.len(), LEN);
117133
}}"#
118134
)?;
119135
Ok(())

0 commit comments

Comments
 (0)