Skip to content

Commit eb21b12

Browse files
committed
transpile: const macros: allow non-variable DeclRefs
for example, mentioning functions should be accepted even in const contexts
1 parent 583b602 commit eb21b12

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-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+
void to_be_compared(void) {}
410+
#define FUNCTION_COMPARISON to_be_compared == to_be_compared
411+
bool function_comparison = FUNCTION_COMPARISON;

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,13 @@ pub unsafe extern "C" fn late_init_var() -> core::ffi::c_int {
429429
i
430430
});
431431
}
432+
#[no_mangle]
433+
pub unsafe extern "C" fn to_be_compared() {}
434+
pub const FUNCTION_COMPARISON: core::ffi::c_int =
435+
(Some(to_be_compared as unsafe extern "C" fn() -> ())
436+
== Some(to_be_compared as unsafe extern "C" fn() -> ())) as core::ffi::c_int;
437+
#[no_mangle]
438+
pub static mut function_comparison: bool = unsafe { FUNCTION_COMPARISON != 0 };
432439
unsafe extern "C" fn run_static_initializers() {
433440
global_static_const_ptr_arithmetic = PTR_ARITHMETIC;
434441
global_static_const_indexing = NESTED_STR[LITERAL_FLOAT as core::ffi::c_int as usize];

0 commit comments

Comments
 (0)