-
Notifications
You must be signed in to change notification settings - Fork 288
Add Neon error codes #1110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
dherman
wants to merge
1
commit into
main
Choose a base branch
from
codex/implement-idea-from-issue-1079
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add Neon error codes #1110
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#[derive(Copy, Clone)] | ||
pub(crate) enum ErrorCode { | ||
UnsupportedExportItem, | ||
WrongContextChannelRef, | ||
ContextMustBeMut, | ||
WrongContextChannel, | ||
MustBeMutRef, | ||
ChannelByRef, | ||
ChannelContextRef, | ||
ContextNotAvailable, | ||
MissingContext, | ||
SelfReceiver, | ||
UnsupportedProperty, | ||
AsyncAttrAsyncFn, | ||
} | ||
|
||
impl ErrorCode { | ||
pub(crate) fn code(self) -> &'static str { | ||
match self { | ||
ErrorCode::UnsupportedExportItem => "N0001", | ||
ErrorCode::WrongContextChannelRef => "N0002", | ||
ErrorCode::ContextMustBeMut => "N0003", | ||
ErrorCode::WrongContextChannel => "N0004", | ||
ErrorCode::MustBeMutRef => "N0005", | ||
ErrorCode::ChannelByRef => "N0006", | ||
ErrorCode::ChannelContextRef => "N0007", | ||
ErrorCode::ContextNotAvailable => "N0008", | ||
ErrorCode::MissingContext => "N0009", | ||
ErrorCode::SelfReceiver => "N0010", | ||
ErrorCode::UnsupportedProperty => "N0011", | ||
ErrorCode::AsyncAttrAsyncFn => "N0012", | ||
} | ||
} | ||
|
||
pub(crate) fn message(self) -> &'static str { | ||
match self { | ||
ErrorCode::UnsupportedExportItem => "`neon::export` can only be applied to functions, consts, and statics.", | ||
ErrorCode::WrongContextChannelRef => "Expected `&mut Cx` instead of a `Channel` reference.", | ||
ErrorCode::ContextMustBeMut => "Context must be a `&mut` reference.", | ||
ErrorCode::WrongContextChannel => "Expected `&mut Cx` instead of `Channel`.", | ||
ErrorCode::MustBeMutRef => "Must be a `&mut` reference.", | ||
ErrorCode::ChannelByRef => "Expected an owned `Channel` instead of a reference.", | ||
ErrorCode::ChannelContextRef => "Expected an owned `Channel` instead of a context reference.", | ||
ErrorCode::ContextNotAvailable => "Context is not available in async functions. Try a `Channel` instead.", | ||
ErrorCode::MissingContext => "Expected a context argument. Try removing the `context` attribute.", | ||
ErrorCode::SelfReceiver => "Exported functions cannot receive `self`.", | ||
ErrorCode::UnsupportedProperty => "unsupported property", | ||
ErrorCode::AsyncAttrAsyncFn => "`async` attribute should not be used with an `async fn`", | ||
} | ||
} | ||
} | ||
|
||
pub(crate) fn error(span: proc_macro2::Span, code: ErrorCode) -> syn::Error { | ||
syn::Error::new(span, format!("{} [{}]", code.message(), code.code())) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,6 +189,8 @@ fn context_parse( | |
// * Context argument must be a `&mut` reference | ||
// * First argument must not be `Channel` | ||
// * Must not be a `self` receiver | ||
use crate::error::{self, ErrorCode}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please place all |
||
|
||
fn check_context(opts: &meta::Meta, sig: &syn::Signature) -> syn::Result<bool> { | ||
// Extract the first argument | ||
let ty = match first_arg(opts, sig)? { | ||
|
@@ -200,28 +202,19 @@ fn check_context(opts: &meta::Meta, sig: &syn::Signature) -> syn::Result<bool> { | |
let ty = match &*ty.ty { | ||
// Tried to use a borrowed Channel | ||
syn::Type::Reference(ty) if !opts.context && is_channel_type(&ty.elem) => { | ||
return Err(syn::Error::new( | ||
ty.elem.span(), | ||
"Expected `&mut Cx` instead of a `Channel` reference.", | ||
)) | ||
return Err(error::error(ty.elem.span(), ErrorCode::WrongContextChannelRef)) | ||
} | ||
|
||
syn::Type::Reference(ty) => ty, | ||
|
||
// Context needs to be a reference | ||
_ if opts.context || is_context_type(&ty.ty) => { | ||
return Err(syn::Error::new( | ||
ty.ty.span(), | ||
"Context must be a `&mut` reference.", | ||
)) | ||
return Err(error::error(ty.ty.span(), ErrorCode::ContextMustBeMut)) | ||
} | ||
|
||
// Hint that `Channel` should be swapped for `&mut Cx` | ||
_ if is_channel_type(&ty.ty) => { | ||
return Err(syn::Error::new( | ||
ty.ty.span(), | ||
"Expected `&mut Cx` instead of `Channel`.", | ||
)) | ||
return Err(error::error(ty.ty.span(), ErrorCode::WrongContextChannel)) | ||
} | ||
|
||
_ => return Ok(false), | ||
|
@@ -234,7 +227,7 @@ fn check_context(opts: &meta::Meta, sig: &syn::Signature) -> syn::Result<bool> { | |
|
||
// Context argument must be mutable | ||
if ty.mutability.is_none() { | ||
return Err(syn::Error::new(ty.span(), "Must be a `&mut` reference.")); | ||
return Err(error::error(ty.span(), ErrorCode::MustBeMutRef)); | ||
} | ||
|
||
// All tests passed! | ||
|
@@ -258,25 +251,22 @@ fn check_channel(opts: &meta::Meta, sig: &syn::Signature) -> syn::Result<bool> { | |
match &*ty.ty { | ||
// Provided `&mut Channel` instead of `Channel` | ||
syn::Type::Reference(ty) if opts.context || is_channel_type(&ty.elem) => { | ||
Err(syn::Error::new( | ||
ty.span(), | ||
"Expected an owned `Channel` instead of a reference.", | ||
)) | ||
Err(error::error(ty.span(), ErrorCode::ChannelByRef)) | ||
} | ||
|
||
// Provided a `&mut Cx` instead of a `Channel` | ||
syn::Type::Reference(ty) if is_context_type(&ty.elem) => Err(syn::Error::new( | ||
syn::Type::Reference(ty) if is_context_type(&ty.elem) => Err(error::error( | ||
ty.elem.span(), | ||
"Expected an owned `Channel` instead of a context reference.", | ||
ErrorCode::ChannelContextRef, | ||
)), | ||
|
||
// Found a `Channel` | ||
_ if opts.context || is_channel_type(&ty.ty) => Ok(true), | ||
|
||
// Tried to use an owned `Cx` | ||
_ if is_context_type(&ty.ty) => Err(syn::Error::new( | ||
_ if is_context_type(&ty.ty) => Err(error::error( | ||
ty.ty.span(), | ||
"Context is not available in async functions. Try a `Channel` instead.", | ||
ErrorCode::ContextNotAvailable, | ||
)), | ||
|
||
_ => Ok(false), | ||
|
@@ -294,9 +284,9 @@ fn first_arg<'a>( | |
|
||
// If context was forced, error to let the user know the mistake | ||
None if opts.context => { | ||
return Err(syn::Error::new( | ||
return Err(error::error( | ||
sig.inputs.span(), | ||
"Expected a context argument. Try removing the `context` attribute.", | ||
ErrorCode::MissingContext, | ||
)) | ||
} | ||
|
||
|
@@ -306,9 +296,9 @@ fn first_arg<'a>( | |
// Expect a typed pattern; self receivers are not supported | ||
match arg { | ||
syn::FnArg::Typed(ty) => Ok(Some(ty)), | ||
syn::FnArg::Receiver(arg) => Err(syn::Error::new( | ||
syn::FnArg::Receiver(arg) => Err(error::error( | ||
arg.span(), | ||
"Exported functions cannot receive `self`.", | ||
ErrorCode::SelfReceiver, | ||
)), | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Neon Error Codes | ||
|
||
Neon compiler errors include a short code in brackets. The table below lists each code and the associated message. | ||
|
||
| Code | Message | | ||
|-------|---------| | ||
| N0001 | `neon::export` can only be applied to functions, consts, and statics. | | ||
| N0002 | Expected `&mut Cx` instead of a `Channel` reference. | | ||
| N0003 | Context must be a `&mut` reference. | | ||
| N0004 | Expected `&mut Cx` instead of `Channel`. | | ||
| N0005 | Must be a `&mut` reference. | | ||
| N0006 | Expected an owned `Channel` instead of a reference. | | ||
| N0007 | Expected an owned `Channel` instead of a context reference. | | ||
| N0008 | Context is not available in async functions. Try a `Channel` instead. | | ||
| N0009 | Expected a context argument. Try removing the `context` attribute. | | ||
| N0010 | Exported functions cannot receive `self`. | | ||
| N0011 | Unsupported property. | | ||
| N0012 | `async` attribute should not be used with an `async fn`. | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All
use
statements should be placed in the top of the file.