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

Aperta
#253 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

  • #254 di @Piping — chiusa senza merge

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Ferma
Stack tecnologico
rust
Ambito
backend, compilers

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.
Lingua principale
Rust
Stelle
601
Fork
34
Merge medio
1h 58m
PR unite (30g)
45

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di makepad/relax

Tutte le issue di makepad/relax

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.