|
|
Subscribe / Log in / New account

Lexical code transformer?

Lexical code transformer?

Posted Sep 30, 2024 23:32 UTC (Mon) by rywang014 (guest, #167182)
Parent article: Coccinelle for Rust

A C change requires lexical transformations to the C callers, and Coccinelle can do that for you. But a C change typically requires a change of abstractions in Rust side. For example now your C function returns an error by returning a negative int, which requires a signature change in Rust abstractions from returning a MyDescriptor to a Result<MyDescriptor> or even something like `SomeRegistrationFromWhichYouCanGetA<MyDescriptor>`. And each caller needs to explicitly handle it - either by a `?` or by some added error handling code. So this is not likely a lexical transformation. How can an automatic tool do that? Even if it can change all callers with an additional `?` it's not likely to be semantically correct.


to post comments

Lexical code transformer?

Posted Oct 1, 2024 9:17 UTC (Tue) by matthias (subscriber, #94967) [Link] (1 responses)

So your use case is that an infallible function now can return an error. But this is a change of abstraction in C as well as in rust. Even if you do not need to change the return type in C, you still have to make sure that erros are handled correctly at each call site. How can an automatic tool do that?

I do not see at all that the task for C is simpler than for rust. To the contrary. If the adding of '?' in rust code is syntactically correct, i.e., if the compiler is happy, then it is most likely also semantically correct. If a call site does not have the proper return type itself, the compiler will notice that the error is not handled. If the resulting code just compiles then there already is error handling for the specific error type.

Of course, there is still the possibility that the error is not handled correctly, as the error path might not have been tested at all. It might just have been dead code. But this is not different to C.

Lexical code transformer?

Posted Oct 1, 2024 15:28 UTC (Tue) by roc (subscriber, #30627) [Link]

Yet another example of "this is easy in C, but hard in Rust ... because C lets you do it wrong".


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds