Apply nits

This commit is contained in:
Michael Goulet 2024-05-13 14:34:47 -04:00
parent 3bcdf3058e
commit fa84018c2e
17 changed files with 73 additions and 137 deletions

View File

@ -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)
});

View File

@ -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()))

View File

@ -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));
}
}

View File

@ -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 {
@ -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;

View File

@ -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
@ -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:?}`");
}
@ -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
@ -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,
})
}

View File

@ -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() {

View File

@ -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.

View File

@ -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;
@ -227,8 +226,8 @@ 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),
@ -236,10 +235,9 @@ impl<'tcx> Relate<'tcx> for ty::AliasTy<'tcx> {
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))
}
@ -255,8 +253,8 @@ 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),
@ -264,10 +262,13 @@ impl<'tcx> Relate<'tcx> for ty::AliasTerm<'tcx> {
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))
}

View File

@ -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};
@ -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: (),
}
@ -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: () }
}
@ -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)
@ -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);
(
@ -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()
}
}
}
}
@ -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: (),
}
@ -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);
(

View File

@ -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),
}
}

View File

@ -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>,
@ -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
@ -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

View File

@ -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

View File

@ -320,14 +320,17 @@ pub enum AliasTermKind {
/// Currently only used if the type alias references opaque types.
/// Can always be normalized away.
WeakTy,
/// UwU
/// An unevaluated const coming from a generic const expression.
UnevaluatedConst,
/// An unevaluated const coming from an associated const.
ProjectionConst,
}
impl AliasTermKind {
pub fn descr(self) -> &'static str {
match self {
AliasTermKind::ProjectionTy => "associated type",
AliasTermKind::ProjectionConst => "associated const",
AliasTermKind::InherentTy => "inherent associated type",
AliasTermKind::OpaqueTy => "opaque type",
AliasTermKind::WeakTy => "type alias",

View File

@ -320,11 +320,11 @@ fn is_mixed_projection_predicate<'tcx>(
&& (term_param_ty.index as usize) < generics.parent_count
{
// The inner-most self type is a type parameter from the current function.
let mut projection_ty = projection_predicate.projection_term;
let mut projection_term = projection_predicate.projection_term;
loop {
match *projection_ty.self_ty().kind() {
match *projection_term.self_ty().kind() {
ty::Alias(ty::Projection, inner_projection_ty) => {
projection_ty = inner_projection_ty.into();
projection_term = inner_projection_ty.into();
},
ty::Param(param_ty) => {
return (param_ty.index as usize) >= generics.parent_count;

View File

@ -1,5 +1,6 @@
//@ aux-crate:assoc_const_equality=assoc-const-equality.rs
//@ edition:2021
//@ ignore-test (FIXME: #125092)
#![crate_name = "user"]

View File

@ -11,7 +11,6 @@ impl TraitWAssocConst for impl Demo { //~ ERROR E0404
fn foo<A: TraitWAssocConst<A=32>>() { //~ ERROR E0658
foo::<Demo>()();
//~^ ERROR is not satisfied
//~| ERROR type mismatch
//~| ERROR expected function, found `()`
}
@ -19,6 +18,5 @@ fn main<A: TraitWAssocConst<A=32>>() {
//~^ ERROR E0658
//~| ERROR E0131
foo::<Demo>();
//~^ ERROR type mismatch
//~| ERROR is not satisfied
//~^ ERROR is not satisfied
}

View File

@ -26,7 +26,7 @@ LL | fn foo<A: TraitWAssocConst<A=32>>() {
= note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date
error[E0658]: associated const equality is incomplete
--> $DIR/issue-105330.rs:18:29
--> $DIR/issue-105330.rs:17:29
|
LL | fn main<A: TraitWAssocConst<A=32>>() {
| ^^^^
@ -44,7 +44,7 @@ LL | impl TraitWAssocConst for impl Demo {
= note: `impl Trait` is only allowed in arguments and return types of functions and methods
error[E0131]: `main` function is not allowed to have generic parameters
--> $DIR/issue-105330.rs:18:8
--> $DIR/issue-105330.rs:17:8
|
LL | fn main<A: TraitWAssocConst<A=32>>() {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ `main` cannot have generic parameters
@ -61,20 +61,6 @@ note: required by a bound in `foo`
LL | fn foo<A: TraitWAssocConst<A=32>>() {
| ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `foo`
error[E0271]: type mismatch resolving `<Demo as TraitWAssocConst>::A == 32`
--> $DIR/issue-105330.rs:12:11
|
LL | foo::<Demo>()();
| ^^^^ expected `32`, found `<Demo as TraitWAssocConst>::A`
|
= note: expected constant `32`
found constant `<Demo as TraitWAssocConst>::A`
note: required by a bound in `foo`
--> $DIR/issue-105330.rs:11:28
|
LL | fn foo<A: TraitWAssocConst<A=32>>() {
| ^^^^ required by this bound in `foo`
error[E0618]: expected function, found `()`
--> $DIR/issue-105330.rs:12:5
|
@ -86,7 +72,7 @@ LL | foo::<Demo>()();
| call expression requires function
error[E0277]: the trait bound `Demo: TraitWAssocConst` is not satisfied
--> $DIR/issue-105330.rs:21:11
--> $DIR/issue-105330.rs:20:11
|
LL | foo::<Demo>();
| ^^^^ the trait `TraitWAssocConst` is not implemented for `Demo`
@ -97,21 +83,7 @@ note: required by a bound in `foo`
LL | fn foo<A: TraitWAssocConst<A=32>>() {
| ^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `foo`
error[E0271]: type mismatch resolving `<Demo as TraitWAssocConst>::A == 32`
--> $DIR/issue-105330.rs:21:11
|
LL | foo::<Demo>();
| ^^^^ expected `32`, found `<Demo as TraitWAssocConst>::A`
|
= note: expected constant `32`
found constant `<Demo as TraitWAssocConst>::A`
note: required by a bound in `foo`
--> $DIR/issue-105330.rs:11:28
|
LL | fn foo<A: TraitWAssocConst<A=32>>() {
| ^^^^ required by this bound in `foo`
error: aborting due to 9 previous errors
error: aborting due to 11 previous errors
Some errors have detailed explanations: E0131, E0271, E0277, E0404, E0562, E0618, E0658.
Some errors have detailed explanations: E0131, E0277, E0404, E0562, E0618, E0658.
For more information about an error, try `rustc --explain E0131`.