Skip to content

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

+1-1
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

+6-2
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

+5-1
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

+2-2
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

+2-2
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

+1-1
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

+7-6
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

+5-1
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

+1-1
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

+1-1
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>> {

compiler/rustc_hir_typeck/src/method/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
333333
self.var_for_def(cause.span, param)
334334
});
335335

336-
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, args);
336+
let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, args);
337337

338338
// Construct an obligation
339339
let poly_trait_ref = ty::Binder::dummy(trait_ref);

compiler/rustc_hir_typeck/src/method/probe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
870870
trait_def_id: DefId,
871871
) {
872872
let trait_args = self.fresh_args_for_item(self.span, trait_def_id);
873-
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, trait_args);
873+
let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);
874874

875875
if self.tcx.is_trait_alias(trait_def_id) {
876876
// For trait aliases, recursively assume all explicitly named traits are relevant

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
19781978
err,
19791979
self_source,
19801980
args,
1981-
ty::TraitRef::new(
1981+
ty::TraitRef::new_from_args(
19821982
self.tcx,
19831983
trait_did,
19841984
self.fresh_args_for_item(sugg_span, trait_did),

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/placeholder_error.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,12 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
256256
(false, None, None, Some(span), String::new())
257257
};
258258

259-
let expected_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new(
259+
let expected_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new_from_args(
260260
self.cx.tcx,
261261
trait_def_id,
262262
expected_args,
263263
));
264-
let actual_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new(
264+
let actual_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new_from_args(
265265
self.cx.tcx,
266266
trait_def_id,
267267
actual_args,

compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
109109
return;
110110
}
111111

112-
let proj_ty = Ty::new_projection(
112+
let proj_ty = Ty::new_projection_from_args(
113113
cx.tcx,
114114
proj.projection_term.def_id,
115115
proj.projection_term.args,

compiler/rustc_middle/src/ty/context.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
240240
assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
241241
let trait_generics = self.generics_of(trait_def_id);
242242
(
243-
ty::TraitRef::new(self, trait_def_id, args.truncate_to(self, trait_generics)),
243+
ty::TraitRef::new_from_args(self, trait_def_id, args.truncate_to(self, trait_generics)),
244244
&args[trait_generics.count()..],
245245
)
246246
}
@@ -261,12 +261,8 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
261261
self.check_args_compatible(def_id, args)
262262
}
263263

264-
fn check_and_mk_args(
265-
self,
266-
def_id: DefId,
267-
args: impl IntoIterator<Item: Into<ty::GenericArg<'tcx>>>,
268-
) -> ty::GenericArgsRef<'tcx> {
269-
self.check_and_mk_args(def_id, args)
264+
fn debug_assert_args_compatible(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) {
265+
self.debug_assert_args_compatible(def_id, args);
270266
}
271267

272268
fn intern_canonical_goal_evaluation_step(

compiler/rustc_middle/src/ty/list.rs

+14
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@ impl<H, T> RawList<H, T> {
133133
}
134134
}
135135

136+
impl<'a, H, T: Copy> rustc_type_ir::inherent::SliceLike for &'a RawList<H, T> {
137+
type Item = T;
138+
139+
type IntoIter = iter::Copied<<&'a [T] as IntoIterator>::IntoIter>;
140+
141+
fn iter(self) -> Self::IntoIter {
142+
(*self).iter()
143+
}
144+
145+
fn as_slice(&self) -> &[Self::Item] {
146+
(*self).as_slice()
147+
}
148+
}
149+
136150
macro_rules! impl_list_empty {
137151
($header_ty:ty, $header_init:expr) => {
138152
impl<T> RawList<$header_ty, T> {

compiler/rustc_middle/src/ty/sty.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl<'tcx> Ty<'tcx> {
499499

500500
#[inline]
501501
pub fn new_opaque(tcx: TyCtxt<'tcx>, def_id: DefId, args: GenericArgsRef<'tcx>) -> Ty<'tcx> {
502-
Ty::new_alias(tcx, ty::Opaque, AliasTy::new(tcx, def_id, args))
502+
Ty::new_alias(tcx, ty::Opaque, AliasTy::new_from_args(tcx, def_id, args))
503503
}
504504

505505
/// Constructs a `TyKind::Error` type with current `ErrorGuaranteed`
@@ -669,6 +669,15 @@ impl<'tcx> Ty<'tcx> {
669669
Ty::new(tcx, Dynamic(obj, reg, repr))
670670
}
671671

672+
#[inline]
673+
pub fn new_projection_from_args(
674+
tcx: TyCtxt<'tcx>,
675+
item_def_id: DefId,
676+
args: ty::GenericArgsRef<'tcx>,
677+
) -> Ty<'tcx> {
678+
Ty::new_alias(tcx, ty::Projection, AliasTy::new_from_args(tcx, item_def_id, args))
679+
}
680+
672681
#[inline]
673682
pub fn new_projection(
674683
tcx: TyCtxt<'tcx>,
@@ -1409,7 +1418,7 @@ impl<'tcx> Ty<'tcx> {
14091418
let assoc_items = tcx.associated_item_def_ids(
14101419
tcx.require_lang_item(hir::LangItem::DiscriminantKind, None),
14111420
);
1412-
Ty::new_projection(tcx, assoc_items[0], tcx.mk_args(&[self.into()]))
1421+
Ty::new_projection_from_args(tcx, assoc_items[0], tcx.mk_args(&[self.into()]))
14131422
}
14141423

14151424
ty::Pat(ty, _) => ty.discriminant_ty(tcx),

compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'tcx> ConstToPat<'tcx> {
220220
tcx,
221221
ObligationCause::dummy(),
222222
self.param_env,
223-
ty::TraitRef::new(
223+
ty::TraitRef::new_from_args(
224224
tcx,
225225
partial_eq_trait_id,
226226
tcx.with_opt_host_effect_param(

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ where
527527
};
528528

529529
for assumption in
530-
self.cx().item_bounds(alias_ty.def_id).iter_instantiated(self.cx(), &alias_ty.args)
530+
self.cx().item_bounds(alias_ty.def_id).iter_instantiated(self.cx(), alias_ty.args)
531531
{
532532
candidates.extend(G::probe_and_consider_implied_clause(
533533
self,
@@ -603,7 +603,7 @@ where
603603
// Consider all of the auto-trait and projection bounds, which don't
604604
// need to be recorded as a `BuiltinImplSource::Object` since they don't
605605
// really have a vtable base...
606-
for bound in bounds {
606+
for bound in bounds.iter() {
607607
match bound.skip_binder() {
608608
ty::ExistentialPredicate::Trait(_) => {
609609
// Skip principal

0 commit comments

Comments
 (0)