Skip to content
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

Improve location of some compiler errors #257

Merged
merged 2 commits into from
Mar 8, 2024

Conversation

escritorio-gustavo
Copy link
Contributor

@escritorio-gustavo escritorio-gustavo commented Mar 7, 2024

This PR improves where the compiler error points when there is an attribute error.

This errors now point at the exact location they should

  • The #[ts(as = "...")] attribute would point at the token after the string when receiving invalid path syntax
  • The #[ts(optional = nullable)] would do the same when receiving anything other than nullable

Errors about incompatible attributes point at the entire field definition

fn parse_assign_from_str<T>(input: ParseStream) -> Result<T> where T: Parse {
input.parse::<Token![=]>()?;
match Lit::parse(input)? {
Lit::Str(string) => string.parse(),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In case of failure, this will point inside the string, rather than after it

Copy link
Contributor Author

@escritorio-gustavo escritorio-gustavo Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before:
image

After:
image

use crate::utils::{parse_attrs, parse_docs};

#[derive(Default)]
pub struct FieldAttr {
pub type_as: Option<String>,
pub type_as: Option<Path>,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This enables checking if the string contains valid syntax for a path

@@ -93,11 +93,11 @@ fn format_field(
}

if type_as.is_some() && type_override.is_some() {
syn_err!("`type` is not compatible with `as`")
syn_err!(field.ident.span(); "`type` is not compatible with `as`")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite where I'd like the error to show up, but it's the best I could do, at least pointing the user at the correct field

Copy link
Contributor Author

@escritorio-gustavo escritorio-gustavo Mar 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, I can make it so the entire field is marked, attributes, identifier and type, by using syn::Error::new_spanned

Before:
image

After:
image

@escritorio-gustavo escritorio-gustavo changed the title Improve location of some errors Improve location of some compiler errors Mar 7, 2024
@NyxCode
Copy link
Collaborator

NyxCode commented Mar 7, 2024

This is great, love it!

@escritorio-gustavo escritorio-gustavo merged commit 474cc86 into main Mar 8, 2024
10 checks passed
@escritorio-gustavo escritorio-gustavo deleted the better-error-locations branch March 8, 2024 10:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants