Skip to content

Commit 2066245

Browse files
committed
transpile: Pass override_ty in recreate_const_macro_from_expansions
1 parent d5f2c3e commit 2066245

3 files changed

Lines changed: 76 additions & 78 deletions

File tree

c2rust-transpile/src/translator/mod.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,11 +2707,12 @@ impl<'c> Translation<'c> {
27072707
.try_fold::<Option<(WithStmts<Box<Expr>>, CTypeId)>, _, _>(None, |canonical, &id| {
27082708
self.can_convert_const_macro_expansion(id)?;
27092709

2710-
let ty = self.ast_context[id]
2711-
.kind
2712-
.get_type()
2713-
.ok_or_else(|| format_err!("Invalid expression type"))?;
2714-
let expr = self.convert_expr(ctx, id, None)?;
2710+
let override_ty = self.expr_override_types.get(&id).copied();
2711+
let expr = self.convert_expr(ctx, id, override_ty)?;
2712+
let ty = override_ty
2713+
.or_else(|| self.ast_context[id].kind.get_qual_type())
2714+
.ok_or_else(|| format_err!("Invalid expression type"))?
2715+
.ctype;
27152716

27162717
// Join ty and cur_ty to the smaller of the two types. If the
27172718
// types are not cast-compatible, abort the fold.
@@ -4560,13 +4561,12 @@ impl<'c> Translation<'c> {
45604561

45614562
let val = WithStmts::new_val(mk().path_expr(vec![rust_name]));
45624563

4563-
let expr_kind = &self.ast_context[expr_id].kind;
4564-
// TODO We'd like to get rid of this cast eventually (see #1321).
4565-
// Currently, const macros do not get the correct `override_ty` themselves,
4566-
// so they aren't declared with the correct portable type,
4567-
// but its uses are expecting the correct portable type,
4568-
// so we need to cast it to the `override_ty` here.
4569-
let expr_ty = override_ty.or_else(|| expr_kind.get_qual_type());
4564+
// Rust `const` variables have a single consistent type, determined by
4565+
// `recreate_const_macro_from_expansions`, while in C each macro expansion has its own type,
4566+
// determined by the surrounding context.
4567+
// Since the expansion sites are expecting a particular type, we need to cast it here
4568+
// if it differs from the `const` type.
4569+
let expr_ty = override_ty.or_else(|| self.ast_context[expr_id].kind.get_qual_type());
45704570
if let Some(expr_ty) = expr_ty {
45714571
self.convert_cast(
45724572
ctx,

c2rust-transpile/tests/snapshots/snapshots__transpile@macros.c.2021.snap

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ pub struct ntlmdata {
3636
pub target_info_len: ::core::ffi::c_uint,
3737
}
3838
pub const true_0: ::core::ffi::c_int = 1 as ::core::ffi::c_int;
39-
pub const UINTPTR_MAX: ::core::ffi::c_ulong = 18446744073709551615 as ::core::ffi::c_ulong;
39+
pub const UINTPTR_MAX: uintptr_t = 18446744073709551615 as uintptr_t;
4040
pub const LITERAL_INT: ::core::ffi::c_int = 0xffff as ::core::ffi::c_int;
4141
pub const LITERAL_BOOL: ::core::ffi::c_int = true_0;
42-
pub const LITERAL_FLOAT: ::core::ffi::c_double = 3.14f64;
42+
pub const LITERAL_FLOAT: ::core::ffi::c_float = 3.14f32;
4343
pub const LITERAL_CHAR: ::core::ffi::c_int = 'x' as i32;
4444
pub const LITERAL_STR: [::core::ffi::c_char; 6] =
4545
unsafe { ::core::mem::transmute::<[u8; 6], [::core::ffi::c_char; 6]>(*b"hello\0") };
@@ -48,7 +48,7 @@ pub const LITERAL_STRUCT: S = S {
4848
};
4949
pub const NESTED_INT: ::core::ffi::c_int = LITERAL_INT;
5050
pub const NESTED_BOOL: ::core::ffi::c_int = LITERAL_BOOL;
51-
pub const NESTED_FLOAT: ::core::ffi::c_double = LITERAL_FLOAT;
51+
pub const NESTED_FLOAT: ::core::ffi::c_float = LITERAL_FLOAT;
5252
pub const NESTED_CHAR: ::core::ffi::c_int = LITERAL_CHAR;
5353
pub const NESTED_STR: [::core::ffi::c_char; 6] = LITERAL_STR;
5454
pub const NESTED_STRUCT: S = LITERAL_STRUCT;
@@ -66,7 +66,7 @@ pub unsafe extern "C" fn local_muts() {
6666
unsafe {
6767
let mut literal_int: ::core::ffi::c_int = LITERAL_INT;
6868
let mut literal_bool: bool = LITERAL_BOOL != 0;
69-
let mut literal_float: ::core::ffi::c_float = LITERAL_FLOAT as ::core::ffi::c_float;
69+
let mut literal_float: ::core::ffi::c_float = LITERAL_FLOAT;
7070
let mut literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char;
7171
let mut literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr();
7272
let mut literal_str: [::core::ffi::c_char; 6] = LITERAL_STR;
@@ -78,7 +78,7 @@ pub unsafe extern "C" fn local_muts() {
7878
let mut literal_struct: S = LITERAL_STRUCT;
7979
let mut nested_int: ::core::ffi::c_int = NESTED_INT;
8080
let mut nested_bool: bool = NESTED_BOOL != 0;
81-
let mut nested_float: ::core::ffi::c_float = NESTED_FLOAT as ::core::ffi::c_float;
81+
let mut nested_float: ::core::ffi::c_float = NESTED_FLOAT;
8282
let mut nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char;
8383
let mut nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr();
8484
let mut nested_str: [::core::ffi::c_char; 6] = NESTED_STR;
@@ -91,7 +91,7 @@ pub unsafe extern "C" fn local_muts() {
9191
let mut int_arithmetic: ::core::ffi::c_int =
9292
NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int;
9393
let mut mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double
94-
+ NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double
94+
+ NESTED_FLOAT as ::core::ffi::c_double * LITERAL_CHAR as ::core::ffi::c_double
9595
- true_0 as ::core::ffi::c_double)
9696
as ::core::ffi::c_float;
9797
let mut parens: ::core::ffi::c_int = PARENS;
@@ -100,7 +100,7 @@ pub unsafe extern "C" fn local_muts() {
100100
let mut narrowing_cast: ::core::ffi::c_char = LITERAL_INT as ::core::ffi::c_char;
101101
let mut conversion_cast: ::core::ffi::c_double = CONVERSION_CAST;
102102
let mut indexing: ::core::ffi::c_char =
103-
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize];
103+
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_double as ::core::ffi::c_int as usize];
104104
let mut str_concatenation_ptr: *const ::core::ffi::c_char =
105105
b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char;
106106
let mut str_concatenation: [::core::ffi::c_char; 18] =
@@ -109,7 +109,7 @@ pub unsafe extern "C" fn local_muts() {
109109
(LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32;
110110
let mut ref_indexing: *const ::core::ffi::c_char = NESTED_STR
111111
.as_ptr()
112-
.offset(LITERAL_FLOAT as ::core::ffi::c_int as isize)
112+
.offset(LITERAL_FLOAT as ::core::ffi::c_double as ::core::ffi::c_int as isize)
113113
as *const ::core::ffi::c_char;
114114
let mut ref_struct: *const S = &mut LITERAL_STRUCT as *mut S;
115115
let mut ternary: ::core::ffi::c_int = if LITERAL_BOOL != 0 {
@@ -122,9 +122,9 @@ pub unsafe extern "C" fn local_muts() {
122122
let mut builtin_0: ::core::ffi::c_int =
123123
(LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32;
124124
let mut indexing_0: ::core::ffi::c_char =
125-
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize];
125+
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_double as ::core::ffi::c_int as usize];
126126
let mut mixed: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double
127-
+ NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double
127+
+ NESTED_FLOAT as ::core::ffi::c_double * LITERAL_CHAR as ::core::ffi::c_double
128128
- true_0 as ::core::ffi::c_double)
129129
as ::core::ffi::c_float;
130130
let mut i: ::core::ffi::c_int = 0 as ::core::ffi::c_int;
@@ -141,7 +141,7 @@ pub unsafe extern "C" fn local_consts() {
141141
unsafe {
142142
let literal_int: ::core::ffi::c_int = LITERAL_INT;
143143
let literal_bool: bool = LITERAL_BOOL != 0;
144-
let literal_float: ::core::ffi::c_float = LITERAL_FLOAT as ::core::ffi::c_float;
144+
let literal_float: ::core::ffi::c_float = LITERAL_FLOAT;
145145
let literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char;
146146
let literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr();
147147
let literal_str: [::core::ffi::c_char; 6] = LITERAL_STR;
@@ -153,7 +153,7 @@ pub unsafe extern "C" fn local_consts() {
153153
let literal_struct: S = LITERAL_STRUCT;
154154
let nested_int: ::core::ffi::c_int = NESTED_INT;
155155
let nested_bool: bool = NESTED_BOOL != 0;
156-
let nested_float: ::core::ffi::c_float = NESTED_FLOAT as ::core::ffi::c_float;
156+
let nested_float: ::core::ffi::c_float = NESTED_FLOAT;
157157
let nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char;
158158
let nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr();
159159
let nested_str: [::core::ffi::c_char; 6] = NESTED_STR;
@@ -165,7 +165,7 @@ pub unsafe extern "C" fn local_consts() {
165165
let nested_struct: S = NESTED_STRUCT;
166166
let int_arithmetic: ::core::ffi::c_int = NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int;
167167
let mixed_arithmetic: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double
168-
+ NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double
168+
+ NESTED_FLOAT as ::core::ffi::c_double * LITERAL_CHAR as ::core::ffi::c_double
169169
- true_0 as ::core::ffi::c_double)
170170
as ::core::ffi::c_float;
171171
let parens: ::core::ffi::c_int = PARENS;
@@ -174,7 +174,7 @@ pub unsafe extern "C" fn local_consts() {
174174
let narrowing_cast: ::core::ffi::c_char = LITERAL_INT as ::core::ffi::c_char;
175175
let conversion_cast: ::core::ffi::c_double = CONVERSION_CAST;
176176
let indexing: ::core::ffi::c_char =
177-
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize];
177+
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_double as ::core::ffi::c_int as usize];
178178
let str_concatenation_ptr: *const ::core::ffi::c_char =
179179
b"hello hello world\0".as_ptr() as *const ::core::ffi::c_char;
180180
let str_concatenation: [::core::ffi::c_char; 18] =
@@ -183,7 +183,7 @@ pub unsafe extern "C" fn local_consts() {
183183
(LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32;
184184
let ref_indexing: *const ::core::ffi::c_char = NESTED_STR
185185
.as_ptr()
186-
.offset(LITERAL_FLOAT as ::core::ffi::c_int as isize)
186+
.offset(LITERAL_FLOAT as ::core::ffi::c_double as ::core::ffi::c_int as isize)
187187
as *const ::core::ffi::c_char;
188188
let ref_struct: *const S = &mut LITERAL_STRUCT as *mut S;
189189
let ternary: ::core::ffi::c_int = if LITERAL_BOOL != 0 {
@@ -196,9 +196,9 @@ pub unsafe extern "C" fn local_consts() {
196196
let mut builtin_0: ::core::ffi::c_int =
197197
(LITERAL_INT as ::core::ffi::c_uint).leading_zeros() as i32;
198198
let mut indexing_0: ::core::ffi::c_char =
199-
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize];
199+
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_double as ::core::ffi::c_int as usize];
200200
let mut mixed: ::core::ffi::c_float = (LITERAL_INT as ::core::ffi::c_double
201-
+ NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double
201+
+ NESTED_FLOAT as ::core::ffi::c_double * LITERAL_CHAR as ::core::ffi::c_double
202202
- true_0 as ::core::ffi::c_double)
203203
as ::core::ffi::c_float;
204204
let mut i: ::core::ffi::c_int = 0 as ::core::ffi::c_int;
@@ -212,8 +212,7 @@ pub unsafe extern "C" fn local_consts() {
212212
}
213213
static mut global_static_const_literal_int: ::core::ffi::c_int = LITERAL_INT;
214214
static mut global_static_const_literal_bool: bool = LITERAL_BOOL != 0;
215-
static mut global_static_const_literal_float: ::core::ffi::c_float =
216-
LITERAL_FLOAT as ::core::ffi::c_float;
215+
static mut global_static_const_literal_float: ::core::ffi::c_float = LITERAL_FLOAT;
217216
static mut global_static_const_literal_char: ::core::ffi::c_char =
218217
LITERAL_CHAR as ::core::ffi::c_char;
219218
static mut global_static_const_literal_str_ptr: *const ::core::ffi::c_char = LITERAL_STR.as_ptr();
@@ -226,8 +225,7 @@ static mut global_static_const_literal_array: [::core::ffi::c_int; 3] = [
226225
static mut global_static_const_literal_struct: S = LITERAL_STRUCT;
227226
static mut global_static_const_nested_int: ::core::ffi::c_int = NESTED_INT;
228227
static mut global_static_const_nested_bool: bool = NESTED_BOOL != 0;
229-
static mut global_static_const_nested_float: ::core::ffi::c_float =
230-
NESTED_FLOAT as ::core::ffi::c_float;
228+
static mut global_static_const_nested_float: ::core::ffi::c_float = NESTED_FLOAT;
231229
static mut global_static_const_nested_char: ::core::ffi::c_char =
232230
NESTED_CHAR as ::core::ffi::c_char;
233231
static mut global_static_const_nested_str_ptr: *const ::core::ffi::c_char = NESTED_STR.as_ptr();
@@ -241,7 +239,8 @@ static mut global_static_const_nested_struct: S = NESTED_STRUCT;
241239
static mut global_static_const_int_arithmetic: ::core::ffi::c_int =
242240
NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int;
243241
static mut global_static_const_mixed_arithmetic: ::core::ffi::c_float =
244-
(LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double
242+
(LITERAL_INT as ::core::ffi::c_double
243+
+ NESTED_FLOAT as ::core::ffi::c_double * LITERAL_CHAR as ::core::ffi::c_double
245244
- true_0 as ::core::ffi::c_double) as ::core::ffi::c_float;
246245
static mut global_static_const_parens: ::core::ffi::c_int = PARENS;
247246
static mut global_static_const_ptr_arithmetic: *const ::core::ffi::c_char =
@@ -270,8 +269,7 @@ pub static mut global_const_literal_int: ::core::ffi::c_int = LITERAL_INT;
270269
#[no_mangle]
271270
pub static mut global_const_literal_bool: bool = LITERAL_BOOL != 0;
272271
#[no_mangle]
273-
pub static mut global_const_literal_float: ::core::ffi::c_float =
274-
LITERAL_FLOAT as ::core::ffi::c_float;
272+
pub static mut global_const_literal_float: ::core::ffi::c_float = LITERAL_FLOAT;
275273
#[no_mangle]
276274
pub static mut global_const_literal_char: ::core::ffi::c_char = LITERAL_CHAR as ::core::ffi::c_char;
277275
#[no_mangle]
@@ -291,8 +289,7 @@ pub static mut global_const_nested_int: ::core::ffi::c_int = NESTED_INT;
291289
#[no_mangle]
292290
pub static mut global_const_nested_bool: bool = NESTED_BOOL != 0;
293291
#[no_mangle]
294-
pub static mut global_const_nested_float: ::core::ffi::c_float =
295-
NESTED_FLOAT as ::core::ffi::c_float;
292+
pub static mut global_const_nested_float: ::core::ffi::c_float = NESTED_FLOAT;
296293
#[no_mangle]
297294
pub static mut global_const_nested_char: ::core::ffi::c_char = NESTED_CHAR as ::core::ffi::c_char;
298295
#[no_mangle]
@@ -312,7 +309,8 @@ pub static mut global_const_int_arithmetic: ::core::ffi::c_int =
312309
NESTED_INT + LITERAL_INT + 1 as ::core::ffi::c_int;
313310
#[no_mangle]
314311
pub static mut global_const_mixed_arithmetic: ::core::ffi::c_float =
315-
(LITERAL_INT as ::core::ffi::c_double + NESTED_FLOAT * LITERAL_CHAR as ::core::ffi::c_double
312+
(LITERAL_INT as ::core::ffi::c_double
313+
+ NESTED_FLOAT as ::core::ffi::c_double * LITERAL_CHAR as ::core::ffi::c_double
316314
- true_0 as ::core::ffi::c_double) as ::core::ffi::c_float;
317315
#[no_mangle]
318316
pub static mut global_const_parens: ::core::ffi::c_int = PARENS;
@@ -447,8 +445,7 @@ pub unsafe extern "C" fn use_local_value() -> ::core::ffi::c_int {
447445
#[no_mangle]
448446
pub unsafe extern "C" fn use_portable_type(mut len: uintptr_t) -> bool {
449447
unsafe {
450-
return len
451-
<= (UINTPTR_MAX as uintptr_t).wrapping_div(2 as ::core::ffi::c_int as uintptr_t);
448+
return len <= UINTPTR_MAX.wrapping_div(2 as ::core::ffi::c_int as uintptr_t);
452449
}
453450
}
454451
#[no_mangle]
@@ -472,10 +469,11 @@ pub unsafe extern "C" fn late_init_var() -> ::core::ffi::c_int {
472469
unsafe extern "C" fn c2rust_run_static_initializers() {
473470
unsafe {
474471
global_static_const_ptr_arithmetic = PTR_ARITHMETIC;
475-
global_static_const_indexing = NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize];
472+
global_static_const_indexing =
473+
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_double as ::core::ffi::c_int as usize];
476474
global_static_const_ref_indexing = NESTED_STR
477475
.as_ptr()
478-
.offset(LITERAL_FLOAT as ::core::ffi::c_int as isize)
476+
.offset(LITERAL_FLOAT as ::core::ffi::c_double as ::core::ffi::c_int as isize)
479477
as *const ::core::ffi::c_char;
480478
global_static_const_ternary = if LITERAL_BOOL != 0 {
481479
1 as ::core::ffi::c_int
@@ -484,10 +482,11 @@ unsafe extern "C" fn c2rust_run_static_initializers() {
484482
};
485483
global_static_const_member = LITERAL_STRUCT.i;
486484
global_const_ptr_arithmetic = PTR_ARITHMETIC;
487-
global_const_indexing = NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_int as usize];
485+
global_const_indexing =
486+
NESTED_STR[LITERAL_FLOAT as ::core::ffi::c_double as ::core::ffi::c_int as usize];
488487
global_const_ref_indexing = NESTED_STR
489488
.as_ptr()
490-
.offset(LITERAL_FLOAT as ::core::ffi::c_int as isize)
489+
.offset(LITERAL_FLOAT as ::core::ffi::c_double as ::core::ffi::c_int as isize)
491490
as *const ::core::ffi::c_char;
492491
global_const_ternary = if LITERAL_BOOL != 0 {
493492
1 as ::core::ffi::c_int

0 commit comments

Comments
 (0)