Skip to content

Commit 3450aa3

Browse files
authored
Rollup merge of #103621 - fee1-dead-contrib:iat-fix-use, r=cjgillot
Correctly resolve Inherent Associated Types I don't know if this is the best way to do this, but at least it is one way.
2 parents aebf7c4 + 3aef6c6 commit 3450aa3

File tree

10 files changed

+66
-72
lines changed

10 files changed

+66
-72
lines changed

Diff for: compiler/rustc_hir_analysis/src/astconv/mod.rs

+14
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

Diff for: src/librustdoc/clean/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1381,7 +1381,8 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
13811381
ty::Projection(proj) => Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id),
13821382
// Rustdoc handles `ty::Error`s by turning them into `Type::Infer`s.
13831383
ty::Error(_) => return Type::Infer,
1384-
_ => bug!("clean: expected associated type, found `{:?}`", ty),
1384+
// Otherwise, this is an inherent associated type.
1385+
_ => return clean_middle_ty(ty, cx, None),
13851386
};
13861387
let trait_ = clean_path(&hir::Path { span, res, segments: &[] }, cx);
13871388
register_res(cx, trait_.res);

Diff for: src/test/rustdoc-ui/ambiguous-inherent-assoc-ty.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// check-pass
12
// This test ensures that rustdoc does not panic on inherented associated types
23
// that are referred to without fully-qualified syntax.
34

@@ -9,8 +10,4 @@ pub struct Struct;
910
impl Struct {
1011
pub type AssocTy = usize;
1112
pub const AssocConst: Self::AssocTy = 42;
12-
//~^ ERROR ambiguous associated type
13-
//~| HELP use fully-qualified syntax
14-
//~| ERROR ambiguous associated type
15-
//~| HELP use fully-qualified syntax
1613
}

Diff for: src/test/rustdoc-ui/ambiguous-inherent-assoc-ty.stderr

-15
This file was deleted.

Diff for: src/test/ui/assoc-inherent.rs

-20
This file was deleted.

Diff for: src/test/ui/assoc-inherent.stderr

-17
This file was deleted.
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() {}
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+
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+
}

Diff for: src/test/ui/resolve/resolve-self-in-impl.stderr

+15-15
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)