Skip to content

Commit d0ade3f

Browse files
Rollup merge of #116800 - compiler-errors:rpitit-gat-outlives, r=jackh726
Fix implied outlives check for GAT in RPITIT We enforce certain `Self: 'lt` bounds for GATs to save space for more sophisticated implied bounds, but those currently operate on the HIR. Code was easily reworked to operate on def-ids so that we can properly let these suggestions propagate through synthetic associated types like RPITITs and AFITs. r? `@jackh726` or `@aliemjay` Fixes #116789
2 parents 4c1c8ab + 17ec3cd commit d0ade3f

File tree

3 files changed

+68
-33
lines changed

3 files changed

+68
-33
lines changed

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+27-33
Original file line numberDiff line numberDiff line change
@@ -314,9 +314,10 @@ fn check_trait_item(tcx: TyCtxt<'_>, trait_item: &hir::TraitItem<'_>) {
314314
/// fn into_iter<'a>(&'a self) -> Self::Iter<'a>;
315315
/// }
316316
/// ```
317-
fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRef]) {
317+
fn check_gat_where_clauses(tcx: TyCtxt<'_>, trait_def_id: LocalDefId) {
318318
// Associates every GAT's def_id to a list of possibly missing bounds detected by this lint.
319319
let mut required_bounds_by_item = FxHashMap::default();
320+
let associated_items = tcx.associated_items(trait_def_id);
320321

321322
// Loop over all GATs together, because if this lint suggests adding a where-clause bound
322323
// to one GAT, it might then require us to an additional bound on another GAT.
@@ -325,8 +326,8 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
325326
// those GATs.
326327
loop {
327328
let mut should_continue = false;
328-
for gat_item in associated_items {
329-
let gat_def_id = gat_item.id.owner_id;
329+
for gat_item in associated_items.in_definition_order() {
330+
let gat_def_id = gat_item.def_id.expect_local();
330331
let gat_item = tcx.associated_item(gat_def_id);
331332
// If this item is not an assoc ty, or has no args, then it's not a GAT
332333
if gat_item.kind != ty::AssocKind::Type {
@@ -342,18 +343,18 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
342343
// This is calculated by taking the intersection of the bounds that each item
343344
// constrains the GAT with individually.
344345
let mut new_required_bounds: Option<FxHashSet<ty::Clause<'_>>> = None;
345-
for item in associated_items {
346-
let item_def_id = item.id.owner_id;
346+
for item in associated_items.in_definition_order() {
347+
let item_def_id = item.def_id.expect_local();
347348
// Skip our own GAT, since it does not constrain itself at all.
348349
if item_def_id == gat_def_id {
349350
continue;
350351
}
351352

352353
let param_env = tcx.param_env(item_def_id);
353354

354-
let item_required_bounds = match item.kind {
355+
let item_required_bounds = match tcx.associated_item(item_def_id).kind {
355356
// In our example, this corresponds to `into_iter` method
356-
hir::AssocItemKind::Fn { .. } => {
357+
ty::AssocKind::Fn => {
357358
// For methods, we check the function signature's return type for any GATs
358359
// to constrain. In the `into_iter` case, we see that the return type
359360
// `Self::Iter<'a>` is a GAT we want to gather any potential missing bounds from.
@@ -369,12 +370,12 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
369370
// We also assume that all of the function signature's parameter types
370371
// are well formed.
371372
&sig.inputs().iter().copied().collect(),
372-
gat_def_id.def_id,
373+
gat_def_id,
373374
gat_generics,
374375
)
375376
}
376377
// In our example, this corresponds to the `Iter` and `Item` associated types
377-
hir::AssocItemKind::Type => {
378+
ty::AssocKind::Type => {
378379
// If our associated item is a GAT with missing bounds, add them to
379380
// the param-env here. This allows this GAT to propagate missing bounds
380381
// to other GATs.
@@ -391,11 +392,11 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
391392
.instantiate_identity_iter_copied()
392393
.collect::<Vec<_>>(),
393394
&FxIndexSet::default(),
394-
gat_def_id.def_id,
395+
gat_def_id,
395396
gat_generics,
396397
)
397398
}
398-
hir::AssocItemKind::Const => None,
399+
ty::AssocKind::Const => None,
399400
};
400401

401402
if let Some(item_required_bounds) = item_required_bounds {
@@ -431,7 +432,12 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
431432
}
432433

433434
for (gat_def_id, required_bounds) in required_bounds_by_item {
434-
let gat_item_hir = tcx.hir().expect_trait_item(gat_def_id.def_id);
435+
// Don't suggest adding `Self: 'a` to a GAT that can't be named
436+
if tcx.is_impl_trait_in_trait(gat_def_id.to_def_id()) {
437+
continue;
438+
}
439+
440+
let gat_item_hir = tcx.hir().expect_trait_item(gat_def_id);
435441
debug!(?required_bounds);
436442
let param_env = tcx.param_env(gat_def_id);
437443

@@ -441,21 +447,16 @@ fn check_gat_where_clauses(tcx: TyCtxt<'_>, associated_items: &[hir::TraitItemRe
441447
ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => {
442448
!region_known_to_outlive(
443449
tcx,
444-
gat_def_id.def_id,
450+
gat_def_id,
445451
param_env,
446452
&FxIndexSet::default(),
447453
a,
448454
b,
449455
)
450456
}
451-
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => !ty_known_to_outlive(
452-
tcx,
453-
gat_def_id.def_id,
454-
param_env,
455-
&FxIndexSet::default(),
456-
a,
457-
b,
458-
),
457+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => {
458+
!ty_known_to_outlive(tcx, gat_def_id, param_env, &FxIndexSet::default(), a, b)
459+
}
459460
_ => bug!("Unexpected ClauseKind"),
460461
})
461462
.map(|clause| clause.to_string())
@@ -534,7 +535,7 @@ fn augment_param_env<'tcx>(
534535
fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
535536
tcx: TyCtxt<'tcx>,
536537
param_env: ty::ParamEnv<'tcx>,
537-
item_def_id: hir::OwnerId,
538+
item_def_id: LocalDefId,
538539
to_check: T,
539540
wf_tys: &FxIndexSet<Ty<'tcx>>,
540541
gat_def_id: LocalDefId,
@@ -567,7 +568,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
567568
// reflected in a where clause on the GAT itself.
568569
for (ty, ty_idx) in &types {
569570
// In our example, requires that `Self: 'a`
570-
if ty_known_to_outlive(tcx, item_def_id.def_id, param_env, &wf_tys, *ty, *region_a) {
571+
if ty_known_to_outlive(tcx, item_def_id, param_env, &wf_tys, *ty, *region_a) {
571572
debug!(?ty_idx, ?region_a_idx);
572573
debug!("required clause: {ty} must outlive {region_a}");
573574
// Translate into the generic parameters of the GAT. In
@@ -606,14 +607,7 @@ fn gather_gat_bounds<'tcx, T: TypeFoldable<TyCtxt<'tcx>>>(
606607
if matches!(**region_b, ty::ReStatic | ty::ReError(_)) || region_a == region_b {
607608
continue;
608609
}
609-
if region_known_to_outlive(
610-
tcx,
611-
item_def_id.def_id,
612-
param_env,
613-
&wf_tys,
614-
*region_a,
615-
*region_b,
616-
) {
610+
if region_known_to_outlive(tcx, item_def_id, param_env, &wf_tys, *region_a, *region_b) {
617611
debug!(?region_a_idx, ?region_b_idx);
618612
debug!("required clause: {region_a} must outlive {region_b}");
619613
// Translate into the generic parameters of the GAT.
@@ -1114,8 +1108,8 @@ fn check_trait(tcx: TyCtxt<'_>, item: &hir::Item<'_>) {
11141108
});
11151109

11161110
// Only check traits, don't check trait aliases
1117-
if let hir::ItemKind::Trait(_, _, _, _, items) = item.kind {
1118-
check_gat_where_clauses(tcx, items);
1111+
if let hir::ItemKind::Trait(..) = item.kind {
1112+
check_gat_where_clauses(tcx, item.owner_id.def_id);
11191113
}
11201114
}
11211115

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// edition: 2021
2+
3+
use std::future::Future;
4+
5+
trait Trait {
6+
type Gat<'a>;
7+
//~^ ERROR missing required bound on `Gat`
8+
async fn foo(&self) -> Self::Gat<'_>;
9+
}
10+
11+
trait Trait2 {
12+
type Gat<'a>;
13+
//~^ ERROR missing required bound on `Gat`
14+
async fn foo(&self) -> impl Future<Output = Self::Gat<'_>>;
15+
}
16+
17+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error: missing required bound on `Gat`
2+
--> $DIR/gat-outlives.rs:6:5
3+
|
4+
LL | type Gat<'a>;
5+
| ^^^^^^^^^^^^-
6+
| |
7+
| help: add the required where clause: `where Self: 'a`
8+
|
9+
= note: this bound is currently required to ensure that impls have maximum flexibility
10+
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information
11+
12+
error: missing required bound on `Gat`
13+
--> $DIR/gat-outlives.rs:12:5
14+
|
15+
LL | type Gat<'a>;
16+
| ^^^^^^^^^^^^-
17+
| |
18+
| help: add the required where clause: `where Self: 'a`
19+
|
20+
= note: this bound is currently required to ensure that impls have maximum flexibility
21+
= note: we are soliciting feedback, see issue #87479 <https://github.com/rust-lang/rust/issues/87479> for more information
22+
23+
error: aborting due to 2 previous errors
24+

0 commit comments

Comments
 (0)