Skip to content

Commit 1ef5529

Browse files
authored
properly handle ASTEntryTag::TagStaticAssertDecl conversion (#1342)
visit children nodes in StaticAssertDecl, so all children exprs will be inserted in `ast_context.c_exprs`. without visiting it, we will get panic 'no entry found for key' when translator try to convert `_Static_assert`. * Fixes #1332.
2 parents c589c0a + 0ea9471 commit 1ef5529

File tree

5 files changed

+35
-17
lines changed

5 files changed

+35
-17
lines changed

c2rust-transpile/src/c_ast/conversion.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2360,13 +2360,19 @@ impl ConversionContext {
23602360
}
23612361

23622362
ASTEntryTag::TagStaticAssertDecl if expected_ty & DECL != 0 => {
2363-
let assert_expr = CExprId(
2364-
node.children[0].expect("StaticAssert must point to an expression"),
2365-
);
2363+
let assert_expr =
2364+
node.children[0].expect("StaticAssert must point to an expression");
2365+
let assert_expr = self.visit_expr(assert_expr);
2366+
23662367
let message = if node.children.len() > 1 {
2367-
Some(CExprId(
2368-
node.children[1].expect("Expected static assert message"),
2369-
))
2368+
let message = node.children[1].expect("Expected static assert message");
2369+
let message = untyped_context
2370+
.ast_nodes
2371+
.get(&message)
2372+
.expect("Expected string literal node");
2373+
let message = from_value::<String>(message.extras[2].clone())
2374+
.expect("string literal bytes");
2375+
Some(message)
23702376
} else {
23712377
None
23722378
};
@@ -2375,6 +2381,7 @@ impl ConversionContext {
23752381
message,
23762382
};
23772383
self.add_decl(new_id, located(node, static_assert));
2384+
self.processed_nodes.insert(new_id, OTHER_DECL);
23782385
}
23792386

23802387
t => panic!("Could not translate node {:?} as type {}", t, expected_ty),

c2rust-transpile/src/c_ast/iterators.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,16 +184,7 @@ fn immediate_decl_children(kind: &CDeclKind) -> Vec<SomeId> {
184184
Field { typ, .. } => intos![typ.ctype],
185185
MacroObject { .. } | MacroFunction { .. } => vec![],
186186
NonCanonicalDecl { canonical_decl } => intos![canonical_decl],
187-
StaticAssert {
188-
assert_expr,
189-
message,
190-
} => {
191-
if let Some(message) = message {
192-
intos![assert_expr, message]
193-
} else {
194-
intos![assert_expr]
195-
}
196-
}
187+
StaticAssert { assert_expr, .. } => intos![assert_expr],
197188
}
198189
}
199190

c2rust-transpile/src/c_ast/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1151,7 +1151,7 @@ pub enum CDeclKind {
11511151

11521152
StaticAssert {
11531153
assert_expr: CExprId,
1154-
message: Option<CExprId>,
1154+
message: Option<String>,
11551155
},
11561156
}
11571157

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
source: c2rust-transpile/tests/snapshots.rs
3+
expression: cat tests/snapshots/static_assert.rs
4+
input_file: c2rust-transpile/tests/snapshots/static_assert.c
5+
---
6+
#![allow(
7+
dead_code,
8+
mutable_transmutes,
9+
non_camel_case_types,
10+
non_snake_case,
11+
non_upper_case_globals,
12+
unused_assignments,
13+
unused_mut
14+
)]
15+
#[no_mangle]
16+
pub unsafe extern "C" fn static_assert() {}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
void static_assert() {
2+
_Static_assert (1);
3+
_Static_assert (1, "Expression evaluates to false");
4+
}

0 commit comments

Comments
 (0)