Skip to content

Commit

Permalink
Rollup merge of #103621 - fee1-dead-contrib:iat-fix-use, r=cjgillot
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Dylan-DPC authored Nov 5, 2022
2 parents aebf7c4 + 3aef6c6 commit 3450aa3
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 72 deletions.
14 changes: 14 additions & 0 deletions compiler/rustc_hir_analysis/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1381,7 +1381,8 @@ fn clean_qpath<'tcx>(hir_ty: &hir::Ty<'tcx>, cx: &mut DocContext<'tcx>) -> Type
ty::Projection(proj) => Res::Def(DefKind::Trait, proj.trait_ref(cx.tcx).def_id),
// Rustdoc handles `ty::Error`s by turning them into `Type::Infer`s.
ty::Error(_) => return Type::Infer,
_ => bug!("clean: expected associated type, found `{:?}`", ty),
// Otherwise, this is an inherent associated type.
_ => return clean_middle_ty(ty, cx, None),
};
let trait_ = clean_path(&hir::Path { span, res, segments: &[] }, cx);
register_res(cx, trait_.res);
Expand Down
5 changes: 1 addition & 4 deletions src/test/rustdoc-ui/ambiguous-inherent-assoc-ty.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// check-pass
// This test ensures that rustdoc does not panic on inherented associated types
// that are referred to without fully-qualified syntax.

Expand All @@ -9,8 +10,4 @@ pub struct Struct;
impl Struct {
pub type AssocTy = usize;
pub const AssocConst: Self::AssocTy = 42;
//~^ ERROR ambiguous associated type
//~| HELP use fully-qualified syntax
//~| ERROR ambiguous associated type
//~| HELP use fully-qualified syntax
}
15 changes: 0 additions & 15 deletions src/test/rustdoc-ui/ambiguous-inherent-assoc-ty.stderr

This file was deleted.

20 changes: 0 additions & 20 deletions src/test/ui/assoc-inherent.rs

This file was deleted.

17 changes: 0 additions & 17 deletions src/test/ui/assoc-inherent.stderr

This file was deleted.

10 changes: 10 additions & 0 deletions src/test/ui/associated-inherent-types/assoc-inherent-no-body.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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

14 changes: 14 additions & 0 deletions src/test/ui/associated-inherent-types/assoc-inherent-use.rs
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 15 additions & 15 deletions src/test/ui/resolve/resolve-self-in-impl.stderr
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
error: `Self` is not valid in the self type of an impl block
--> $DIR/resolve-self-in-impl.rs:14:13
--> $DIR/resolve-self-in-impl.rs:16:6
|
LL | impl Tr for Self {}
| ^^^^
LL | impl 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
--> $DIR/resolve-self-in-impl.rs:17:8
|
LL | impl Tr for S<Self> {}
| ^^^^
LL | impl S<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:16:6
--> $DIR/resolve-self-in-impl.rs:18:7
|
LL | impl Self {}
| ^^^^
LL | impl (Self, 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:17:8
--> $DIR/resolve-self-in-impl.rs:14:13
|
LL | impl S<Self> {}
| ^^^^
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:18:7
--> $DIR/resolve-self-in-impl.rs:15:15
|
LL | impl (Self, Self) {}
| ^^^^ ^^^^
LL | impl Tr for S<Self> {}
| ^^^^
|
= note: replace `Self` with a different type

Expand Down

0 comments on commit 3450aa3

Please sign in to comment.