Skip to content

Commit 391ef47

Browse files
committed
Simplify typeck entry.
1 parent 83da9a8 commit 391ef47

File tree

1 file changed

+46
-51
lines changed
  • compiler/rustc_hir_typeck/src

1 file changed

+46
-51
lines changed

compiler/rustc_hir_typeck/src/lib.rs

+46-51
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,9 @@ pub struct LocalTy<'tcx> {
106106
/// (notably closures), `typeck_results(def_id)` would wind up
107107
/// redirecting to the owning function.
108108
fn primary_body_of(
109-
tcx: TyCtxt<'_>,
110-
id: hir::HirId,
109+
node: Node<'_>,
111110
) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnSig<'_>>)> {
112-
match tcx.hir().get(id) {
111+
match node {
113112
Node::Item(item) => match item.kind {
114113
hir::ItemKind::Const(ty, body) | hir::ItemKind::Static(ty, _, body) => {
115114
Some((body, Some(ty), None))
@@ -143,8 +142,7 @@ fn has_typeck_results(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
143142
}
144143

145144
if let Some(def_id) = def_id.as_local() {
146-
let id = tcx.hir().local_def_id_to_hir_id(def_id);
147-
primary_body_of(tcx, id).is_some()
145+
primary_body_of(tcx.hir().get_by_def_id(def_id)).is_some()
148146
} else {
149147
false
150148
}
@@ -199,10 +197,11 @@ fn typeck_with_fallback<'tcx>(
199197
}
200198

201199
let id = tcx.hir().local_def_id_to_hir_id(def_id);
200+
let node = tcx.hir().get(id);
202201
let span = tcx.hir().span(id);
203202

204203
// Figure out what primary body this item has.
205-
let (body_id, body_ty, fn_sig) = primary_body_of(tcx, id).unwrap_or_else(|| {
204+
let (body_id, body_ty, fn_sig) = primary_body_of(node).unwrap_or_else(|| {
206205
span_bug!(span, "can't type-check body of {:?}", def_id);
207206
});
208207
let body = tcx.hir().body(body_id);
@@ -231,53 +230,49 @@ fn typeck_with_fallback<'tcx>(
231230

232231
check_fn(&mut fcx, fn_sig, decl, def_id, body, None);
233232
} else {
234-
let expected_type = body_ty
235-
.and_then(|ty| match ty.kind {
236-
hir::TyKind::Infer => Some(fcx.astconv().ast_ty_to_ty(ty)),
237-
_ => None,
238-
})
239-
.unwrap_or_else(|| match tcx.hir().get(id) {
240-
Node::AnonConst(_) => match tcx.hir().get(tcx.hir().parent_id(id)) {
241-
Node::Expr(&hir::Expr {
242-
kind: hir::ExprKind::ConstBlock(ref anon_const),
243-
..
244-
}) if anon_const.hir_id == id => fcx.next_ty_var(TypeVariableOrigin {
233+
let expected_type = if let Some(&hir::Ty { kind: hir::TyKind::Infer, span, .. }) = body_ty {
234+
Some(fcx.next_ty_var(TypeVariableOrigin {
235+
kind: TypeVariableOriginKind::TypeInference,
236+
span,
237+
}))
238+
} else if let Node::AnonConst(_) = node {
239+
match tcx.hir().get(tcx.hir().parent_id(id)) {
240+
Node::Expr(&hir::Expr {
241+
kind: hir::ExprKind::ConstBlock(ref anon_const), ..
242+
}) if anon_const.hir_id == id => Some(fcx.next_ty_var(TypeVariableOrigin {
243+
kind: TypeVariableOriginKind::TypeInference,
244+
span,
245+
})),
246+
Node::Ty(&hir::Ty { kind: hir::TyKind::Typeof(ref anon_const), .. })
247+
if anon_const.hir_id == id =>
248+
{
249+
Some(fcx.next_ty_var(TypeVariableOrigin {
245250
kind: TypeVariableOriginKind::TypeInference,
246251
span,
247-
}),
248-
Node::Ty(&hir::Ty { kind: hir::TyKind::Typeof(ref anon_const), .. })
249-
if anon_const.hir_id == id =>
250-
{
251-
fcx.next_ty_var(TypeVariableOrigin {
252-
kind: TypeVariableOriginKind::TypeInference,
253-
span,
254-
})
255-
}
256-
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), .. })
257-
| Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), .. }) => {
258-
let operand_ty = asm.operands.iter().find_map(|(op, _op_sp)| match op {
259-
hir::InlineAsmOperand::Const { anon_const }
260-
if anon_const.hir_id == id =>
261-
{
262-
// Inline assembly constants must be integers.
263-
Some(fcx.next_int_var())
264-
}
265-
hir::InlineAsmOperand::SymFn { anon_const }
266-
if anon_const.hir_id == id =>
267-
{
268-
Some(fcx.next_ty_var(TypeVariableOrigin {
269-
kind: TypeVariableOriginKind::MiscVariable,
270-
span,
271-
}))
272-
}
273-
_ => None,
274-
});
275-
operand_ty.unwrap_or_else(fallback)
276-
}
277-
_ => fallback(),
278-
},
279-
_ => fallback(),
280-
});
252+
}))
253+
}
254+
Node::Expr(&hir::Expr { kind: hir::ExprKind::InlineAsm(asm), .. })
255+
| Node::Item(&hir::Item { kind: hir::ItemKind::GlobalAsm(asm), .. }) => {
256+
asm.operands.iter().find_map(|(op, _op_sp)| match op {
257+
hir::InlineAsmOperand::Const { anon_const } if anon_const.hir_id == id => {
258+
// Inline assembly constants must be integers.
259+
Some(fcx.next_int_var())
260+
}
261+
hir::InlineAsmOperand::SymFn { anon_const } if anon_const.hir_id == id => {
262+
Some(fcx.next_ty_var(TypeVariableOrigin {
263+
kind: TypeVariableOriginKind::MiscVariable,
264+
span,
265+
}))
266+
}
267+
_ => None,
268+
})
269+
}
270+
_ => None,
271+
}
272+
} else {
273+
None
274+
};
275+
let expected_type = expected_type.unwrap_or_else(fallback);
281276

282277
let expected_type = fcx.normalize(body.value.span, expected_type);
283278
fcx.require_type_is_sized(expected_type, body.value.span, traits::ConstSized);

0 commit comments

Comments
 (0)