Allow static initializers to be const evaluated

This commit is contained in:
Lukas Wirth 2024-11-02 12:43:11 +01:00
parent d1fbfc676b
commit 94c35f6138
7 changed files with 87 additions and 28 deletions

View file

@ -486,13 +486,19 @@ pub(super) fn definition(
}
}
Definition::Static(it) => {
let source = it.source(db)?;
let mut body = source.value.body()?.syntax().clone();
if let Some(macro_file) = source.file_id.macro_file() {
let span_map = db.expansion_span_map(macro_file);
body = prettify_macro_expansion(db, body, &span_map, it.krate(db).into());
let body = it.render_eval(db, edition);
match body {
Ok(it) => Some(it),
Err(_) => {
let source = it.source(db)?;
let mut body = source.value.body()?.syntax().clone();
if let Some(macro_file) = source.file_id.macro_file() {
let span_map = db.expansion_span_map(macro_file);
body = prettify_macro_expansion(db, body, &span_map, it.krate(db).into());
}
Some(body.to_string())
}
}
Some(body.to_string())
}
_ => None,
};

View file

@ -1544,7 +1544,7 @@ const foo$0: u32 = {
```
```rust
static foo: u32 = 456
static foo: u32 = 456 (0x1C8)
```
"#]],
);