Skip to content

Commit 30b6fe3

Browse files
committed
Correctly resolve Inherent Associated Types
1 parent 160b194 commit 30b6fe3

File tree

7 files changed

+63
-52
lines changed

7 files changed

+63
-52
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19101910
}
19111911
}
19121912
}
1913+
1914+
// see if we can satisfy using an inherent associated type
1915+
for impl_ in tcx.inherent_impls(adt_def.did()) {
1916+
let assoc_ty = tcx.associated_items(impl_).find_by_name_and_kind(
1917+
tcx,
1918+
assoc_ident,
1919+
ty::AssocKind::Type,
1920+
*impl_,
1921+
);
1922+
if let Some(assoc_ty) = assoc_ty {
1923+
let ty = tcx.type_of(assoc_ty.def_id);
1924+
return Ok((ty, DefKind::AssocTy, assoc_ty.def_id));
1925+
}
1926+
}
19131927
}
19141928

19151929
// Find the type of the associated item, and the trait where the associated

src/test/ui/assoc-inherent.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/test/ui/assoc-inherent.stderr

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(inherent_associated_types)]
2+
#![allow(incomplete_features)]
3+
4+
struct Foo;
5+
6+
impl Foo {
7+
type Baz; //~ ERROR associated type in `impl` without body
8+
}
9+
10+
fn main() {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: associated type in `impl` without body
2+
--> $DIR/assoc-inherent-no-body.rs:7:5
3+
|
4+
LL | type Baz;
5+
| ^^^^^^^^-
6+
| |
7+
| help: provide a definition for the type: `= <type>;`
8+
9+
error: aborting due to previous error
10+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// check-pass
2+
#![feature(inherent_associated_types)]
3+
#![allow(incomplete_features)]
4+
5+
struct Foo;
6+
7+
impl Foo {
8+
type Bar = isize;
9+
}
10+
11+
fn main() {
12+
let x: Foo::Bar;
13+
x = 0isize;
14+
}

src/test/ui/resolve/resolve-self-in-impl.stderr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,40 @@
11
error: `Self` is not valid in the self type of an impl block
2-
--> $DIR/resolve-self-in-impl.rs:14:13
2+
--> $DIR/resolve-self-in-impl.rs:16:6
33
|
4-
LL | impl Tr for Self {}
5-
| ^^^^
4+
LL | impl Self {}
5+
| ^^^^
66
|
77
= note: replace `Self` with a different type
88

99
error: `Self` is not valid in the self type of an impl block
10-
--> $DIR/resolve-self-in-impl.rs:15:15
10+
--> $DIR/resolve-self-in-impl.rs:17:8
1111
|
12-
LL | impl Tr for S<Self> {}
13-
| ^^^^
12+
LL | impl S<Self> {}
13+
| ^^^^
1414
|
1515
= note: replace `Self` with a different type
1616

1717
error: `Self` is not valid in the self type of an impl block
18-
--> $DIR/resolve-self-in-impl.rs:16:6
18+
--> $DIR/resolve-self-in-impl.rs:18:7
1919
|
20-
LL | impl Self {}
21-
| ^^^^
20+
LL | impl (Self, Self) {}
21+
| ^^^^ ^^^^
2222
|
2323
= note: replace `Self` with a different type
2424

2525
error: `Self` is not valid in the self type of an impl block
26-
--> $DIR/resolve-self-in-impl.rs:17:8
26+
--> $DIR/resolve-self-in-impl.rs:14:13
2727
|
28-
LL | impl S<Self> {}
29-
| ^^^^
28+
LL | impl Tr for Self {}
29+
| ^^^^
3030
|
3131
= note: replace `Self` with a different type
3232

3333
error: `Self` is not valid in the self type of an impl block
34-
--> $DIR/resolve-self-in-impl.rs:18:7
34+
--> $DIR/resolve-self-in-impl.rs:15:15
3535
|
36-
LL | impl (Self, Self) {}
37-
| ^^^^ ^^^^
36+
LL | impl Tr for S<Self> {}
37+
| ^^^^
3838
|
3939
= note: replace `Self` with a different type
4040

0 commit comments

Comments
 (0)