Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions c2rust-transpile/src/translator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3338,13 +3338,15 @@ impl<'c> Translation<'c> {
.ok_or_else(|| format_err!("Missing declref {:?}", decl_id))?
.kind;
if ctx.is_const {
// TODO Determining which declarations have been declared within the scope of the const macro expr
// vs. which are out-of-scope of the const macro is non-trivial,
// so for now, we don't allow const macros referencing any declarations.
return Err(format_translation_err!(
self.ast_context.display_loc(src_loc),
"Cannot yet refer to declarations in a const expr",
));
if let CDeclKind::Variable { .. } = decl {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't do this initially because I wasn't sure about other declarations within a function and how that interacted with global scope, so I wanted to err on the safer side, since this is supposed to be temporary.

// TODO Determining which variables have been declared within the scope of the const macro expr
// vs. which are out-of-scope of the const macro is non-trivial,
// so for now, we don't allow const macros referencing any variables.
return Err(format_translation_err!(
self.ast_context.display_loc(src_loc),
"Cannot yet refer to variables in a const expr",
));
}

#[allow(unreachable_code)] // TODO temporary (see above).
if let CDeclKind::Variable {
Expand Down
4 changes: 4 additions & 0 deletions c2rust-transpile/tests/snapshots/macros.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,3 +405,7 @@ unsigned int ntlm_v2_blob_len(struct ntlmdata *ntlm) { return NTLMv2_BLOB_LEN; }
int late_init_var() {
return LATE_INIT_VAR;
}

enum foo { Variant1, };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work if this is defined within a function, the macro is defined outside of it, and used within it?

#define VARIANT1 Variant1
enum foo fooval = VARIANT1;
5 changes: 5 additions & 0 deletions c2rust-transpile/tests/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub type zstd_platform_dependent_type = core::ffi::c_long;
pub struct ntlmdata {
pub target_info_len: core::ffi::c_uint,
}
pub type foo = core::ffi::c_uint;
pub const Variant1: foo = 0;
pub const UINTPTR_MAX: core::ffi::c_ulong = 18446744073709551615 as core::ffi::c_ulong;
pub const true_0: core::ffi::c_int = 1 as core::ffi::c_int;
pub const LITERAL_INT: core::ffi::c_int = 0xffff as core::ffi::c_int;
Expand Down Expand Up @@ -429,6 +431,9 @@ pub unsafe extern "C" fn late_init_var() -> core::ffi::c_int {
i
});
}
pub const VARIANT1: core::ffi::c_int = Variant1 as core::ffi::c_int;
#[no_mangle]
pub static mut fooval: foo = VARIANT1 as foo;
unsafe extern "C" fn run_static_initializers() {
global_static_const_ptr_arithmetic = PTR_ARITHMETIC;
global_static_const_indexing = NESTED_STR[LITERAL_FLOAT as core::ffi::c_int as usize];
Expand Down