Skip to content

Commit 855035b

Browse files
committed
convert some GenericArg to Term
1 parent e3e432d commit 855035b

File tree

27 files changed

+180
-154
lines changed

27 files changed

+180
-154
lines changed

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -379,13 +379,13 @@ fn compare_method_predicate_entailment<'tcx>(
379379
// Annoyingly, asking for the WF predicates of an array (with an unevaluated const (only?))
380380
// will give back the well-formed predicate of the same array.
381381
let mut wf_args_seen: FxHashSet<_> = wf_args.iter().copied().collect();
382-
while let Some(arg) = wf_args.pop() {
382+
while let Some(term) = wf_args.pop() {
383383
let Some(obligations) = rustc_trait_selection::traits::wf::obligations(
384384
infcx,
385385
param_env,
386386
impl_m_def_id,
387387
0,
388-
arg,
388+
term,
389389
impl_m_span,
390390
) else {
391391
continue;
@@ -402,9 +402,9 @@ fn compare_method_predicate_entailment<'tcx>(
402402
| ty::ClauseKind::TypeOutlives(..)
403403
| ty::ClauseKind::Projection(..),
404404
) => ocx.register_obligation(obligation),
405-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
406-
if wf_args_seen.insert(arg) {
407-
wf_args.push(arg)
405+
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
406+
if wf_args_seen.insert(term) {
407+
wf_args.push(term)
408408
}
409409
}
410410
_ => {}

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
7676
)
7777
}
7878

79-
fn register_wf_obligation(
80-
&self,
81-
span: Span,
82-
loc: Option<WellFormedLoc>,
83-
arg: ty::GenericArg<'tcx>,
84-
) {
79+
fn register_wf_obligation(&self, span: Span, loc: Option<WellFormedLoc>, term: ty::Term<'tcx>) {
8580
let cause = traits::ObligationCause::new(
8681
span,
8782
self.body_def_id,
@@ -91,7 +86,7 @@ impl<'tcx> WfCheckingCtxt<'_, 'tcx> {
9186
self.tcx(),
9287
cause,
9388
self.param_env,
94-
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg))),
89+
ty::ClauseKind::WellFormed(term),
9590
));
9691
}
9792
}
@@ -1486,7 +1481,7 @@ fn check_where_clauses<'tcx>(wfcx: &WfCheckingCtxt<'_, 'tcx>, span: Span, def_id
14861481
tcx.def_span(param.def_id),
14871482
matches!(param.kind, GenericParamDefKind::Type { .. })
14881483
.then(|| WellFormedLoc::Ty(param.def_id.expect_local())),
1489-
default,
1484+
default.as_term().unwrap(),
14901485
);
14911486
}
14921487
}

compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -374,9 +374,12 @@ fn check_predicates<'tcx>(
374374

375375
// Include the well-formed predicates of the type parameters of the impl.
376376
for arg in tcx.impl_trait_ref(impl1_def_id).unwrap().instantiate_identity().args {
377+
let Some(term) = arg.as_term() else {
378+
continue;
379+
};
377380
let infcx = &tcx.infer_ctxt().build(TypingMode::non_body_analysis());
378381
let obligations =
379-
wf::obligations(infcx, tcx.param_env(impl1_def_id), impl1_def_id, 0, arg, span)
382+
wf::obligations(infcx, tcx.param_env(impl1_def_id), impl1_def_id, 0, term, span)
380383
.unwrap();
381384

382385
assert!(!obligations.has_infer());

compiler/rustc_hir_typeck/src/fn_ctxt/_impl.rs

+6-8
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use rustc_infer::infer::{DefineOpaqueTypes, InferResult};
2222
use rustc_lint::builtin::SELF_CONSTRUCTOR_FROM_OUTER_ITEM;
2323
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, AutoBorrowMutability};
2424
use rustc_middle::ty::{
25-
self, AdtKind, CanonicalUserType, GenericArgKind, GenericArgsRef, GenericParamDefKind,
26-
IsIdentity, Ty, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitableExt, UserArgs, UserSelfTy,
25+
self, AdtKind, CanonicalUserType, GenericArgsRef, GenericParamDefKind, IsIdentity, Ty, TyCtxt,
26+
TypeFoldable, TypeVisitable, TypeVisitableExt, UserArgs, UserSelfTy,
2727
};
2828
use rustc_middle::{bug, span_bug};
2929
use rustc_session::lint;
@@ -573,7 +573,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
573573
/// Registers an obligation for checking later, during regionck, that `arg` is well-formed.
574574
pub(crate) fn register_wf_obligation(
575575
&self,
576-
arg: ty::GenericArg<'tcx>,
576+
term: ty::Term<'tcx>,
577577
span: Span,
578578
code: traits::ObligationCauseCode<'tcx>,
579579
) {
@@ -583,16 +583,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
583583
self.tcx,
584584
cause,
585585
self.param_env,
586-
ty::Binder::dummy(ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg))),
586+
ty::ClauseKind::WellFormed(term),
587587
));
588588
}
589589

590590
/// Registers obligations that all `args` are well-formed.
591591
pub(crate) fn add_wf_bounds(&self, args: GenericArgsRef<'tcx>, span: Span) {
592-
for arg in args.iter().filter(|arg| {
593-
matches!(arg.unpack(), GenericArgKind::Type(..) | GenericArgKind::Const(..))
594-
}) {
595-
self.register_wf_obligation(arg, span, ObligationCauseCode::WellFormed(None));
592+
for term in args.iter().filter_map(ty::GenericArg::as_term) {
593+
self.register_wf_obligation(term, span, ObligationCauseCode::WellFormed(None));
596594
}
597595
}
598596

compiler/rustc_hir_typeck/src/writeback.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
863863
Resolver { fcx, span, body, nested_goals, should_normalize }
864864
}
865865

866-
fn report_error(&self, p: impl Into<ty::GenericArg<'tcx>>) -> ErrorGuaranteed {
866+
fn report_error(&self, p: impl Into<ty::Term<'tcx>>) -> ErrorGuaranteed {
867867
if let Some(guar) = self.fcx.tainted_by_errors() {
868868
guar
869869
} else {
@@ -887,7 +887,7 @@ impl<'cx, 'tcx> Resolver<'cx, 'tcx> {
887887
new_err: impl Fn(TyCtxt<'tcx>, ErrorGuaranteed) -> T,
888888
) -> T
889889
where
890-
T: Into<ty::GenericArg<'tcx>> + TypeSuperFoldable<TyCtxt<'tcx>> + Copy,
890+
T: Into<ty::Term<'tcx>> + TypeSuperFoldable<TyCtxt<'tcx>> + Copy,
891891
{
892892
let tcx = self.fcx.tcx;
893893
// We must deeply normalize in the new solver, since later lints expect

compiler/rustc_infer/src/infer/mod.rs

+13-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ use rustc_middle::traits::solve::Goal;
3131
use rustc_middle::ty::error::{ExpectedFound, TypeError};
3232
use rustc_middle::ty::{
3333
self, BoundVarReplacerDelegate, ConstVid, FloatVid, GenericArg, GenericArgKind, GenericArgs,
34-
GenericArgsRef, GenericParamDefKind, InferConst, IntVid, PseudoCanonicalInput, Ty, TyCtxt,
35-
TyVid, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable, TypeVisitableExt, TypingEnv,
36-
TypingMode, fold_regions,
34+
GenericArgsRef, GenericParamDefKind, InferConst, IntVid, PseudoCanonicalInput, Term, TermKind,
35+
Ty, TyCtxt, TyVid, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitable,
36+
TypeVisitableExt, TypingEnv, TypingMode, fold_regions,
3737
};
3838
use rustc_span::{Span, Symbol};
3939
use snapshot::undo_log::InferCtxtUndoLogs;
@@ -1401,6 +1401,16 @@ impl<'tcx> TyOrConstInferVar {
14011401
}
14021402
}
14031403

1404+
/// Tries to extract an inference variable from a type or a constant, returns `None`
1405+
/// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`) and
1406+
/// for constants other than `ty::ConstKind::Infer(_)` (or `InferConst::Fresh`).
1407+
pub fn maybe_from_term(term: Term<'tcx>) -> Option<Self> {
1408+
match term.unpack() {
1409+
TermKind::Ty(ty) => Self::maybe_from_ty(ty),
1410+
TermKind::Const(ct) => Self::maybe_from_const(ct),
1411+
}
1412+
}
1413+
14041414
/// Tries to extract an inference variable from a type, returns `None`
14051415
/// for types other than `ty::Infer(_)` (or `InferTy::Fresh*`).
14061416
fn maybe_from_ty(ty: Ty<'tcx>) -> Option<Self> {

compiler/rustc_middle/src/ty/generic_args.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,17 @@ impl<'tcx> GenericArg<'tcx> {
250250
}
251251

252252
#[inline]
253-
pub fn as_type(self) -> Option<Ty<'tcx>> {
253+
pub fn as_region(self) -> Option<ty::Region<'tcx>> {
254254
match self.unpack() {
255-
GenericArgKind::Type(ty) => Some(ty),
255+
GenericArgKind::Lifetime(re) => Some(re),
256256
_ => None,
257257
}
258258
}
259259

260260
#[inline]
261-
pub fn as_region(self) -> Option<ty::Region<'tcx>> {
261+
pub fn as_type(self) -> Option<Ty<'tcx>> {
262262
match self.unpack() {
263-
GenericArgKind::Lifetime(re) => Some(re),
263+
GenericArgKind::Type(ty) => Some(ty),
264264
_ => None,
265265
}
266266
}
@@ -273,6 +273,15 @@ impl<'tcx> GenericArg<'tcx> {
273273
}
274274
}
275275

276+
#[inline]
277+
pub fn as_term(self) -> Option<ty::Term<'tcx>> {
278+
match self.unpack() {
279+
GenericArgKind::Lifetime(_) => None,
280+
GenericArgKind::Type(ty) => Some(ty.into()),
281+
GenericArgKind::Const(ct) => Some(ct.into()),
282+
}
283+
}
284+
276285
/// Unpack the `GenericArg` as a region when it is known certainly to be a region.
277286
pub fn expect_region(self) -> ty::Region<'tcx> {
278287
self.as_region().unwrap_or_else(|| bug!("expected a region, but found another kind"))

compiler/rustc_middle/src/ty/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ use crate::ty::codec::{TyDecoder, TyEncoder};
110110
pub use crate::ty::diagnostics::*;
111111
use crate::ty::fast_reject::SimplifiedType;
112112
use crate::ty::util::Discr;
113+
use crate::ty::walk::TypeWalker;
113114

114115
pub mod abstract_const;
115116
pub mod adjustment;
@@ -631,6 +632,20 @@ impl<'tcx> Term<'tcx> {
631632
TermKind::Const(ct) => ct.is_ct_infer(),
632633
}
633634
}
635+
636+
/// Iterator that walks `self` and any types reachable from
637+
/// `self`, in depth-first order. Note that just walks the types
638+
/// that appear in `self`, it does not descend into the fields of
639+
/// structs or variants. For example:
640+
///
641+
/// ```text
642+
/// isize => { isize }
643+
/// Foo<Bar<isize>> => { Foo<Bar<isize>>, Bar<isize>, isize }
644+
/// [isize] => { [isize], isize }
645+
/// ```
646+
pub fn walk(self) -> TypeWalker<TyCtxt<'tcx>> {
647+
TypeWalker::new(self.into())
648+
}
634649
}
635650

636651
const TAG_MASK: usize = 0b11;

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3247,7 +3247,7 @@ define_print! {
32473247
ty::ClauseKind::ConstArgHasType(ct, ty) => {
32483248
p!("the constant `", print(ct), "` has type `", print(ty), "`")
32493249
},
3250-
ty::ClauseKind::WellFormed(arg) => p!(print(arg), " well-formed"),
3250+
ty::ClauseKind::WellFormed(term) => p!(print(term), " well-formed"),
32513251
ty::ClauseKind::ConstEvaluatable(ct) => {
32523252
p!("the constant `", print(ct), "` can be evaluated")
32533253
}

compiler/rustc_next_trait_solver/src/delegate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub trait SolverDelegate: Deref<Target = Self::Infcx> + Sized {
3636
fn well_formed_goals(
3737
&self,
3838
param_env: <Self::Interner as Interner>::ParamEnv,
39-
arg: <Self::Interner as Interner>::GenericArg,
39+
term: <Self::Interner as Interner>::Term,
4040
) -> Option<Vec<Goal<Self::Interner, <Self::Interner as Interner>::Predicate>>>;
4141

4242
fn clone_opaque_types_for_query_response(

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -533,8 +533,8 @@ where
533533
ty::PredicateKind::DynCompatible(trait_def_id) => {
534534
self.compute_dyn_compatible_goal(trait_def_id)
535535
}
536-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
537-
self.compute_well_formed_goal(Goal { param_env, predicate: arg })
536+
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
537+
self.compute_well_formed_goal(Goal { param_env, predicate: term })
538538
}
539539
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
540540
self.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
@@ -1012,9 +1012,9 @@ where
10121012
pub(super) fn well_formed_goals(
10131013
&self,
10141014
param_env: I::ParamEnv,
1015-
arg: I::GenericArg,
1015+
term: I::Term,
10161016
) -> Option<Vec<Goal<I, I::Predicate>>> {
1017-
self.delegate.well_formed_goals(param_env, arg)
1017+
self.delegate.well_formed_goals(param_env, term)
10181018
}
10191019

10201020
pub(super) fn trait_ref_is_knowable(

compiler/rustc_next_trait_solver/src/solve/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ where
126126
}
127127

128128
#[instrument(level = "trace", skip(self))]
129-
fn compute_well_formed_goal(&mut self, goal: Goal<I, I::GenericArg>) -> QueryResult<I> {
129+
fn compute_well_formed_goal(&mut self, goal: Goal<I, I::Term>) -> QueryResult<I> {
130130
match self.well_formed_goals(goal.param_env, goal.predicate) {
131131
Some(goals) => {
132132
self.add_goals(GoalSource::Misc, goals);

compiler/rustc_privacy/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ where
157157
ty.visit_with(self)
158158
}
159159
ty::ClauseKind::ConstEvaluatable(ct) => ct.visit_with(self),
160-
ty::ClauseKind::WellFormed(arg) => arg.visit_with(self),
160+
ty::ClauseKind::WellFormed(term) => term.visit_with(self),
161161
}
162162
}
163163

compiler/rustc_smir/src/rustc_smir/convert/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -638,8 +638,8 @@ impl<'tcx> Stable<'tcx> for ty::ClauseKind<'tcx> {
638638
const_.stable(tables),
639639
ty.stable(tables),
640640
),
641-
ClauseKind::WellFormed(generic_arg) => {
642-
stable_mir::ty::ClauseKind::WellFormed(generic_arg.unpack().stable(tables))
641+
ClauseKind::WellFormed(term) => {
642+
stable_mir::ty::ClauseKind::WellFormed(term.unpack().stable(tables))
643643
}
644644
ClauseKind::ConstEvaluatable(const_) => {
645645
stable_mir::ty::ClauseKind::ConstEvaluatable(const_.stable(tables))

compiler/rustc_smir/src/stable_mir/ty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ pub enum ClauseKind {
14621462
TypeOutlives(TypeOutlivesPredicate),
14631463
Projection(ProjectionPredicate),
14641464
ConstArgHasType(TyConst, Ty),
1465-
WellFormed(GenericArgKind),
1465+
WellFormed(TermKind),
14661466
ConstEvaluatable(TyConst),
14671467
}
14681468

0 commit comments

Comments
 (0)