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
This commit is contained in:
commit
5b68f9c46a
1 changed files with 69 additions and 75 deletions
|
@ -216,25 +216,23 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
|
|||
| Node::Expr(&Expr { kind: ExprKind::Path(_), .. })
|
||||
| Node::TraitRef(..) => {
|
||||
let path = match parent_node {
|
||||
Node::Ty(&Ty {
|
||||
kind: TyKind::Path(QPath::Resolved(_, ref path)), ..
|
||||
})
|
||||
Node::Ty(&Ty { kind: TyKind::Path(QPath::Resolved(_, path)), .. })
|
||||
| Node::Expr(&Expr {
|
||||
kind: ExprKind::Path(QPath::Resolved(_, ref path)),
|
||||
kind:
|
||||
ExprKind::Path(QPath::Resolved(_, path))
|
||||
| ExprKind::Struct(&QPath::Resolved(_, path), ..),
|
||||
..
|
||||
}) => Some(&**path),
|
||||
Node::Expr(&Expr { kind: ExprKind::Struct(ref path, ..), .. }) => {
|
||||
if let QPath::Resolved(_, ref path) = **path {
|
||||
Some(&**path)
|
||||
} else {
|
||||
None
|
||||
})
|
||||
| Node::TraitRef(&TraitRef { path, .. }) => &*path,
|
||||
_ => {
|
||||
tcx.sess.delay_span_bug(
|
||||
DUMMY_SP,
|
||||
&format!("unexpected const parent path {:?}", parent_node),
|
||||
);
|
||||
return tcx.types.err;
|
||||
}
|
||||
}
|
||||
Node::TraitRef(&TraitRef { ref path, .. }) => Some(&**path),
|
||||
_ => None,
|
||||
};
|
||||
|
||||
if let Some(path) = path {
|
||||
// We've encountered an `AnonConst` in some path, so we need to
|
||||
// figure out which generic parameter it corresponds to and return
|
||||
// the relevant type.
|
||||
|
@ -275,7 +273,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
|
|||
}
|
||||
};
|
||||
|
||||
generics
|
||||
let ty = generics
|
||||
.params
|
||||
.iter()
|
||||
.filter(|param| {
|
||||
|
@ -286,10 +284,13 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
|
|||
}
|
||||
})
|
||||
.nth(arg_index)
|
||||
.map(|param| tcx.type_of(param.def_id))
|
||||
.map(|param| tcx.type_of(param.def_id));
|
||||
|
||||
if let Some(ty) = ty {
|
||||
ty
|
||||
} else {
|
||||
// This is no generic parameter associated with the arg. This is
|
||||
// probably from an extra arg where one is not needed.
|
||||
.unwrap_or_else(|| {
|
||||
tcx.sess.delay_span_bug(
|
||||
DUMMY_SP,
|
||||
&format!(
|
||||
|
@ -298,13 +299,6 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: DefId) -> Ty<'_> {
|
|||
),
|
||||
);
|
||||
tcx.types.err
|
||||
})
|
||||
} else {
|
||||
tcx.sess.delay_span_bug(
|
||||
DUMMY_SP,
|
||||
&format!("unexpected const parent path {:?}", parent_node,),
|
||||
);
|
||||
tcx.types.err
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue