sway icon indicating copy to clipboard operation
sway copied to clipboard

`create_array_from_vec` coerces uneven `str[N]` elements with an `assert!` panic

Open JoshuaBatty opened this issue 3 months ago • 1 comments

Description

Typing an array literal of str[N] values with different lengths inside the LSP causes the backend to panic in sway-core/src/ir_generation/const_eval.rs:268.

The panic comes from create_array_from_vec asserting that every element unified with the inferred array element type. In an incomplete-editing scenario we really just want a structured error, not for the compiler abort and thus the language server to crash.

Ideally, we handle this similar to other ErrorRecovery efforts where the best effort lexed, parsed, and typed AST's are still returned for the LSP to work with.

Steps to Reproduce

script;

const WORDS_CONST = [__to_str_array("lumen"), __to_str_array("food")];

fn main() {
    let _ = WORDS_CONST;
}

this crashes the language server with:

thread '<unnamed>' panicked at sway-core/src/ir_generation/const_eval.rs:268:5:
assertion failed: {
    let unify_check = UnifyCheck::coercion(lookup.engines);
    element_types.iter().all(|tid| unify_check.check(*tid, elem_type))
}
stack backtrace:
   0: __rustc::rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: sway_core::ir_generation::const_eval::create_array_from_vec
   4: sway_core::ir_generation::const_eval::const_eval_typed_expr
   5: sway_core::ir_generation::const_eval::compile_constant_expression_to_constant
   6: sway_core::ir_generation::const_eval::compile_const_decl
   7: sway_core::ir_generation::compile::compile_constants
   8: sway_core::ir_generation::compile::compile_constants_for_package::traverse
   9: sway_core::parsed_to_ast
  10: sway_core::compile_to_ast
  11: forc_pkg::pkg::check
  12: sway_lsp::core::session::compile
  13: sway_lsp::core::session::parse_project
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

JoshuaBatty avatar Sep 29 '25 04:09 JoshuaBatty

@JoshuaBatty Can I pick this issue up, I have zero pending issues now .

Rustix69 avatar Oct 02 '25 19:10 Rustix69