Skip to content

Commit 3c06ed0

Browse files
Fix array to slice autoderef in const
1 parent aac2639 commit 3c06ed0

File tree

1 file changed

+31
-5
lines changed
  • compiler/rustc_mir_build/src/thir/cx

1 file changed

+31
-5
lines changed

compiler/rustc_mir_build/src/thir/cx/expr.rs

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ impl<'tcx> Cx<'tcx> {
124124
ExprKind::Deref { arg: self.thir.exprs.push(expr) }
125125
}
126126
Adjust::Deref(Some(deref)) => {
127-
// We don't need to do call adjust_span here since
128-
// deref coercions always start with a built-in deref.
129-
let call = deref.method_call(self.tcx(), expr.ty);
127+
let source_ty = expr.ty;
130128

131129
expr = Expr {
132130
temp_lifetime,
@@ -140,9 +138,37 @@ impl<'tcx> Cx<'tcx> {
140138
},
141139
};
142140

143-
let expr = Box::new([self.thir.exprs.push(expr)]);
141+
let deref_arg_expr = self.thir.exprs.push(expr);
144142

145-
self.overloaded_place(hir_expr, adjustment.target, Some(call), expr, deref.span)
143+
// FIXME: this is a hack to allow us to evaluate `<[T; N]>::deref` as const, since
144+
// it's really just a pointer unsize from `&[T; N]` -> `&[T]`
145+
if let ty::Array(elem_ty, _) = source_ty.kind() {
146+
expr = Expr {
147+
temp_lifetime,
148+
ty: self.tcx.mk_ref(
149+
deref.region,
150+
ty::TypeAndMut { ty: self.tcx.mk_slice(elem_ty), mutbl: deref.mutbl },
151+
),
152+
span,
153+
kind: ExprKind::Pointer {
154+
cast: PointerCast::Unsize,
155+
source: deref_arg_expr,
156+
},
157+
};
158+
ExprKind::Deref { arg: self.thir.exprs.push(expr) }
159+
} else {
160+
// We don't need to do call adjust_span here since
161+
// deref coercions always start with a built-in deref.
162+
let call = deref.method_call(self.tcx(), source_ty);
163+
164+
self.overloaded_place(
165+
hir_expr,
166+
adjustment.target,
167+
Some(call),
168+
Box::new([deref_arg_expr]),
169+
deref.span,
170+
)
171+
}
146172
}
147173
Adjust::Borrow(AutoBorrow::Ref(_, m)) => ExprKind::Borrow {
148174
borrow_kind: m.to_borrow_kind(),

0 commit comments

Comments
 (0)