GCC Front-End For Rust

Alternative Rust Compiler for GCC

View the Project on GitHub

December 2024 Monthly report

Overview

Thanks again to Open Source Security, inc and Embecosm for their ongoing support for this project.

Project update

This month of development was focused on three major milestones of compiler development: auto traits, built-in macros and for-loops, which are all important in order to compile core 1.49 and later, the Rust-for-Linux project.

The most widely known auto traits in Rust may be Send and Sync, which are used all throughout the Rust standard library to indicate thread-safety. As such, these traits are often used in trait-bounds to restrict sharing behavior to thread-safe objects:

trait Foo {
    fn foo(&self);
}

struct Safe;

// We cannot send pointers safely between threads
struct NonThreadSafe(*const i32);

impl Foo for Safe { fn foo(&self) { } }
impl Foo for NonThreadSafe { fn foo(&self) { } }

fn take_foo(foo: &(dyn Foo)) {
    foo.foo();
}

fn take_thread_safe_foo(foo: &(dyn Foo + Send + Sync)) {
    foo.foo();
}

fn main() {
    let s = Safe;

    let i = 15;
    let n_s = NonThreadSafe(&i as *const i32);

    take_foo(&s);
    take_foo(&n_s);

    take_thread_safe_foo(&s);
    take_thread_safe_foo(&n_s);
}

which errors out with the following:

   Compiling playground v0.0.1 (/playground)
error[E0277]: `*const i32` cannot be shared between threads safely
  --> src/main.rs:31:26
   |
31 |     take_thread_safe_foo(&n_s);
   |                          ^^^^ `*const i32` cannot be shared between threads safely
   |
   = help: within `NonThreadSafe`, the trait `Sync` is not implemented for `*const i32`, which is required by `NonThreadSafe: Sync`
note: required because it appears within the type `NonThreadSafe`
  --> src/main.rs:8:8
   |
8  | struct NonThreadSafe(*const i32);
   |        ^^^^^^^^^^^^^
   = note: required for the cast from `&NonThreadSafe` to `&dyn Foo + Send + Sync`

error[E0277]: `*const i32` cannot be sent between threads safely
  --> src/main.rs:31:26
   |
31 |     take_thread_safe_foo(&n_s);
   |                          ^^^^ `*const i32` cannot be sent between threads safely
   |
   = help: within `NonThreadSafe`, the trait `Send` is not implemented for `*const i32`, which is required by `NonThreadSafe: Send`
note: required because it appears within the type `NonThreadSafe`
  --> src/main.rs:8:8
   |
8  | struct NonThreadSafe(*const i32);
   |        ^^^^^^^^^^^^^
   = note: required for the cast from `&NonThreadSafe` to `&dyn Foo + Send + Sync`

Part of our pipeline was lacking support for these automatic trait bounds, which now enables us to build this code properly. Two missing features remain for properly handling these special bounds:

  1. Additional trait bounds can only be automatic traits

We have to restrict the usage of additional trait bounds to auto traits exclusively, as rustc does:

fn takes_only_clone_copy_and_fromstr(a: &(dyn Clone + Copy + FromStr)) {}

will give out the following error:

error[E0225]: only auto traits can be used as additional traits in a trait object
  --> src/main.rs:16:55
   |
16 | fn takes_only_clone_copy_and_fromstr(a: &(dyn Clone + Copy + FromStr)) {}
   |                                               -----   ^^^^ additional non-auto trait
   |                                               |
   |                                               first non-auto trait
   |
   = help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Clone + Copy + FromStr {}`
   = note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

https://doc.rust-lang.org/stable/error_codes/E0225.html

We must add additional logic to our typechecker in order to check for these bounds, and make sure that the additional bounds we resolve to only refer to auto traits - otherwise, error out with an appropriate error message. This isn’t a terribly difficult feature to add, but it does require a bit of experience with our type system.

  1. Implementing Send and Sync properly

Finally, we must implement the Send and Sync traits properly, and auto traits in general, so that they are applied automatically unless you opt out of it. One major difficulty comes from having to automatically opt-out types that contains other opted-out types, like in the following example:

#![feature(auto_traits)]
#![feature(negative_impls)]

auto trait MegaAuto {}

struct S;

impl !MegaAuto for S {}

struct S_Wrap(S);

fn take_mega_auto(a: &dyn MegaAuto) {}

fn main() {
    let a = S;
    let b = S_Wrap(S);

    take_mega_auto(&a);
    take_mega_auto(&b);
}

which errors out with the following information:


error[E0277]: the trait bound `S: MegaAuto` is not satisfied
  --> src/main.rs:18:20
   |
18 |     take_mega_auto(&a);
   |                    ^^ the trait `MegaAuto` is not implemented for `S`
   |
   = note: required for the cast from `&S` to `&dyn MegaAuto`

error[E0277]: the trait bound `S: MegaAuto` is not satisfied in `S_Wrap`
  --> src/main.rs:19:20
   |
19 |     take_mega_auto(&b);
   |                    ^^ within `S_Wrap`, the trait `MegaAuto` is not implemented for `S`
   |
note: required because it appears within the type `S_Wrap`
  --> src/main.rs:10:8
   |
10 | struct S_Wrap(S);
   |        ^^^^^^
   = note: required for the cast from `&S_Wrap` to `&dyn MegaAuto`

This will be a lot more involved, and will require important changes to our type system.

The rest of the month was focused on for-loops and built-in derive macros, which first required a major refactor of our AST and HIR. The problem is as follows: for-loops desugar using the following pattern:

for <pattern> in <iterator> {
  <body>
}

// becomes

{
    let result = match ::std::iter::IntoIterator::into_iter(<iterator>) {
        mut iter => {
            loop {
                let mut __next;
                match ::std::iter::Iterator::next(&mut iter) {
                    ::std::option::Option::Some(val) => __next = val,
                    ::std::option::Option::None => break
                };
                let <pattern> = __next;
                <body>;
            }
        }
    };
    result
}

The paths used in this desugaring are a bit special, as they all start with a leading ::. This is used to indicate that the first path segment should refer to an external crate, and that the path should resolve in one of these crates. This is very useful to prevent ambiguity: If a user were to define their own std module, with an inner option module and an Option enum, simply using the std::option::Option path would resolve to this instead of the expected standard enum.

If for some reason this user-defined enum were to be different, the desugared code would not work, and you would end up with cryptic errors. But since the path is required to resolve in the extern crate std, there are no issues. However, gccrs cannot yet compile the standard library, or distribute it and link it to each program being compiled. Meaning that we would never be able to resolve those paths, at least until we get to compile the standard library. But the standard library contains for-loops, so we’re in a real pickle.

Thankfully, those specific paths used for desugars or macro expansions often refer to specific items that the compiler knows about: lang items. By instead using “lang item paths”, we can refer to these specific items and enum variants to properly desugar our for-loops. But these paths are extremely different from regular, segmented paths, and required a big refactor of both our AST and HIR. Which then allowed us to almost complete the desugaring of for-loops, and improve our handling of built-in macros - another kind of compiler expansion that makes use of lang items you probably already know about, such as Copy or Clone.

We still have a few built-in derives to properly implement in order to support all of them, and close out the Macro expansion milestone of the project. These built-in derives are used all throughout std, and throughout most Rust code, meaning they are essential for compiling the Rust standard library.

If you are interested in helping with these efforts, feel free to pick up any of the issues mentioned in our Call for Contribution or to join the project! We are always available on GitHub or on our Zulip and always happy to see new faces.

Community call

We will have our next monthly community call on the 13th of January at 10am UTC. You can subscribe to our calendar to see when the next one will be held. The call is open to everyone, even if you would just like to sit-in and listen. You can also subscribe to our mailing-list or join our Zulip chat to be notified of upcoming events.

Call for contribution

Completed Activities

Contributors this month

Overall Task Status

Category Last Month This Month Delta
TODO 316 308 -8
In Progress 80 88 +8
Completed 907 922 +15

Bugs

Category Last Month This Month Delta
TODO 112 110 -2
In Progress 40 38 -2
Completed 450 458 +8

Test Cases

TestCases Last Month This Month Delta
Passing 9266 9329 +63
Failed - - -
XFAIL 264 262 -2
XPASS - - -

Milestones Progress

Milestone Last Month This Month Delta Start Date Completion Date Target Target GCC
Name resolution 2.0 rework 14% 20% - 1st Jun 2024 - 1st Apr 2025 GCC 15.1
Macro expansion 29% 33% +4% 1st Jun 2024 - 1st Jan 2025 GCC 15.1
Lang items 90% 90% - 1st Jul 2024 - 21st Nov 2024 GCC 15.1
Indexing fixes 0% 100% +100% 21st Jul 2024 25th Dec 2024 15th Nov 2024 GCC 15.1
Iterator fixes 0% 100% +100% 21st Jul 2024 25th Dec 2024 15th Nov 2024 GCC 15.1
Auto traits improvements 0% 60% +60% 15th Sep 2024 - 21st Dec 2024 GCC 15.1
Remaining typecheck issues 88% 88% - 21st Oct 2024 - 1st Mar 2025 GCC 15.1
cfg-core 0% 0% - 1st Dec 2024 - 1st Mar 2025 GCC 15.1
Codegen fixes 0% 0% - 7th Oct 2024 - 1st Mar 2025 GCC 15.1
Upcoming Milestone Last Month This Month Delta Start Date Completion Date Target Target GCC
Question mark operator 0% 0% - 15th Dec 2024 - 21st Feb 2025 GCC 15.1
Specialization 0% 0% - 1st Jan 2025 - 1st Mar 2025 GCC 15.1
Inline assembly 100% 100% - 1st Jun 2024 26th Aug 2024 15th Sep 2024 GCC 15.1
Borrow checker improvements 100% 100% - 1st Jun 2024 26th Aug 2024 15th Sep 2024 GCC 15.1
Rustc Testsuite Adaptor 0% 0% - 1st Jun 2024 - 15th Sep 2024 GCC 15.1
blackbox intrinsic 0% 0% - 28th Oct 2024 - 28th Jan 2025 GCC 15.1
Unstable RfL features 0% 0% - 7th Jan 2025 - 1st Mar 2025 GCC 15.1
cfg-rfl 0% 0% - 7th Jan 2025 - 15th Feb 2025 GCC 15.1
alloc parser issues 100% 100% - 7th Jan 2025 31st Jun 2024 28th Jan 2025 GCC 15.1
let-else 0% 0% - 28th Jan 2025 - 28th Feb 2025 GCC 15.1
Explicit generics with impl Trait 0% 0% - 28th Feb 2025 - 28th Mar 2025 GCC 15.1
Downgrade to Rust 1.49 0% 0% - - - 1st Apr 2025 GCC 15.1
offsetof!() builtin macro 0% 0% - 15th Mar 2025 - 15th May 2025 GCC 15.1
Generic Associated Types 0% 0% - 15th Mar 2025 - 15th Jun 2025 GCC 16.1
RfL const generics 0% 0% - 1st May 2025 - 15th Jun 2025 GCC 16.1
frontend plugin hooks 0% 0% - 15th May 2025 - 7th Jul 2025 GCC 16.1
Handling the testsuite issues 0% 0% - 15th Sep 2024 - 15th Sep 2025 GCC 16.1
std parser issues 100% 100% - 7th Jan 2025 31st Jun 2024 28th Jan 2025 GCC 16.1
main shim 0% 0% - 28th Jul 2025 - 15th Sep 2025 GCC 16.1
Past Milestone Last Month This Month Delta Start Date Completion Date Target Target GCC
Data Structures 1 - Core 100% 100% - 30th Nov 2020 27th Jan 2021 29th Jan 2021 GCC 14.1
Control Flow 1 - Core 100% 100% - 28th Jan 2021 10th Feb 2021 26th Feb 2021 GCC 14.1
Data Structures 2 - Generics 100% 100% - 11th Feb 2021 14th May 2021 28th May 2021 GCC 14.1
Data Structures 3 - Traits 100% 100% - 20th May 2021 17th Sep 2021 27th Aug 2021 GCC 14.1
Control Flow 2 - Pattern Matching 100% 100% - 20th Sep 2021 9th Dec 2021 29th Nov 2021 GCC 14.1
Macros and cfg expansion 100% 100% - 1st Dec 2021 31st Mar 2022 28th Mar 2022 GCC 14.1
Imports and Visibility 100% 100% - 29th Mar 2022 13th Jul 2022 27th May 2022 GCC 14.1
Const Generics 100% 100% - 30th May 2022 10th Oct 2022 17th Oct 2022 GCC 14.1
Initial upstream patches 100% 100% - 10th Oct 2022 13th Nov 2022 13th Nov 2022 GCC 14.1
Upstream initial patchset 100% 100% - 13th Nov 2022 13th Dec 2022 19th Dec 2022 GCC 14.1
Update GCC’s master branch 100% 100% - 1st Jan 2023 21st Feb 2023 3rd Mar 2023 GCC 14.1
Final set of upstream patches 100% 100% - 16th Nov 2022 1st May 2023 30th Apr 2023 GCC 14.1
Borrow Checking 1 100% 100% - TBD 8th Jan 2024 15th Aug 2023 GCC 14.1
Procedural Macros 1 100% 100% - 13th Apr 2023 6th Aug 2023 6th Aug 2023 GCC 14.1
GCC 13.2 Release 100% 100% - 13th Apr 2023 22nd Jul 2023 15th Jul 2023 GCC 14.1
GCC 14 Stage 3 100% 100% - 1st Sep 2023 20th Sep 2023 1st Nov 2023 GCC 14.1
GCC 14.1 Release 100% 100% - 2nd Jan 2024 2nd Jun 2024 15th Apr 2024 GCC 14.1
formatargs!() support 100% 100% - 15th Feb 2024 - 1st Apr 2024 GCC 14.1
GCC 14.2 100% 100% - 7th Jun 2024 15th Jun 2024 15th Jun 2024 GCC 14.2
GCC 15.1 100% 100% - 21st Jun 2024 31st Jun 2024 1st Jul 2024 GCC 15.1
Unhandled attributes 100% 100% - 1st Jul 2024 15th Aug 2024 15th Aug 2024 GCC 15.1
Deref and DerefMut improvements 100% 100% - 28th Sep 2024 25th Oct 2024 28th Dec 2024 GCC 15.1

Planned Activities

Risks

We have now entered Stage 3 of GCC development, and all of the patches we needed to get upstreamed have been upstreamed. The risk present in this table is no longer present.

Risk Impact (1-3) Likelihood (0-10) Risk (I * L) Mitigation
Missing features for GCC 15.1 deadline 2 0 0 Start working on required features as early as July (6mo ahead)

Detailed changelog