#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))),
}
}
#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:
makes compiler panic, in both
debugandrelease, mode with:The version of
from_shared_ascii_str_arraythat caused this panic was: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: