Skip to content

Commit 3ffe946

Browse files
authored
transpile: const macros: skip DeclRefs (#1387)
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. This should fix some of the issues surfaced in #1371 when the order of top-level decl being converted was changed.
2 parents bc70454 + bb60b4a commit 3ffe946

File tree

3 files changed

+79
-15
lines changed

3 files changed

+79
-15
lines changed

c2rust-transpile/src/translator/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3338,6 +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+
));
3348+
3349+
#[allow(unreachable_code)] // TODO temporary (see above).
33413350
if let CDeclKind::Variable {
33423351
has_static_duration: true,
33433352
..

c2rust-transpile/tests/snapshots/macros.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,3 +386,22 @@ int local_fn(void) { return 1234; }
386386
int use_local_value(void) { return LOCAL_VALUE; }
387387

388388
bool use_portable_type(uintptr_t len) { return len <= UINTPTR_MAX / 2; }
389+
390+
// From `curl`'s `curl_ntlm_core.c`.
391+
392+
struct ntlmdata {
393+
unsigned int target_info_len;
394+
};
395+
396+
// Should not translate since it references an out-of-scope `ntlm` variable.
397+
#define NTLMv2_BLOB_LEN (44 - 16 + ntlm->target_info_len + 4)
398+
399+
unsigned int ntlm_v2_blob_len(struct ntlmdata *ntlm) { return NTLMv2_BLOB_LEN; }
400+
401+
// The variable `i` lacks an initializer, but is still declared within the macro,
402+
// so it should be translated like `STMT_EXPR` is.
403+
#define LATE_INIT_VAR ({ int i; i = 1; i; })
404+
405+
int late_init_var() {
406+
return LATE_INIT_VAR;
407+
}

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

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ pub struct fn_ptrs {
2929
pub fn2: Option<unsafe extern "C" fn(core::ffi::c_int) -> core::ffi::c_int>,
3030
}
3131
pub type zstd_platform_dependent_type = core::ffi::c_long;
32+
#[derive(Copy, Clone)]
33+
#[repr(C)]
34+
pub struct ntlmdata {
35+
pub target_info_len: core::ffi::c_uint,
36+
}
3237
pub const UINTPTR_MAX: core::ffi::c_ulong = 18446744073709551615 as core::ffi::c_ulong;
3338
pub const true_0: core::ffi::c_int = 1 as core::ffi::c_int;
3439
pub const LITERAL_INT: core::ffi::c_int = 0xffff as core::ffi::c_int;
@@ -58,19 +63,6 @@ pub const PTR_ARITHMETIC: *const core::ffi::c_char = unsafe {
5863
};
5964
pub const WIDENING_CAST: core::ffi::c_ulonglong = LITERAL_INT as core::ffi::c_ulonglong;
6065
pub const CONVERSION_CAST: core::ffi::c_double = LITERAL_INT as core::ffi::c_double;
61-
pub const STMT_EXPR: core::ffi::c_float = ({
62-
let mut builtin: core::ffi::c_int = (LITERAL_INT as core::ffi::c_uint).leading_zeros() as i32;
63-
let mut indexing: core::ffi::c_char = NESTED_STR[LITERAL_FLOAT as core::ffi::c_int as usize];
64-
let mut mixed: core::ffi::c_float =
65-
(LITERAL_INT as core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as core::ffi::c_double
66-
- true_0 as core::ffi::c_double) as core::ffi::c_float;
67-
let mut i: core::ffi::c_int = 0 as core::ffi::c_int;
68-
while i < builtin {
69-
mixed += indexing as core::ffi::c_float;
70-
i += 1;
71-
}
72-
mixed
73-
});
7466
#[no_mangle]
7567
pub unsafe extern "C" fn local_muts() {
7668
let mut literal_int: core::ffi::c_int = LITERAL_INT;
@@ -123,7 +115,22 @@ pub unsafe extern "C" fn local_muts() {
123115
2 as core::ffi::c_int
124116
};
125117
let mut member: core::ffi::c_int = LITERAL_STRUCT.i;
126-
let mut stmt_expr: core::ffi::c_float = STMT_EXPR;
118+
let mut stmt_expr: core::ffi::c_float = ({
119+
let mut builtin_0: core::ffi::c_int =
120+
(LITERAL_INT as core::ffi::c_uint).leading_zeros() as i32;
121+
let mut indexing_0: core::ffi::c_char =
122+
NESTED_STR[LITERAL_FLOAT as core::ffi::c_int as usize];
123+
let mut mixed: core::ffi::c_float = (LITERAL_INT as core::ffi::c_double
124+
+ NESTED_FLOAT * LITERAL_CHAR as core::ffi::c_double
125+
- true_0 as core::ffi::c_double)
126+
as core::ffi::c_float;
127+
let mut i: core::ffi::c_int = 0 as core::ffi::c_int;
128+
while i < builtin_0 {
129+
mixed += indexing_0 as core::ffi::c_float;
130+
i += 1;
131+
}
132+
mixed
133+
});
127134
}
128135
#[no_mangle]
129136
pub unsafe extern "C" fn local_consts() {
@@ -177,7 +184,22 @@ pub unsafe extern "C" fn local_consts() {
177184
2 as core::ffi::c_int
178185
};
179186
let member: core::ffi::c_int = LITERAL_STRUCT.i;
180-
let stmt_expr: core::ffi::c_float = STMT_EXPR;
187+
let stmt_expr: core::ffi::c_float = ({
188+
let mut builtin_0: core::ffi::c_int =
189+
(LITERAL_INT as core::ffi::c_uint).leading_zeros() as i32;
190+
let mut indexing_0: core::ffi::c_char =
191+
NESTED_STR[LITERAL_FLOAT as core::ffi::c_int as usize];
192+
let mut mixed: core::ffi::c_float = (LITERAL_INT as core::ffi::c_double
193+
+ NESTED_FLOAT * LITERAL_CHAR as core::ffi::c_double
194+
- true_0 as core::ffi::c_double)
195+
as core::ffi::c_float;
196+
let mut i: core::ffi::c_int = 0 as core::ffi::c_int;
197+
while i < builtin_0 {
198+
mixed += indexing_0 as core::ffi::c_float;
199+
i += 1;
200+
}
201+
mixed
202+
});
181203
}
182204
static mut global_static_const_literal_int: core::ffi::c_int = LITERAL_INT;
183205
static mut global_static_const_literal_bool: bool = LITERAL_BOOL != 0;
@@ -393,6 +415,20 @@ pub unsafe extern "C" fn use_local_value() -> core::ffi::c_int {
393415
pub unsafe extern "C" fn use_portable_type(mut len: uintptr_t) -> bool {
394416
return len <= (UINTPTR_MAX as uintptr_t).wrapping_div(2 as uintptr_t);
395417
}
418+
#[no_mangle]
419+
pub unsafe extern "C" fn ntlm_v2_blob_len(mut ntlm: *mut ntlmdata) -> core::ffi::c_uint {
420+
return ((44 as core::ffi::c_int - 16 as core::ffi::c_int) as core::ffi::c_uint)
421+
.wrapping_add((*ntlm).target_info_len)
422+
.wrapping_add(4 as core::ffi::c_uint);
423+
}
424+
#[no_mangle]
425+
pub unsafe extern "C" fn late_init_var() -> core::ffi::c_int {
426+
return ({
427+
let mut i: core::ffi::c_int = 0;
428+
i = 1 as core::ffi::c_int;
429+
i
430+
});
431+
}
396432
unsafe extern "C" fn run_static_initializers() {
397433
global_static_const_ptr_arithmetic = PTR_ARITHMETIC;
398434
global_static_const_indexing = NESTED_STR[LITERAL_FLOAT as core::ffi::c_int as usize];

0 commit comments

Comments
 (0)