Skip to content

Commit 6074746

Browse files
Merge pull request #22415 from youngspe/extern-wildcard-param
fix: allow wildcard params in foreign fn declarations
2 parents 753080e + 4bb5ca3 commit 6074746

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

crates/hir-def/src/expr_store/lower.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2570,9 +2570,9 @@ impl<'db> ExprCollector<'db> {
25702570
}
25712571

25722572
fn collect_extern_fn_param(&mut self, pat: Option<ast::Pat>) -> PatId {
2573-
// `extern` functions cannot have pattern-matched parameters, and furthermore, the identifiers
2574-
// in their parameters are always interpreted as bindings, even if in a normal function they
2575-
// won't be, because they would refer to a path pattern.
2573+
// parameters of functions in `extern` blocks can only be simple identifiers and wildcards.
2574+
// Furthermore, the identifiers in their parameters are always interpreted as bindings, even
2575+
// if in a normal function they won't be, because they would refer to a path pattern.
25762576
let Some(pat) = pat else { return self.missing_pat() };
25772577

25782578
match &pat {
@@ -2588,6 +2588,7 @@ impl<'db> ExprCollector<'db> {
25882588
self.add_definition_to_binding(binding, pat);
25892589
pat
25902590
}
2591+
ast::Pat::WildcardPat(_) => self.alloc_pat(Pat::Wild, AstPtr::new(&pat)),
25912592
_ => {
25922593
self.store.diagnostics.push(ExpressionStoreDiagnostics::PatternArgInExternFn {
25932594
node: self.expander.in_file(AstPtr::new(&pat)),

crates/ide-diagnostics/src/handlers/pattern_arg_in_extern_fn.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,24 @@ pub(crate) fn pattern_arg_in_extern_fn(
2020
mod tests {
2121
use crate::tests::check_diagnostics;
2222

23+
#[test]
24+
fn ident_pattern_allowed() {
25+
check_diagnostics(
26+
r#"
27+
unsafe extern { fn foo(a: i32); }
28+
"#,
29+
);
30+
}
31+
32+
#[test]
33+
fn wildcard_pattern_allowed() {
34+
check_diagnostics(
35+
r#"
36+
unsafe extern { fn foo(_: i32); }
37+
"#,
38+
);
39+
}
40+
2341
#[test]
2442
fn tuple_pattern() {
2543
check_diagnostics(

0 commit comments

Comments
 (0)