Skip to content

transpile: Add MacroInvocationInfo - #1970

Open
Rua wants to merge 2 commits into
immunant:masterfrom
Rua:macro-invocation-info
Open

Rua wants to merge 2 commits into
immunant:masterfrom
Rua:macro-invocation-info

Conversation

@Rua

@Rua Rua commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Following up from #1792, this adds the necessary infrastructure to the AST to handle functional macro arguments. The MacroInvocationInfo::arguments value is currently unused.

@Rua
Rua force-pushed the macro-invocation-info branch 2 times, most recently from 2f8538e to 367c4d8 Compare August 8, 2026 16:14
@Rua
Rua force-pushed the macro-invocation-info branch 3 times, most recently from 9884df3 to 00e7abe Compare August 22, 2026 08:26
info.arguments.insert(parameter.clone(), expr_id);
} else {
// This expression is the result of a macro expansion.
info.expr_id = expr_id;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Could this already be a different value that's not u64::MAX or expr_id? We could check.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

expr_id = CExprId(new_id) so is it guaranteed to always be unique? In that case we don't need to check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The only way it could be not unique is if the macro invocation key is reused for multiple invocations. I've added an assert for that now.

.into_iter()
.map(|key| {
let info = macro_infos
.remove(&key)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can't a key be needed multiple times here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

A key refers to a single macro invocation, and each invocation should end up in macro_invocations exactly once.

.map(|key| {
let info = macro_infos
.remove(&key)
.expect("No macro_infos entry for key {key}");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

expect takes a fixed string not a format, so {key} here is printed as is.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed.

.try_fold::<Option<(WithStmts<Box<Expr>>, CTypeId)>, _, _>(None, |canonical, &id| {
self.can_convert_const_macro_expansion(id)?;
.try_fold::<Option<(WithStmts<Box<Expr>>, CTypeId)>, _, _>(None, |canonical, info| {
let &MacroInvocationInfo { expr_id, .. } = info.as_ref();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

let expr_id = info.expr_id; would be simpler (and you wouldn't need the new import).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I expect to add more fields here in a future PR.

@@ -1047,13 +1068,38 @@ impl ConversionContext {
};

if expected_ty & EXPR != 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What about the callers of expr_possibly_as_stmt? I.e. expressions used as statements?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Those get wrapped in a CStmtKind::Expr, in which the statement part and the expression part are handled separately.

@Rua
Rua force-pushed the macro-invocation-info branch 2 times, most recently from 58d0ef4 to 2a6372a Compare September 7, 2026 16:53
@Rua
Rua force-pushed the macro-invocation-info branch from 2a6372a to 759cf38 Compare September 7, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants