Skip to content

Compiler panics on String::from_shared_ascii_str_array #7681

Description

@ironcev

#7683 temporarily introduced String::from_shared_ascii_str_array. Original PR description explained the controversy around it. In the end, I've decided to remove that constructor. However, the compiler panic I noticed while using that constructor is not related to the question of having it in the API or not, and is worth looking at.

This script:

script;

use std::string::*;

fn main() {
    let string_array = __to_str_array("ABCDEF");
    let string = String::from_shared_ascii_str_array(string_array);
    poke(string.ptr());
    poke(__addr_of(string_array));
}

#[inline(never)]
fn poke<T>(_t: T) {}

makes compiler panic, in both debug and release, mode with:

thread 'main' (1062609) panicked at sway-ir/src/analysis/memory_utils.rs:755:10:
Expected arg to be a pointer

The version of from_shared_ascii_str_array that caused this panic was:

pub fn from_shared_ascii_str_array<s: S>(s: S) -> Self {
    __assert_is_str_array::<s>();

    let len = __size_of_str_array::<S>();
    let ptr = __addr_of(s);
    
    Self {
        bytes: Bytes::from_moved_raw_slice(__transmute::<(raw_ptr, u64), raw_slice>((ptr, len))),
    }
}

Later on it was replaced by this version, on which I didn't try to reproduce the above panic, but I assume it still happens:

pub fn from_shared_ascii_str_array<const N: u64>(s: str[N]) -> Self {
    Self {
        bytes: Bytes::from_moved_raw_slice(__transmute::<(raw_ptr, u64), raw_slice>((__addr_of(s), N))),
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcompilerGeneral compiler. Should eventually become more specific as the issue is triagedcompiler: irIRgen and sway-ir including optimization passes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions