Skip to content

False E0608: cannot index into Vec<T, Global> in no_std project with custom target #22628

Description

@Eywuu

rust-analyzer version: rust-analyzer 0.3.2929-standalone

rustc version: rustc 1.98.0-nightly (b30f3df3b 2026-06-11)

editor or extension: Neovim

relevant settings:

Project uses a custom no_std target through .cargo/config.toml:

[unstable]
json-target-spec = true
build-std-features = ["compiler-builtins-mem"]
build-std = ["core", "compiler_builtins", "alloc"]

[build]
target = "x86_64-eywuOS.json"

[target.'cfg(target_os = "none")']
runner = "./run.sh"

Custom target JSON:

{
    "llvm-target": "x86_64-unknown-none",
    "data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
    "arch": "x86_64",
    "target-endian": "little",
    "target-pointer-width": 64,
    "target-c-int-width": 32,
    "os": "none",
    "executables": true,
    "linker-flavor": "ld.lld",
    "linker": "rust-lld",
    "pre-link-args": {"ld.lld": ["-Tlinker.ld"]},
    "panic-strategy": "abort",
    "disable-redzone": true,
    "features": "-mmx,-sse,+soft-float",
    "rustc-abi": "x86-softfloat"
}

The crate is a no_std kernel using alloc and a custom global allocator. cargo check succeeds, but rust-analyzer reports a false E0608.

repository link (if public, optional): Not public.

code snippet to reproduce:

#![no_std]
#![no_main]
#![feature(abi_x86_interrupt)]

extern crate alloc;

use alloc::boxed::Box;
use alloc::vec::Vec;
use core::alloc::{GlobalAlloc, Layout};
use spin::Mutex;

const STACK_SIZE: usize = 4096;

#[derive(Debug, Clone, Copy)]
pub struct PhysAddr(pub u64);

#[allow(dead_code)]
#[derive(Debug)]
pub struct Task {
    pub id: usize,
    pub stack_pointer: u64,
    pub page_table: PhysAddr,
    pub kernel_stack_top: u64,
    stack: Box<[u8; STACK_SIZE]>,
}

pub struct Scheduler {
    pub tasks: Vec<Task>,
    pub current: usize,
}

pub fn switch_task(scheduler: &mut Scheduler, next: usize) {
    // rust-analyzer reports:
    // E0608: cannot index into a value of type `Vec<Task, Global>`
    //
    // However, `cargo check` succeeds.
    let next_rsp = scheduler.tasks[next].stack_pointer;
    let next_pml4 = scheduler.tasks[next].page_table;
    let next_kstack_top = scheduler.tasks[next].kernel_stack_top;

    let _ = (next_rsp, next_pml4, next_kstack_top);
}

#[global_allocator]
static ALLOCATOR: LockedAllocator = LockedAllocator::new();

pub struct LockedAllocator(Mutex<LinkedListAllocator>);

impl LockedAllocator {
    pub const fn new() -> Self {
        Self(Mutex::new(LinkedListAllocator::new()))
    }
}

pub struct LinkedListAllocator;

impl LinkedListAllocator {
    pub const fn new() -> Self {
        Self
    }

    unsafe fn alloc_inner(&mut self, _layout: Layout) -> *mut u8 {
        core::ptr::null_mut()
    }

    unsafe fn add_free_block(&mut self, _addr: usize, _size: usize) {}
}

unsafe impl GlobalAlloc for LockedAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        unsafe { self.0.lock().alloc_inner(layout) }
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        unsafe {
            self.0.lock().add_free_block(ptr as usize, layout.size());
        }
    }
}

cargo check succeeds for the real project and only emits unrelated warnings, but rust-analyzer inside neovim reports:

E0608: cannot index into a value of type `Vec<Task, Global>`

on valid indexing into alloc::vec::Vec<Task>.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: bug

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions