Skip to content

Commit 63a54d9

Browse files
committed
Don't suppress lint type_alias_bounds for ty aliases containing inherent assoc tys
1 parent 02a2f02 commit 63a54d9

File tree

5 files changed

+55
-41
lines changed

5 files changed

+55
-41
lines changed

compiler/rustc_lint/src/builtin.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,30 +1425,27 @@ impl TypeAliasBounds {
14251425

14261426
impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds {
14271427
fn check_item(&mut self, cx: &LateContext<'_>, item: &hir::Item<'_>) {
1428-
let hir::ItemKind::TyAlias(hir_ty, type_alias_generics) = &item.kind else { return };
1428+
let hir::ItemKind::TyAlias(hir_ty, generics) = &item.kind else { return };
14291429

1430-
// Bounds of lazy type aliases and TAITs are respected.
1431-
if cx.tcx.type_alias_is_lazy(item.owner_id) {
1430+
// There must not be a where clause.
1431+
if generics.predicates.is_empty() {
14321432
return;
14331433
}
14341434

1435-
let ty = cx.tcx.type_of(item.owner_id).skip_binder();
1436-
if ty.has_inherent_projections() {
1437-
// Bounds of type aliases that contain opaque types or inherent projections are
1438-
// respected. E.g: `type X = impl Trait;`, `type X = (impl Trait, Y);`, `type X =
1439-
// Type::Inherent;`.
1435+
// Bounds of lazy type aliases and TAITs are respected.
1436+
if cx.tcx.type_alias_is_lazy(item.owner_id) {
14401437
return;
14411438
}
14421439

1443-
// There must not be a where clause
1444-
if type_alias_generics.predicates.is_empty() {
1445-
return;
1446-
}
1440+
// NOTE(inherent_associated_types): While we currently do take some bounds in type
1441+
// aliases into consideration during IAT *selection*, we don't perform full use+def
1442+
// site wfchecking for such type aliases. Therefore TAB should still trigger.
1443+
// See also `tests/ui/associated-inherent-types/type-alias-bounds.rs`.
14471444

14481445
let mut where_spans = Vec::new();
14491446
let mut inline_spans = Vec::new();
14501447
let mut inline_sugg = Vec::new();
1451-
for p in type_alias_generics.predicates {
1448+
for p in generics.predicates {
14521449
let span = p.span();
14531450
if p.in_where_clause() {
14541451
where_spans.push(span);
@@ -1469,10 +1466,7 @@ impl<'tcx> LateLintPass<'tcx> for TypeAliasBounds {
14691466
cx.emit_span_lint(
14701467
TYPE_ALIAS_BOUNDS,
14711468
where_spans,
1472-
BuiltinTypeAliasWhereClause {
1473-
suggestion: type_alias_generics.where_clause_span,
1474-
sub,
1475-
},
1469+
BuiltinTypeAliasWhereClause { suggestion: generics.where_clause_span, sub },
14761470
);
14771471
}
14781472

compiler/rustc_type_ir/src/visit.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,10 +241,6 @@ pub trait TypeVisitableExt<I: Interner>: TypeVisitable<I> {
241241
self.has_type_flags(TypeFlags::HAS_ALIAS)
242242
}
243243

244-
fn has_inherent_projections(&self) -> bool {
245-
self.has_type_flags(TypeFlags::HAS_TY_INHERENT)
246-
}
247-
248244
fn has_opaque_types(&self) -> bool {
249245
self.has_type_flags(TypeFlags::HAS_TY_OPAQUE)
250246
}

tests/ui/associated-inherent-types/type-alias-bounds-are-enforced.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//@ compile-flags: --crate-type=lib
2+
//@ check-pass
3+
4+
#![feature(inherent_associated_types)]
5+
#![allow(incomplete_features)]
6+
7+
// FIXME(inherent_associated_types):
8+
// While we currently do take some clauses of the ParamEnv into consideration
9+
// when performing IAT selection, we do not perform full well-formedness checking
10+
// for (eager) type alias definition and usage sites.
11+
//
12+
// Therefore it's *correct* for lint `type_alias_bounds` to fire here despite the
13+
// fact that removing `Bound` from `T` in `Alias` would lead to an error!
14+
//
15+
// Obviously, the present situation isn't ideal and we should fix it in one way
16+
// or another. Either we somehow delay IAT selection until after HIR ty lowering
17+
// to avoid the need to specify any bounds inside (eager) type aliases or we
18+
// force the overarching type alias to be *lazy* (similar to TAITs) which would
19+
// automatically lead to full wfchecking and lint TAB getting suppressed.
20+
21+
pub type Alias<T: Bound> = (Source<T>::Assoc,);
22+
//~^ WARN bounds on generic parameters are not enforced in type aliases
23+
24+
pub struct Source<T>(T);
25+
pub trait Bound {}
26+
27+
impl<T: Bound> Source<T> {
28+
pub type Assoc = ();
29+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
warning: bounds on generic parameters are not enforced in type aliases
2+
--> $DIR/type-alias-bounds.rs:21:19
3+
|
4+
LL | pub type Alias<T: Bound> = (Source<T>::Assoc,);
5+
| ^^^^^
6+
|
7+
= note: `#[warn(type_alias_bounds)]` on by default
8+
help: the bound will not be checked when the type alias is used, and should be removed
9+
|
10+
LL - pub type Alias<T: Bound> = (Source<T>::Assoc,);
11+
LL + pub type Alias<T> = (Source<T>::Assoc,);
12+
|
13+
14+
warning: 1 warning emitted
15+

0 commit comments

Comments
 (0)