compiler/rustc_ast/src/expand/allocator.rs RUST 93 lines View on github.com → Search inside
1use rustc_macros::StableHash;2use rustc_span::{Symbol, sym};34#[derive(Clone, Debug, Copy, Eq, PartialEq, StableHash)]5pub enum AllocatorKind {6    /// Use `#[global_allocator]` as global allocator.7    Global,8    /// Use the default implementation in libstd as global allocator.9    Default,10}1112pub fn global_fn_name(base: Symbol) -> String {13    format!("__rust_{base}")14}1516pub fn default_fn_name(base: Symbol) -> String {17    format!("__rdl_{base}")18}1920pub const ALLOC_ERROR_HANDLER: Symbol = sym::alloc_error_handler;21pub const NO_ALLOC_SHIM_IS_UNSTABLE: &str = "__rust_no_alloc_shim_is_unstable_v2";2223/// Argument or return type for methods in the allocator shim24#[derive(Copy, Clone)]25pub enum AllocatorTy {26    Layout,27    Never,28    Ptr,29    ResultPtr,30    Unit,31    Usize,32}3334/// Some allocator methods are known to the compiler: they act more like35/// intrinsics/language primitives than library-defined functions.36/// FIXME: ideally this would be derived from attributes like `#[rustc_allocator]`,37/// so we don't have two sources of truth.38#[derive(Copy, Clone, Debug)]39pub enum SpecialAllocatorMethod {40    Alloc,41    AllocZeroed,42    Dealloc,43    Realloc,44}4546/// A method that will be codegened in the allocator shim.47#[derive(Copy, Clone)]48pub struct AllocatorMethod {49    pub name: Symbol,50    pub special: Option<SpecialAllocatorMethod>,51    pub inputs: &'static [AllocatorMethodInput],52    pub output: AllocatorTy,53}5455pub struct AllocatorMethodInput {56    pub name: &'static str,57    pub ty: AllocatorTy,58}5960pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[61    AllocatorMethod {62        name: sym::alloc,63        special: Some(SpecialAllocatorMethod::Alloc),64        inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],65        output: AllocatorTy::ResultPtr,66    },67    AllocatorMethod {68        name: sym::dealloc,69        special: Some(SpecialAllocatorMethod::Dealloc),70        inputs: &[71            AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },72            AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },73        ],74        output: AllocatorTy::Unit,75    },76    AllocatorMethod {77        name: sym::realloc,78        special: Some(SpecialAllocatorMethod::Realloc),79        inputs: &[80            AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },81            AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },82            AllocatorMethodInput { name: "new_size", ty: AllocatorTy::Usize },83        ],84        output: AllocatorTy::ResultPtr,85    },86    AllocatorMethod {87        name: sym::alloc_zeroed,88        special: Some(SpecialAllocatorMethod::AllocZeroed),89        inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],90        output: AllocatorTy::ResultPtr,91    },92];

Code quality findings 1

Warning: Direct indexing (e.g., `vec[i]`, `slice[i]`) panics on out-of-bounds access. Prefer using `.get(index)` or `.get_mut(index)` which return Option<&T>/Option<&mut T>.
warning correctness unchecked-indexing
pub inputs: &'static [AllocatorMethodInput],

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.