diag: improvements to patterns in bare fn ptr and trait decl#143469
diag: improvements to patterns in bare fn ptr and trait decl#143469Ezrashaw wants to merge 2 commits intorust-lang:mainfrom
Conversation
|
r? @fee1-dead rustbot has assigned @fee1-dead. Use |
This comment has been minimized.
This comment has been minimized.
423de1c to
038c8d4
Compare
compiler-errors
left a comment
There was a problem hiding this comment.
Thanks for the cleanup, but I think this still needs a slight tweak to (continue to) explain the requirement that only default-body methods can take pattern args.
| Self::check_decl_no_pat(&bfty.decl, |span, _, _| { | ||
| self.dcx().emit_err(errors::PatternFnPointer { span }); | ||
| Self::check_decl_no_pat(&bfty.decl, |span, ident, _| { | ||
| // `self` in bare fn ptr is already an error; don't add an extra one. |
There was a problem hiding this comment.
Please delay a bug here rather than only conditionally emitting an error. You can do that by using dcx.create_err().emit_unless(bool).
| @@ -1,8 +1,12 @@ | |||
| Trait methods currently cannot take patterns as arguments. | |||
| Trait methods cannot take patterns as arguments. | |||
There was a problem hiding this comment.
Can you mention that if they have a default body that they can indeed take patterns
| LL | fn foo((x, y): (i32, i32)); | ||
| | ^^^^^^ | ||
| | | ||
| = note: upgrading editions will fix this error |
There was a problem hiding this comment.
This error message isn't right. Patterns are still not allowed in body-less trait methods in ed>2015.
|
|
||
| parse_pattern_method_param_without_body = patterns aren't allowed in methods without bodies | ||
| .suggestion = give this argument a name or use an underscore to ignore it | ||
| parse_pattern_in_trait_fn_in_2015 = patterns aren't allowed in trait methods in the 2015 edition |
There was a problem hiding this comment.
This should probably continue to mention methods without bodies, since that's still required for this not to be an error.
|
☔ The latest upstream changes (presumably #143958) made this pull request unmergeable. Please resolve the merge conflicts. |
|
@Ezrashaw any updates on this? thanks |
This PR addresses two diagnostic issues.
In edition 2015, trait fn declarations with function bodies can receive the "patterns aren't allowed in methods without bodies". This behaviour is correct (in edition 2015, trait functions have only a limited subset of patterns because of syntactic ambiguity wrt optional named parameters), but the diagnostic is confusing. This PR changes the message to explain that patterns are not allowed (even with function bodies) in edition 2015.
In bare function pointers (
fn(T) -> U), patterns are not allowed (except for_and plain argument names). Currently, this error sometimes gives the "patterns aren't allowed in methods without bodies error". This PR ensures we always get the proper error for patterns in function pointers.rust_error_codesdocs improved per Clarify that patterns may be used in trait implementations when explaining E0642 #97282.Fixes #143113 and fixes #97282.