Skip to content

Commit 3c322bc

Browse files
authored
Rollup merge of #140320 - lcnr:wf-use-term, r=compiler-errors
replace `GenericArg` with `Term` where applicable r? types
2 parents 443358d + 85fa395 commit 3c322bc

File tree

27 files changed

+189
-174
lines changed

27 files changed

+189
-174
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

+15-28
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

@@ -1320,27 +1318,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13201318
infer_args: bool,
13211319
) -> ty::GenericArg<'tcx> {
13221320
let tcx = self.fcx.tcx();
1323-
match param.kind {
1324-
GenericParamDefKind::Lifetime => self
1325-
.fcx
1326-
.re_infer(
1327-
self.span,
1328-
rustc_hir_analysis::hir_ty_lowering::RegionInferReason::Param(param),
1329-
)
1330-
.into(),
1331-
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => {
1332-
if !infer_args && let Some(default) = param.default_value(tcx) {
1333-
// If we have a default, then it doesn't matter that we're not inferring
1334-
// the type/const arguments: We provide the default where any is missing.
1335-
return default.instantiate(tcx, preceding_args);
1336-
}
1337-
// If no type/const arguments were provided, we have to infer them.
1338-
// This case also occurs as a result of some malformed input, e.g.,
1339-
// a lifetime argument being given instead of a type/const parameter.
1340-
// Using inference instead of `Error` gives better error messages.
1341-
self.fcx.var_for_def(self.span, param)
1342-
}
1321+
if !infer_args && let Some(default) = param.default_value(tcx) {
1322+
// If we have a default, then it doesn't matter that we're not inferring
1323+
// the type/const arguments: We provide the default where any is missing.
1324+
return default.instantiate(tcx, preceding_args);
13431325
}
1326+
// If no type/const arguments were provided, we have to infer them.
1327+
// This case also occurs as a result of some malformed input, e.g.,
1328+
// a lifetime argument being given instead of a type/const parameter.
1329+
// Using inference instead of `Error` gives better error messages.
1330+
self.fcx.var_for_def(self.span, param)
13441331
}
13451332
}
13461333

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
@@ -529,8 +529,8 @@ where
529529
ty::PredicateKind::DynCompatible(trait_def_id) => {
530530
self.compute_dyn_compatible_goal(trait_def_id)
531531
}
532-
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(arg)) => {
533-
self.compute_well_formed_goal(Goal { param_env, predicate: arg })
532+
ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(term)) => {
533+
self.compute_well_formed_goal(Goal { param_env, predicate: term })
534534
}
535535
ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => {
536536
self.compute_const_evaluatable_goal(Goal { param_env, predicate: ct })
@@ -1008,9 +1008,9 @@ where
10081008
pub(super) fn well_formed_goals(
10091009
&self,
10101010
param_env: I::ParamEnv,
1011-
arg: I::GenericArg,
1011+
term: I::Term,
10121012
) -> Option<Vec<Goal<I, I::Predicate>>> {
1013-
self.delegate.well_formed_goals(param_env, arg)
1013+
self.delegate.well_formed_goals(param_env, term)
10141014
}
10151015

10161016
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
@@ -137,7 +137,7 @@ where
137137
}
138138

139139
#[instrument(level = "trace", skip(self))]
140-
fn compute_well_formed_goal(&mut self, goal: Goal<I, I::GenericArg>) -> QueryResult<I> {
140+
fn compute_well_formed_goal(&mut self, goal: Goal<I, I::Term>) -> QueryResult<I> {
141141
match self.well_formed_goals(goal.param_env, goal.predicate) {
142142
Some(goals) => {
143143
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)