Improved errors for invalid self arguments #4276
Merged
+79 −24
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
While writing tests, I forgot to add
#[wasm_bindgen]on animplblock and the proc macro panicked with the message "arguments cannot beself". Since this was a panic, I tried to debug the proc macro for like 10 minutes before I noticed the missing#[wasm_bindgen]in my test.This PR improves the error messages for
selfarguments in invalid positions. Instead of panics, users now get proper errors with helpful error messages.Before:
After:
For imported/
externfunctions with aselfargument, I added a similar error message that suggests usingthisinstead ofself.These new error messages should (1) make these issues easier to fix for our users and (2) make it clear that it's an error with user code and not the macro.
To implement the better error messages, I initially added a new parameter to
function_from_declto hold the function position (free, extern, impl), but realized that this single parameter could representallow_self: bool,self_ty: Option<&Ident>, andis_from_impl: boolall in a single parameter. So I did that. In total, I added 1 new parameter and removed 3 old ones fromfunction_from_decl. This makes the diff a bit larger, but I couldn't resist when it cleaned up the API offunction_from_declso nicely.