Skip to content

Commit

Permalink
Auto merge of #126813 - compiler-errors:SliceLike, r=<try>
Browse files Browse the repository at this point in the history
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
  • Loading branch information
bors committed Jun 21, 2024
2 parents c1b336c + 0770597 commit d906ddd
Show file tree
Hide file tree
Showing 51 changed files with 367 additions and 222 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/check/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ pub(crate) fn check_item_type(tcx: TyCtxt<'_>, def_id: LocalDefId) {
tcx,
assoc_item,
assoc_item,
ty::TraitRef::new(tcx, def_id.to_def_id(), trait_args),
ty::TraitRef::new_from_args(tcx, def_id.to_def_id(), trait_args),
);
}
_ => {}
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_hir_analysis/src/check/compare_impl_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2032,7 +2032,7 @@ pub(super) fn check_type_bounds<'tcx>(
// to its definition type. This should be the param-env we use to *prove* the
// predicate too, but we don't do that because of performance issues.
// See <https://github.com/rust-lang/rust/pull/117542#issue-1976337685>.
let trait_projection_ty = Ty::new_projection(tcx, trait_ty.def_id, rebased_args);
let trait_projection_ty = Ty::new_projection_from_args(tcx, trait_ty.def_id, rebased_args);
let impl_identity_ty = tcx.type_of(impl_ty.def_id).instantiate_identity();
let normalize_param_env = param_env_with_gat_bounds(tcx, impl_ty, impl_trait_ref);
for mut obligation in util::elaborate(tcx, obligations) {
Expand Down Expand Up @@ -2230,7 +2230,11 @@ fn param_env_with_gat_bounds<'tcx>(
_ => predicates.push(
ty::Binder::bind_with_vars(
ty::ProjectionPredicate {
projection_term: ty::AliasTerm::new(tcx, trait_ty.def_id, rebased_args),
projection_term: ty::AliasTerm::new_from_args(
tcx,
trait_ty.def_id,
rebased_args,
),
term: normalize_impl_ty.into(),
},
bound_vars,
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_hir_analysis/src/check/intrinsic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,11 @@ pub fn check_intrinsic_type(
ty::Region::new_bound(tcx, ty::INNERMOST, br),
param(0),
)],
Ty::new_projection(tcx, discriminant_def_id, tcx.mk_args(&[param(0).into()])),
Ty::new_projection_from_args(
tcx,
discriminant_def_id,
tcx.mk_args(&[param(0).into()]),
),
)
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
item_segment,
trait_ref.args,
);
Ty::new_projection(self.tcx(), item_def_id, item_args)
Ty::new_projection_from_args(self.tcx(), item_def_id, item_args)
} else {
// There are no late-bound regions; we can just ignore the binder.
let (mut mpart_sugg, mut inferred_sugg) = (None, None);
Expand Down Expand Up @@ -1607,7 +1607,7 @@ pub fn suggest_impl_trait<'tcx>(
let item_ty = ocx.normalize(
&ObligationCause::dummy(),
param_env,
Ty::new_projection(infcx.tcx, assoc_item_def_id, args),
Ty::new_projection_from_args(infcx.tcx, assoc_item_def_id, args),
);
// FIXME(compiler-errors): We may benefit from resolving regions here.
if ocx.select_where_possible().is_empty()
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_analysis/src/collect/item_bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn associated_type_bounds<'tcx>(
span: Span,
filter: PredicateFilter,
) -> &'tcx [(ty::Clause<'tcx>, Span)] {
let item_ty = Ty::new_projection(
let item_ty = Ty::new_projection_from_args(
tcx,
assoc_item_def_id.to_def_id(),
GenericArgs::identity_for_item(tcx, assoc_item_def_id),
Expand Down Expand Up @@ -108,7 +108,7 @@ pub(super) fn explicit_item_bounds_with_filter(
tcx,
opaque_def_id.expect_local(),
opaque_ty.bounds,
Ty::new_projection(
Ty::new_projection_from_args(
tcx,
def_id.to_def_id(),
ty::GenericArgs::identity_for_item(tcx, def_id),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
debug!(?alias_args);

ty::AliasTerm::new(tcx, assoc_item.def_id, alias_args)
ty::AliasTerm::new_from_args(tcx, assoc_item.def_id, alias_args)
});

// Provide the resolved type of the associated constant to `type_of(AnonConst)`.
Expand Down
13 changes: 7 additions & 6 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
debug!(?bound_vars);

let poly_trait_ref = ty::Binder::bind_with_vars(
ty::TraitRef::new(tcx, trait_def_id, generic_args),
ty::TraitRef::new_from_args(tcx, trait_def_id, generic_args),
bound_vars,
);

Expand Down Expand Up @@ -759,7 +759,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
Some((trait_def_id, trait_segment, span)),
);
}
ty::TraitRef::new(self.tcx(), trait_def_id, generic_args)
ty::TraitRef::new_from_args(self.tcx(), trait_def_id, generic_args)
}

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

let ty = Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new(tcx, assoc_item, args));
let ty =
Ty::new_alias(tcx, ty::Inherent, ty::AliasTy::new_from_args(tcx, assoc_item, args));

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

Ty::new_projection(tcx, item_def_id, item_args)
Ty::new_projection_from_args(tcx, item_def_id, item_args)
}

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

if in_trait {
Ty::new_projection(tcx, def_id, args)
Ty::new_projection_from_args(tcx, def_id, args)
} else {
Ty::new_opaque(tcx, def_id, args)
}
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_hir_typeck/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3108,7 +3108,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let element_ty = ocx.normalize(
&cause,
self.param_env,
Ty::new_projection(self.tcx, index_trait_output_def_id, impl_trait_ref.args),
Ty::new_projection_from_args(
self.tcx,
index_trait_output_def_id,
impl_trait_ref.args,
),
);

let true_errors = ocx.select_where_possible();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// For the purposes of this function, we hope that it is a `struct` type, and that our current `expr` is a literal of
// that struct type.
let impl_trait_self_ref = if self.tcx.is_trait_alias(obligation.impl_or_alias_def_id) {
ty::TraitRef::new(
ty::TraitRef::new_from_args(
self.tcx,
obligation.impl_or_alias_def_id,
ty::GenericArgs::identity_for_item(self.tcx, obligation.impl_or_alias_def_id),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/fn_ctxt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ impl<'tcx> HirTyLowerer<'tcx> for FnCtxt<'_, 'tcx> {
trait_ref.args,
);

Ty::new_projection(self.tcx(), item_def_id, item_args)
Ty::new_projection_from_args(self.tcx(), item_def_id, item_args)
}

fn probe_adt(&self, span: Span, ty: Ty<'tcx>) -> Option<ty::AdtDef<'tcx>> {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.var_for_def(cause.span, param)
});

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

// Construct an obligation
let poly_trait_ref = ty::Binder::dummy(trait_ref);
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/probe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ impl<'a, 'tcx> ProbeContext<'a, 'tcx> {
trait_def_id: DefId,
) {
let trait_args = self.fresh_args_for_item(self.span, trait_def_id);
let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, trait_args);
let trait_ref = ty::TraitRef::new_from_args(self.tcx, trait_def_id, trait_args);

if self.tcx.is_trait_alias(trait_def_id) {
// For trait aliases, recursively assume all explicitly named traits are relevant
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1978,7 +1978,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
err,
self_source,
args,
ty::TraitRef::new(
ty::TraitRef::new_from_args(
self.tcx,
trait_did,
self.fresh_args_for_item(sugg_span, trait_did),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,12 @@ impl<'tcx> NiceRegionError<'_, 'tcx> {
(false, None, None, Some(span), String::new())
};

let expected_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new(
let expected_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new_from_args(
self.cx.tcx,
trait_def_id,
expected_args,
));
let actual_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new(
let actual_trait_ref = self.cx.resolve_vars_if_possible(ty::TraitRef::new_from_args(
self.cx.tcx,
trait_def_id,
actual_args,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_lint/src/opaque_hidden_inferred_bound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<'tcx> LateLintPass<'tcx> for OpaqueHiddenInferredBound {
return;
}

let proj_ty = Ty::new_projection(
let proj_ty = Ty::new_projection_from_args(
cx.tcx,
proj.projection_term.def_id,
proj.projection_term.args,
Expand Down
6 changes: 5 additions & 1 deletion compiler/rustc_middle/src/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
assert_matches!(self.def_kind(trait_def_id), DefKind::Trait);
let trait_generics = self.generics_of(trait_def_id);
(
ty::TraitRef::new(self, trait_def_id, args.truncate_to(self, trait_generics)),
ty::TraitRef::new_from_args(self, trait_def_id, args.truncate_to(self, trait_generics)),
&args[trait_generics.count()..],
)
}
Expand Down Expand Up @@ -269,6 +269,10 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
self.check_and_mk_args(def_id, args)
}

fn debug_assert_args_compatible(self, def_id: DefId, args: ty::GenericArgsRef<'tcx>) {
self.debug_assert_args_compatible(def_id, args);
}

fn intern_canonical_goal_evaluation_step(
self,
step: solve::inspect::CanonicalGoalEvaluationStep<TyCtxt<'tcx>>,
Expand Down
14 changes: 14 additions & 0 deletions compiler/rustc_middle/src/ty/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ impl<H, T> RawList<H, T> {
}
}

impl<'a, H, T: Copy> rustc_type_ir::inherent::SliceLike for &'a RawList<H, T> {
type Item = T;

type IntoIter = iter::Copied<<&'a [T] as IntoIterator>::IntoIter>;

fn into_iter(self) -> Self::IntoIter {
(*self).iter()
}

fn as_slice(&self) -> &[Self::Item] {
(*self).as_slice()
}
}

macro_rules! impl_list_empty {
($header_ty:ty, $header_init:expr) => {
impl<T> RawList<$header_ty, T> {
Expand Down
13 changes: 11 additions & 2 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ impl<'tcx> Ty<'tcx> {

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

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

#[inline]
pub fn new_projection_from_args(
tcx: TyCtxt<'tcx>,
item_def_id: DefId,
args: ty::GenericArgsRef<'tcx>,
) -> Ty<'tcx> {
Ty::new_alias(tcx, ty::Projection, AliasTy::new_from_args(tcx, item_def_id, args))
}

#[inline]
pub fn new_projection(
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -1409,7 +1418,7 @@ impl<'tcx> Ty<'tcx> {
let assoc_items = tcx.associated_item_def_ids(
tcx.require_lang_item(hir::LangItem::DiscriminantKind, None),
);
Ty::new_projection(tcx, assoc_items[0], tcx.mk_args(&[self.into()]))
Ty::new_projection_from_args(tcx, assoc_items[0], tcx.mk_args(&[self.into()]))
}

ty::Pat(ty, _) => ty.discriminant_ty(tcx),
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/thir/pattern/const_to_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ impl<'tcx> ConstToPat<'tcx> {
tcx,
ObligationCause::dummy(),
self.param_env,
ty::TraitRef::new(
ty::TraitRef::new_from_args(
tcx,
partial_eq_trait_id,
tcx.with_opt_host_effect_param(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_next_trait_solver/src/solve/assembly/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ where
};

for assumption in
self.cx().item_bounds(alias_ty.def_id).iter_instantiated(self.cx(), &alias_ty.args)
self.cx().item_bounds(alias_ty.def_id).iter_instantiated(self.cx(), alias_ty.args)
{
candidates.extend(G::probe_and_consider_implied_clause(
self,
Expand Down Expand Up @@ -603,7 +603,7 @@ where
// Consider all of the auto-trait and projection bounds, which don't
// need to be recorded as a `BuiltinImplSource::Object` since they don't
// really have a vtable base...
for bound in bounds {
for bound in bounds.into_iter() {
match bound.skip_binder() {
ty::ExistentialPredicate::Trait(_) => {
// Skip principal
Expand Down
Loading

0 comments on commit d906ddd

Please sign in to comment.