Skip to content

Cannot use &str with the Index trait (LLVM assertion failure: "Invoking a function with bad signature") #18487

Closed
@nodakai

Description

@nodakai
use std::ops::Index;

struct S {
    x: Vec<String>
}

impl Index<uint, str> for S {
    fn index<'a> (&'a self, n: &uint) -> &'a str {
        self.x[*n].as_slice()
    }
}

fn main() {
    let s = S { x: vec![ "X".to_string() ] };
    println!("{}", &s[0]);
}

rustc: /build/rust-git/src/rust/src/llvm/lib/IR/Instructions.cpp:545: void llvm::InvokeInst::init(llvm::Value*, llvm::BasicBlock*, llvm::BasicBlock*, llvm::ArrayRef<llvm::Value*>, const llvm::Twine&): Assertion ((Args.size() == FTy->getNumParams()) || (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && "Invoking a function with bad signature"' failed. Aborted`

Also, &s[0] is ugly.

Another approach:

use std::ops::Index;

struct S {
    x: Vec<String>
}

impl Index<uint, & &str> for S {
    fn index<'a> (&'a self, n: &uint) -> & &'a str {
        &self.x[*n].as_slice()
    }
}

fn main() {
    let s = S { x: vec![ "X".to_string() ] };
    println!("{}", s[0]);
}

This will get nowhere because self.x[*n].as_slice() creates a local string slice object which will quickly goes out of scope. Perhaps we need to wait for introduction of the IndexGet trait.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions