Final Report

GCCRS Logo

Enhancing user errors & Error Code Support for GCC Rust (GCCRS) Frontend


Description:

The goal of this project is to enhance the user experience of gnu gccrs by introducing and implementing error codes that are compatible with rustc. This will facilitate the integration of the two test suites and enable the rustc testsuite to run on gccrs. The project will require modifying the gccrs frontend code to generate more informative error messages and rich locations to assist users and developers in improving their code.


What was done:

  • Restructured the ErrorCode struct into an enum class, alongside the respective functions like make_description and make_url. This modification was carried out to enforce accurate error codes and establish an API for emitting error codes.
    • Updated & refactored the error handling code to use the new error code framework.
    • Addressed a prevailing memory leak that was present in the previous error code framework.
  • Enhanced the Error struct’s functionality to encompass storage of error codes and locations. This was used as a wrapper function to store and emit error codes specifically tailored for parsing and expansion errors.
  • Modified error handling code to use rich locations, which offer more details about the error location and help users and developers to improve their code.
  • Refined error messages to be more user-friendly and informative.
  • Conducted a comprehensive comparison between the rustc test suite and gccrs, resulting in the identification and rectification of several issues, including bugs and internal compiler errors. This iterative process led to substantial improvements in the gccrs stability. The detailed analysis is available on dedicated comparison page.

Previous & Current state:

In a prior development phase, David Malcolm extended the capabilities of GCC diagnostics to incorporate support for associating diagnostics with rules from coding standards. We are using this feature to associate gcc-rust error codes. The initial introduction included a solitary error, denoted as E0054 , addressing non-allowed cast operations to bool. Following the integration of further type-checking mechanisms, gccrs commenced emitting error codes and messages that closely resembled those of rustc.

fn main() {
    let x = 5;
    let x_is_nonzero = x as bool; 

    0u32 as char; 

    let x = &[1_usize, 2] as [usize];

    let a = &0u8; 
    let y: u32 = a as u32; 
}

Previous State:

The previous version exhibited the following output:

~/gccrs/gcc/testsuite/rust/compile/all-cast.rs:3:24: error: invalid cast ‘<integer>’ to ‘bool’ [E0054]
    3 |     let x_is_nonzero = x as bool; 
      |                        ^    ~~~~
~/gccrs/gcc/testsuite/rust/compile/all-cast.rs:5:5: error:invalid cast ‘u32’ to ‘char’ [E0054]
    5 |     0u32 as char; 
      |     ^~~~    ~~~~
~/gccrs/gcc/testsuite/rust/compile/all-cast.rs:7:13: error: invalid cast ‘& [usize:CAPACITY]’ to ‘[usize]’ [E0054]
    7 |     let x = &[1_usize, 2] as [usize]; 
      |             ^                ~
~/gccrs/gcc/testsuite/rust/compile/all-cast.rs:10:18: error: invalid cast ‘& u8’ to ‘u32’ [E0054]
   10 |     let y: u32 = a as u32;
      |                  ^    ~~~

Current State:

With the incorporation of the latest type checking code, the system now emits more specific error codes and associated error messages.

~/gccrs/gcc/testsuite/rust/compile/all-cast.rs:3:24: error: cannot cast ‘<integer>’ as ‘bool’ [E0054]
    3 |     let x_is_nonzero = x as bool;
      |                        ^    ~~~~
~/gccrs/gcc/testsuite/rust/compile/all-cast.rs:5:5: error: cannot cast ‘u32’ as ‘char’, only ‘u8’ can be cast as ‘char’ [E0604]
    5 |     0u32 as char; 
      |     ^~~~    ~~~~
~/gccrs/gcc/testsuite/rust/compile/all-cast.rs:7:13: error: cast to unsized type: ‘& [usize:CAPACITY]’ as ‘[usize]’ [E0620]
    7 |     let x = &[1_usize, 2] as [usize]; 
      |             ^                ~
~/gccrs/gcc/testsuite/rust/compile/all-cast.rs:10:18: error: casting ‘& u8’ as ‘u32’ is invalid [E0606]
   10 |     let y: u32 = a as u32; 
      |                  ^    ~~~

Bugs Finding:

A comprehensive compilation of the bugs I discovered is available on the dedicated bugs page. Among these, here are several intriguing bugs captured my attention and were successfully resolved over the course of my project:

ICE with return expression outside of context:

One of the intriguing bugs uncovered during the investigation of error codes in gccrs pertained to an internal compiler error - ICE. Specifically, gccrs encountered issues when attempting to access an empty stack, which not only posed a security concern but was also not permitted in rustc. This issue has now been addressed, resulting in the proper emission of error codes and messages, aligning with the behavior in rustc.

const FOO: u32 = return 0; 

Previously, this triggered an error causing an internal compiler error. Now, the issue is rectified, and gccrs emits the appropriate error code and message, in line with rustc’s behavior.

empty-stack.rs:1:18: error: return statement outside of function body [E0572]
    1 | const FOO: u32 = return 0; // error: return statement outside of function body
      |                  ^~~~~~

Resolve type path error:

I raised an issue that played a pivotal role in identifying and addressing an issue that contributed to enabling proper support for ?Sized and Sized trait bounds within the gccrs. This issue was subsequently resolved by my mentor.

Contributions Overview:

Pull Requests:

For a comprehensive overview of the pull requests I’ve submitted, visit pull requests page. Alternatively, you can also view the pull requests I’ve initiated on GitHub and on this tracking issue.

Issues:

To delve deeper into the intricacies of the issues I’ve engaged with, feel free to explore the dedicated issue page. Furthermore, you can also explore the issues I’ve raised, all viewable on GitHub and also on this tracking issue.

Learning Journey:

Gained Insights:

  • The most significant skill I acquired involved navigating extensive C++ codebases. It was my inaugural experience with such immense code complexity.
    • Moreover, I delved into the intricacies of compiler internals, unveiling operational nuances beyond the scope of conventional academic compiler courses.
  • Mastered effective communication and collaborative problem-solving. Despite my initial apprehension towards English communication, the unwavering support and encouragement from my mentors facilitated a transformative shift, enabling me to overcome this fear and communicate more confidently.
  • Embraced a paradigm shift: Prioritizing progress over perfection. A sage piece of advice from my mentor, Philip Herron, catalyzed my work approach.
  • Leveraged automation for productivity enhancement and error reduction:
    • Utilized Python-driven automation to validate error codes and messages. This approach streamlined the process, obviating manual effort and minimizing error risks.
  • Honed time and project management adeptness using versatile tools, notably GitHub Projects

Overcoming Challenges:

In my initial attempts, I encountered difficulty in successfully passing the evaluations carried out by the CI/CD system. In this context, my mentor Arthur Cohen guided me to pass the CI/CD. Moreover, during this collaborative effort, we uncovered a weakness within the current CI/CD framework, highlighted in this particular issue: CI fails if test file contains the word “unexpected” or “unresolved” #2386.

Work for the future:

  • Expanding the range of error codes and messages to cover more scenarios.
  • Updating the error handling code for the existing error codes to incorporate rich locations, which offer more details about the error location, and facilitate users and developers to debug and enhance their code.
  • Fixing the ICE issues that are still present in the current version of gccrs.

Acknowledgements:

I am very grateful to the following people who made this project possible and supported me throughout the process. Their expertise, feedback and encouragement were invaluable for me.

Top