Skip to content

Commit 102f436

Browse files
committed
transpile: const macros: allow non-variable DeclRefs
for example, mentioning enum constants should be accepted in const contexts
1 parent 583b602 commit 102f436

File tree

3 files changed

+18
-7
lines changed

3 files changed

+18
-7
lines changed

c2rust-transpile/src/translator/mod.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3338,13 +3338,15 @@ impl<'c> Translation<'c> {
33383338
.ok_or_else(|| format_err!("Missing declref {:?}", decl_id))?
33393339
.kind;
33403340
if ctx.is_const {
3341-
// TODO Determining which declarations have been declared within the scope of the const macro expr
3342-
// vs. which are out-of-scope of the const macro is non-trivial,
3343-
// so for now, we don't allow const macros referencing any declarations.
3344-
return Err(format_translation_err!(
3345-
self.ast_context.display_loc(src_loc),
3346-
"Cannot yet refer to declarations in a const expr",
3347-
));
3341+
if let CDeclKind::Variable { .. } = decl {
3342+
// TODO Determining which variables have been declared within the scope of the const macro expr
3343+
// vs. which are out-of-scope of the const macro is non-trivial,
3344+
// so for now, we don't allow const macros referencing any variables.
3345+
return Err(format_translation_err!(
3346+
self.ast_context.display_loc(src_loc),
3347+
"Cannot yet refer to variables in a const expr",
3348+
));
3349+
}
33483350

33493351
#[allow(unreachable_code)] // TODO temporary (see above).
33503352
if let CDeclKind::Variable {

c2rust-transpile/tests/snapshots/macros.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,3 +405,7 @@ unsigned int ntlm_v2_blob_len(struct ntlmdata *ntlm) { return NTLMv2_BLOB_LEN; }
405405
int late_init_var() {
406406
return LATE_INIT_VAR;
407407
}
408+
409+
enum foo { Variant1, };
410+
#define VARIANT1 Variant1
411+
enum foo fooval = VARIANT1;

c2rust-transpile/tests/snapshots/[email protected]

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ pub type zstd_platform_dependent_type = core::ffi::c_long;
3434
pub struct ntlmdata {
3535
pub target_info_len: core::ffi::c_uint,
3636
}
37+
pub type foo = core::ffi::c_uint;
38+
pub const Variant1: foo = 0;
3739
pub const UINTPTR_MAX: core::ffi::c_ulong = 18446744073709551615 as core::ffi::c_ulong;
3840
pub const true_0: core::ffi::c_int = 1 as core::ffi::c_int;
3941
pub const LITERAL_INT: core::ffi::c_int = 0xffff as core::ffi::c_int;
@@ -429,6 +431,9 @@ pub unsafe extern "C" fn late_init_var() -> core::ffi::c_int {
429431
i
430432
});
431433
}
434+
pub const VARIANT1: core::ffi::c_int = Variant1 as core::ffi::c_int;
435+
#[no_mangle]
436+
pub static mut fooval: foo = VARIANT1 as foo;
432437
unsafe extern "C" fn run_static_initializers() {
433438
global_static_const_ptr_arithmetic = PTR_ARITHMETIC;
434439
global_static_const_indexing = NESTED_STR[LITERAL_FLOAT as core::ffi::c_int as usize];

0 commit comments

Comments
 (0)