Skip to content

Commit 5b68f9c

Browse files
authored
Rollup merge of #70478 - lcnr:refactor-type_of, r=varkor
Refactor type_of for constants If I have to look at this function for a few hours I want it to at least look good. r? @varkor
2 parents 23d3fa2 + c339b2e commit 5b68f9c

File tree

1 file changed

+71
-77
lines changed

1 file changed

+71
-77
lines changed

src/librustc_typeck/collect/type_of.rs

+71-77
Original file line numberDiff line numberDiff line change
@@ -216,93 +216,87 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
216216
| Node::Expr(&Expr { kind: ExprKind::Path(_), .. })
217217
| Node::TraitRef(..) => {
218218
let path = match parent_node {
219-
Node::Ty(&Ty {
220-
kind: TyKind::Path(QPath::Resolved(_, ref path)), ..
221-
})
219+
Node::Ty(&Ty { kind: TyKind::Path(QPath::Resolved(_, path)), .. })
222220
| Node::Expr(&Expr {
223-
kind: ExprKind::Path(QPath::Resolved(_, ref path)),
221+
kind:
222+
ExprKind::Path(QPath::Resolved(_, path))
223+
| ExprKind::Struct(&QPath::Resolved(_, path), ..),
224224
..
225-
}) => Some(&**path),
226-
Node::Expr(&Expr { kind: ExprKind::Struct(ref path, ..), .. }) => {
227-
if let QPath::Resolved(_, ref path) = **path {
228-
Some(&**path)
229-
} else {
230-
None
231-
}
225+
})
226+
| Node::TraitRef(&TraitRef { path, .. }) => &*path,
227+
_ => {
228+
tcx.sess.delay_span_bug(
229+
DUMMY_SP,
230+
&format!("unexpected const parent path {:?}", parent_node),
231+
);
232+
return tcx.types.err;
232233
}
233-
Node::TraitRef(&TraitRef { ref path, .. }) => Some(&**path),
234-
_ => None,
235234
};
236235

237-
if let Some(path) = path {
238-
// We've encountered an `AnonConst` in some path, so we need to
239-
// figure out which generic parameter it corresponds to and return
240-
// the relevant type.
241-
242-
let (arg_index, segment) = path
243-
.segments
244-
.iter()
245-
.filter_map(|seg| seg.args.as_ref().map(|args| (args.args, seg)))
246-
.find_map(|(args, seg)| {
247-
args.iter()
248-
.filter(|arg| arg.is_const())
249-
.enumerate()
250-
.filter(|(_, arg)| arg.id() == hir_id)
251-
.map(|(index, _)| (index, seg))
252-
.next()
253-
})
254-
.unwrap_or_else(|| {
255-
bug!("no arg matching AnonConst in path");
256-
});
257-
258-
// Try to use the segment resolution if it is valid, otherwise we
259-
// default to the path resolution.
260-
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
261-
let generics = match res {
262-
Res::Def(DefKind::Ctor(..), def_id) => {
263-
tcx.generics_of(tcx.parent(def_id).unwrap())
264-
}
265-
Res::Def(_, def_id) => tcx.generics_of(def_id),
266-
res => {
267-
tcx.sess.delay_span_bug(
268-
DUMMY_SP,
269-
&format!(
270-
"unexpected anon const res {:?} in path: {:?}",
271-
res, path,
272-
),
273-
);
274-
return tcx.types.err;
236+
// We've encountered an `AnonConst` in some path, so we need to
237+
// figure out which generic parameter it corresponds to and return
238+
// the relevant type.
239+
240+
let (arg_index, segment) = path
241+
.segments
242+
.iter()
243+
.filter_map(|seg| seg.args.as_ref().map(|args| (args.args, seg)))
244+
.find_map(|(args, seg)| {
245+
args.iter()
246+
.filter(|arg| arg.is_const())
247+
.enumerate()
248+
.filter(|(_, arg)| arg.id() == hir_id)
249+
.map(|(index, _)| (index, seg))
250+
.next()
251+
})
252+
.unwrap_or_else(|| {
253+
bug!("no arg matching AnonConst in path");
254+
});
255+
256+
// Try to use the segment resolution if it is valid, otherwise we
257+
// default to the path resolution.
258+
let res = segment.res.filter(|&r| r != Res::Err).unwrap_or(path.res);
259+
let generics = match res {
260+
Res::Def(DefKind::Ctor(..), def_id) => {
261+
tcx.generics_of(tcx.parent(def_id).unwrap())
262+
}
263+
Res::Def(_, def_id) => tcx.generics_of(def_id),
264+
res => {
265+
tcx.sess.delay_span_bug(
266+
DUMMY_SP,
267+
&format!(
268+
"unexpected anon const res {:?} in path: {:?}",
269+
res, path,
270+
),
271+
);
272+
return tcx.types.err;
273+
}
274+
};
275+
276+
let ty = generics
277+
.params
278+
.iter()
279+
.filter(|param| {
280+
if let ty::GenericParamDefKind::Const = param.kind {
281+
true
282+
} else {
283+
false
275284
}
276-
};
285+
})
286+
.nth(arg_index)
287+
.map(|param| tcx.type_of(param.def_id));
277288

278-
generics
279-
.params
280-
.iter()
281-
.filter(|param| {
282-
if let ty::GenericParamDefKind::Const = param.kind {
283-
true
284-
} else {
285-
false
286-
}
287-
})
288-
.nth(arg_index)
289-
.map(|param| tcx.type_of(param.def_id))
290-
// This is no generic parameter associated with the arg. This is
291-
// probably from an extra arg where one is not needed.
292-
.unwrap_or_else(|| {
293-
tcx.sess.delay_span_bug(
294-
DUMMY_SP,
295-
&format!(
296-
"missing generic parameter for `AnonConst`, parent: {:?}, res: {:?}",
297-
parent_node, res
298-
),
299-
);
300-
tcx.types.err
301-
})
289+
if let Some(ty) = ty {
290+
ty
302291
} else {
292+
// This is no generic parameter associated with the arg. This is
293+
// probably from an extra arg where one is not needed.
303294
tcx.sess.delay_span_bug(
304295
DUMMY_SP,
305-
&format!("unexpected const parent path {:?}", parent_node,),
296+
&format!(
297+
"missing generic parameter for `AnonConst`, parent: {:?}, res: {:?}",
298+
parent_node, res
299+
),
306300
);
307301
tcx.types.err
308302
}

0 commit comments

Comments
 (0)