Skip to content

Commit fc4cfe0

Browse files
authored
Rollup merge of #116039 - estebank:nested-tait, r=compiler-errors
Account for nested `impl Trait` in TAIT Fix #116031. r? `@compiler-errors`
2 parents faf13dd + f2ede49 commit fc4cfe0

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

compiler/rustc_hir_analysis/src/collect/type_of/opaque.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_errors::StashKey;
22
use rustc_hir::def_id::LocalDefId;
33
use rustc_hir::intravisit::{self, Visitor};
4-
use rustc_hir::{self as hir, Expr, ImplItem, Item, Node, TraitItem};
4+
use rustc_hir::{self as hir, def, Expr, ImplItem, Item, Node, TraitItem};
55
use rustc_middle::hir::nested_filter;
66
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
77
use rustc_span::DUMMY_SP;
@@ -74,9 +74,14 @@ pub(super) fn find_opaque_ty_constraints_for_tait(tcx: TyCtxt<'_>, def_id: Local
7474

7575
hidden.ty
7676
} else {
77+
let mut parent_def_id = def_id;
78+
while tcx.def_kind(parent_def_id) == def::DefKind::OpaqueTy {
79+
// Account for `type Alias = impl Trait<Foo = impl Trait>;` (#116031)
80+
parent_def_id = tcx.local_parent(parent_def_id);
81+
}
7782
let reported = tcx.sess.emit_err(UnconstrainedOpaqueType {
7883
span: tcx.def_span(def_id),
79-
name: tcx.item_name(tcx.local_parent(def_id).to_def_id()),
84+
name: tcx.item_name(parent_def_id.to_def_id()),
8085
what: match tcx.hir().get(scope) {
8186
_ if scope == hir::CRATE_HIR_ID => "module",
8287
Node::Item(hir::Item { kind: hir::ItemKind::Mod(_), .. }) => "module",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#![feature(type_alias_impl_trait)]
2+
3+
pub type Tait = impl Iterator<Item = (&'db Key, impl Iterator)>;
4+
//~^ ERROR use of undeclared lifetime name `'db`
5+
//~| ERROR cannot find type `Key` in this scope
6+
//~| ERROR unconstrained opaque type
7+
//~| ERROR unconstrained opaque type
8+
9+
pub fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
error[E0261]: use of undeclared lifetime name `'db`
2+
--> $DIR/nested-impl-trait-in-tait.rs:3:40
3+
|
4+
LL | pub type Tait = impl Iterator<Item = (&'db Key, impl Iterator)>;
5+
| ^^^ undeclared lifetime
6+
|
7+
= note: for more information on higher-ranked polymorphism, visit https://doc.rust-lang.org/nomicon/hrtb.html
8+
help: consider making the bound lifetime-generic with a new `'db` lifetime
9+
|
10+
LL | pub type Tait = impl for<'db> Iterator<Item = (&'db Key, impl Iterator)>;
11+
| ++++++++
12+
help: consider introducing lifetime `'db` here
13+
|
14+
LL | pub type Tait<'db> = impl Iterator<Item = (&'db Key, impl Iterator)>;
15+
| +++++
16+
17+
error[E0412]: cannot find type `Key` in this scope
18+
--> $DIR/nested-impl-trait-in-tait.rs:3:44
19+
|
20+
LL | pub type Tait = impl Iterator<Item = (&'db Key, impl Iterator)>;
21+
| ^^^ not found in this scope
22+
|
23+
help: consider importing this struct
24+
|
25+
LL + use std::thread::local_impl::Key;
26+
|
27+
28+
error: unconstrained opaque type
29+
--> $DIR/nested-impl-trait-in-tait.rs:3:17
30+
|
31+
LL | pub type Tait = impl Iterator<Item = (&'db Key, impl Iterator)>;
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
|
34+
= note: `Tait` must be used in combination with a concrete type within the same module
35+
36+
error: unconstrained opaque type
37+
--> $DIR/nested-impl-trait-in-tait.rs:3:49
38+
|
39+
LL | pub type Tait = impl Iterator<Item = (&'db Key, impl Iterator)>;
40+
| ^^^^^^^^^^^^^
41+
|
42+
= note: `Tait` must be used in combination with a concrete type within the same module
43+
44+
error: aborting due to 4 previous errors
45+
46+
Some errors have detailed explanations: E0261, E0412.
47+
For more information about an error, try `rustc --explain E0261`.

0 commit comments

Comments
 (0)