Description
Code using patterns in parameters of foreign functions, function pointers or trait methods without body is not accepted by the compiler.
extern {
fn f((a, b): (u8, u16)); // ERROR
}
type T = fn((a, b): (u8, u16)); // ERROR
trait Tr {
fn f((a, b): (u8, u16)); // ERROR
}
Previously some simple patterns like &ident
, &&ident
or mut ident
were allowed in these positions, now they aren't. Such patterns weren't properly type checked, e.g. type A = fn(&arg: u8);
compiled despite u8
not being a reference.
What are these errors for?
Full patterns don't make sense in functions without bodies, but simple identifiers may be useful for documenting argument purposes even if they aren't actually used - type Callback = fn(useful_name: u8)
.
By restricting patterns in body-less function signatures to ident: TYPE
we can make argument names optional and accept simply a TYPE
in argument position (type T = fn(u8)
) without introducing parsing ambiguities.
How to fix this warning/error
Remove &
or mut
from the pattern and make the function parameter a single identifier or _
.
Current status
- Properly enforce the "patterns aren't allowed in foreign functions" rule #35015 made patterns in foreign functions and function pointers hard errors
- Prohibit patterns in trait methods without bodies #37378 made patterns in trait methods without body warn-by-default lints
- Accept interpolated patterns in trait method parameters #45775 made patterns in trait methods without body hard errors, except for
mut IDENT
- Transition future compat lints to {ERROR, DENY} - Take 2 #65785 made patterns in trait methods without body deny-by-default lints
- ? made patterns in trait methods without body hard errors
- On "pattern in arg for fn def without body" emit structured suggestion #78927 provide structured suggestions
Metadata
Metadata
Assignees
Labels
Type
Projects
Status