Skip to content

Commit 901ab2b

Browse files
Fix a panic in ast::TypeBound::kind()
1 parent 0adc11b commit 901ab2b

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/tools/rust-analyzer/crates/hir-def/src/expr_store/lower.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,8 @@ impl<'db> ExprCollector<'db> {
949949
node: ast::TypeBound,
950950
impl_trait_lower_fn: ImplTraitLowerFn<'_>,
951951
) -> TypeBound {
952-
match node.kind() {
952+
let Some(kind) = node.kind() else { return TypeBound::Error };
953+
match kind {
953954
ast::TypeBoundKind::PathType(binder, path_type) => {
954955
let binder = match binder.and_then(|it| it.generic_param_list()) {
955956
Some(gpl) => gpl

src/tools/rust-analyzer/crates/hir-def/src/expr_store/tests/signatures.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,3 +197,15 @@ fn allowed3(baz: impl Baz<Assoc = Qux<impl Foo>>) {}
197197
"#]],
198198
);
199199
}
200+
201+
#[test]
202+
fn regression_21138() {
203+
lower_and_print(
204+
r#"
205+
fn foo(v: for<'a> Trait1 + Trait2) {}
206+
"#,
207+
expect![[r#"
208+
fn foo(dyn for<'a> Trait1 + Trait2) {...}
209+
"#]],
210+
);
211+
}

src/tools/rust-analyzer/crates/syntax/src/ast/node_ext.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -813,13 +813,16 @@ pub enum TypeBoundKind {
813813
}
814814

815815
impl ast::TypeBound {
816-
pub fn kind(&self) -> TypeBoundKind {
816+
pub fn kind(&self) -> Option<TypeBoundKind> {
817817
if let Some(path_type) = support::children(self.syntax()).next() {
818-
TypeBoundKind::PathType(self.for_binder(), path_type)
818+
Some(TypeBoundKind::PathType(self.for_binder(), path_type))
819+
} else if let Some(for_binder) = support::children::<ast::ForType>(&self.syntax).next() {
820+
let Some(ast::Type::PathType(path_type)) = for_binder.ty() else { return None };
821+
Some(TypeBoundKind::PathType(for_binder.for_binder(), path_type))
819822
} else if let Some(args) = self.use_bound_generic_args() {
820-
TypeBoundKind::Use(args)
823+
Some(TypeBoundKind::Use(args))
821824
} else if let Some(lifetime) = self.lifetime() {
822-
TypeBoundKind::Lifetime(lifetime)
825+
Some(TypeBoundKind::Lifetime(lifetime))
823826
} else {
824827
unreachable!()
825828
}

0 commit comments

Comments
 (0)