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

@ -726,7 +726,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceProjectionWith<'_, 'tcx> {
)
.expect("expected to be able to unify goal projection with dyn's projection"),
);
proj.term.ty().unwrap()
proj.term.expect_type()
} else {
ty.super_fold_with(self)
}

View file

@ -17,7 +17,7 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
) -> QueryResult<'tcx> {
let tcx = self.interner();
let opaque_ty = goal.predicate.alias;
let expected = goal.predicate.term.ty().expect("no such thing as an opaque const");
let expected = goal.predicate.term.as_type().expect("no such thing as an opaque const");
match (goal.param_env.reveal(), self.solver_mode()) {
(Reveal::UserFacing, SolverMode::Normal) => {

View file

@ -553,7 +553,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
}
fn is_self_referential_projection(&self, p: ty::PolyProjectionPredicate<'tcx>) -> bool {
if let Some(ty) = p.term().skip_binder().ty() {
if let Some(ty) = p.term().skip_binder().as_type() {
matches!(ty.kind(), ty::Alias(ty::Projection, proj) if proj == &p.skip_binder().projection_term.expect_ty(self.tcx))
} else {
false

View file

@ -1112,7 +1112,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
{
Some((
DefIdOrName::DefId(def_id),
pred.kind().rebind(proj.term.ty().unwrap()),
pred.kind().rebind(proj.term.expect_type()),
pred.kind().rebind(args.as_slice()),
))
} else {
@ -1129,7 +1129,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
{
Some((
DefIdOrName::Name("trait object"),
pred.rebind(proj.term.ty().unwrap()),
pred.rebind(proj.term.expect_type()),
pred.rebind(args.as_slice()),
))
} else {
@ -1157,7 +1157,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
{
Some((
name,
pred.kind().rebind(proj.term.ty().unwrap()),
pred.kind().rebind(proj.term.expect_type()),
pred.kind().rebind(args.as_slice()),
))
} else {
@ -3840,7 +3840,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
})
} else if let Some(where_pred) = where_pred.as_projection_clause()
&& let Some(failed_pred) = failed_pred.as_projection_clause()
&& let Some(found) = failed_pred.skip_binder().term.ty()
&& let Some(found) = failed_pred.skip_binder().term.as_type()
{
type_diffs = vec![Sorts(ty::error::ExpectedFound {
expected: where_pred

View file

@ -259,7 +259,7 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
obligations.len = ?self.obligations.len(),
"AssocTypeNormalizer: normalized type"
);
normalized_ty.ty().unwrap()
normalized_ty.expect_type()
}
ty::Projection => {
@ -289,7 +289,7 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
)
.ok()
.flatten()
.map(|term| term.ty().unwrap())
.map(|term| term.expect_type())
.map(|normalized_ty| {
PlaceholderReplacer::replace_placeholders(
infcx,

View file

@ -946,7 +946,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
// since we don't actually use them.
&mut vec![],
)
.ty()
.as_type()
.unwrap();
if let ty::Dynamic(data, ..) = ty.kind() { data.principal() } else { None }

View file

@ -286,7 +286,7 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>(
// implemented, but rather from a "second order" obligation, where an associated
// type has a projection coming from another associated type.
// See `tests/ui/traits/assoc-type-in-superbad.rs` for an example.
if let Some(term_ty) = proj.term.ty()
if let Some(term_ty) = proj.term.as_type()
&& let Some(impl_item_span) = ty_to_impl_span(term_ty)
{
cause.span = impl_item_span;