Rollup merge of #125750 - compiler-errors:expect, r=lcnr
Align `Term` methods with `GenericArg` methods, add `Term::expect_*` * `Term::ty` -> `Term::as_type`. * `Term::ct` -> `Term::as_const`. * Adds `Term::expect_type` and `Term::expect_const`, and uses them in favor of `.ty().unwrap()`, etc. I could also shorten these to `as_ty` and then do `GenericArg::as_ty` as well, but I do think the `as_` is important to signal that this is a conversion method, and not a getter, like `Const::ty` is. r? types
This commit is contained in:
commit
b477f89041
23 changed files with 46 additions and 36 deletions
|
@ -624,14 +624,22 @@ impl<'tcx> Term<'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn ty(&self) -> Option<Ty<'tcx>> {
|
||||
pub fn as_type(&self) -> Option<Ty<'tcx>> {
|
||||
if let TermKind::Ty(ty) = self.unpack() { Some(ty) } else { None }
|
||||
}
|
||||
|
||||
pub fn ct(&self) -> Option<Const<'tcx>> {
|
||||
pub fn expect_type(&self) -> Ty<'tcx> {
|
||||
self.as_type().expect("expected a type, but found a const")
|
||||
}
|
||||
|
||||
pub fn as_const(&self) -> Option<Const<'tcx>> {
|
||||
if let TermKind::Const(c) = self.unpack() { Some(c) } else { None }
|
||||
}
|
||||
|
||||
pub fn expect_const(&self) -> Const<'tcx> {
|
||||
self.as_const().expect("expected a const, but found a type")
|
||||
}
|
||||
|
||||
pub fn into_arg(self) -> GenericArg<'tcx> {
|
||||
match self.unpack() {
|
||||
TermKind::Ty(ty) => ty.into(),
|
||||
|
|
|
@ -1077,7 +1077,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
|
|||
}
|
||||
|
||||
p!(")");
|
||||
if let Some(ty) = return_ty.skip_binder().ty() {
|
||||
if let Some(ty) = return_ty.skip_binder().as_type() {
|
||||
if !ty.is_unit() {
|
||||
p!(" -> ", print(return_ty));
|
||||
}
|
||||
|
@ -1144,7 +1144,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
|
|||
for (assoc_item_def_id, term) in assoc_items {
|
||||
// Skip printing `<{coroutine@} as Coroutine<_>>::Return` from async blocks,
|
||||
// unless we can find out what coroutine return type it comes from.
|
||||
let term = if let Some(ty) = term.skip_binder().ty()
|
||||
let term = if let Some(ty) = term.skip_binder().as_type()
|
||||
&& let ty::Alias(ty::Projection, proj) = ty.kind()
|
||||
&& let Some(assoc) = tcx.opt_associated_item(proj.def_id)
|
||||
&& assoc.trait_container(tcx) == tcx.lang_items().coroutine_trait()
|
||||
|
@ -1322,7 +1322,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
|
|||
p!(pretty_fn_sig(
|
||||
tys,
|
||||
false,
|
||||
proj.skip_binder().term.ty().expect("Return type was a const")
|
||||
proj.skip_binder().term.as_type().expect("Return type was a const")
|
||||
));
|
||||
resugared = true;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue