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:
许杰友 Jieyou Xu (Joe) 2024-06-04 08:25:48 +01:00 committed by GitHub
commit b477f89041
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
23 changed files with 46 additions and 36 deletions

View file

@ -42,7 +42,7 @@ fn is_arg_ty_unified_in_fn<'tcx>(
cx.tcx.predicates_of(fn_id).predicates.iter().any(|(clause, _)| {
clause
.as_projection_clause()
.and_then(|p| p.map_bound(|p| p.term.ty()).transpose())
.and_then(|p| p.map_bound(|p| p.term.as_type()).transpose())
.is_some_and(|ty| ty.skip_binder() == arg_ty_in_args)
}) || fn_sig
.inputs()

View file

@ -311,7 +311,7 @@ fn is_mixed_projection_predicate<'tcx>(
) -> bool {
let generics = cx.tcx.generics_of(callee_def_id);
// The predicate requires the projected type to equal a type parameter from the parent context.
if let Some(term_ty) = projection_predicate.term.ty()
if let Some(term_ty) = projection_predicate.term.as_type()
&& let ty::Param(term_param_ty) = term_ty.kind()
&& (term_param_ty.index as usize) < generics.parent_count
{
@ -370,7 +370,7 @@ fn replace_types<'tcx>(
if replaced.insert(param_ty.index) {
for projection_predicate in projection_predicates {
if projection_predicate.projection_term.self_ty() == param_ty.to_ty(cx.tcx)
&& let Some(term_ty) = projection_predicate.term.ty()
&& let Some(term_ty) = projection_predicate.term.as_type()
&& let ty::Param(term_param_ty) = term_ty.kind()
{
let projection = projection_predicate

View file

@ -100,12 +100,12 @@ fn get_args_to_check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Ve
{
if ord_preds
.iter()
.any(|ord| Some(ord.self_ty()) == return_ty_pred.term.ty())
.any(|ord| Some(ord.self_ty()) == return_ty_pred.term.as_type())
{
args_to_check.push((i, "Ord".to_string()));
} else if partial_ord_preds
.iter()
.any(|pord| pord.self_ty() == return_ty_pred.term.ty().unwrap())
.any(|pord| pord.self_ty() == return_ty_pred.term.expect_type())
{
args_to_check.push((i, "PartialOrd".to_string()));
}

View file

@ -750,7 +750,7 @@ pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<ExprFnSig<'t
let output = bounds
.projection_bounds()
.find(|p| lang_items.fn_once_output().map_or(false, |id| id == p.item_def_id()))
.map(|p| p.map_bound(|p| p.term.ty().unwrap()));
.map(|p| p.map_bound(|p| p.term.expect_type()));
Some(ExprFnSig::Trait(bound.map_bound(|b| b.args.type_at(0)), output, None))
},
_ => None,
@ -798,7 +798,7 @@ fn sig_from_bounds<'tcx>(
// Multiple different fn trait impls. Is this even allowed?
return None;
}
output = Some(pred.kind().rebind(p.term.ty().unwrap()));
output = Some(pred.kind().rebind(p.term.expect_type()));
},
_ => (),
}
@ -836,7 +836,7 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: AliasTy<'tcx>) -> Option
// Multiple different fn trait impls. Is this even allowed?
return None;
}
output = pred.kind().rebind(p.term.ty()).transpose();
output = pred.kind().rebind(p.term.as_type()).transpose();
},
_ => (),
}