Skip to content

Commit

Permalink
Apply nits
Browse files Browse the repository at this point in the history
  • Loading branch information
compiler-errors committed May 13, 2024
1 parent 3bcdf30 commit fa84018
Show file tree
Hide file tree
Showing 17 changed files with 73 additions and 137 deletions.
3 changes: 0 additions & 3 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
);
debug!(?alias_args);

// Note that we're indeed also using `AliasTy` (alias *type*) for associated
// *constants* to represent *const projections*. Alias *term* would be a more
// appropriate name but alas.
ty::AliasTerm::new(tcx, assoc_item.def_id, alias_args)
});

Expand Down
18 changes: 5 additions & 13 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -625,22 +625,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
let bound_predicate = pred.kind();
match bound_predicate.skip_binder() {
ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
let pred = bound_predicate.rebind(pred);
// `<Foo as Iterator>::Item = String`.
let projection_term = pred.skip_binder().projection_term;

let args_with_infer_self = tcx.mk_args_from_iter(
std::iter::once(Ty::new_var(tcx, ty::TyVid::ZERO).into())
.chain(projection_term.args.iter().skip(1)),
);

let quiet_projection_ty =
ty::AliasTerm::new(tcx, projection_term.def_id, args_with_infer_self);

let term = pred.skip_binder().term;
let projection_term = pred.projection_term;
let quiet_projection_term =
projection_term.with_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));

let term = pred.term;
let obligation = format!("{projection_term} = {term}");
let quiet = format!("{quiet_projection_ty} = {term}");
let quiet = format!("{quiet_projection_term} = {term}");

bound_span_label(projection_term.self_ty(), &obligation, &quiet);
Some((obligation, projection_term.self_ty()))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,23 +258,20 @@ fn unconstrained_parent_impl_args<'tcx>(
// unconstrained parameters.
for (clause, _) in impl_generic_predicates.predicates.iter() {
if let ty::ClauseKind::Projection(proj) = clause.kind().skip_binder() {
let projection_term = proj.projection_term;
let projected_term = proj.term;

let unbound_trait_ref = projection_term.trait_ref(tcx);
let unbound_trait_ref = proj.projection_term.trait_ref(tcx);
if Some(unbound_trait_ref) == impl_trait_ref {
continue;
}

unconstrained_parameters.extend(cgp::parameters_for(tcx, projection_term, true));
unconstrained_parameters.extend(cgp::parameters_for(tcx, proj.projection_term, true));

for param in cgp::parameters_for(tcx, projected_term, false) {
for param in cgp::parameters_for(tcx, proj.term, false) {
if !unconstrained_parameters.contains(&param) {
constrained_params.insert(param.0);
}
}

unconstrained_parameters.extend(cgp::parameters_for(tcx, projected_term, true));
unconstrained_parameters.extend(cgp::parameters_for(tcx, proj.term, true));
}
}

Expand Down
9 changes: 1 addition & 8 deletions compiler/rustc_hir_typeck/src/method/suggest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ use std::borrow::Cow;
use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
use super::{CandidateSource, MethodError, NoMatchData};
use rustc_hir::intravisit::Visitor;
use std::iter;

impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
fn is_fn_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
Expand Down Expand Up @@ -788,14 +787,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let pred = bound_predicate.rebind(pred);
// `<Foo as Iterator>::Item = String`.
let projection_term = pred.skip_binder().projection_term;

let args_with_infer_self = tcx.mk_args_from_iter(
iter::once(Ty::new_var(tcx, ty::TyVid::ZERO).into())
.chain(projection_term.args.iter().skip(1)),
);

let quiet_projection_term =
ty::AliasTerm::new(tcx, projection_term.def_id, args_with_infer_self);
projection_term.with_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));

let term = pred.skip_binder().term;

Expand Down
13 changes: 8 additions & 5 deletions compiler/rustc_infer/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub enum ProjectionCacheEntry<'tcx> {
Ambiguous,
Recur,
Error,
NormalizedTy {
NormalizedTerm {
ty: NormalizedTerm<'tcx>,
/// If we were able to successfully evaluate the
/// corresponding cache entry key during predicate
Expand Down Expand Up @@ -186,7 +186,7 @@ impl<'tcx> ProjectionCache<'_, 'tcx> {
return;
}
let fresh_key =
map.insert(key, ProjectionCacheEntry::NormalizedTy { ty: value, complete: None });
map.insert(key, ProjectionCacheEntry::NormalizedTerm { ty: value, complete: None });
assert!(!fresh_key, "never started projecting `{key:?}`");
}

Expand All @@ -197,13 +197,16 @@ impl<'tcx> ProjectionCache<'_, 'tcx> {
pub fn complete(&mut self, key: ProjectionCacheKey<'tcx>, result: EvaluationResult) {
let mut map = self.map();
match map.get(&key) {
Some(ProjectionCacheEntry::NormalizedTy { ty, complete: _ }) => {
Some(ProjectionCacheEntry::NormalizedTerm { ty, complete: _ }) => {
info!("ProjectionCacheEntry::complete({:?}) - completing {:?}", key, ty);
let mut ty = ty.clone();
if result.must_apply_considering_regions() {
ty.obligations = vec![];
}
map.insert(key, ProjectionCacheEntry::NormalizedTy { ty, complete: Some(result) });
map.insert(
key,
ProjectionCacheEntry::NormalizedTerm { ty, complete: Some(result) },
);
}
ref value => {
// Type inference could "strand behind" old cache entries. Leave
Expand All @@ -215,7 +218,7 @@ impl<'tcx> ProjectionCache<'_, 'tcx> {

pub fn is_complete(&mut self, key: ProjectionCacheKey<'tcx>) -> Option<EvaluationResult> {
self.map().get(&key).and_then(|res| match res {
ProjectionCacheEntry::NormalizedTy { ty: _, complete } => *complete,
ProjectionCacheEntry::NormalizedTerm { ty: _, complete } => *complete,
_ => None,
})
}
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,6 @@ impl<'tcx> Term<'tcx> {
}
}

/// This function returns the inner `AliasTy` for a `ty::Alias` or `ConstKind::Unevaluated`.
pub fn to_alias_term(self) -> Option<AliasTerm<'tcx>> {
match self.unpack() {
TermKind::Ty(ty) => match *ty.kind() {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/ty/print/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3216,7 +3216,8 @@ define_print_and_forward_display! {
ty::AliasTermKind::ProjectionTy
| ty::AliasTermKind::WeakTy
| ty::AliasTermKind::OpaqueTy
| ty::AliasTermKind::UnevaluatedConst => {
| ty::AliasTermKind::UnevaluatedConst
| ty::AliasTermKind::ProjectionConst => {
// If we're printing verbosely, or don't want to invoke queries
// (`is_impl_trait_in_trait`), then fall back to printing the def path.
// This is likely what you want if you're debugging the compiler anyways.
Expand Down
19 changes: 10 additions & 9 deletions compiler/rustc_middle/src/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::ty::{
GenericArgKind, GenericArgsRef, ImplSubject, Term, TermKind, Ty, TyCtxt, TypeFoldable,
};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::DefId;
use rustc_macros::TypeVisitable;
use rustc_target::spec::abi;
Expand Down Expand Up @@ -227,19 +226,18 @@ impl<'tcx> Relate<'tcx> for ty::AliasTy<'tcx> {
if a.def_id != b.def_id {
Err(TypeError::ProjectionMismatched(expected_found(a.def_id, b.def_id)))
} else {
let args = match relation.tcx().def_kind(a.def_id) {
DefKind::OpaqueTy => relate_args_with_variances(
let args = match a.kind(relation.tcx()) {
ty::Opaque => relate_args_with_variances(
relation,
a.def_id,
relation.tcx().variances_of(a.def_id),
a.args,
b.args,
false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle
)?,
DefKind::AssocTy | DefKind::AssocConst | DefKind::TyAlias => {
ty::Projection | ty::Weak | ty::Inherent => {
relate_args_invariantly(relation, a.args, b.args)?
}
def => bug!("unknown alias DefKind: {def:?}"),
};
Ok(ty::AliasTy::new(relation.tcx(), a.def_id, args))
}
Expand All @@ -255,19 +253,22 @@ impl<'tcx> Relate<'tcx> for ty::AliasTerm<'tcx> {
if a.def_id != b.def_id {
Err(TypeError::ProjectionMismatched(expected_found(a.def_id, b.def_id)))
} else {
let args = match relation.tcx().def_kind(a.def_id) {
DefKind::OpaqueTy => relate_args_with_variances(
let args = match a.kind(relation.tcx()) {
ty::AliasTermKind::OpaqueTy => relate_args_with_variances(
relation,
a.def_id,
relation.tcx().variances_of(a.def_id),
a.args,
b.args,
false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle
)?,
DefKind::AssocTy | DefKind::AssocConst | DefKind::TyAlias => {
ty::AliasTermKind::ProjectionTy
| ty::AliasTermKind::WeakTy
| ty::AliasTermKind::InherentTy
| ty::AliasTermKind::UnevaluatedConst
| ty::AliasTermKind::ProjectionConst => {
relate_args_invariantly(relation, a.args, b.args)?
}
def => bug!("unknown alias DefKind: {def:?}"),
};
Ok(ty::AliasTerm::new(relation.tcx(), a.def_id, args))
}
Expand Down
43 changes: 23 additions & 20 deletions compiler/rustc_middle/src/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rustc_span::symbol::{sym, Symbol};
use rustc_span::{Span, DUMMY_SP};
use rustc_target::abi::{FieldIdx, VariantIdx, FIRST_VARIANT};
use rustc_target::spec::abi::{self, Abi};
use std::assert_matches::{assert_matches, debug_assert_matches};
use std::assert_matches::debug_assert_matches;
use std::borrow::Cow;
use std::iter;
use std::ops::{ControlFlow, Deref, Range};
Expand Down Expand Up @@ -1137,8 +1137,8 @@ pub struct AliasTerm<'tcx> {
/// aka. `tcx.parent(def_id)`.
pub def_id: DefId,

/// This field exists to prevent the creation of `AliasTy` without using
/// [AliasTy::new].
/// This field exists to prevent the creation of `AliasTerm` without using
/// [AliasTerm::new].
_use_alias_term_new_instead: (),
}

Expand Down Expand Up @@ -1202,13 +1202,15 @@ impl<'tcx> AliasTerm<'tcx> {
}

pub fn expect_ty(self, tcx: TyCtxt<'tcx>) -> AliasTy<'tcx> {
assert_matches!(
self.kind(tcx),
match self.kind(tcx) {
ty::AliasTermKind::ProjectionTy
| ty::AliasTermKind::OpaqueTy
| ty::AliasTermKind::WeakTy
| ty::AliasTermKind::InherentTy
);
| ty::AliasTermKind::InherentTy
| ty::AliasTermKind::OpaqueTy
| ty::AliasTermKind::WeakTy => {}
ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => {
bug!("Cannot turn `UnevaluatedConst` into `AliasTy`")
}
}
ty::AliasTy { def_id: self.def_id, args: self.args, _use_alias_ty_new_instead: () }
}

Expand All @@ -1223,13 +1225,14 @@ impl<'tcx> AliasTerm<'tcx> {
}
DefKind::OpaqueTy => ty::AliasTermKind::OpaqueTy,
DefKind::TyAlias => ty::AliasTermKind::WeakTy,
DefKind::AssocConst | DefKind::AnonConst => ty::AliasTermKind::UnevaluatedConst,
DefKind::AnonConst => ty::AliasTermKind::UnevaluatedConst,
DefKind::AssocConst => ty::AliasTermKind::ProjectionConst,
kind => bug!("unexpected DefKind in AliasTy: {kind:?}"),
}
}
}

/// The following methods work only with (trait) associated type projections.
/// The following methods work only with (trait) associated item projections.
impl<'tcx> AliasTerm<'tcx> {
pub fn self_ty(self) -> Ty<'tcx> {
self.args.type_at(0)
Expand Down Expand Up @@ -1269,7 +1272,6 @@ impl<'tcx> AliasTerm<'tcx> {
self,
tcx: TyCtxt<'tcx>,
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
debug_assert!(matches!(tcx.def_kind(self.def_id), DefKind::AssocTy | DefKind::AssocConst));
let trait_def_id = self.trait_def_id(tcx);
let trait_generics = tcx.generics_of(trait_def_id);
(
Expand Down Expand Up @@ -1304,12 +1306,14 @@ impl<'tcx> AliasTerm<'tcx> {
AliasTy { def_id: self.def_id, args: self.args, _use_alias_ty_new_instead: () },
)
.into(),
ty::AliasTermKind::UnevaluatedConst => ty::Const::new_unevaluated(
tcx,
ty::UnevaluatedConst::new(self.def_id, self.args),
tcx.type_of(self.def_id).instantiate(tcx, self.args),
)
.into(),
ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => {
ty::Const::new_unevaluated(
tcx,
ty::UnevaluatedConst::new(self.def_id, self.args),
tcx.type_of(self.def_id).instantiate(tcx, self.args),
)
.into()
}
}
}
}
Expand Down Expand Up @@ -1358,7 +1362,7 @@ pub struct AliasTy<'tcx> {
/// aka. `tcx.parent(def_id)`.
pub def_id: DefId,

/// This field exists to prevent the creation of `AliasTy` without using
/// This field exists to prevent the creation of `AliasT` without using
/// [AliasTy::new].
_use_alias_ty_new_instead: (),
}
Expand Down Expand Up @@ -1422,7 +1426,6 @@ impl<'tcx> AliasTy<'tcx> {
self,
tcx: TyCtxt<'tcx>,
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
debug_assert!(matches!(tcx.def_kind(self.def_id), DefKind::AssocTy | DefKind::AssocConst));
let trait_def_id = self.trait_def_id(tcx);
let trait_generics = tcx.generics_of(trait_def_id);
(
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_smir/src/rustc_smir/convert/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -723,9 +723,9 @@ impl<'tcx> Stable<'tcx> for ty::ProjectionPredicate<'tcx> {
type T = stable_mir::ty::ProjectionPredicate;

fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
let ty::ProjectionPredicate { projection_term: projection_ty, term } = self;
let ty::ProjectionPredicate { projection_term, term } = self;
stable_mir::ty::ProjectionPredicate {
projection_term: projection_ty.stable(tables),
projection_term: projection_term.stable(tables),
term: term.unpack().stable(tables),
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_trait_selection/src/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ pub(super) fn poly_project_and_unify_term<'cx, 'tcx>(
/// ```
/// If successful, this may result in additional obligations.
///
/// See [poly_project_and_unify_type] for an explanation of the return value.
/// See [poly_project_and_unify_term] for an explanation of the return value.
#[instrument(level = "debug", skip(selcx))]
fn project_and_unify_term<'cx, 'tcx>(
selcx: &mut SelectionContext<'cx, 'tcx>,
Expand Down Expand Up @@ -395,7 +395,7 @@ pub(super) fn opt_normalize_projection_term<'a, 'b, 'tcx>(
debug!("recur cache");
return Err(InProgress);
}
Err(ProjectionCacheEntry::NormalizedTy { ty, complete: _ }) => {
Err(ProjectionCacheEntry::NormalizedTerm { ty, complete: _ }) => {
// This is the hottest path in this function.
//
// If we find the value in the cache, then return it along
Expand Down Expand Up @@ -522,7 +522,7 @@ fn normalize_to_error<'a, 'tcx>(
| ty::AliasTermKind::InherentTy
| ty::AliasTermKind::OpaqueTy
| ty::AliasTermKind::WeakTy => selcx.infcx.next_ty_var(cause.span).into(),
ty::AliasTermKind::UnevaluatedConst => selcx
ty::AliasTermKind::UnevaluatedConst | ty::AliasTermKind::ProjectionConst => selcx
.infcx
.next_const_var(
selcx
Expand Down
26 changes: 1 addition & 25 deletions compiler/rustc_trait_selection/src/traits/wf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,31 +437,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
/// Pushes the obligations required for an alias (except inherent) to be WF
/// into `self.out`.
fn compute_alias_ty(&mut self, data: ty::AliasTy<'tcx>) {
// A projection is well-formed if
//
// (a) its predicates hold (*)
// (b) its args are wf
//
// (*) The predicates of an associated type include the predicates of
// the trait that it's contained in. For example, given
//
// trait A<T>: Clone {
// type X where T: Copy;
// }
//
// The predicates of `<() as A<i32>>::X` are:
// [
// `(): Sized`
// `(): Clone`
// `(): A<i32>`
// `i32: Sized`
// `i32: Clone`
// `i32: Copy`
// ]
let obligations = self.nominal_obligations(data.def_id, data.args);
self.out.extend(obligations);

self.compute_projection_args(data.args);
self.compute_alias_term(data.into());
}

/// Pushes the obligations required for an alias (except inherent) to be WF
Expand Down
Loading

0 comments on commit fa84018

Please sign in to comment.