Skip to content

Commit 9cb6463

Browse files
committed
Fix conversion to StaticDef and add test
1 parent 4c9e842 commit 9cb6463

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

compiler/stable_mir/src/compiler_interface.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub trait Context {
162162
fn krate(&self, def_id: DefId) -> Crate;
163163
fn instance_name(&self, def: InstanceDef, trimmed: bool) -> Symbol;
164164

165-
/// Return the number of bytes for a pointer size.
165+
/// Return information about the target machine.
166166
fn target_info(&self) -> MachineInfo;
167167
}
168168

compiler/stable_mir/src/mir/mono.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl TryFrom<CrateItem> for StaticDef {
220220
type Error = crate::Error;
221221

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

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

+16
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)