Skip to content

Commit a95444c

Browse files
Register const preds for Deref adjustments in HIR typeck
1 parent 9f57edf commit a95444c

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+26-4
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
254254
}
255255

256256
for a in &adj {
257-
if let Adjust::NeverToAny = a.kind {
258-
if a.target.is_ty_var() {
259-
self.diverging_type_vars.borrow_mut().insert(a.target);
260-
debug!("apply_adjustments: adding `{:?}` as diverging type var", a.target);
257+
match a.kind {
258+
Adjust::NeverToAny => {
259+
if a.target.is_ty_var() {
260+
self.diverging_type_vars.borrow_mut().insert(a.target);
261+
debug!("apply_adjustments: adding `{:?}` as diverging type var", a.target);
262+
}
263+
}
264+
Adjust::Deref(Some(overloaded_deref)) => {
265+
self.enforce_context_effects(
266+
expr.span,
267+
overloaded_deref.method_call(self.tcx),
268+
self.tcx.mk_args(&[a.target.into()]),
269+
);
270+
}
271+
Adjust::Deref(None) => {
272+
// FIXME(effects): We *could* enforce `&T: ~const Deref` here.
273+
}
274+
Adjust::Pointer(_pointer_coercion) => {
275+
// FIXME(effects): We should probably enforce these.
276+
}
277+
Adjust::ReborrowPin(_region, _mutability) => {
278+
// FIXME(effects): We could enforce these; they correspond to
279+
// `&mut T: DerefMut` tho, so it's kinda moot.
280+
}
281+
Adjust::Borrow(_) => {
282+
// No effects to enforce here.
261283
}
262284
}
263285
}

compiler/rustc_middle/src/ty/adjustment.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
use rustc_hir as hir;
1+
use rustc_hir::def_id::DefId;
22
use rustc_hir::lang_items::LangItem;
3+
use rustc_hir::{self as hir};
34
use rustc_macros::{HashStable, TyDecodable, TyEncodable, TypeFoldable, TypeVisitable};
45
use rustc_span::Span;
56
use rustc_target::abi::FieldIdx;
@@ -125,18 +126,16 @@ pub struct OverloadedDeref<'tcx> {
125126

126127
impl<'tcx> OverloadedDeref<'tcx> {
127128
/// Get the zst function item type for this method call.
128-
pub fn method_call(&self, tcx: TyCtxt<'tcx>, source: Ty<'tcx>) -> Ty<'tcx> {
129+
pub fn method_call(&self, tcx: TyCtxt<'tcx>) -> DefId {
129130
let trait_def_id = match self.mutbl {
130131
hir::Mutability::Not => tcx.require_lang_item(LangItem::Deref, None),
131132
hir::Mutability::Mut => tcx.require_lang_item(LangItem::DerefMut, None),
132133
};
133-
let method_def_id = tcx
134-
.associated_items(trait_def_id)
134+
tcx.associated_items(trait_def_id)
135135
.in_definition_order()
136136
.find(|m| m.kind == ty::AssocKind::Fn)
137137
.unwrap()
138-
.def_id;
139-
Ty::new_fn_def(tcx, method_def_id, [source])
138+
.def_id
140139
}
141140
}
142141

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,9 @@ impl<'tcx> Cx<'tcx> {
136136
Adjust::Deref(Some(deref)) => {
137137
// We don't need to do call adjust_span here since
138138
// deref coercions always start with a built-in deref.
139-
let call = deref.method_call(self.tcx(), expr.ty);
139+
let call_def_id = deref.method_call(self.tcx());
140+
let overloaded_callee =
141+
Ty::new_fn_def(self.tcx(), call_def_id, self.tcx().mk_args(&[expr.ty.into()]));
140142

141143
expr = Expr {
142144
temp_lifetime,
@@ -150,7 +152,13 @@ impl<'tcx> Cx<'tcx> {
150152

151153
let expr = Box::new([self.thir.exprs.push(expr)]);
152154

153-
self.overloaded_place(hir_expr, adjustment.target, Some(call), expr, deref.span)
155+
self.overloaded_place(
156+
hir_expr,
157+
adjustment.target,
158+
Some(overloaded_callee),
159+
expr,
160+
deref.span,
161+
)
154162
}
155163
Adjust::Borrow(AutoBorrow::Ref(_, m)) => ExprKind::Borrow {
156164
borrow_kind: m.to_borrow_kind(),

0 commit comments

Comments
 (0)