Skip to content

Commit 415905a

Browse files
committed
Implement consecutive shorthand projections
1 parent 8bf5a8d commit 415905a

17 files changed

+244
-50
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
115115
pub(super) fn complain_about_assoc_item_not_found<I>(
116116
&self,
117117
all_candidates: impl Fn() -> I,
118-
qself: AssocItemQSelf,
118+
qself: AssocItemQSelf<'tcx>,
119119
assoc_tag: ty::AssocTag,
120120
assoc_ident: Ident,
121121
span: Span,

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+41-8
Original file line numberDiff line numberDiff line change
@@ -208,18 +208,20 @@ pub trait HirTyLowerer<'tcx> {
208208
/// The "qualified self" of an associated item path.
209209
///
210210
/// For diagnostic purposes only.
211-
enum AssocItemQSelf {
211+
enum AssocItemQSelf<'tcx> {
212212
Trait(DefId),
213213
TyParam(LocalDefId, Span),
214214
SelfTyAlias,
215+
AssocTy(Ty<'tcx>),
215216
}
216217

217-
impl AssocItemQSelf {
218-
fn to_string(&self, tcx: TyCtxt<'_>) -> String {
218+
impl<'tcx> AssocItemQSelf<'tcx> {
219+
fn to_string(&self, tcx: TyCtxt<'tcx>) -> String {
219220
match *self {
220221
Self::Trait(def_id) => tcx.def_path_str(def_id),
221222
Self::TyParam(def_id, _) => tcx.hir_ty_param_name(def_id).to_string(),
222223
Self::SelfTyAlias => kw::SelfUpper.to_string(),
224+
Self::AssocTy(ty) => ty.to_string(),
223225
}
224226
}
225227
}
@@ -1010,8 +1012,8 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
10101012
fn probe_single_bound_for_assoc_item<I>(
10111013
&self,
10121014
all_candidates: impl Fn() -> I,
1013-
qself: AssocItemQSelf,
1014-
assoc_tag: AssocTag,
1015+
qself: AssocItemQSelf<'tcx>,
1016+
assoc_tag: ty::AssocTag,
10151017
assoc_ident: Ident,
10161018
span: Span,
10171019
constraint: Option<&hir::AssocItemConstraint<'tcx>>,
@@ -1305,15 +1307,45 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
13051307
)?
13061308
}
13071309
(
1308-
&ty::Param(_),
1309-
Res::SelfTyParam { trait_: param_did } | Res::Def(DefKind::TyParam, param_did),
1310+
ty::Param(_),
1311+
Res::SelfTyParam { trait_: param_def_id }
1312+
| Res::Def(DefKind::TyParam, param_def_id),
13101313
) => self.probe_single_ty_param_bound_for_assoc_item(
1311-
param_did.expect_local(),
1314+
param_def_id.expect_local(),
13121315
qself.span,
13131316
mode.assoc_tag(),
13141317
assoc_ident,
13151318
span,
13161319
)?,
1320+
// FIXME(fmease):
1321+
// Require the pre-lowered projectee (the HIR QSelf) to have `DefKind::AssocTy`. Rephrased,
1322+
// `T::Assoc::Assoc` typeck'ing shouldn't imply `Identity<T::Assoc>::Assoc` typeck'ing where
1323+
// `Identity` is an eager (i.e., non-lazy) type alias. We should do this
1324+
// * for consistency with lazy type aliases (`ty::Weak`)
1325+
// * for consistency with the fact that `T::Assoc` typeck'ing doesn't imply `Identity<T>::Assoc`
1326+
// typeck'ing
1327+
(ty::Alias(ty::Projection, alias_ty), _ /* Res::Def(DefKind::AssocTy, _) */) => {
1328+
// FIXME: Utilizing `item_bounds` for this is cycle-prone.
1329+
let predicates = tcx.item_bounds(alias_ty.def_id).instantiate(tcx, alias_ty.args);
1330+
1331+
self.probe_single_bound_for_assoc_item(
1332+
|| {
1333+
let trait_refs = predicates.iter().filter_map(|pred| {
1334+
pred.as_trait_clause().map(|t| t.map_bound(|t| t.trait_ref))
1335+
});
1336+
traits::transitive_bounds_that_define_assoc_item(
1337+
tcx,
1338+
trait_refs,
1339+
assoc_ident,
1340+
)
1341+
},
1342+
AssocItemQSelf::AssocTy(qself_ty),
1343+
mode.assoc_tag(),
1344+
assoc_ident,
1345+
span,
1346+
None,
1347+
)?
1348+
}
13171349
_ => {
13181350
let kind_str = assoc_tag_str(mode.assoc_tag());
13191351
let reported = if variant_resolution.is_some() {
@@ -1466,6 +1498,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
14661498
);
14671499
});
14681500
}
1501+
14691502
Ok(result)
14701503
}
14711504

src/tools/tidy/src/issues.txt

-1
Original file line numberDiff line numberDiff line change
@@ -1837,7 +1837,6 @@ ui/issues/issue-23024.rs
18371837
ui/issues/issue-23036.rs
18381838
ui/issues/issue-23041.rs
18391839
ui/issues/issue-23046.rs
1840-
ui/issues/issue-23073.rs
18411840
ui/issues/issue-2311-2.rs
18421841
ui/issues/issue-2311.rs
18431842
ui/issues/issue-2312.rs
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait ChooseMe {
2+
type Type;
3+
}
4+
5+
trait PickMe {
6+
type Type;
7+
}
8+
9+
trait HaveItAll {
10+
type See: ChooseMe + PickMe;
11+
}
12+
13+
struct Env<T: HaveItAll>(T::See::Type);
14+
//~^ ERROR ambiguous associated type `Type` in bounds of `<T as HaveItAll>::See`
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0221]: ambiguous associated type `Type` in bounds of `<T as HaveItAll>::See`
2+
--> $DIR/consecutive-shorthand-projections-ambiguous.rs:13:26
3+
|
4+
LL | type Type;
5+
| --------- ambiguous `Type` from `ChooseMe`
6+
...
7+
LL | type Type;
8+
| --------- ambiguous `Type` from `PickMe`
9+
...
10+
LL | struct Env<T: HaveItAll>(T::See::Type);
11+
| ^^^^^^^^^^^^ ambiguous associated type `Type`
12+
|
13+
help: use fully-qualified syntax to disambiguate
14+
|
15+
LL | struct Env<T: HaveItAll>(<<T as HaveItAll>::See as ChooseMe>::Type);
16+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
17+
help: use fully-qualified syntax to disambiguate
18+
|
19+
LL | struct Env<T: HaveItAll>(<<T as HaveItAll>::See as PickMe>::Type);
20+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21+
22+
error: aborting due to 1 previous error
23+
24+
For more information about this error, try `rustc --explain E0221`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// FIXME: Description
2+
3+
fn parametrized<T: Trait<Ty: Trait>>() {
4+
let _: T::Ty::Ty; //~ ERROR associated type `Ty` not found for `<T as Trait>::Ty`
5+
}
6+
7+
trait Trait {
8+
type Ty;
9+
}
10+
11+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0220]: associated type `Ty` not found for `<T as Trait>::Ty`
2+
--> $DIR/consecutive-shorthand-projections-not-in-item-bounds.rs:4:19
3+
|
4+
LL | let _: T::Ty::Ty;
5+
| ^^ there is an associated type `Ty` in the trait `Trait`
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0220`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ check-pass
2+
3+
fn make<T: Indir0<Ty: Indir1<Ty = Struct>>>() {
4+
let _ = T::Ty::Ty { field: 0 };
5+
}
6+
7+
trait Indir0 {
8+
type Ty: Indir1;
9+
}
10+
11+
trait Indir1 {
12+
type Ty;
13+
}
14+
15+
struct Struct {
16+
field: i32
17+
}
18+
19+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//@ check-pass
2+
3+
fn factory0<T: Factory>() {
4+
let _: T::Output::Category;
5+
}
6+
7+
fn factory1<T: Factory<Output: Product<Category = u16>>>(category: u16) {
8+
let _: T::Output::Category = category;
9+
}
10+
11+
fn factory2<T: Factory>(_: T::Output::Category) {}
12+
13+
trait Factory {
14+
type Output: Product;
15+
}
16+
17+
impl Factory for () {
18+
type Output = u128;
19+
}
20+
21+
trait Product {
22+
type Category;
23+
}
24+
25+
impl Product for u128 {
26+
type Category = u16;
27+
}
28+
29+
/////////////////////////
30+
31+
fn chain<C: Chain<Link = C>>(chain: C) {
32+
let _: C::Link::Link::Link::Link::Link = chain;
33+
}
34+
35+
trait Chain { type Link: Chain; }
36+
37+
impl Chain for () {
38+
type Link = Self;
39+
}
40+
41+
/////////////////////////
42+
43+
fn scope<'r, T: Main<'static, (i32, U), 1>, U, const Q: usize>() {
44+
let _: T::Next<'r, (), Q>::Final;
45+
}
46+
47+
trait Main<'a, T, const N: usize> {
48+
type Next<'b, U, const M: usize>: Aux<'a, 'b, (T, U), N, M>;
49+
}
50+
51+
impl<'a, T, const N: usize> Main<'a, T, N> for () {
52+
type Next<'_b, _U, const _M: usize> = ();
53+
}
54+
55+
trait Aux<'a, 'b, T, const N: usize, const M: usize> {
56+
type Final;
57+
}
58+
59+
impl<'a, 'b, T, const N: usize, const M: usize> Aux<'a, 'b, T, N, M> for () {
60+
type Final = [[(T, &'a (), &'b ()); N]; M];
61+
}
62+
63+
/////////////////////////
64+
65+
fn main() {
66+
factory0::<()>();
67+
factory1::<()>(360);
68+
factory2::<()>(720);
69+
let _: <() as Factory>::Output::Category;
70+
71+
chain(());
72+
let _: <() as Chain>::Link::Link::Link;
73+
74+
scope::<(), bool, 32>();
75+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1+
//@ check-pass
2+
13
#![feature(associated_type_defaults)]
24

35
trait Foo { type T; }
6+
47
trait Bar {
58
type Foo: Foo;
6-
type FooT = <<Self as Bar>::Foo>::T; //~ ERROR ambiguous associated type
9+
type FooT = <<Self as Bar>::Foo>::T;
710
}
811

912
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error[E0223]: ambiguous associated type
2+
--> $DIR/non-consecutive-shorthand-projections.rs:16:12
3+
|
4+
LL | let _: Identity<T::Project>::Project;
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
help: if there were a trait named `Example` with associated type `Project` implemented for `Identity<<T as Trait>::Project>`, you could use the fully-qualified path
8+
|
9+
LL | let _: <Identity<<T as Trait>::Project> as Example>::Project;
10+
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11+
12+
error: aborting due to 1 previous error
13+
14+
For more information about this error, try `rustc --explain E0223`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// FIXME(fmease): Proper explainer.
2+
// Of course, under #22519 (arbitrary shorthand projections), this should obviously typeck.
3+
// For reference, `T::Alias` typeck'ing does *not* imply `Identity<T>::Alias` typeck'ing.
4+
//@ revisions: eager lazy
5+
//@[eager] check-pass
6+
#![cfg_attr(lazy, feature(lazy_type_alias), allow(incomplete_features))]
7+
8+
type Identity<T> = T;
9+
10+
trait Trait {
11+
type Project: Trait;
12+
}
13+
14+
fn scope<T: Trait>() {
15+
// FIXME(fmease): Reject this under [eager], too!
16+
let _: Identity<T::Project>::Project; //[lazy]~ ERROR ambiguous associated type
17+
}
18+
19+
fn main() {}

tests/ui/issues/issue-23073.stderr

-14
This file was deleted.

tests/ui/qualified/qualified-path-params-2.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Check that qualified paths with type parameters
22
// fail during type checking and not during parsing
3-
43
struct S;
54

65
trait Tr {
@@ -15,7 +14,6 @@ impl S {
1514
fn f<T>() {}
1615
}
1716

18-
type A = <S as Tr>::A::f<u8>;
19-
//~^ ERROR ambiguous associated type
17+
type A = <S as Tr>::A::f<u8>; //~ ERROR associated type `f` not found for `<S as Tr>::A`
2018

2119
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
error[E0223]: ambiguous associated type
2-
--> $DIR/qualified-path-params-2.rs:18:10
1+
error[E0220]: associated type `f` not found for `<S as Tr>::A`
2+
--> $DIR/qualified-path-params-2.rs:17:24
33
|
44
LL | type A = <S as Tr>::A::f<u8>;
5-
| ^^^^^^^^^^^^^^^^^^^
6-
|
7-
help: if there were a trait named `Example` with associated type `f` implemented for `<S as Tr>::A`, you could use the fully-qualified path
8-
|
9-
LL - type A = <S as Tr>::A::f<u8>;
10-
LL + type A = <<S as Tr>::A as Example>::f;
11-
|
5+
| ^ there is a similarly named associated type `A` in the trait `Tr`
126

137
error: aborting due to 1 previous error
148

15-
For more information about this error, try `rustc --explain E0223`.
9+
For more information about this error, try `rustc --explain E0220`.

tests/ui/ufcs/ufcs-partially-resolved.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn main() {
3333
<u8 as Tr>::N::NN; //~ ERROR cannot find associated type `N` in trait `Tr`
3434
<u8 as E>::N::NN; //~ ERROR expected trait, found enum `E`
3535
<u8 as A>::N::NN; //~ ERROR expected trait, found type alias `A`
36-
let _: <u8 as Tr>::Y::NN; //~ ERROR ambiguous associated type
36+
let _: <u8 as Tr>::Y::NN; //~ ERROR associated type `NN` not found for `<u8 as Tr>::Y`
3737
let _: <u8 as E>::Y::NN; //~ ERROR expected trait, found enum `E`
3838
<u8 as Tr>::Y::NN; //~ ERROR no associated item named `NN` found for type `u16`
3939
<u8 as E>::Y::NN; //~ ERROR expected trait, found enum `E`

0 commit comments

Comments
 (0)