about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDing Xiang Fei <dingxiangfei2009@protonmail.ch>2025-09-25 01:53:34 +0800
committerXiangfei Ding <dingxiangfei2009@protonmail.ch>2025-09-25 01:54:25 +0800
commitb77de834c031890c048f8164d4b5979d2511c00e (patch)
treefd0ff7d64673b4cfeae8f5115ef7520b57faa85f
parent120162e30ffa92308a6a3a76f2328467fbd10ced (diff)
downloadrust-b77de834c031890c048f8164d4b5979d2511c00e.tar.gz
rust-b77de834c031890c048f8164d4b5979d2511c00e.zip
mark THIR use as candidate for constness check
-rw-r--r--compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs5
-rw-r--r--tests/ui/coercion/no_local_for_coerced_const-issue-143671.rs8
2 files changed, 12 insertions, 1 deletions
diff --git a/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs b/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs
index d6dc12e5955..3a5839f2d40 100644
--- a/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs
+++ b/compiler/rustc_mir_build/src/builder/expr/as_rvalue.rs
@@ -661,8 +661,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
     /// operations that can be const-folded today.
     fn check_constness(&self, mut kind: &'a ExprKind<'tcx>) -> bool {
         loop {
+            debug!(?kind, "check_constness");
             match kind {
-                &ExprKind::PointerCoercion {
+                &ExprKind::ValueTypeAscription { source: eid, user_ty: _, user_ty_span: _ }
+                | &ExprKind::Use { source: eid }
+                | &ExprKind::PointerCoercion {
                     cast: PointerCoercion::Unsize,
                     source: eid,
                     is_from_as_cast: _,
diff --git a/tests/ui/coercion/no_local_for_coerced_const-issue-143671.rs b/tests/ui/coercion/no_local_for_coerced_const-issue-143671.rs
index 5924809f34c..38479d9070b 100644
--- a/tests/ui/coercion/no_local_for_coerced_const-issue-143671.rs
+++ b/tests/ui/coercion/no_local_for_coerced_const-issue-143671.rs
@@ -29,6 +29,7 @@ const Y: X<'static, i32> = X { f: &0 };
 fn main() {
     let _: [X<'static, dyn Display>; 0] = [Y; 0];
     coercion_on_weak_in_const();
+    coercion_on_weak_as_cast();
 }
 
 fn coercion_on_weak_in_const() {
@@ -36,3 +37,10 @@ fn coercion_on_weak_in_const() {
     const Y: [Weak<dyn Send>; 0] = [X; 0];
     let _ = Y;
 }
+
+fn coercion_on_weak_as_cast() {
+    const Y: X<'static, i32> = X { f: &0 };
+    // What happens in the following code is that
+    // a constant is explicitly coerced into
+    let _a: [X<'static, dyn Display>; 0] = [Y as X<'static, dyn Display>; 0];
+}