Skip to content

Commit 5285e19

Browse files
Apply nits
1 parent 3bcdf30 commit 5285e19

File tree

15 files changed

+55
-128
lines changed

15 files changed

+55
-128
lines changed

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/bounds.rs

-3
Original file line numberDiff line numberDiff line change
@@ -421,9 +421,6 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
421421
);
422422
debug!(?alias_args);
423423

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

Diff for: compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+5-13
Original file line numberDiff line numberDiff line change
@@ -625,22 +625,14 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
625625
let bound_predicate = pred.kind();
626626
match bound_predicate.skip_binder() {
627627
ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
628-
let pred = bound_predicate.rebind(pred);
629628
// `<Foo as Iterator>::Item = String`.
630-
let projection_term = pred.skip_binder().projection_term;
631-
632-
let args_with_infer_self = tcx.mk_args_from_iter(
633-
std::iter::once(Ty::new_var(tcx, ty::TyVid::ZERO).into())
634-
.chain(projection_term.args.iter().skip(1)),
635-
);
636-
637-
let quiet_projection_ty =
638-
ty::AliasTerm::new(tcx, projection_term.def_id, args_with_infer_self);
639-
640-
let term = pred.skip_binder().term;
629+
let projection_term = pred.projection_term;
630+
let quiet_projection_term =
631+
projection_term.with_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
641632

633+
let term = pred.term;
642634
let obligation = format!("{projection_term} = {term}");
643-
let quiet = format!("{quiet_projection_ty} = {term}");
635+
let quiet = format!("{quiet_projection_term} = {term}");
644636

645637
bound_span_label(projection_term.self_ty(), &obligation, &quiet);
646638
Some((obligation, projection_term.self_ty()))

Diff for: compiler/rustc_hir_analysis/src/impl_wf_check/min_specialization.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -258,23 +258,20 @@ fn unconstrained_parent_impl_args<'tcx>(
258258
// unconstrained parameters.
259259
for (clause, _) in impl_generic_predicates.predicates.iter() {
260260
if let ty::ClauseKind::Projection(proj) = clause.kind().skip_binder() {
261-
let projection_term = proj.projection_term;
262-
let projected_term = proj.term;
263-
264-
let unbound_trait_ref = projection_term.trait_ref(tcx);
261+
let unbound_trait_ref = proj.projection_term.trait_ref(tcx);
265262
if Some(unbound_trait_ref) == impl_trait_ref {
266263
continue;
267264
}
268265

269-
unconstrained_parameters.extend(cgp::parameters_for(tcx, projection_term, true));
266+
unconstrained_parameters.extend(cgp::parameters_for(tcx, proj.projection_term, true));
270267

271-
for param in cgp::parameters_for(tcx, projected_term, false) {
268+
for param in cgp::parameters_for(tcx, proj.term, false) {
272269
if !unconstrained_parameters.contains(&param) {
273270
constrained_params.insert(param.0);
274271
}
275272
}
276273

277-
unconstrained_parameters.extend(cgp::parameters_for(tcx, projected_term, true));
274+
unconstrained_parameters.extend(cgp::parameters_for(tcx, proj.term, true));
278275
}
279276
}
280277

Diff for: compiler/rustc_hir_typeck/src/method/suggest.rs

+1-8
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ use std::borrow::Cow;
4646
use super::probe::{AutorefOrPtrAdjustment, IsSuggestion, Mode, ProbeScope};
4747
use super::{CandidateSource, MethodError, NoMatchData};
4848
use rustc_hir::intravisit::Visitor;
49-
use std::iter;
5049

5150
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
5251
fn is_fn_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
@@ -788,14 +787,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
788787
let pred = bound_predicate.rebind(pred);
789788
// `<Foo as Iterator>::Item = String`.
790789
let projection_term = pred.skip_binder().projection_term;
791-
792-
let args_with_infer_self = tcx.mk_args_from_iter(
793-
iter::once(Ty::new_var(tcx, ty::TyVid::ZERO).into())
794-
.chain(projection_term.args.iter().skip(1)),
795-
);
796-
797790
let quiet_projection_term =
798-
ty::AliasTerm::new(tcx, projection_term.def_id, args_with_infer_self);
791+
projection_term.with_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
799792

800793
let term = pred.skip_binder().term;
801794

Diff for: compiler/rustc_infer/src/traits/project.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub enum ProjectionCacheEntry<'tcx> {
9393
Ambiguous,
9494
Recur,
9595
Error,
96-
NormalizedTy {
96+
NormalizedTerm {
9797
ty: NormalizedTerm<'tcx>,
9898
/// If we were able to successfully evaluate the
9999
/// corresponding cache entry key during predicate
@@ -186,7 +186,7 @@ impl<'tcx> ProjectionCache<'_, 'tcx> {
186186
return;
187187
}
188188
let fresh_key =
189-
map.insert(key, ProjectionCacheEntry::NormalizedTy { ty: value, complete: None });
189+
map.insert(key, ProjectionCacheEntry::NormalizedTerm { ty: value, complete: None });
190190
assert!(!fresh_key, "never started projecting `{key:?}`");
191191
}
192192

@@ -197,13 +197,16 @@ impl<'tcx> ProjectionCache<'_, 'tcx> {
197197
pub fn complete(&mut self, key: ProjectionCacheKey<'tcx>, result: EvaluationResult) {
198198
let mut map = self.map();
199199
match map.get(&key) {
200-
Some(ProjectionCacheEntry::NormalizedTy { ty, complete: _ }) => {
200+
Some(ProjectionCacheEntry::NormalizedTerm { ty, complete: _ }) => {
201201
info!("ProjectionCacheEntry::complete({:?}) - completing {:?}", key, ty);
202202
let mut ty = ty.clone();
203203
if result.must_apply_considering_regions() {
204204
ty.obligations = vec![];
205205
}
206-
map.insert(key, ProjectionCacheEntry::NormalizedTy { ty, complete: Some(result) });
206+
map.insert(
207+
key,
208+
ProjectionCacheEntry::NormalizedTerm { ty, complete: Some(result) },
209+
);
207210
}
208211
ref value => {
209212
// Type inference could "strand behind" old cache entries. Leave
@@ -215,7 +218,7 @@ impl<'tcx> ProjectionCache<'_, 'tcx> {
215218

216219
pub fn is_complete(&mut self, key: ProjectionCacheKey<'tcx>) -> Option<EvaluationResult> {
217220
self.map().get(&key).and_then(|res| match res {
218-
ProjectionCacheEntry::NormalizedTy { ty: _, complete } => *complete,
221+
ProjectionCacheEntry::NormalizedTerm { ty: _, complete } => *complete,
219222
_ => None,
220223
})
221224
}

Diff for: compiler/rustc_middle/src/ty/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,6 @@ impl<'tcx> Term<'tcx> {
629629
}
630630
}
631631

632-
/// This function returns the inner `AliasTy` for a `ty::Alias` or `ConstKind::Unevaluated`.
633632
pub fn to_alias_term(self) -> Option<AliasTerm<'tcx>> {
634633
match self.unpack() {
635634
TermKind::Ty(ty) => match *ty.kind() {

Diff for: compiler/rustc_middle/src/ty/relate.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use crate::ty::{
1010
GenericArgKind, GenericArgsRef, ImplSubject, Term, TermKind, Ty, TyCtxt, TypeFoldable,
1111
};
1212
use rustc_hir as hir;
13-
use rustc_hir::def::DefKind;
1413
use rustc_hir::def_id::DefId;
1514
use rustc_macros::TypeVisitable;
1615
use rustc_target::spec::abi;
@@ -227,19 +226,18 @@ impl<'tcx> Relate<'tcx> for ty::AliasTy<'tcx> {
227226
if a.def_id != b.def_id {
228227
Err(TypeError::ProjectionMismatched(expected_found(a.def_id, b.def_id)))
229228
} else {
230-
let args = match relation.tcx().def_kind(a.def_id) {
231-
DefKind::OpaqueTy => relate_args_with_variances(
229+
let args = match a.kind(relation.tcx()) {
230+
ty::Opaque => relate_args_with_variances(
232231
relation,
233232
a.def_id,
234233
relation.tcx().variances_of(a.def_id),
235234
a.args,
236235
b.args,
237236
false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle
238237
)?,
239-
DefKind::AssocTy | DefKind::AssocConst | DefKind::TyAlias => {
238+
ty::Projection | ty::Weak | ty::Inherent => {
240239
relate_args_invariantly(relation, a.args, b.args)?
241240
}
242-
def => bug!("unknown alias DefKind: {def:?}"),
243241
};
244242
Ok(ty::AliasTy::new(relation.tcx(), a.def_id, args))
245243
}
@@ -255,19 +253,21 @@ impl<'tcx> Relate<'tcx> for ty::AliasTerm<'tcx> {
255253
if a.def_id != b.def_id {
256254
Err(TypeError::ProjectionMismatched(expected_found(a.def_id, b.def_id)))
257255
} else {
258-
let args = match relation.tcx().def_kind(a.def_id) {
259-
DefKind::OpaqueTy => relate_args_with_variances(
256+
let args = match a.kind(relation.tcx()) {
257+
ty::AliasTermKind::OpaqueTy => relate_args_with_variances(
260258
relation,
261259
a.def_id,
262260
relation.tcx().variances_of(a.def_id),
263261
a.args,
264262
b.args,
265263
false, // do not fetch `type_of(a_def_id)`, as it will cause a cycle
266264
)?,
267-
DefKind::AssocTy | DefKind::AssocConst | DefKind::TyAlias => {
265+
ty::AliasTermKind::ProjectionTy
266+
| ty::AliasTermKind::WeakTy
267+
| ty::AliasTermKind::InherentTy
268+
| ty::AliasTermKind::UnevaluatedConst => {
268269
relate_args_invariantly(relation, a.args, b.args)?
269270
}
270-
def => bug!("unknown alias DefKind: {def:?}"),
271271
};
272272
Ok(ty::AliasTerm::new(relation.tcx(), a.def_id, args))
273273
}

Diff for: compiler/rustc_middle/src/ty/sty.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc_span::symbol::{sym, Symbol};
2222
use rustc_span::{Span, DUMMY_SP};
2323
use rustc_target::abi::{FieldIdx, VariantIdx, FIRST_VARIANT};
2424
use rustc_target::spec::abi::{self, Abi};
25-
use std::assert_matches::{assert_matches, debug_assert_matches};
25+
use std::assert_matches::debug_assert_matches;
2626
use std::borrow::Cow;
2727
use std::iter;
2828
use std::ops::{ControlFlow, Deref, Range};
@@ -1137,8 +1137,8 @@ pub struct AliasTerm<'tcx> {
11371137
/// aka. `tcx.parent(def_id)`.
11381138
pub def_id: DefId,
11391139

1140-
/// This field exists to prevent the creation of `AliasTy` without using
1141-
/// [AliasTy::new].
1140+
/// This field exists to prevent the creation of `AliasTerm` without using
1141+
/// [AliasTerm::new].
11421142
_use_alias_term_new_instead: (),
11431143
}
11441144

@@ -1202,13 +1202,15 @@ impl<'tcx> AliasTerm<'tcx> {
12021202
}
12031203

12041204
pub fn expect_ty(self, tcx: TyCtxt<'tcx>) -> AliasTy<'tcx> {
1205-
assert_matches!(
1206-
self.kind(tcx),
1205+
match self.kind(tcx) {
12071206
ty::AliasTermKind::ProjectionTy
1208-
| ty::AliasTermKind::OpaqueTy
1209-
| ty::AliasTermKind::WeakTy
1210-
| ty::AliasTermKind::InherentTy
1211-
);
1207+
| ty::AliasTermKind::InherentTy
1208+
| ty::AliasTermKind::OpaqueTy
1209+
| ty::AliasTermKind::WeakTy => {}
1210+
ty::AliasTermKind::UnevaluatedConst => {
1211+
bug!("Cannot turn `UnevaluatedConst` into `AliasTy`")
1212+
}
1213+
}
12121214
ty::AliasTy { def_id: self.def_id, args: self.args, _use_alias_ty_new_instead: () }
12131215
}
12141216

@@ -1229,7 +1231,7 @@ impl<'tcx> AliasTerm<'tcx> {
12291231
}
12301232
}
12311233

1232-
/// The following methods work only with (trait) associated type projections.
1234+
/// The following methods work only with (trait) associated item projections.
12331235
impl<'tcx> AliasTerm<'tcx> {
12341236
pub fn self_ty(self) -> Ty<'tcx> {
12351237
self.args.type_at(0)
@@ -1269,7 +1271,6 @@ impl<'tcx> AliasTerm<'tcx> {
12691271
self,
12701272
tcx: TyCtxt<'tcx>,
12711273
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
1272-
debug_assert!(matches!(tcx.def_kind(self.def_id), DefKind::AssocTy | DefKind::AssocConst));
12731274
let trait_def_id = self.trait_def_id(tcx);
12741275
let trait_generics = tcx.generics_of(trait_def_id);
12751276
(
@@ -1358,7 +1359,7 @@ pub struct AliasTy<'tcx> {
13581359
/// aka. `tcx.parent(def_id)`.
13591360
pub def_id: DefId,
13601361

1361-
/// This field exists to prevent the creation of `AliasTy` without using
1362+
/// This field exists to prevent the creation of `AliasT` without using
13621363
/// [AliasTy::new].
13631364
_use_alias_ty_new_instead: (),
13641365
}
@@ -1422,7 +1423,6 @@ impl<'tcx> AliasTy<'tcx> {
14221423
self,
14231424
tcx: TyCtxt<'tcx>,
14241425
) -> (ty::TraitRef<'tcx>, &'tcx [ty::GenericArg<'tcx>]) {
1425-
debug_assert!(matches!(tcx.def_kind(self.def_id), DefKind::AssocTy | DefKind::AssocConst));
14261426
let trait_def_id = self.trait_def_id(tcx);
14271427
let trait_generics = tcx.generics_of(trait_def_id);
14281428
(

Diff for: compiler/rustc_smir/src/rustc_smir/convert/ty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -723,9 +723,9 @@ impl<'tcx> Stable<'tcx> for ty::ProjectionPredicate<'tcx> {
723723
type T = stable_mir::ty::ProjectionPredicate;
724724

725725
fn stable(&self, tables: &mut Tables<'_>) -> Self::T {
726-
let ty::ProjectionPredicate { projection_term: projection_ty, term } = self;
726+
let ty::ProjectionPredicate { projection_term, term } = self;
727727
stable_mir::ty::ProjectionPredicate {
728-
projection_term: projection_ty.stable(tables),
728+
projection_term: projection_term.stable(tables),
729729
term: term.unpack().stable(tables),
730730
}
731731
}

Diff for: compiler/rustc_trait_selection/src/traits/project.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ pub(super) fn poly_project_and_unify_term<'cx, 'tcx>(
232232
/// ```
233233
/// If successful, this may result in additional obligations.
234234
///
235-
/// See [poly_project_and_unify_type] for an explanation of the return value.
235+
/// See [poly_project_and_unify_term] for an explanation of the return value.
236236
#[instrument(level = "debug", skip(selcx))]
237237
fn project_and_unify_term<'cx, 'tcx>(
238238
selcx: &mut SelectionContext<'cx, 'tcx>,
@@ -395,7 +395,7 @@ pub(super) fn opt_normalize_projection_term<'a, 'b, 'tcx>(
395395
debug!("recur cache");
396396
return Err(InProgress);
397397
}
398-
Err(ProjectionCacheEntry::NormalizedTy { ty, complete: _ }) => {
398+
Err(ProjectionCacheEntry::NormalizedTerm { ty, complete: _ }) => {
399399
// This is the hottest path in this function.
400400
//
401401
// If we find the value in the cache, then return it along

Diff for: compiler/rustc_trait_selection/src/traits/wf.rs

+1-25
Original file line numberDiff line numberDiff line change
@@ -437,31 +437,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
437437
/// Pushes the obligations required for an alias (except inherent) to be WF
438438
/// into `self.out`.
439439
fn compute_alias_ty(&mut self, data: ty::AliasTy<'tcx>) {
440-
// A projection is well-formed if
441-
//
442-
// (a) its predicates hold (*)
443-
// (b) its args are wf
444-
//
445-
// (*) The predicates of an associated type include the predicates of
446-
// the trait that it's contained in. For example, given
447-
//
448-
// trait A<T>: Clone {
449-
// type X where T: Copy;
450-
// }
451-
//
452-
// The predicates of `<() as A<i32>>::X` are:
453-
// [
454-
// `(): Sized`
455-
// `(): Clone`
456-
// `(): A<i32>`
457-
// `i32: Sized`
458-
// `i32: Clone`
459-
// `i32: Copy`
460-
// ]
461-
let obligations = self.nominal_obligations(data.def_id, data.args);
462-
self.out.extend(obligations);
463-
464-
self.compute_projection_args(data.args);
440+
self.compute_alias_term(data.into());
465441
}
466442

467443
/// Pushes the obligations required for an alias (except inherent) to be WF

Diff for: compiler/rustc_type_ir/src/predicate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ pub enum AliasTermKind {
320320
/// Currently only used if the type alias references opaque types.
321321
/// Can always be normalized away.
322322
WeakTy,
323-
/// UwU
323+
/// An unevaluate const, either coming from a generic const expression or associated const.
324324
UnevaluatedConst,
325325
}
326326

Diff for: src/tools/clippy/clippy_lints/src/needless_borrows_for_generic_args.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,11 @@ fn is_mixed_projection_predicate<'tcx>(
320320
&& (term_param_ty.index as usize) < generics.parent_count
321321
{
322322
// The inner-most self type is a type parameter from the current function.
323-
let mut projection_ty = projection_predicate.projection_term;
323+
let mut projection_term = projection_predicate.projection_term;
324324
loop {
325-
match *projection_ty.self_ty().kind() {
325+
match *projection_term.self_ty().kind() {
326326
ty::Alias(ty::Projection, inner_projection_ty) => {
327-
projection_ty = inner_projection_ty.into();
327+
projection_term = inner_projection_ty.into();
328328
},
329329
ty::Param(param_ty) => {
330330
return (param_ty.index as usize) >= generics.parent_count;

Diff for: tests/ui/associated-consts/issue-105330.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,12 @@ impl TraitWAssocConst for impl Demo { //~ ERROR E0404
1111
fn foo<A: TraitWAssocConst<A=32>>() { //~ ERROR E0658
1212
foo::<Demo>()();
1313
//~^ ERROR is not satisfied
14-
//~| ERROR type mismatch
1514
//~| ERROR expected function, found `()`
1615
}
1716

1817
fn main<A: TraitWAssocConst<A=32>>() {
1918
//~^ ERROR E0658
2019
//~| ERROR E0131
2120
foo::<Demo>();
22-
//~^ ERROR type mismatch
23-
//~| ERROR is not satisfied
21+
//~^ ERROR is not satisfied
2422
}

0 commit comments

Comments
 (0)