Skip to content

Commit 4614ab7

Browse files
authored
Rollup merge of rust-lang#121991 - oli-obk:merge_opaque_types_defined_by_queries, r=compiler-errors
Merge impl_trait_in_assoc_types_defined_by query back into `opaque_types_defined_by` Instead, when we're collecting opaques for associated items, we choose the right collection mode depending on whether we're collecting for an associated item of a trait impl or not. r? ``@compiler-errors`` follow up to rust-lang#121838
2 parents 07dadc5 + da35734 commit 4614ab7

File tree

7 files changed

+35
-53
lines changed

7 files changed

+35
-53
lines changed

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

+8-20
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,7 @@ pub(super) fn find_opaque_ty_constraints_for_impl_trait_in_assoc_type(
4646
for &assoc_id in tcx.associated_item_def_ids(impl_def_id) {
4747
let assoc = tcx.associated_item(assoc_id);
4848
match assoc.kind {
49-
ty::AssocKind::Const | ty::AssocKind::Fn => {
50-
locator.check(assoc_id.expect_local(), ImplTraitSource::AssocTy)
51-
}
49+
ty::AssocKind::Const | ty::AssocKind::Fn => locator.check(assoc_id.expect_local()),
5250
// Associated types don't have bodies, so they can't constrain hidden types
5351
ty::AssocKind::Type => {}
5452
}
@@ -182,15 +180,9 @@ struct TaitConstraintLocator<'tcx> {
182180
typeck_types: Vec<ty::OpaqueHiddenType<'tcx>>,
183181
}
184182

185-
#[derive(Debug)]
186-
enum ImplTraitSource {
187-
AssocTy,
188-
TyAlias,
189-
}
190-
191183
impl TaitConstraintLocator<'_> {
192184
#[instrument(skip(self), level = "debug")]
193-
fn check(&mut self, item_def_id: LocalDefId, source: ImplTraitSource) {
185+
fn check(&mut self, item_def_id: LocalDefId) {
194186
// Don't try to check items that cannot possibly constrain the type.
195187
if !self.tcx.has_typeck_results(item_def_id) {
196188
debug!("no constraint: no typeck results");
@@ -242,12 +234,8 @@ impl TaitConstraintLocator<'_> {
242234
continue;
243235
}
244236
constrained = true;
245-
let opaque_types_defined_by = match source {
246-
ImplTraitSource::AssocTy => {
247-
self.tcx.impl_trait_in_assoc_types_defined_by(item_def_id)
248-
}
249-
ImplTraitSource::TyAlias => self.tcx.opaque_types_defined_by(item_def_id),
250-
};
237+
let opaque_types_defined_by = self.tcx.opaque_types_defined_by(item_def_id);
238+
251239
if !opaque_types_defined_by.contains(&self.def_id) {
252240
self.tcx.dcx().emit_err(TaitForwardCompat {
253241
span: hidden_type.span,
@@ -308,29 +296,29 @@ impl<'tcx> intravisit::Visitor<'tcx> for TaitConstraintLocator<'tcx> {
308296
}
309297
fn visit_expr(&mut self, ex: &'tcx Expr<'tcx>) {
310298
if let hir::ExprKind::Closure(closure) = ex.kind {
311-
self.check(closure.def_id, ImplTraitSource::TyAlias);
299+
self.check(closure.def_id);
312300
}
313301
intravisit::walk_expr(self, ex);
314302
}
315303
fn visit_item(&mut self, it: &'tcx Item<'tcx>) {
316304
trace!(?it.owner_id);
317305
// The opaque type itself or its children are not within its reveal scope.
318306
if it.owner_id.def_id != self.def_id {
319-
self.check(it.owner_id.def_id, ImplTraitSource::TyAlias);
307+
self.check(it.owner_id.def_id);
320308
intravisit::walk_item(self, it);
321309
}
322310
}
323311
fn visit_impl_item(&mut self, it: &'tcx ImplItem<'tcx>) {
324312
trace!(?it.owner_id);
325313
// The opaque type itself or its children are not within its reveal scope.
326314
if it.owner_id.def_id != self.def_id {
327-
self.check(it.owner_id.def_id, ImplTraitSource::TyAlias);
315+
self.check(it.owner_id.def_id);
328316
intravisit::walk_impl_item(self, it);
329317
}
330318
}
331319
fn visit_trait_item(&mut self, it: &'tcx TraitItem<'tcx>) {
332320
trace!(?it.owner_id);
333-
self.check(it.owner_id.def_id, ImplTraitSource::TyAlias);
321+
self.check(it.owner_id.def_id);
334322
intravisit::walk_trait_item(self, it);
335323
}
336324
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {

compiler/rustc_middle/src/query/mod.rs

-9
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,6 @@ rustc_queries! {
344344
}
345345
}
346346

347-
query impl_trait_in_assoc_types_defined_by(
348-
key: LocalDefId
349-
) -> &'tcx ty::List<LocalDefId> {
350-
desc {
351-
|tcx| "computing the opaque types defined by `{}`",
352-
tcx.def_path_str(key.to_def_id())
353-
}
354-
}
355-
356347
/// Returns the list of bounds that can be used for
357348
/// `SelectionCandidate::ProjectionCandidate(_)` and
358349
/// `ProjectionTyCandidate::TraitDef`.

compiler/rustc_ty_utils/src/opaque_types.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ enum CollectionMode {
3434
}
3535

3636
impl<'tcx> OpaqueTypeCollector<'tcx> {
37-
fn new(tcx: TyCtxt<'tcx>, item: LocalDefId, mode: CollectionMode) -> Self {
37+
fn new(tcx: TyCtxt<'tcx>, item: LocalDefId) -> Self {
38+
let mode = match tcx.def_kind(tcx.local_parent(item)) {
39+
DefKind::Impl { of_trait: true } => CollectionMode::ImplTraitInAssocTypes,
40+
_ => CollectionMode::TypeAliasImplTraitTransition,
41+
};
3842
Self { tcx, opaques: Vec::new(), item, seen: Default::default(), span: None, mode }
3943
}
4044

@@ -287,23 +291,13 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for OpaqueTypeCollector<'tcx> {
287291
}
288292
}
289293

290-
fn impl_trait_in_assoc_types_defined_by<'tcx>(
291-
tcx: TyCtxt<'tcx>,
292-
item: LocalDefId,
293-
) -> &'tcx ty::List<LocalDefId> {
294-
let mut collector = OpaqueTypeCollector::new(tcx, item, CollectionMode::ImplTraitInAssocTypes);
295-
super::sig_types::walk_types(tcx, item, &mut collector);
296-
tcx.mk_local_def_ids(&collector.opaques)
297-
}
298-
299294
fn opaque_types_defined_by<'tcx>(
300295
tcx: TyCtxt<'tcx>,
301296
item: LocalDefId,
302297
) -> &'tcx ty::List<LocalDefId> {
303298
let kind = tcx.def_kind(item);
304299
trace!(?kind);
305-
let mut collector =
306-
OpaqueTypeCollector::new(tcx, item, CollectionMode::TypeAliasImplTraitTransition);
300+
let mut collector = OpaqueTypeCollector::new(tcx, item);
307301
super::sig_types::walk_types(tcx, item, &mut collector);
308302
match kind {
309303
DefKind::AssocFn
@@ -346,6 +340,5 @@ fn opaque_types_defined_by<'tcx>(
346340
}
347341

348342
pub(super) fn provide(providers: &mut Providers) {
349-
*providers =
350-
Providers { opaque_types_defined_by, impl_trait_in_assoc_types_defined_by, ..*providers };
343+
*providers = Providers { opaque_types_defined_by, ..*providers };
351344
}

tests/ui/type-alias-impl-trait/hidden_behind_struct_field2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl Trait for Bar {
1515
type Assoc = impl std::fmt::Debug;
1616
fn foo() -> Foo {
1717
Foo { field: () }
18-
//~^ ERROR: item constrains opaque type that is not in its signature
18+
//~^ ERROR: mismatched types
1919
}
2020
}
2121

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
error: item constrains opaque type that is not in its signature
1+
error[E0308]: mismatched types
22
--> $DIR/hidden_behind_struct_field2.rs:17:22
33
|
4+
LL | type Assoc = impl std::fmt::Debug;
5+
| -------------------- the expected opaque type
6+
LL | fn foo() -> Foo {
47
LL | Foo { field: () }
5-
| ^^
8+
| ^^ expected opaque type, found `()`
69
|
7-
= note: this item must mention the opaque type in its signature in order to be able to register hidden types
8-
note: this item must mention the opaque type in its signature in order to be able to register hidden types
10+
= note: expected opaque type `<Bar as Trait>::Assoc`
11+
found unit type `()`
12+
note: this item must have the opaque type in its signature in order to be able to register hidden types
913
--> $DIR/hidden_behind_struct_field2.rs:16:8
1014
|
1115
LL | fn foo() -> Foo {
1216
| ^^^
1317

1418
error: aborting due to 1 previous error
1519

20+
For more information about this error, try `rustc --explain E0308`.

tests/ui/type-alias-impl-trait/hidden_behind_struct_field3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ impl Trait for Bar {
1717
type Assoc = impl Iterator<Item = Foo>;
1818
fn foo() -> Self::Assoc {
1919
vec![Foo { field: () }].into_iter()
20-
//~^ ERROR item constrains opaque type that is not in its signature
20+
//~^ ERROR mismatched types
2121
}
2222
}
2323

Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
error: item constrains opaque type that is not in its signature
1+
error[E0308]: mismatched types
22
--> $DIR/hidden_behind_struct_field3.rs:19:27
33
|
4+
LL | type Assoc2 = impl std::fmt::Debug;
5+
| -------------------- the expected opaque type
6+
...
47
LL | vec![Foo { field: () }].into_iter()
5-
| ^^
8+
| ^^ expected opaque type, found `()`
69
|
7-
= note: this item must mention the opaque type in its signature in order to be able to register hidden types
8-
note: this item must mention the opaque type in its signature in order to be able to register hidden types
10+
= note: expected opaque type `<Bar as Trait>::Assoc2`
11+
found unit type `()`
12+
note: this item must have the opaque type in its signature in order to be able to register hidden types
913
--> $DIR/hidden_behind_struct_field3.rs:18:8
1014
|
1115
LL | fn foo() -> Self::Assoc {
1216
| ^^^
1317

1418
error: aborting due to 1 previous error
1519

20+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)