Skip to content

transpile: Refactor, document and correct uses of used() flag - #1975

Open
Rua wants to merge 12 commits into
immunant:masterfrom
Rua:is_used
Open

transpile: Refactor, document and correct uses of used() flag#1975
Rua wants to merge 12 commits into
immunant:masterfrom
Rua:is_used

Conversation

@Rua

@Rua Rua commented Aug 16, 2026

Copy link
Copy Markdown
Contributor

I've documented how is_used is supposed to be set, and added/removed used() accordingly.

If you're wondering what's up with some of the code in the exprs.c test suddenly disappearing: The transpiler normally eliminates unused code, which includes unused results of binary expressions:

  • 1 + 1; gets eliminated altogether
  • 1 + func(); gets reduced to func();

But for some reason unary expressions were not handled the same way, and were emitted even if they didn't do anything. Now they get removed too.

@Rua Rua changed the title transpile: Refactor and correct uses of used() flag transpile: Refactor, document and correct uses of used() flag Aug 16, 2026
@Rua
Rua force-pushed the is_used branch 6 times, most recently from c02d700 to 95b17f7 Compare August 16, 2026 16:02
@fw-immunant
fw-immunant self-requested a review August 18, 2026 06:47
@fw-immunant fw-immunant self-assigned this Aug 18, 2026

@fw-immunant fw-immunant left a comment

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.

Code changes all look good, but before merging I want to go verify that this doesn't interfere with the way Hayroll inserts no-op expressions into c2rust inputs to link C preprocessor macro expansions with their c2rust translations.

op if op.is_logical() => {
let lhs = self.convert_condition(ctx, true, lhs)?;
let rhs = self.convert_condition(ctx, true, rhs)?;
let lhs = self.convert_condition(ctx.used(), true, lhs)?;

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.

Shouldn't this inherit from the operator? E.g. in a == b the two operands are only used if the entire comparison 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.

Logical operators are || &&, that's a comparison operator.

}
}?;

// Some unused unary operators (`-foo()`) may have side effects, so we need

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 you add a test for volatile reads and writes (something like volatile int vi; -vi;) and make sure we're not losing them? And __extension__ although I think we already have a test for that.

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 noticed that this code already loses the vi; statement in the current master (without the negation):

volatile int vi;
vi;

Is that a problem? I'm not sure what the semantics of C are meant to be here. But if this is supposed to emit a volatile read, then it would need to be fixed separately from this PR.

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've made a separate PR for that issue now, and made this one depend on it.

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.

3 participants