MIR ICE on reference-to-reference deref coercion `as` casts (`&String as &str`, `&Arc::from("hi") as &str`)

Open
#253 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

  • #254 by @Piping — closed without merging

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Stale
Tech stack
rust
Domain
backend, compilers

Research direction

Start with the cast handling in libs/relax/types/src/typeck.rs, then trace record_cast_borrow, cast_expr in libs/relax/compiler/src/mir/build/thir/cx/expr.rs, and Coerce::DerefRef in libs/relax/compiler/src/lower.rs. Run the listed relax check and relax test reproducers first. Done means the valid shared and mutable deref casts compile and run, while the invalid casts retain the reported errors; pull request #254 is already open for this issue.

Written by the indexing model from the issue text.

Description

Minimal reproducer

Pure std only (edition = "2021"):

fn main() {
    let s = String::from("hi");
    let r: &str = &s as &str;
    println!("{r}");
}

Also reproduces with open inference variables in the source or target referent, and mutable deref coercions:

use std::sync::Arc;

fn main() {
    let a: &str = &Arc::from("hi") as &str;
    let mut s = String::from("hi");
    let ms: &mut str = &mut s as &mut str;
    let v = vec![1u8, 2];
    let sl: &[u8] = &v as &[_];
    println!("{a} {ms} {sl:?}");
}

Actual behavior (relax check / relax test)

internal: internal compiler error while checking the borrows of repro::main: repro::main: MIR bug: ArrayToPointer cast from unexpected type &String -- Relax bug, report it
internal: internal compiler error while checking the borrows of repro::main: repro::main: MIR bug: bad assignment (&String = &str): NoSolution -- Relax bug, report it

(Same two MIR bugs for &Arc<str>, &mut String, and &Vec<u8, Global>.)

Expected behavior

Matches rustc: deref coercion as casts between references compile and execute without errors, selecting Deref::deref for shared target references and DerefMut::deref_mut for mutable target references, while invalid casts (&s as &mut str, &s as &usize) keep reporting E0605 and missing DerefMut (&mut arc as &mut str) keeps reporting E0596.

Root cause

  1. libs/relax/types/src/typeck.rs (ExprKind::Cast): the (TyKind::Ref, TyKind::Ref) deref coercion branch called self.set_deref_ref(x.0, derefs, None) without recording the trailing Adjust::Borrow at the target type.
  2. record_cast_borrow then saw self.b.coerce(e.0) (Some(Coerce::DerefRef(_))) and appended Adjust::Borrow(tm, borrow_target(source, tm)) using the source reference type (e.g. &String / &Arc<str>), leaving ety_adjusted(source) != expr_ty.
  3. libs/relax/types/src/mir/build/thir/cx/expr.rs (cast_expr) then fell through to else if matches!(source_ty, TyKind::Ref(..)) and emitted PointerCoercion::ArrayToPointer, causing borrowck to report both MIR ICEs.
  4. Additionally, (TyKind::Ref, TyKind::Ref) only deferred casts when the immediate referent was TyKind::Infer(_), missing referents containing open inference variables (&Arc::from("hi") as &str, &v as &[_], deferred multi-step deref), and libs/relax/compiler/src/lower.rs (Coerce::DerefRef) used the source reference's mutability rather than the target reference's mutability when choosing Deref vs DerefMut.
Dominant language
Rust
Stars
601
Forks
34
Avg merge
1h 58m
Merged PRs (30d)
45

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from makepad/relax

All issues in makepad/relax

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.