Skip to content

Commit b477f89

Browse files
authored
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
2 parents 6c2cf0b + 273b990 commit b477f89

File tree

23 files changed

+46
-36
lines changed

23 files changed

+46
-36
lines changed

compiler/rustc_codegen_ssa/src/debuginfo/type_names.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ fn push_debuginfo_type_name<'tcx>(
263263
let ExistentialProjection { def_id: item_def_id, term, .. } =
264264
tcx.instantiate_bound_regions_with_erased(bound);
265265
// FIXME(associated_const_equality): allow for consts here
266-
(item_def_id, term.ty().unwrap())
266+
(item_def_id, term.expect_type())
267267
})
268268
.collect();
269269

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2281,7 +2281,7 @@ fn try_report_async_mismatch<'tcx>(
22812281
&& let Some(proj) = proj.no_bound_vars()
22822282
&& infcx.can_eq(
22832283
error.root_obligation.param_env,
2284-
proj.term.ty().unwrap(),
2284+
proj.term.expect_type(),
22852285
impl_sig.output(),
22862286
)
22872287
{

compiler/rustc_hir_analysis/src/check/compare_impl_item/refine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ fn report_mismatched_rpitit_signature<'tcx>(
267267
.explicit_item_bounds(future_ty.def_id)
268268
.iter_instantiated_copied(tcx, future_ty.args)
269269
.find_map(|(clause, _)| match clause.kind().no_bound_vars()? {
270-
ty::ClauseKind::Projection(proj) => proj.term.ty(),
270+
ty::ClauseKind::Projection(proj) => proj.term.as_type(),
271271
_ => None,
272272
})
273273
else {

compiler/rustc_hir_analysis/src/check/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,9 @@ fn fn_sig_suggestion<'tcx>(
441441
output = if let ty::Alias(_, alias_ty) = *output.kind() {
442442
tcx.explicit_item_super_predicates(alias_ty.def_id)
443443
.iter_instantiated_copied(tcx, alias_ty.args)
444-
.find_map(|(bound, _)| bound.as_projection_clause()?.no_bound_vars()?.term.ty())
444+
.find_map(|(bound, _)| {
445+
bound.as_projection_clause()?.no_bound_vars()?.term.as_type()
446+
})
445447
.unwrap_or_else(|| {
446448
span_bug!(
447449
ident.span,

compiler/rustc_hir_typeck/src/closure.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
485485
};
486486

487487
// Since this is a return parameter type it is safe to unwrap.
488-
let ret_param_ty = projection.skip_binder().term.ty().unwrap();
488+
let ret_param_ty = projection.skip_binder().term.expect_type();
489489
let ret_param_ty = self.resolve_vars_if_possible(ret_param_ty);
490490
debug!(?ret_param_ty);
491491

@@ -956,7 +956,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
956956
let output_ty = self.resolve_vars_if_possible(predicate.term);
957957
debug!("deduce_future_output_from_projection: output_ty={:?}", output_ty);
958958
// This is a projection on a Fn trait so will always be a type.
959-
Some(output_ty.ty().unwrap())
959+
Some(output_ty.expect_type())
960960
}
961961

962962
/// Converts the types that the user supplied, in case that doing

compiler/rustc_hir_typeck/src/typeck_root_ctxt.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ impl<'tcx> TypeckRootCtxt<'tcx> {
160160
{
161161
// If the projection predicate (Foo::Bar == X) has X as a non-TyVid,
162162
// we need to make it into one.
163-
if let Some(vid) = predicate.term.ty().and_then(|ty| ty.ty_vid()) {
163+
if let Some(vid) = predicate.term.as_type().and_then(|ty| ty.ty_vid()) {
164164
debug!("infer_var_info: {:?}.output = true", vid);
165165
infer_var_info.entry(vid).or_default().output = true;
166166
}

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ impl<'tcx> InferCtxt<'tcx> {
425425
ty::ClauseKind::Projection(projection_predicate)
426426
if projection_predicate.projection_term.def_id == item_def_id =>
427427
{
428-
projection_predicate.term.ty()
428+
projection_predicate.term.as_type()
429429
}
430430
_ => None,
431431
})

compiler/rustc_infer/src/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,8 @@ pub enum ValuePairs<'tcx> {
424424
impl<'tcx> ValuePairs<'tcx> {
425425
pub fn ty(&self) -> Option<(Ty<'tcx>, Ty<'tcx>)> {
426426
if let ValuePairs::Terms(ExpectedFound { expected, found }) = self
427-
&& let Some(expected) = expected.ty()
428-
&& let Some(found) = found.ty()
427+
&& let Some(expected) = expected.as_type()
428+
&& let Some(found) = found.as_type()
429429
{
430430
Some((expected, found))
431431
} else {

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
8383
};
8484
// Only check types, since those are the only things that may
8585
// have opaques in them anyways.
86-
let Some(proj_term) = proj.term.ty() else { return };
86+
let Some(proj_term) = proj.term.as_type() else { return };
8787

8888
// HACK: `impl Trait<Assoc = impl Trait2>` from an RPIT is "ok"...
8989
if let ty::Alias(ty::Opaque, opaque_ty) = *proj_term.kind()

compiler/rustc_middle/src/ty/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -624,14 +624,22 @@ impl<'tcx> Term<'tcx> {
624624
}
625625
}
626626

627-
pub fn ty(&self) -> Option<Ty<'tcx>> {
627+
pub fn as_type(&self) -> Option<Ty<'tcx>> {
628628
if let TermKind::Ty(ty) = self.unpack() { Some(ty) } else { None }
629629
}
630630

631-
pub fn ct(&self) -> Option<Const<'tcx>> {
631+
pub fn expect_type(&self) -> Ty<'tcx> {
632+
self.as_type().expect("expected a type, but found a const")
633+
}
634+
635+
pub fn as_const(&self) -> Option<Const<'tcx>> {
632636
if let TermKind::Const(c) = self.unpack() { Some(c) } else { None }
633637
}
634638

639+
pub fn expect_const(&self) -> Const<'tcx> {
640+
self.as_const().expect("expected a const, but found a type")
641+
}
642+
635643
pub fn into_arg(self) -> GenericArg<'tcx> {
636644
match self.unpack() {
637645
TermKind::Ty(ty) => ty.into(),

compiler/rustc_middle/src/ty/print/pretty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
10771077
}
10781078

10791079
p!(")");
1080-
if let Some(ty) = return_ty.skip_binder().ty() {
1080+
if let Some(ty) = return_ty.skip_binder().as_type() {
10811081
if !ty.is_unit() {
10821082
p!(" -> ", print(return_ty));
10831083
}
@@ -1144,7 +1144,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
11441144
for (assoc_item_def_id, term) in assoc_items {
11451145
// Skip printing `<{coroutine@} as Coroutine<_>>::Return` from async blocks,
11461146
// unless we can find out what coroutine return type it comes from.
1147-
let term = if let Some(ty) = term.skip_binder().ty()
1147+
let term = if let Some(ty) = term.skip_binder().as_type()
11481148
&& let ty::Alias(ty::Projection, proj) = ty.kind()
11491149
&& let Some(assoc) = tcx.opt_associated_item(proj.def_id)
11501150
&& assoc.trait_container(tcx) == tcx.lang_items().coroutine_trait()
@@ -1322,7 +1322,7 @@ pub trait PrettyPrinter<'tcx>: Printer<'tcx> + fmt::Write {
13221322
p!(pretty_fn_sig(
13231323
tys,
13241324
false,
1325-
proj.skip_binder().term.ty().expect("Return type was a const")
1325+
proj.skip_binder().term.as_type().expect("Return type was a const")
13261326
));
13271327
resugared = true;
13281328
}

compiler/rustc_trait_selection/src/solve/assembly/structural_traits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ impl<'tcx> TypeFolder<TyCtxt<'tcx>> for ReplaceProjectionWith<'_, 'tcx> {
726726
)
727727
.expect("expected to be able to unify goal projection with dyn's projection"),
728728
);
729-
proj.term.ty().unwrap()
729+
proj.term.expect_type()
730730
} else {
731731
ty.super_fold_with(self)
732732
}

compiler/rustc_trait_selection/src/solve/normalizes_to/opaque_types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl<'tcx> EvalCtxt<'_, InferCtxt<'tcx>> {
1717
) -> QueryResult<'tcx> {
1818
let tcx = self.interner();
1919
let opaque_ty = goal.predicate.alias;
20-
let expected = goal.predicate.term.ty().expect("no such thing as an opaque const");
20+
let expected = goal.predicate.term.as_type().expect("no such thing as an opaque const");
2121

2222
match (goal.param_env.reveal(), self.solver_mode()) {
2323
(Reveal::UserFacing, SolverMode::Normal) => {

compiler/rustc_trait_selection/src/traits/auto_trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
553553
}
554554

555555
fn is_self_referential_projection(&self, p: ty::PolyProjectionPredicate<'tcx>) -> bool {
556-
if let Some(ty) = p.term().skip_binder().ty() {
556+
if let Some(ty) = p.term().skip_binder().as_type() {
557557
matches!(ty.kind(), ty::Alias(ty::Projection, proj) if proj == &p.skip_binder().projection_term.expect_ty(self.tcx))
558558
} else {
559559
false

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1112,7 +1112,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
11121112
{
11131113
Some((
11141114
DefIdOrName::DefId(def_id),
1115-
pred.kind().rebind(proj.term.ty().unwrap()),
1115+
pred.kind().rebind(proj.term.expect_type()),
11161116
pred.kind().rebind(args.as_slice()),
11171117
))
11181118
} else {
@@ -1129,7 +1129,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
11291129
{
11301130
Some((
11311131
DefIdOrName::Name("trait object"),
1132-
pred.rebind(proj.term.ty().unwrap()),
1132+
pred.rebind(proj.term.expect_type()),
11331133
pred.rebind(args.as_slice()),
11341134
))
11351135
} else {
@@ -1157,7 +1157,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
11571157
{
11581158
Some((
11591159
name,
1160-
pred.kind().rebind(proj.term.ty().unwrap()),
1160+
pred.kind().rebind(proj.term.expect_type()),
11611161
pred.kind().rebind(args.as_slice()),
11621162
))
11631163
} else {
@@ -3840,7 +3840,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
38403840
})
38413841
} else if let Some(where_pred) = where_pred.as_projection_clause()
38423842
&& let Some(failed_pred) = failed_pred.as_projection_clause()
3843-
&& let Some(found) = failed_pred.skip_binder().term.ty()
3843+
&& let Some(found) = failed_pred.skip_binder().term.as_type()
38443844
{
38453845
type_diffs = vec![Sorts(ty::error::ExpectedFound {
38463846
expected: where_pred

compiler/rustc_trait_selection/src/traits/normalize.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
259259
obligations.len = ?self.obligations.len(),
260260
"AssocTypeNormalizer: normalized type"
261261
);
262-
normalized_ty.ty().unwrap()
262+
normalized_ty.expect_type()
263263
}
264264

265265
ty::Projection => {
@@ -289,7 +289,7 @@ impl<'a, 'b, 'tcx> TypeFolder<TyCtxt<'tcx>> for AssocTypeNormalizer<'a, 'b, 'tcx
289289
)
290290
.ok()
291291
.flatten()
292-
.map(|term| term.ty().unwrap())
292+
.map(|term| term.expect_type())
293293
.map(|normalized_ty| {
294294
PlaceholderReplacer::replace_placeholders(
295295
infcx,

compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -946,7 +946,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
946946
// since we don't actually use them.
947947
&mut vec![],
948948
)
949-
.ty()
949+
.as_type()
950950
.unwrap();
951951

952952
if let ty::Dynamic(data, ..) = ty.kind() { data.principal() } else { None }

compiler/rustc_trait_selection/src/traits/wf.rs

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

compiler/rustc_traits/src/normalize_projection_ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ fn normalize_canonicalized_projection_ty<'tcx>(
5858
// FIXME(associated_const_equality): All users of normalize_canonicalized_projection_ty
5959
// expected a type, but there is the possibility it could've been a const now.
6060
// Maybe change it to a Term later?
61-
Ok(NormalizationResult { normalized_ty: answer.ty().unwrap() })
61+
Ok(NormalizationResult { normalized_ty: answer.expect_type() })
6262
},
6363
)
6464
}

src/tools/clippy/clippy_lints/src/methods/iter_on_single_or_empty_collections.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn is_arg_ty_unified_in_fn<'tcx>(
4242
cx.tcx.predicates_of(fn_id).predicates.iter().any(|(clause, _)| {
4343
clause
4444
.as_projection_clause()
45-
.and_then(|p| p.map_bound(|p| p.term.ty()).transpose())
45+
.and_then(|p| p.map_bound(|p| p.term.as_type()).transpose())
4646
.is_some_and(|ty| ty.skip_binder() == arg_ty_in_args)
4747
}) || fn_sig
4848
.inputs()

src/tools/clippy/clippy_lints/src/needless_borrows_for_generic_args.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ fn is_mixed_projection_predicate<'tcx>(
311311
) -> bool {
312312
let generics = cx.tcx.generics_of(callee_def_id);
313313
// The predicate requires the projected type to equal a type parameter from the parent context.
314-
if let Some(term_ty) = projection_predicate.term.ty()
314+
if let Some(term_ty) = projection_predicate.term.as_type()
315315
&& let ty::Param(term_param_ty) = term_ty.kind()
316316
&& (term_param_ty.index as usize) < generics.parent_count
317317
{
@@ -370,7 +370,7 @@ fn replace_types<'tcx>(
370370
if replaced.insert(param_ty.index) {
371371
for projection_predicate in projection_predicates {
372372
if projection_predicate.projection_term.self_ty() == param_ty.to_ty(cx.tcx)
373-
&& let Some(term_ty) = projection_predicate.term.ty()
373+
&& let Some(term_ty) = projection_predicate.term.as_type()
374374
&& let ty::Param(term_param_ty) = term_ty.kind()
375375
{
376376
let projection = projection_predicate

src/tools/clippy/clippy_lints/src/unit_return_expecting_ord.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ fn get_args_to_check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Ve
100100
{
101101
if ord_preds
102102
.iter()
103-
.any(|ord| Some(ord.self_ty()) == return_ty_pred.term.ty())
103+
.any(|ord| Some(ord.self_ty()) == return_ty_pred.term.as_type())
104104
{
105105
args_to_check.push((i, "Ord".to_string()));
106106
} else if partial_ord_preds
107107
.iter()
108-
.any(|pord| pord.self_ty() == return_ty_pred.term.ty().unwrap())
108+
.any(|pord| pord.self_ty() == return_ty_pred.term.expect_type())
109109
{
110110
args_to_check.push((i, "PartialOrd".to_string()));
111111
}

src/tools/clippy/clippy_utils/src/ty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ pub fn ty_sig<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<ExprFnSig<'t
750750
let output = bounds
751751
.projection_bounds()
752752
.find(|p| lang_items.fn_once_output().map_or(false, |id| id == p.item_def_id()))
753-
.map(|p| p.map_bound(|p| p.term.ty().unwrap()));
753+
.map(|p| p.map_bound(|p| p.term.expect_type()));
754754
Some(ExprFnSig::Trait(bound.map_bound(|b| b.args.type_at(0)), output, None))
755755
},
756756
_ => None,
@@ -798,7 +798,7 @@ fn sig_from_bounds<'tcx>(
798798
// Multiple different fn trait impls. Is this even allowed?
799799
return None;
800800
}
801-
output = Some(pred.kind().rebind(p.term.ty().unwrap()));
801+
output = Some(pred.kind().rebind(p.term.expect_type()));
802802
},
803803
_ => (),
804804
}
@@ -836,7 +836,7 @@ fn sig_for_projection<'tcx>(cx: &LateContext<'tcx>, ty: AliasTy<'tcx>) -> Option
836836
// Multiple different fn trait impls. Is this even allowed?
837837
return None;
838838
}
839-
output = pred.kind().rebind(p.term.ty()).transpose();
839+
output = pred.kind().rebind(p.term.as_type()).transpose();
840840
},
841841
_ => (),
842842
}

0 commit comments

Comments
 (0)