Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 5b270e1

Browse files
committed
Auto merge of rust-lang#126813 - compiler-errors:SliceLike, r=lcnr
Add `SliceLike` to `rustc_type_ir`, use it in the generic solver code (+ some other changes) First, we split out `TraitRef::new_from_args` which takes *just* `ty::GenericArgsRef` from `TraitRef::new` which takes `impl IntoIterator<Item: Into<GenericArg>>`. I will explain in a minute why. Second, we introduce `SliceLike`, which allows us to be generic over `List<T>` and `[T]`. This trait has an `as_slice()` and `into_iter()` method, and some other convenience functions. However, importantly, since types like `I::GenericArgs` now implement `SliceLike` rather than `IntoIter<Item = I::GenericArg>`, we can't use `TraitRef::new` on this directly. That's where `new_from_args` comes in. Finally, we adjust all the code to use these slice operators. Some things get simpler, some things get a bit more annoying since we need to use `as_slice()` in a few places. 🤷 r? lcnr
2 parents 6b0f4b5 + d521e21 commit 5b270e1

File tree

52 files changed

+378
-254
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+378
-254
lines changed

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
719719
tcx,
720720
assoc_item,
721721
assoc_item,
722-
ty::TraitRef::new(tcx, def_id.to_def_id(), trait_args),
722+
ty::TraitRef::new_from_args(tcx, def_id.to_def_id(), trait_args),
723723
);
724724
}
725725
_ => {}

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,7 +2032,7 @@ pub(super) fn check_type_bounds<'tcx>(
20322032
// to its definition type. This should be the param-env we use to *prove* the
20332033
// predicate too, but we don't do that because of performance issues.
20342034
// See <https://github.com/rust-lang/rust/pull/117542#issue-1976337685>.
2035-
let trait_projection_ty = Ty::new_projection(tcx, trait_ty.def_id, rebased_args);
2035+
let trait_projection_ty = Ty::new_projection_from_args(tcx, trait_ty.def_id, rebased_args);
20362036
let impl_identity_ty = tcx.type_of(impl_ty.def_id).instantiate_identity();
20372037
let normalize_param_env = param_env_with_gat_bounds(tcx, impl_ty, impl_trait_ref);
20382038
for mut obligation in util::elaborate(tcx, obligations) {
@@ -2230,7 +2230,11 @@ fn param_env_with_gat_bounds<'tcx>(
22302230
_ => predicates.push(
22312231
ty::Binder::bind_with_vars(
22322232
ty::ProjectionPredicate {
2233-
projection_term: ty::AliasTerm::new(tcx, trait_ty.def_id, rebased_args),
2233+
projection_term: ty::AliasTerm::new_from_args(
2234+
tcx,
2235+
trait_ty.def_id,
2236+
rebased_args,
2237+
),
22342238
term: normalize_impl_ty.into(),
22352239
},
22362240
bound_vars,

compiler/rustc_hir_analysis/src/check/intrinsic.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,11 @@ pub fn check_intrinsic_type(
504504
ty::Region::new_bound(tcx, ty::INNERMOST, br),
505505
param(0),
506506
)],
507-
Ty::new_projection(tcx, discriminant_def_id, tcx.mk_args(&[param(0).into()])),
507+
Ty::new_projection_from_args(
508+
tcx,
509+
discriminant_def_id,
510+
tcx.mk_args(&[param(0).into()]),
511+
),
508512
)
509513
}
510514

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
423423
item_segment,
424424
trait_ref.args,
425425
);
426-
Ty::new_projection(self.tcx(), item_def_id, item_args)
426+
Ty::new_projection_from_args(self.tcx(), item_def_id, item_args)
427427
} else {
428428
// There are no late-bound regions; we can just ignore the binder.
429429
let (mut mpart_sugg, mut inferred_sugg) = (None, None);
@@ -1607,7 +1607,7 @@ pub fn suggest_impl_trait<'tcx>(
16071607
let item_ty = ocx.normalize(
16081608
&ObligationCause::dummy(),
16091609
param_env,
1610-
Ty::new_projection(infcx.tcx, assoc_item_def_id, args),
1610+
Ty::new_projection_from_args(infcx.tcx, assoc_item_def_id, args),
16111611
);
16121612
// FIXME(compiler-errors): We may benefit from resolving regions here.
16131613
if ocx.select_where_possible().is_empty()

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fn associated_type_bounds<'tcx>(
2323
span: Span,
2424
filter: PredicateFilter,
2525
) -> &'tcx [(ty::Clause<'tcx>, Span)] {
26-
let item_ty = Ty::new_projection(
26+
let item_ty = Ty::new_projection_from_args(
2727
tcx,
2828
assoc_item_def_id.to_def_id(),
2929
GenericArgs::identity_for_item(tcx, assoc_item_def_id),
@@ -108,7 +108,7 @@ pub(super) fn explicit_item_bounds_with_filter(
108108
tcx,
109109
opaque_def_id.expect_local(),
110110
opaque_ty.bounds,
111-
Ty::new_projection(
111+
Ty::new_projection_from_args(
112112
tcx,
113113
def_id.to_def_id(),
114114
ty::GenericArgs::identity_for_item(tcx, def_id),

compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
409409
);
410410
debug!(?alias_args);
411411

412-
ty::AliasTerm::new(tcx, assoc_item.def_id, alias_args)
412+
ty::AliasTerm::new_from_args(tcx, assoc_item.def_id, alias_args)
413413
});
414414

415415
// Provide the resolved type of the associated constant to `type_of(AnonConst)`.

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
693693
debug!(?bound_vars);
694694

695695
let poly_trait_ref = ty::Binder::bind_with_vars(
696-
ty::TraitRef::new(tcx, trait_def_id, generic_args),
696+
ty::TraitRef::new_from_args(tcx, trait_def_id, generic_args),
697697
bound_vars,
698698
);
699699

@@ -759,7 +759,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
759759
Some((trait_def_id, trait_segment, span)),
760760
);
761761
}
762-
ty::TraitRef::new(self.tcx(), trait_def_id, generic_args)
762+
ty::TraitRef::new_from_args(self.tcx(), trait_def_id, generic_args)
763763
}
764764

765765
fn probe_trait_that_defines_assoc_item(
@@ -789,7 +789,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
789789
// Type aliases defined in crates that have the
790790
// feature `lazy_type_alias` enabled get encoded as a type alias that normalization will
791791
// then actually instantiate the where bounds of.
792-
let alias_ty = ty::AliasTy::new(tcx, did, args);
792+
let alias_ty = ty::AliasTy::new_from_args(tcx, did, args);
793793
Ty::new_alias(tcx, ty::Weak, alias_ty)
794794
} else {
795795
tcx.at(span).type_of(did).instantiate(tcx, args)
@@ -1267,7 +1267,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
12671267
.chain(args.into_iter().skip(parent_args.len())),
12681268
);
12691269

1270-
let ty = Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new(tcx, assoc_item, args));
1270+
let ty =
1271+
Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new_from_args(tcx, assoc_item, args));
12711272

12721273
Ok(Some((ty, assoc_item)))
12731274
}
@@ -1534,7 +1535,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
15341535
let item_args =
15351536
self.lower_generic_args_of_assoc_item(span, item_def_id, item_segment, trait_ref.args);
15361537

1537-
Ty::new_projection(tcx, item_def_id, item_args)
1538+
Ty::new_projection_from_args(tcx, item_def_id, item_args)
15381539
}
15391540

15401541
pub fn prohibit_generic_args<'a>(
@@ -2302,7 +2303,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
23022303
debug!(?args);
23032304

23042305
if in_trait {
2305-
Ty::new_projection(tcx, def_id, args)
2306+
Ty::new_projection_from_args(tcx, def_id, args)
23062307
} else {
23072308
Ty::new_opaque(tcx, def_id, args)
23082309
}

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3108,7 +3108,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
31083108
let element_ty = ocx.normalize(
31093109
&cause,
31103110
self.param_env,
3111-
Ty::new_projection(self.tcx, index_trait_output_def_id, impl_trait_ref.args),
3111+
Ty::new_projection_from_args(
3112+
self.tcx,
3113+
index_trait_output_def_id,
3114+
impl_trait_ref.args,
3115+
),
31123116
);
31133117

31143118
let true_errors = ocx.select_where_possible();

compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
569569
// For the purposes of this function, we hope that it is a `struct` type, and that our current `expr` is a literal of
570570
// that struct type.
571571
let impl_trait_self_ref = if self.tcx.is_trait_alias(obligation.impl_or_alias_def_id) {
572-
ty::TraitRef::new(
572+
ty::TraitRef::new_from_args(
573573
self.tcx,
574574
obligation.impl_or_alias_def_id,
575575
ty::GenericArgs::identity_for_item(self.tcx, obligation.impl_or_alias_def_id),

compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
297297
trait_ref.args,
298298
);
299299

300-
Ty::new_projection(self.tcx(), item_def_id, item_args)
300+
Ty::new_projection_from_args(self.tcx(), item_def_id, item_args)
301301
}
302302

303303
fn probe_adt(&self, span: Span, ty: Ty<'tcx>) -> Option<ty::AdtDef<'tcx>> {

0 commit comments

Comments
 (0)