From 7c5ea7ffd648cb1170510a7b50b0294a30891ffd Mon Sep 17 00:00:00 2001 From: Ruiyang Wang Date: Sat, 7 Feb 2026 17:57:53 +0000 Subject: [PATCH] Fix bound var resolution for trait aliases Handle DefKind::TraitAlias in resolve_bound_vars so that associated item constraints and return type notation work through trait aliases. --- .../src/collect/resolve_bound_vars.rs | 5 +++-- .../return-type-notation/trait-alias.rs | 13 +++++++++++++ .../trait-alias-bound-vars.rs | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 tests/ui/associated-type-bounds/return-type-notation/trait-alias.rs create mode 100644 tests/ui/associated-type-bounds/trait-alias-bound-vars.rs diff --git a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs index 26f79d374075b..d4a01d26a1fc4 100644 --- a/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs +++ b/compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs @@ -1695,7 +1695,8 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { | DefKind::Union | DefKind::Enum | DefKind::TyAlias - | DefKind::Trait, + | DefKind::Trait + | DefKind::TraitAlias, def_id, ) if depth == 0 => Some(def_id), _ => None, @@ -1865,7 +1866,7 @@ impl<'a, 'tcx> BoundVarContext<'a, 'tcx> { if constraint.gen_args.parenthesized == hir::GenericArgsParentheses::ReturnTypeNotation { let bound_vars = if let Some(type_def_id) = type_def_id - && self.tcx.def_kind(type_def_id) == DefKind::Trait + && let DefKind::Trait | DefKind::TraitAlias = self.tcx.def_kind(type_def_id) && let Some((mut bound_vars, assoc_fn)) = BoundVarContext::supertrait_hrtb_vars( self.tcx, type_def_id, diff --git a/tests/ui/associated-type-bounds/return-type-notation/trait-alias.rs b/tests/ui/associated-type-bounds/return-type-notation/trait-alias.rs new file mode 100644 index 0000000000000..88f60a72afa19 --- /dev/null +++ b/tests/ui/associated-type-bounds/return-type-notation/trait-alias.rs @@ -0,0 +1,13 @@ +// Regression test for . +//@ check-pass +#![feature(return_type_notation, trait_alias)] + +trait Tr { + fn f() -> impl Sized; +} + +trait Al = Tr; + +fn f>() {} + +fn main() {} diff --git a/tests/ui/associated-type-bounds/trait-alias-bound-vars.rs b/tests/ui/associated-type-bounds/trait-alias-bound-vars.rs new file mode 100644 index 0000000000000..4644cee210083 --- /dev/null +++ b/tests/ui/associated-type-bounds/trait-alias-bound-vars.rs @@ -0,0 +1,14 @@ +// Check that we're successfully collecting bound vars behind trait aliases. +// Regression test for . +//@ check-pass +//@ needs-rustc-debug-assertions +#![feature(trait_alias)] + +trait A<'a> { type X; } +trait B: for<'a> A<'a> {} +trait C = B; + +fn f() where T: C {} +fn g() where T: C A<'r>> {} + +fn main() {}