MIR ICE on reference-to-reference deref coercion `as` casts (`&String as &str`, `&Arc::from("hi") as &str`)
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
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
libs/relax/types/src/typeck.rs(ExprKind::Cast): the(TyKind::Ref, TyKind::Ref)deref coercion branch calledself.set_deref_ref(x.0, derefs, None)without recording the trailingAdjust::Borrowat the target type.record_cast_borrowthen sawself.b.coerce(e.0)(Some(Coerce::DerefRef(_))) and appendedAdjust::Borrow(tm, borrow_target(source, tm))using the source reference type (e.g.&String/&Arc<str>), leavingety_adjusted(source) != expr_ty.libs/relax/types/src/mir/build/thir/cx/expr.rs(cast_expr) then fell through toelse if matches!(source_ty, TyKind::Ref(..))and emittedPointerCoercion::ArrayToPointer, causing borrowck to report both MIR ICEs.- Additionally,
(TyKind::Ref, TyKind::Ref)only deferred casts when the immediate referent wasTyKind::Infer(_), missing referents containing open inference variables (&Arc::from("hi") as &str,&v as &[_], deferred multi-step deref), andlibs/relax/compiler/src/lower.rs(Coerce::DerefRef) used the source reference's mutability rather than the target reference's mutability when choosingDerefvsDerefMut.
- 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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from makepad/relax
-
std: `SliceIndex<str>` missing for `std::ops` ranges, so generic `R: SliceIndex<str>` bounds failOpen
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 25/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 42/100
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 18/100
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 18/100
Maintainers usually reply within 1 day
Similar issues
-
CI/CD ignore-for-release
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
eclipse-ankaios/ankaios#844 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
symposium-dev/symposium#530 ·
Maintainers usually reply within 5 days
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
topgrade-rs/topgrade#2540 ·
Maintainers usually reply within 1 day