Can somebody point me to the Rust compiler internals documentation?


#1

Hi,
Can somebody point me to the Rust compiler internals documentation?


#2

Can you be more specific about what sort of documentation you’re looking for? Maybe how the compiler is structured, or maybe API documentation of the functions (generated by rustdoc, like we host of std?), or maybe a guide to making changes?


#3

I’m looking for documentation about things like the panic routine, and code that the compiler inserts into binaries apart from the code that is being compiled, etc.


#4

The code for panic! itself is a part of libcore:

macro_rules! panic {
    () => (
        panic!("explicit panic")
    );
    ($msg:expr) => ({
        static _MSG_FILE_LINE: (&'static str, &'static str, u32) = ($msg, file!(), line!());
        $crate::panicking::panic(&_MSG_FILE_LINE)
    });
    ($fmt:expr, $($arg:tt)*) => ({
        // The leading _'s are to avoid dead code warnings if this is
        // used inside a dead function. Just `#[allow(dead_code)]` is
        // insufficient, since the user may have
        // `#[forbid(dead_code)]` and which cannot be overridden.
        static _FILE_LINE: (&'static str, u32) = (file!(), line!());
        $crate::panicking::panic_fmt(format_args!($fmt, $($arg)*), &_FILE_LINE)
    });
}

It calls ::core::panicking::panic_fmt:

#[cold] #[inline(never)]
pub fn panic_fmt(fmt: fmt::Arguments, file_line: &(&'static str, u32)) -> ! {
    #[allow(improper_ctypes)]
    extern {
        #[lang = "panic_fmt"]
        #[unwind]
        fn panic_impl(fmt: fmt::Arguments, file: &'static str, line: u32) -> !;
    }
    let (file, line) = *file_line;
    unsafe { panic_impl(fmt, file, line) }
}

Which is just a thin wrapper around rust_begin_unwind:

#[lang = "panic_fmt"]
#[unwind]
pub extern fn rust_begin_unwind(msg: fmt::Arguments,
                                file: &'static str, line: u32) -> ! {
    begin_unwind_fmt(msg, &(file, line))
}

#5

What do you mean when you say “code the compiler inserts into binaries”?


#6

@Manishearth has the nightly compiler docs on his github.io: https://manishearth.github.io/rust-internals-docs/rustc/


#7

What do you mean when you say “code the compiler inserts into binaries”?

I don’t know what @nabeelomer meant, but panic_bounds_check language item seems to be a good example.

FYI, it’s in the same place: src/libcore/panicking.rs.