Re: [PATCH 1/3] rust: add static_call support

From: Miguel Ojeda
Date: Fri Jun 07 2024 - 07:46:39 EST


On Fri, Jun 7, 2024 at 12:52 PM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> I'm sorry, but 30+ years of reading ! as NOT (or factorial) isn't going
> to go away. So I'm reading your macros do NOT rule.

It makes it clear what is macro call or not. They could have gone for
UPPERCASE names (for instance), yes. On the other hand, they do not
work like C macros and are ~hygienic, so it also makes sense to avoid
confusion here.

I mean, I am not suggesting to do a CPP-pass to Rust files, but if
someone really, really wanted to mix them in a single file, it would
be nice to not confuse the two kinds. :)

Generally they feel "closer" to the language (given what they
do/support) compared to the CPP ones, so it also makes sense they
don't "shout" so much compared to UPPERCASE, if that makes sense.

> The above exaple fails, because the next token is :ident, whatever the
> heck that might be. Also, extra points for line-noise due to lack of
> whitespace.

$name:ident means "match what Rust would consider an identifier here
and call it $name for the purposes of this macro".

So, for instance, $x:ident matches:

a
a2
a_b

But it would not match:

2a
a-b
a _b

For the usual reasons why those are not identifiers.

https://godbolt.org/z/G7v4j67dc

fn f(x: i32) -> i32 {
x * 2
}

macro_rules! f {
($x:ident) => { $x * 2 }
}

fn main() {
let a = 42;

let b = f(a); // Function.
let c = f!(a); // Macro.

//let c = f!(a2); // Works, but the variable does not exist.
//let c = f!(2a); // Error: no rules expected the token `2a`.

//let c = f!(a_b); // Works, but the variable does not exist.
//let c = f!(a-b); // Error: no rules expected the token `-`.
//let c = f!(a_ b); // Error: no rules expected the token `b`.

println!("{a} {b} {c}");
}

I hope this makes it clearer.

> You just need to extend the rust thing to be able to consume C header
> files.

I agree, because in practice it is quite useful for a language like
Rust that consuming C header files is "natively" supported.

Though it also has downsides and is a big decision, which is why, like
Alice mentioned, some people agree, and some people don't.
Nevertheless, we have been doing our best for a long time to get the
best we can for the kernel -- just 2 days ago we told the Rust project
in one of our meetings that it would be nice to see that particular
"project goal" from that document realized (among others).

Cheers,
Miguel