1

I've seen some Rust codebases use the #[repr(C)] macro (is that what it's called?), however, I couldn't find much information about it but that it sets the type layout in memory to the same layout as 'C's.

Here's what I would like to know: is this a preprocessor directive restricted to the compiler and not the language itself (even though there aren't any other compiler front-ends for Rust), and why does Rust even have a memory layout different than that of Cs? (it's just that I've never had to do this in another language).

Here's a nice situation to demonstrate what I meant: if someone creates another compiler for Rust, are they required to implement this macro, or is it a compiler specific thing?

13
  • github.com/rust-lang/reference/blob/… Is repr(C) a preprocessor directive? Rust has preprocessor? What kind of "pre-processor" do you have in mind?
    – KamilCuk
    Commented Apr 23, 2021 at 20:02
  • @KamilCuk I think I'm using the wrong term here, definitely correct me if you know the correct one, but I meant that the compiler front-end preprocesses the structure to which this macro is applied with this layout, and then does the compilations/optimisations.
    – skevelis
    Commented Apr 23, 2021 at 20:07
  • Sure it does, that's what a compiler does - processes the text and then generates code.
    – KamilCuk
    Commented Apr 23, 2021 at 20:08
  • 1
    are they required - well, there's no "Rust police" that will come and fine you for not implementing it....
    – KamilCuk
    Commented Apr 23, 2021 at 20:12
  • 2
    Just a small sidenote, #pragma is part of C. And while #pragma is preprocessing directive, the compiler has to make decisions after preprocessor, as #pragma STDC FENV_ACCESS ON in a function affects only in that function, so it needs to know when the function ends, which is not something preprocessor does.
    – KamilCuk
    Commented Apr 23, 2021 at 22:21

2 Answers 2

12

#[repr(C)] is not a preprocessor directive, since Rust doesn't use a preprocessor 1. It is an attribute. Rust doesn't have a complete specification, but the repr attribute is mentioned in the Rust reference, so it is absolutely a part of the language. Implementation-wise, attributes are parsed the same way all other Rust code is, and are stored in the same AST. Rust has no "attribute pass": attributes are an actual part of the language. If someone else were to implement a Rust compiler, they would need to implement #[repr(C)].

Furthermore, #[repr(C)] can't be implemented without some compiler magic. In the absence of a #[repr(...)], Rust compilers are free to arrange the fields of a struct/enum however they want to (and they do take advantage of this for optimization purposes!).

Rust does have a good reason for using it's own memory layout. If compilers aren't tied to how a struct is written in the source code, they can do optimisations like not storing struct fields that are never read from, reordering fields for better performance, enum tag pooling2, and using spare bits throughout NonZero*s in the struct to store data (the last one isn't happening yet, but might in the future). But the main reason is that Rust has things that just don't make sense in C. For instance, Rust has zero-sized types (like () and [i8; 0]) which can't exist in C, trait vtables, enums with fields, generic types, all of which cause problems when trying to translate them to C.


1 Okay, you could use the C preprocessor with Rust if you really wanted to. Please don't.

2 For example, enum Food { Apple, Pizza(Topping) } enum Topping { Pineapple, Mushroom, Garlic } can be stored in just 1 byte since there are only 4 possible Food values that can be created.

1
  • This was an excellent explanation. I'm surprised the Rust compiler is already doing such optimisations. I can't promise you not to use the C preprocessor though ;)
    – skevelis
    Commented Apr 24, 2021 at 4:36
1

What is this?

It is not a macro it is an attribute.

The book has a good chapter on what macros are and it mentions that there are "Attribute-like macros":

The term macro refers to a family of features in Rust: declarative macros with macro_rules! and three kinds of procedural macros:

  • Custom #[derive] macros that specify code added with the derive attribute used on structs and enums
  • Attribute-like macros that define custom attributes usable on any item
  • Function-like macros that look like function calls but operate on the tokens specified as their argument

Attribute-like macros are what you could use like attributes. For example:

#[route(GET, "/")]
fn index() {}

It does look like the repr attribute doesn't it 😃

So what is an attribute then?

Luckily Rust has great resources like rust-by-example which includes:

An attribute is metadata applied to some module, crate or item. This metadata can be used to/for:

  • conditional compilation of code
  • set crate name, version and type (binary or library)
  • disable lints (warnings)
  • enable compiler features (macros, glob imports, etc.)
  • link to a foreign library
  • mark functions as unit tests
  • mark functions that will be part of a benchmark

The rust reference is also something you usually look at when you need to know something more in depth. (chapter for attributes)

To the compiler authors out there:

If you were to write a rust compiler, and wanted to support things like the standard library or other crates then you would 100% need to implement these. Because the libraries use these and need them.

Otherwise I guess you could come up with a subset of rust that your compiler supports. But then most people wouldn't use it..

Why does rust not just use the C layout?

The nomicon explains why rust needs to be able to reorder fields of structs for example. For reasons of saving space and being more efficient. It is related to, among other things, generics and monomorphization. In repr(C) fields of structs must be in the same order as the definition.

The C representation is designed for dual purposes. One purpose is for creating types that are interoperable with the C Language. The second purpose is to create types that you can soundly perform operations on that rely on data layout such as reinterpreting values as a different type.

0

Not the answer you're looking for? Browse other questions tagged or ask your own question.