1
Fork 0

Correctly resolve Inherent Associated Types

This commit is contained in:
Deadbeef 2022-10-27 07:11:15 +00:00
parent 160b194295
commit 30b6fe37a6
7 changed files with 64 additions and 53 deletions

View file

@ -1910,6 +1910,20 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
} }
} }
} }
// see if we can satisfy using an inherent associated type
for impl_ in tcx.inherent_impls(adt_def.did()) {
let assoc_ty = tcx.associated_items(impl_).find_by_name_and_kind(
tcx,
assoc_ident,
ty::AssocKind::Type,
*impl_,
);
if let Some(assoc_ty) = assoc_ty {
let ty = tcx.type_of(assoc_ty.def_id);
return Ok((ty, DefKind::AssocTy, assoc_ty.def_id));
}
}
} }
// Find the type of the associated item, and the trait where the associated // Find the type of the associated item, and the trait where the associated

View file

@ -1,20 +0,0 @@
// Test that inherent associated types work with
// inherent_associated_types feature gate.
#![feature(inherent_associated_types)]
#![allow(incomplete_features)]
struct Foo;
impl Foo {
type Bar = isize;
}
impl Foo {
type Baz; //~ ERROR associated type in `impl` without body
}
fn main() {
let x : Foo::Bar; //~ERROR ambiguous associated type
x = 0isize;
}

View file

@ -1,17 +0,0 @@
error: associated type in `impl` without body
--> $DIR/assoc-inherent.rs:14:5
|
LL | type Baz;
| ^^^^^^^^-
| |
| help: provide a definition for the type: `= <type>;`
error[E0223]: ambiguous associated type
--> $DIR/assoc-inherent.rs:18:13
|
LL | let x : Foo::Bar;
| ^^^^^^^^ help: use fully-qualified syntax: `<Foo as Trait>::Bar`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0223`.

View file

@ -0,0 +1,10 @@
#![feature(inherent_associated_types)]
#![allow(incomplete_features)]
struct Foo;
impl Foo {
type Baz; //~ ERROR associated type in `impl` without body
}
fn main() {}

View file

@ -0,0 +1,10 @@
error: associated type in `impl` without body
--> $DIR/assoc-inherent-no-body.rs:7:5
|
LL | type Baz;
| ^^^^^^^^-
| |
| help: provide a definition for the type: `= <type>;`
error: aborting due to previous error

View file

@ -0,0 +1,14 @@
// check-pass
#![feature(inherent_associated_types)]
#![allow(incomplete_features)]
struct Foo;
impl Foo {
type Bar = isize;
}
fn main() {
let x: Foo::Bar;
x = 0isize;
}

View file

@ -1,19 +1,3 @@
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:14:13
|
LL | impl Tr for Self {}
| ^^^^
|
= note: replace `Self` with a different type
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:15:15
|
LL | impl Tr for S<Self> {}
| ^^^^
|
= note: replace `Self` with a different type
error: `Self` is not valid in the self type of an impl block error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:16:6 --> $DIR/resolve-self-in-impl.rs:16:6
| |
@ -38,6 +22,22 @@ LL | impl (Self, Self) {}
| |
= note: replace `Self` with a different type = note: replace `Self` with a different type
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:14:13
|
LL | impl Tr for Self {}
| ^^^^
|
= note: replace `Self` with a different type
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:15:15
|
LL | impl Tr for S<Self> {}
| ^^^^
|
= note: replace `Self` with a different type
error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:19:1: 19:23>` error[E0391]: cycle detected when computing trait implemented by `<impl at $DIR/resolve-self-in-impl.rs:19:1: 19:23>`
--> $DIR/resolve-self-in-impl.rs:19:1 --> $DIR/resolve-self-in-impl.rs:19:1
| |